title: equality constrained least square fitting using python
body: <p>Can anyone help me for writing a python script? The problem is stated below.</p>
  
  <p>I need to perform a equality constrained linear least square fitting of some linear equations. For a specific linear equation, the matrix equation looks like [Y]<em>{nX1}=[X]</em>{nXm}[P]_{mX1}, where Y and P are vectors and X is a matrix and n, m are dimension of the matrix. Further, there is a equality constraint on P which is Sum(P(i))=0.0. Do anyone have idea how do I proceed to solve that? Which function of python is suitable for this? I saw few of discussions on scipy.optimize.fmin_slsqp() function but use of this function is not very straightforward.</p>
  
  <p>However, I can easily do the unconstrained least square fitting of the above problem by using numpy. Here is the small script.</p>
  
  <pre><code>import numpy as np
  from numpy import matrix
  from numpy import arange,array,ones,linalg
  x = np.random.randn(500,3)
  A=matrix([[3.0, 4.0, -7.0],[4.0, -5.0, 1.0],[3.0, 2.0, -5.0]])
  y=A*x.T
  w=linalg.lstsq(x,y.T)[0]
  print w.T
  </code></pre>
  
  <p>Here I had generated a random vector x and then generated vector y by matrix multiplication of A and x.T. Then I use the x and y vectors and evaluated the elements of matrix  A. The above script perfectly reproduces the matrix A. This gives me the confidence that the lsf works. However, it is just least square fitting. There is no constraints.</p>
  
  <p>Another thing, is it possible to convert constrained lsf problem to unconstrained lsf problem. If so, then I can use the above script. The method of lagrange multiplier is one of the possible choice. However, I am facing difficulty to evaluate the multiplier for my problem. This is because, the equation contains large no of variables. If there is </p>
  
  <p>By the way the similar function for constrained lsf in MATLAB is lsqlin.</p>
  
  <p>I am struggling with this for last week. Please help me out in this regard.  If you have any other suggestions then that will also be great.</p>
  
  <p>Best wishes</p>