### Aptech Systems, Inc. Worldwide Headquarters

Address:

Aptech Systems, Inc.

2350 East Germann Road, Suite #21

Chandler, AZ 85286Phone: 360.886.7100

FAX: 360.886.8922**Ready to Get Started?**### Request Quote & Product Information

### Industry Solutions

### Products

### Resources

### Support

### Training & Events

Want more guidance while learning about the full functionality of GAUSS and its capabilities? Get in touch for in-person training or browse additional references below.

### Tutorials

Step-by-step, informative lessons for those who want to dive into GAUSS and achieve their goals, fast.

### What’s New in GAUSS 17

### Want to find out more?

### Have a Specific Question?

### Q&A: Register and Login

### Support Plans

Premier Support and Platinum Premier Support are annually renewable membership programs that provide you with important benefits including technical support, product maintenance, and substantial cost-saving features for your GAUSS System or the GAUSS Engine.

### User Forums

Join our community to see why our users are considered some of the most active and helpful in the industry!

### Where to Buy

Available across the globe, you can have access to GAUSS no matter where you are.

### Recent Tags

applications character vectors CML CMLMT Constrained Optimization datasets dates dlibrary dllcall econometrics Editor error error codes error handling errors Excel file i/o floating network GAUSS Engine GAUSS Light graphics GUI hotkeys installation license licensing linux loading data loop loops matrix manipulation Maximum Likelihood Maxlik MaxLikMT Memory multidimensional array optimization Optmum output PQG graphics procs random numbers strings structures threading### Recent Questions

- Does Gauss have similar function as " %in%" in R or "find" in matlab
- Shall I change from I7 6700K to Ryzen X1800
- inther procedure
- About precision of cdfMvn and multi-thread
- Axis fonts, LaTeX font in legend
- Indexing Procedures inside a loop
- G0008 : Syntax error
- basic syntax
- explain the following Code.
- G0064 : Operand missing

### Features

### Time Series 2.0 MT

### Industry Solutions

### Find out more now

### Time Series MT 2.1

### Find out more now

### Find out more now

# Resources

# 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

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;

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

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;

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.