Constrained Optimization and Sparse Matrices

Dear Users,

I'm using Gauss 12 with Constrained Optimization and try to run a Spatial Autoregressive model.

I am trying to implement a log-likelihood function along the lines of LeSage (1999).  This function has two parts, and while the first part is working fine, the second part produces me some headaches. The code for the second part looks as follows (W is a sparse matrix):

{p,f,g,retcode} = co(&llf,b0);

proc llf(p);
local dev;
   dev = ln(abs(eye(n)- pw(p)));
   retp(dev'*dev/rows(dev));
endp;

proc pw(p);
local sum2;
   sparse matrix sum2;
   sum2 = p*W;
   retp(sum2);
endp;

I get the mistake G0181 illegal assignment - type mismatch, although I declared sum2 to be a sparse matrix. I already tried to replace the sparse matrix with its non-sparse counterpart. In that case I get an error during the gradient calculation which is:

C:\gauss12\src\coutil.src(1806) : error G0046 : Columns don't match
Currently active call: _co_gdfd [1806] C:\gauss12\src\coutil.src
Stack trace:
_co_gdfd called from C:\gauss12\src\coutil.src, line 1717
_co_deriv called from C:\gauss12\src\coutil.src, line 308
_co called from C:\gauss12\src\co.src, line 384

Does anybody know how to proceed with this? In the end I think I need a version with sparse matrices, otherwise I don't think I can do these estimations.

best

Oldes

2 Answers



0



I am not sure about the second error, but I can address the first. Dense matrix math is almost always faster than sparse matrix math, because the data structures needed to represent a sparse matrix are more complicated. So, if you have the RAM to make the matrix dense, most of the time you are much better off to do that. However, you can call your series of functions with W as a sparse matrix like this:

new;
cls;

n = 4;
b0 = ones(n,1);

//declare and instantiate
//sparse vector
sparse matrix W;
W = densetosp(rndn(1, n), 0);

//Call main function
llf(b0);

proc llf(p);
   local dev;
   dev = ln(abs(eye(n)- pw(p)));
   retp(dev'*dev/rows(dev));
endp;

proc pw(p);
   //declare sum2 as a sparse matrix
   sparse matrix sum2;
	
   //convert 'p' to a sparse matrix
   //before multiplication
   sum2 = densetosp(p, 0)*W;
   retp(sum2);
endp;


0



In the CO application module, the objective function must be a scalar value, and thus the procedure computing the objective function must return a scalar. If the objective function does not return a scalar, then you will get errors like the one that you mention.

Your Answer

2 Answers

0

I am not sure about the second error, but I can address the first. Dense matrix math is almost always faster than sparse matrix math, because the data structures needed to represent a sparse matrix are more complicated. So, if you have the RAM to make the matrix dense, most of the time you are much better off to do that. However, you can call your series of functions with W as a sparse matrix like this:

new;
cls;

n = 4;
b0 = ones(n,1);

//declare and instantiate
//sparse vector
sparse matrix W;
W = densetosp(rndn(1, n), 0);

//Call main function
llf(b0);

proc llf(p);
   local dev;
   dev = ln(abs(eye(n)- pw(p)));
   retp(dev'*dev/rows(dev));
endp;

proc pw(p);
   //declare sum2 as a sparse matrix
   sparse matrix sum2;
	
   //convert 'p' to a sparse matrix
   //before multiplication
   sum2 = densetosp(p, 0)*W;
   retp(sum2);
endp;
0

In the CO application module, the objective function must be a scalar value, and thus the procedure computing the objective function must return a scalar. If the objective function does not return a scalar, then you will get errors like the one that you mention.


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.