Announcement

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

  • _st_store not saving data

    Dear Statalist members,

    would be great to have some help! I am relatively new to Stata.

    I have built a function cdsP in Mata. I would like to save the results of the function in "cds_model".

    I am using st_store, but my code does not seem to save the results of the function (price)

    Any help would be greatly appreciated!

    Thanks

    Luca

    Code:
    mata
    
    
    mata clear
    
    
    
    
    real scalar cdsP(real scalar alpha1, real scalar beta, real scalar sigma, real scalar ni, real scalar rr, real scalar lambda, real scalar X)
    {
                        real matrix A, B, C, D, G, H, D1, CDS_model_timesteps, GG
                        real scalar phi, K, price
                                                
                        
                        A = J(1,20,0)
                        B = J(1,20,0)
                        C = J(1,20,0)
                        D = J(1,20,0)
                        G = J(1,20,0)
                        H = J(1,20,0)
                        D1 = st_data(.,("c2-c241"))
                        Num = J(1,20,0)
                        Dem = J(1,20,0)
                                    
                        
                        for (z = 1; z<=20; z++) {
                    
                                phi = (2*sigma^2 + beta^2)^0.5
                                                                                 
                                K = (beta + phi)/(beta - phi)
                    
                                C[1, z] = exp((ni^2 * (z*0.25)^3 )/ 6)
                                                     
                                B[1, z]  = (beta - phi)/sigma^2 + (2*phi)/(sigma^2 * ((1 - K)* exp(phi * (z*0.25))))
                                                         
                                A[1, z] = exp(((alpha1*(beta + phi))/(sigma^2)) * (z*0.25))*((1 - K)/((1 -K) * exp(phi*(z*0.25))))^((2*alpha1)/sigma^2)
                                                         
                                H[1, z] = exp((alpha1*(beta+ phi)+ phi* sigma^2)/ sigma^2 *(z*0.25))*((1 - K)/(1 - K* exp(phi*(z*0.25))))^((2*alpha1)/sigma^2 +2)
                                
                                D[1, z] = D1[X,z+1]
                                
                                G[1, z] = (alpha1/phi) * (exp(phi * (z*0.25))-1) * exp((alpha1 * (beta+phi))/(sigma^2) * (z*0.25))*((1 - K)/((1 - K) * exp(phi*(z*0.25))))^((2 * alpha1)/(sigma^2) +1)
                    
                                Num[1,z] = (exp(B[1,z]*lambda)*D[1,z] *(G[1,z] + H[1,z] * lambda))
                                
                                Dem[1,z] = (A[1,z]*exp(B[1,z] *lambda)*D[1,z])
                    
                    
                                }        
    
                        CDS_model_timesteps = ((1-rr)*Num):/Dem
                        
                        price = rowsum(CDS_model_timesteps)
                        
                                            
                        return(price)
                        
                        _st_store(X,"cds_model",price)
                        
                                             
    }
                        
                        
    
     
     
        
    end

  • #2
    It never gets that far. At the command line in Stata, type
    Code:
    help m2_return

    Comment


    • #3
      Many thanks Joseph, you are obviously right... still learning my way through.

      My aim was to return the value in order to add a control check as I am looping several thousands variables.

      Is there a way to both store the result of my function and display its results so to check the evolution of the code?

      Thanks

      Comment


      • #4
        Originally posted by Luca Blasi View Post
        Is there a way to both store the result of my function and display its results so to check the evolution of the code?
        Try inverting the order of the two lines' executions.

        Comment


        • #5
          Many thanks Joseph. It is always surprising as the answer is so simple!

          Anyway, the code is working now...

          Great to have a strong Stata community available

          Comment

          Working...
          X