Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

equality constrained least square fitting using python

Tags:

python-3.x

Can anyone help me for writing a python script? The problem is stated below.

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]{nX1}=[X]{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.

However, I can easily do the unconstrained least square fitting of the above problem by using numpy. Here is the small script.

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

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.

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

By the way the similar function for constrained lsf in MATLAB is lsqlin.

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.

Best wishes

like image 825
sen Avatar asked Feb 22 '26 21:02

sen


1 Answers

Equality-constrained linear least-squares fitting, for dense matrices, is currently (Scipy 1.5) available in Python with a direct call to the relevant LAPACK routine scipy.linalg.lapack.dgglse. The LAPACK documentation of DGGLSE is here.

As an example, the solution to the problem

min ||A @ x - b||
subject to  C @ x = d

can be obtained using the Python code

from scipy.linalg import lapack

# Define the matrices as usual, then
x = lapack.dgglse(A, C, b, d)[3]
like image 186
divenex Avatar answered Feb 24 '26 16:02

divenex