Announcement

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

  • Stata crashes while using loop within solver

    Hello,

    The following code crashes Stata silently and I cannot figure out why. If I replace w with a simple vector, the code executes with no problems, including the loop. But when I try to optimize, it almost instantly crashes in Stata 16.1 and Stata 17 with a Windows 10 machine. Any help would be appreciated.

    Code:
    mata
    
    void function solver(todo, w, lnf, retailmargin, H)
    {
    omega_p=ownership_p*Delta_p
    g=(ownership_v:*Delta_p')*w'
    
    retailmargin=-pinv(omega_p)*(ex_shares+g)
    
    
    D=J($models, $models, .)
    secondterm=J($models, $models, 0)
    fourthterm=J($models, $models, 0)
    thirdterm=J($models, $models, 0)
    
    for (j = 1; j <= $models; j++){
    for (k=1; k<=$models; k++){
    for (i=1; i <= $models; i++){
    secondterm[j,k]=secondterm[j,k]+ownership_p[i,j]*asarray(secder, i)[j,k]*retailmargin[i,1]
    fourthterm[j,k]=fourthterm[j,k]+ownership_v[i,j]*asarray(secder, i)[j,k]*w[1,i]
    }
    thirdterm[j,k]=ownership_p[k,j]*ex_shares_jacobian[k,j]
    D[j,k]=Delta_p[j,k]+secondterm[j,k]+thirdterm[j,k]+fourthterm[j,k]
    }
    }
    
    Delta_pw=pinv(D)*(Delta_ow*Delta_p)'
    Delta_w=Delta_p*Delta_pw
    h=(ownership_v':*Delta_w')*retailmargin+(ownership _v':*Delta_pw')*ex_shares
    
    s_term=ex_shares+h
    omega_w=ownership_w:*Delta_w
    lnf1 = w' + pinv(omega_w)*s_term
    lnf=lnf1:^2
    }
    
    M=optimize_init()
    optimize_init_evaluator(M, &solver())
    optimize_init_evaluatortype(M, "gf0")
    optimize_init_params(M, starting)
    optimize_init_which(M, "min")
    optimize_init_conv_ptol(M, 1e-6)
    optimize_init_conv_vtol(M, 1e-6)
    optimize_init_tracelevel(M,"value")
    optimize_init_conv_maxiter(M, 10)
    w=optimize(M)
    qui: optimize_result_V_oim(M)
    end

  • #2
    Thanks for the example. I will investigate and report back.

    Comment


    • #3
      There are several syntax issues in your current function which prevent me from running it:

      1: Please use mata: instead of mata especially when developing functions so Mata will stop at the first error instead of keep going, this helps identify and fix the coding issues.
      2: what is the value of global macro $models? If it is not defined, the line

      Code:
       D=J($models, $models, .)
      and some other lines using $models will not compile.

      3. There is an extra space in the line

      Code:
       h=(ownership_v':*Delta_w')*retailmargin+(ownership _v':*Delta_pw')*ex_shares
      between second ownership and _v. The code should be:

      Code:
       h=(ownership_v':*Delta_w')*retailmargin+(ownership_v':*Delta_pw')*ex_shares
      After fix the above, the function compiles but will not run at the line
      Code:
       optimize_init_params(M, starting)
      since starting is not defined. By the way, what do you mean by "crash", does Stata disappear unexpectedly or just display an error?

      Never mind, I get the code and dataset from your email to tech support. I will investigate based on that.
      Last edited by Hua Peng (StataCorp); 11 Jun 2021, 08:16.

      Comment


      • #4
        Thanks again for the example. The crash bug will be fixed in a future Stata update. Our tech support will provide a solution for other issues in the Mata function solver().

        Comment


        • #5
          Thanks for your response, Hua. I believe the spacing issue is one of copying into the forum. I see you got my email. Should I expect an email from tech support?

          I appreciate your quick response.

          Comment


          • #6
            Sure thing. A reply from tech support was sent out this afternoon.

            Comment

            Working...
            X