Announcement

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

  • S not found

    Hi,

    I am trying to write a simple optimization problem in mata. I have to find the vector of weights which minimizes a function (the other matrices are external and I label them as such in the code). However, for some reason it keeps returning an error that S is not found. Any ideas on what the problem could be?

    mata
    void triald1(todo,w, y, g, H)
    {
    external varcov,vol
    y = (w'*vol)*(w'*varcov*w)
    }
    end

    corrmat retioous retvxxus retjph retdbcus , covmat
    matrix define varcov=r(cov)
    matrix define vol=J(4,1,.)
    matrix define vol[1,1]=varcov[1,1]
    matrix define vol[2,1]=varcov[2,2]
    matrix define vol[3,1]=varcov[3,3]
    matrix define vol[4,1]=varcov[4,4]
    mata
    init=J(4,1,0)
    C=(1,1,1,1)
    c=(1)
    Cc=(C,c)

    S=optimize_init()
    optimize_init_constraints(S,Cc)
    optimize_init_evaluator(S, &triald1())
    optimize_init_which(S, "min" )
    optimize_init_evaluatortype(S,"d0")
    optimize_init_params(S,(0.5,0.5,0,0))
    p = optimize(S)
    p
    end

    Thanks

  • #2
    Iskra Sokolovska
    There are (at least) three problems

    1. there is a comformability error when computing y.
    2. varcov and vol are defined as Stata matrices, when they should be Mata objects (matrices in this case)
    3. varcov and vol have to be defined as external outside the function which use them as weel. As a matter of fact there should be defined as external whenever they are called inside a function or in interactive mode

    It should be
    Code:
    mata
    void triald1(todo,w, y, g, H)
    {
    external varcov,vol
    y = (w*vol)*(w*varcov*w') // <-change some of the transpose operators so the matrices are comformable.
    }
    end
    
    corrmat retioous retvxxus retjph retdbcus , covmat
    matrix define varcov=r(cov)
    matrix define vol=J(4,1,.)
    matrix define vol[1,1]=varcov[1,1]
    matrix define vol[2,1]=varcov[2,2]
    matrix define vol[3,1]=varcov[3,3]
    matrix define vol[4,1]=varcov[4,4]
    mata
    
    external varcov, vol // -> defined as external
    varcov = st_matrix("varcov") // -> defines a Mata matrix containing elements of the Stata matrix varcov 
    vol       = diagonal(varcov)    // -> get the diagonal of varcov and and put in the column vector vol. Matrix define vol is no longer necessary
    
    init=J(4,1,0)
    C=(1,1,1,1)
    c=(1)
    Cc=(C,c) 
    
    S=optimize_init()
    optimize_init_constraints(S,Cc)
    optimize_init_evaluator(S, &triald1())
    optimize_init_which(S, "min" )
    optimize_init_evaluatortype(S,"d0")
    optimize_init_params(S,(0.5,0.5,0,0))
    p = optimize(S)
    p
    end

    Comment


    • #3
      Thank you for the answer Christophe, it seems I haven;t received a notification on this earlier. So I tried to run this code and am getting either S not found when I run the whole code, or the same value at every repetition when I run only the loewr sequence (from corrmat on) , regardless of which input variables I use.

      Any ideas on why it is storing one and the same result the whole time? And why is it still showing S not found, only when I run the whole code? This seems like it should be a simple optimization problem...?

      Thank you

      Comment

      Working...
      X