在Stata / Mata中进行优化

时间:2015-01-15 15:08:28

标签: stata

我想在Stata中用Mata命令编写一个循环' optimize'。基本语法是(在.do文件中):

mata: x=runiform(100,2)
mata: F=J(rows(x),1,3)
mata: X=J(1,2,48)
mata: I=J(rows(x),1,1)

mata:
    void mysolver(todo, p, x, X, I, F, lnf, S, H)
        {
        factor = F :* (I + x*p')
        factor_bis= factor , factor
        Cuenta = x :* factor_bis
        Final=I'*Cuenta
        vvv  = Final - X
        lnf  = (vvv*vvv')[1,1]
        }

mata:
    S = optimize_init()
    optimize_init_evaluator(S, &mysolver())
    optimize_init_evaluatortype(S, "v0")
    optimize_init_params(S, J(1,2,0.01))
    optimize_init_which(S,  "min" )
    optimize_init_argument(S, 1, x)
    optimize_init_argument(S, 2, X)
    optimize_init_argument(S, 3, I)
    optimize_init_argument(S, 4, F)
    optimize_init_tracelevel(S,"none")
    optimize_init_conv_ptol(S, 1e-16)
    optimize_init_conv_vtol(S, 1e-16)
    xx=optimize(S)

    st_matrix("param_estim",xx)

end

如何编写程序' optimizo'包含在循环中:

forvalues i=(1)500 {
   ..... 
   optimizo
    }

重复500次优化? (在我的应用中,矩阵在每个周期中都会发生变化)

谢谢。

1 个答案:

答案 0 :(得分:0)

在主代码中,而不是行

xx=optimize(S)

只需写下:

for (i = 1; i <= 500; i++) {

  [Do your matrix changes, call your optimize_init_argument commands if you need to change them]

  xx = optimize(S)
  xx
}