Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-negative least squares for underdetermined system

Consider the following problem:

Find: x_1, x_2, x_3 > 0 such that

67.5 = 60*x_1 +  90*x_2 + 120*x_3  
60   = 30*x_1 + 120*x_2 +  90*x_3

Is there a way to solve this equation in Python? Perhaps with scipy.nnls()?

like image 425
nantitv Avatar asked Feb 23 '14 16:02

nantitv


People also ask

Can least-squares be used on underdetermined system?

Least-squares parameter estimation can be applied to underdetermined just as to overdetermined linear problems. In fact, underdetermined prediction problems are generally more common than overdetermined filtering and adjustment problems.

Can an underdetermined system have no solution?

An underdetermined linear system has either no solution or infinitely many solutions.

How do you determine if a system is overdetermined or underdetermined?

The overdetermined case occurs when the system has been overconstrained — that is, when the equations outnumber the unknowns. In contrast, the underdetermined case occurs when the system has been underconstrained — that is, when the number of equations is fewer than the number of unknowns.

Is an underdetermined system linearly independent?

If a set of vectors is linearly independent, then it corresponds to a system which is overdetermined or which has the same number of variables as equations. If a system is underdetermined, then it corresponds to a set of vectors which are linearly dependent.


1 Answers

Using sympy to solve the equation set symbolically

from sympy import * 

x_1, x_2, x_3 = symbols('x_1 x_2 x_3')

res = solve([Eq(60*x_1+90*x_2+120*x_3, 67.5),
             Eq(30*x_1+120*x_2+90*x_3, 60)],
             [x_1, x_2, x_3])
print res
#{x_1: -1.4*x_3 + 0.6, x_2: -0.4*x_3 + 0.35}

using scipy.optimize.nnls

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import nnls 

A = np.array([[60, 90, 120], 
              [30, 120, 90]])

b = np.array([67.5, 60])

x, rnorm = nnls(A,b)

print x
#[ 0.          0.17857143  0.42857143]
print rnorm
#0.0

Altough this only promises a solution where the parameters are x>=0 so you can get zeros, as you did for this example.

like image 162
M4rtini Avatar answered Oct 13 '22 11:10

M4rtini