Dear Statalist members,
I'm using Stata 17 SE. I would like to know whether it is possible to embed Quadrature( ) in optimize(), and if yes, how to do so.
As demonstrated in the toy examples below, I know how to use Quadrature( ) and optimize() separately, but when I tried to embed one into another, I got error message: "type mismatch: exp.exp: transmorphic found where struct expected".
Here is how I use optimize(), and it works:
Here is how I use Quadrature(), and it works as well:
However, I got error message when I tried something like this, where I attempted to execute Intfunc2() within Optfunc2():
All examples above are for demonstration purposes only. I understand that there may be other ways to do these examples without using Quadrature() and optimize(). I hope that these toy examples are clear enough to convey my question. If anything is unclear, please let me know.
Thank you very much for your help.
I'm using Stata 17 SE. I would like to know whether it is possible to embed Quadrature( ) in optimize(), and if yes, how to do so.
As demonstrated in the toy examples below, I know how to use Quadrature( ) and optimize() separately, but when I tried to embed one into another, I got error message: "type mismatch: exp.exp: transmorphic found where struct expected".
Here is how I use optimize(), and it works:
Code:
cap mata: mata drop S cap mata: mata drop Optfunc1() mata: void Optfunc1(todo, b, P, x, v, g, H) { v =( P -logistic(b *x) )^2 } P =.3 x = 1 S =optimize_init() optimize_init_evaluator(S, &Optfunc1()) optimize_init_which(S, "min") optimize_init_argument(S, 1, P) optimize_init_argument(S, 2, x) optimize_init_params(S, 0) b =optimize(S); b end Iteration 0: f(p) = .04 Iteration 1: f(p) = .00010051 Iteration 2: f(p) = 6.024e-08 (not concave) Iteration 3: f(p) = 4.736e-09 (not concave) Iteration 4: f(p) = 3.710e-10 -.8473895862
Code:
cap mata: mata drop Q cap mata: mata drop Intfunc1() mata: real scalar Intfunc1(real scalar x, real scalar b) { return( logistic(b *x) *x *normalden(x,-.5,1) ) } b =-1.5 Q =Quadrature() Q.setEvaluator(&Intfunc1()) Q.setLimits((., .)) Q.setArgument(1, b) px =Q.integrate(); px end -.5648941335
Code:
cap mata: mata drop Q cap mata: mata drop Intfunc2() cap mata: mata drop S cap mata: mata drop Optfunc2() mata: real scalar Intfunc2(real scalar x, real scalar b) { return( logistic(b *x) *x *normalden(x,-.5,1) ) } end mata: Px =.35 Q =Quadrature() Q.setEvaluator(&Intfunc2()) Q.setLimits((., .)) void Optfunc(todo, b, Px, Q, v, g, H) { Q.setArgument(1, b) //problem begins from this line px =Q.integrate() v =( Px -px )^2 } S =optimize_init() optimize_init_evaluator(S, &Optfunc2()) optimize_init_which(S, "min") optimize_init_argument(S, 1, Px) optimize_init_argument(S, 2, Q) optimize_init_params(S, 0) b =optimize(S); b end
Thank you very much for your help.
Comment