I am trying to estimate a (structural) model with maximum-likelihood. The function I try to estimate has a matrix inverse, which I calculate as intermediate step in the ML program. However, I get an error when I use the parameter I try to estimate inside the matrix inverse function. The error is a r(111) (not found) error.
In the MWE below, I first generate a (random) symmetric matrix axx. I then generate an example dataset. I estimate the parameter beta via maximum likelihood with the mlestimation-function written next. One step within that function is to generate the inverse of a matrix that is a function of beta, and the not-found error happens here. If I replace beta with any scalar, there is no error.
In the MWE below, I first generate a (random) symmetric matrix axx. I then generate an example dataset. I estimate the parameter beta via maximum likelihood with the mlestimation-function written next. One step within that function is to generate the inverse of a matrix that is a function of beta, and the not-found error happens here. If I replace beta with any scalar, there is no error.
Code:
/* [> Generate symmetric matrix of relations, axx <] */ clear set obs 100 gen x = _n expand 100 gsort x gen z=_n-(x-1)*100 gen rel = rbinomial(1,0.2) reshape wide rel , i(x) j(z) mkmat rel*, mat(aplus) mata aplus = st_matrix("aplus") mata a = makesymmetric(aplus) mata st_matrix("axx",a) /* [> Generate dataset <] */ drop _all set obs 100 gen id = _n gen rel = rbinomial(10,0.3) gen rand1 = rnormal(0,1) gen y = rand1 + rel + rel*rand1 /* [> Define ML-function <] */ cap program drop mlestimation program mlestimation args lnf beta sigma tempvar GAMMA LAMBDA CVEC /* [> Generate temporary values/variables <] */ qui gen `GAMMA' = 1/(1+`beta'*rel) mkmat `GAMMA', mat(G) qui egen `LAMBDA' = total(`GAMMA') /* [> Error happens here, as beta is "not found": r(111) <] */ matrix C = invsym(I(100)+`beta'*axx)*G svmat C, name(`CVEC') /* [> Likelihood function <] */ qui replace `lnf' = ln(normalden($ML_y1 - `LAMBDA'*(1-`LAMBDA')*`CVEC')/`sigma')-ln(`sigma') end /* [> Estimate ML <] */ ml model lf mlestimation ( y = ) /beta /sigma ml maximize, difficult
Comment