Hi,
I have the following two functions defined as public in the class nelogit_fp.
Code:
// ML d2 Evaluator for a fixed parameters Logit void nelogit_fp::MLEval(real scalar todo, real rowvector p, v, g, H) { // y, X, and j must have been set. I'll write the check later // We need to get the parameters in the right place real colvector b, o, pij, pi real matrix XS p = p' if (rows(this.S) != 0) { // We have scale. Need to separate the coefficients, and scale the // independent variables. b = p[1..cols(this.X),.] o = p[cols(this.X)+1..rows(p),.] XS = this.X :/ exp(this.S*o) } else { // Don't have scale. The coefficients are all of the value function, and // we use the original X. b = p XS = this.X } // Calculations of probabilities pij = super.logitprob(XS, b, this.j) pi = rowsum(colshape(this.y :* pij, this.j)) // Log-Likelihood this.ll = v = colsum(ln(pi)) if(todo > 0) { real matrix px, dx real colvector wmk, dxk real scalar k // Calculation of the difference from the weighted mean of the explanatory // variables px = pij :* XS for(k=1; k<=cols(XS); k++) { wmk = rowsum(colshape(px[.,k], this.j)) dxk = colshape(colshape(XS[.,k], this.j) :- wmk,1) if (k == 1) dx = dxk else dx = dx , dxk } g = cross(this.y, dx) if (rows(this.S) != 0) { // g = g, this.y'((-dx*b):*S) g = (g, cross(this.y,(-dx*b):*S)) } if(todo > 1) { // Hessian H = cross(px,-dx) if (rows(this.S) != 0) { real matrix Hbo, Hoo // beta omega Hbo = cross((pij:* (1 :+ (dx*b)) :- this.y) :* XS,S) // omega omega Hoo = cross(((pij:* ((-dx*b) :- 1) :+ this.y) :* (XS*b)) :* S,S) // Form it H = ((H , Hbo) \ (Hbo' , Hoo)) } _makesymmetric(H) } } } // Estimate Method to estimate the model void nelogit_fp::Estimate() { transmorphic SS SS = optimize_init() optimize_init_evaluator(SS, &(this.MLEval())) optimize_init_evaluatortype(SS, "d2") real rowvector b0 b0 = (invsym(cross(this.X,this.X))*cross(this.X,this.y))' if(rows(this.S) != 0) b0 = b0, J(1, cols(this.S), 0) optimize_init_params(SS, b0) this.b = optimize(SS) this.b }
Code:
mata: nl = nelogit_fp() nl.setup(st_data(.,"choice"), st_data(.,"`xvars'"), 3, st_data(.,"`svars'")) nl.Estimate() end
Code:
. mata: ------------------------------------------------- mata (type end to exit) ------------------------------------------------------------- : nl = nelogit_fp() : nl.setup(st_data(.,"choice"), st_data(.,"`xvars'"), 3, st_data(.,"`svars'")) : nl.Estimate() nelogit_fp::MLEval(): 3001 expected 6 arguments but received 1 nelogit_fp::Estimate(): - function returned error <istmt>: - function returned error (1 line skipped) --------------------------------------------------------------------------------------------------------------------------------------- r(3001); end of do-file r(3001);
0 Response to optimize issue with evaluator in a class
Post a Comment