Announcement

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

  • #16
    Originally posted by William Lisowski View Post
    Without seeing code it's hard to guess the problem, but the error message is typically given when you reference a temporary variable that does not exist at the time you reference it.
    Code:
    g t=_n
    tsset t
    
    sum grcon2_q
    sum grcon2_q , detail
    gen double ret=grcon2_q
    
    generate double et = .
    generate double ht = .
    generate double nt = .
    generate double st = .
    generate double kt = .
    
    
    drop if _n==1
    capture program drop garchsk
    program garchsk
            version 17
            args lnf a1 b0 b1 b2 c0 c1 c2 g0 g1 g2
            tempvar et yt ht nt st kt psi gam
            
                    
            * initialize variables
            qui gen double `yt'=$ML_y1
            qui replace `et'=0.0
    
            qui sum `yt', detail
            qui replace  `ht'=r(Var)
            qui replace  `nt'=`et'/sqrt(`ht')
            qui replace  `st'=r(skewness)
            qui replace  `kt'=r(kurtosis)
    
            * make garch11 for var, skew , kurtosis.  There is AR1 in the mean equation (for et)
            qui replace `et'=`yt'-`a1'*`yt'[_n-1] if _n>1
            qui replace `ht'=`b0'+`b1'*(`et'[_n-1])^2 + `b2'*`ht'[_n-1] if _n>1
            qui replace `nt'=`et'/sqrt(`ht') if _n>1
            qui replace `st'=`c0'+`c1'*(`nt'[_n-1])^3 + `c2'*`st'[_n-1] if _n>1
            qui replace `kt'=`g0'+`g1'*(`nt'[_n-1])^4 + `g2'*`kt'[_n-1] if _n>1
            
                    
            * make Gram-Charlier liklelihood(lnf)    
            qui gen double `psi'= 1 + (`st'/6) * ((`nt'^3)-(3*`nt'))+((`kt'-3)/24)*(`nt'^4 - (6*`nt'^2) + 3)
            qui gen double `gam'= 1+ ((`st'^2)/6) +(((`kt'-3)^2)/24)
            qui replace `lnf'= -0.5*ln(`ht')-0.5*(`nt'^2) +ln(`psi'^2)-ln(`gam')        
                    
                    
    end
    
    ml model lf garchsk (a1: ret= ) /b0 /b1 /b2 /c0 /c1 /c2 /g0 /g1 /g2,  technique(bhhh) robust  
    
    ml init  /b0=0.0023 /b1=0.0387 /b2=0.9586 /c0=-0.0458 /c1=0.0085 /c2=-0.0227 /g0=3.0471 /g1=0.0055 /g2=0.0882  
    
    ml search
    ml max
    
    
    ml model lf garchsk (a1: ret= ) /b0 /b1 /b2 /c0 /c1 /c2 /g0 /g1 /g2,  technique(bhhh) robust
    
    ml init  /b0=0.0023 /b1=0.0387 /b2=0.9586 /c0=-0.0458 /c1=0.0085 /c2=-0.0227 /g0=3.0471 /g1=0.0055 /g2=0.0882  
    
    ml report
    I'm so sorry for the inconvenience, all what I need to do is to obtain the values of each of the linear predictors of each equation (ht, st, and kt ) from the above program. so I would have a time varying conditional variance, skewness and kurtosis, so I can use that in my research

    Thanks in advance,
    Last edited by Sarah Hussein; 22 Aug 2021, 15:30.

    Comment


    • #17
      I am sorry to be so long in replying; I had thought perhaps someone more familiar with ml would. But by putting it aside, on returning to it I have seen the problem.

      The recommendation in post #13 was not only to create non-temporary variables outside of your garchsk program, but then to replace the use of the temporary variables inside garchsk with those non-temporary variables. As it now stands, the first to references to et inside garchsk are
      Code:
              tempvar et yt ht nt st kt psi gam
              
                      
              * initialize variables
              qui gen double `yt'=$ML_y1
              qui replace `et'=0.0
      The problem is, the tempvar command does not create the temporary variable, it just creates the distinct temporary name. You cannot replace the temporary variable before you have created the variable itself. That is the source of your error message.

      However, this is the code post #13 intended you to try, where I have tried to highlight in red every change I made to the code in post #16 (for brevity I omitted commands before and after this block; nothing would change there).
      Code:
      generate double et = .
      generate double ht = .
      generate double nt = .
      generate double st = .
      generate double kt = .
      
      
      drop if _n==1
      capture program drop garchsk
      program garchsk
              version 17
              args lnf a1 b0 b1 b2 c0 c1 c2 g0 g1 g2
              tempvar yt psi gam
              
                      
              * initialize variables
              qui gen double `yt'=$ML_y1
              qui replace et=0.0
      
              qui sum `yt', detail
              qui replace  ht=r(Var)
              qui replace  nt=et/sqrt(ht)
              qui replace  st=r(skewness)
              qui replace  kt=r(kurtosis)
      
              * make garch11 for var, skew , kurtosis.  There is AR1 in the mean equation (for et)
              qui replace et=`yt'-`a1'*`yt'[_n-1] if _n>1
              qui replace ht=`b0'+`b1'*(et[_n-1])^2 + `b2'*ht[_n-1] if _n>1
              qui replace nt=et/sqrt(ht) if _n>1
              qui replace st=`c0'+`c1'*(nt[_n-1])^3 + `c2'*st[_n-1] if _n>1
              qui replace kt=`g0'+`g1'*(nt[_n-1])^4 + `g2'*kt[_n-1] if _n>1
              
                      
              * make Gram-Charlier liklelihood(lnf)    
              qui gen double `psi'= 1 + (st/6) * ((nt^3)-(3*nt))+((kt-3)/24)*(nt^4 - (6*nt^2) + 3)
              qui gen double `gam'= 1+ ((st^2)/6) +(((kt-3)^2)/24)
              qui replace `lnf'= -0.5*ln(ht)-0.5*(nt^2) +ln(`psi'^2)-ln(`gam')        
                      
                      
      end
      Doing this means that after your code has run, your dataset contains variables et, ht, nt, st, and kt with the values calculated during the most recent run of the garchsk program, which presumably is on the last iteration of ml.

      Now this is where my expertise ends. As a non-user of ml, I don't know if this will solve your underlying problem or not. But on the face of it, if you need the values of these five variables after the program has run, it makes sense to create them in permanent variables rather than struggle to recreate them outside of the program.
      Last edited by William Lisowski; 28 Aug 2021, 09:20.

      Comment


      • #18
        Thanks so much Mr. @William Lisowski, it worked.

        Comment

        Working...
        X