# Can I estimate Non-Negative Least Squares (NNLS) in SHAZAM?

Is it possible to solve a non-negative least-squares constraints problem of the form shown below in SHAZAM?

${\min_{x}\left \| B.x-a \right \|^2_2},\quad where\quad x\geq 0$

Could this be done with the NL command?

edit retag close merge delete

Sort by » oldest newest most voted

Yes, this can be done.

The simplest way is to specify the equation on the NL command like

EQ Y = a + (b*b)*X


will make the coefficient on X always positive because b*b is always zero or positive.

You could also create a SHAZAM procedure using the PROC command which for two variables might look like this:

* PROC NNLS2
*
* SHAZAM Procedure to perform Non Negative Least Squares for 2 variables
*
* INPUTS:     [YIN]
*             [XIN]
*             [STARTIN]
*
* OUTPUTS:    [BETAOUT]
*
PROC NNLS2

MATRIX X1_=[XIN](0,1)
GENR X1_=X1_
MATRIX X2_=[XIN](0,2)
GENR X2_=X2_

NL 1 / NCOEF=[VARSIN] START=[STARTIN] GENRVAR
EQ [YIN]=(B1_*B1_)*X1_+(B2_*B2_)*X2_
END

GENR B11_=B1_*B1_
GENR B22_=B2_*B2_
MATRIX [BETAOUT](1,1)=B11_
MATRIX [BETAOUT](2,1)=B22_

PROCEND

more