Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • using optimize() to solve optimal weights

    Hello,

    i'm trying to solve the following optimization problem:

    max F=x7*(v1*x1+v2*x2+v3*x3+v4*x4+v5*x5+v6*x6)^-1

    x1-x7: financial variables

    v1-v6: weights which i 'm trying to solve to make the "F" to maximize

    F: it takes a value between 0 and 1

    here is the code which i just revise the code from stata's example

    Code:

    mata:
    A=st_data(.,"x1")
    B=st_data(.,"x2")
    C=st_data(.,"x3")
    D=st_data(.,"x4")
    E=st_data(.,"x5")
    I=st_data(.,"x6")
    J=st_data(.,"x7")

    void solve(todo,v, f, g, H)
    {
    external A,B,C,D,E,I,J
    f = J*(v[1]'*A +v[2]'*B +v[3]'*C +v[4]'*D +v[5]'*E +v[6]'*I)^-1
    }

    S=optimize_init()
    optimize_init_evaluator(S,&solve())
    optimize_init_params(S,(0))
    optimize_init_which(S,"max")
    v=optimize(S)
    v
    end


    stata shows that there is an error: <istmt>: 3499 S not found

    Could someone tell me where is the problem? or how do i revise the code?

    thank you in advance

  • #2
    Sarah Yang --

    Your code on the surface seems fine (barring some problem with identification). The one thing is you might want to fix the negative exponent so you have:

    Code:
    f = J*(v[1]'*A +v[2]'*B +v[3]'*C +v[4]'*D +v[5]'*E +v[6]'*I)^(-1)
    Also, sometimes I find that Stata runs "too fast" through the model statement, so breaking it up so that you do:

    Code:
    mata:
    A=st_data(.,"x1")
    B=st_data(.,"x2")
    C=st_data(.,"x3")
    D=st_data(.,"x4")
    E=st_data(.,"x5")
    I=st_data(.,"x6")
    J=st_data(.,"x7")
    
    void solve(todo,v, f, g, H)
    {
    external A,B,C,D,E,I,J
    f = J*(v[1]'*A +v[2]'*B +v[3]'*C +v[4]'*D +v[5]'*E +v[6]'*I)^-1
    }
    end
    
    mata:
    S=optimize_init()
    optimize_init_evaluator(S,&solve())
    optimize_init_params(S,(0))
    optimize_init_which(S,"max")
    v=optimize(S)
    v
    end
    This sometimes helps, oddly.

    Matthew J. Baker

    Comment

    Working...
    X