I have a procedure for an optimization problem (with OPTMT 2.0), but I would like to call that procedure later only to obtain the function value with a different parameter vector.
Here is the procedure:
proc (1) = ch5_lmpost(pars, y, X, ind); local fgamma, lmarg_post; struct modelResults mm; fgamma = pars[2:3]' * (x[., 2:3].^pars[4])'; fgamma = fgamma.^(1 / pars[4]); fgamma = fgamma' + pars[1] .* ones(n, 1); // Evaluate the log marginal posterior lmarg_post = 0.5 * n * ln((y - fgamma)'(y - fgamma)); if ind[1]; mm.Function = lmarg_post; endif; retp(mm); endp;
The optimization works perfectly, but how do I call the procedure afterwards if I just want to get the function value?
2 Answers
1
accepted
Here is an example of calling your function with some simulated data.
new; cls; library optmt; // Replace with your parameters b = { 0.4, 1.1, 0.8, 0.9 }; // Load your data instead y = rndu(100, 1); X = rndu(100, 4); // Declare a modelResults structure to hold the return value struct modelResults rslt; // Call the function with your desired inputs rslt = ch5_lmpost(b, y, X, 1); print rslt.function; proc (1) = ch5_lmpost(pars, y, X, ind); local fgamma, lmarg_post, n; struct modelResults mm; // 'n' was not set in the original post. // I'm not sure it is set correctly here, // you may need to change it. n = rows(X); fgamma = pars[2:3]' * (x[., 2:3].^pars[4])'; fgamma = fgamma.^(1 / pars[4]); fgamma = fgamma' + pars[1] .* ones(n, 1); // Evaluate the log marginal posterior lmarg_post = 0.5 * n * ln((y - fgamma)'(y - fgamma)); if ind[1]; mm.Function = lmarg_post; endif; retp(mm); endp;
Let us know if you have any questions about this.
0
Ok, so I need to define a structure for the function value. I tried it too, but somehow it didn't work. Now it does. Thanks very much!
Your Answer
2 Answers
Here is an example of calling your function with some simulated data.
new; cls; library optmt; // Replace with your parameters b = { 0.4, 1.1, 0.8, 0.9 }; // Load your data instead y = rndu(100, 1); X = rndu(100, 4); // Declare a modelResults structure to hold the return value struct modelResults rslt; // Call the function with your desired inputs rslt = ch5_lmpost(b, y, X, 1); print rslt.function; proc (1) = ch5_lmpost(pars, y, X, ind); local fgamma, lmarg_post, n; struct modelResults mm; // 'n' was not set in the original post. // I'm not sure it is set correctly here, // you may need to change it. n = rows(X); fgamma = pars[2:3]' * (x[., 2:3].^pars[4])'; fgamma = fgamma.^(1 / pars[4]); fgamma = fgamma' + pars[1] .* ones(n, 1); // Evaluate the log marginal posterior lmarg_post = 0.5 * n * ln((y - fgamma)'(y - fgamma)); if ind[1]; mm.Function = lmarg_post; endif; retp(mm); endp;
Let us know if you have any questions about this.
Ok, so I need to define a structure for the function value. I tried it too, but somehow it didn't work. Now it does. Thanks very much!