Announcement

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

  • How to use locals in time series - garch loop model

    Hi

    I'm new to Stata and coding in general.
    I have tried making a code to generate an out of sample test of GARCH.
    The idea is to investigate the forecasting abilities of various GARCH-models, and compare them against the realised volatility. More precisely i'm trying to compare the forecasts of volatility.

    I will estimate the model through an in-sample period of about 10 years, and then do a rolling window forecast of the out-of-sample. (adding one and deleting one in the end -first in last out) So the period stays the same but with more and more out-of-sample forecasts included.

    I'm trying to end up with a list of forecasted variance or standard deviations that I can compare to the realised volatility. One for each forecasted step forward. The shocks to the economy are known, as the out-of-sample period is in the past.

    The return is given by the S&P500 index.

    Looking elsewhere I know I need to start with defining the time in locals, so I can simply move them forward when I re-estimate the model, but I can't seem to get this to work. I simply can't seem to get around it. My return is given by the variable name returnSP500

    Below is the code with the locals. The code is of course longer than that. But getting past the first four lines is already a problem.

    Code:
    // I want to be able to the sample one or five periods at a time. I thought I needed some macros to help me//
    //define where to stop the in-sample and start the out of sample from.//
    
    local start = 1 month_id  //I want to start my first in-sample when i'm in the first period of my dataset//
    local frequency = 5       //This should be how often the parameters are re-estimated in the out-of sample. I'm not sure I need it, and thinking about setting it to 1.//
    local end if month_id==2005m1  //End of in-sample period//
    
    
    //I Should do a loop, as I want Stata to give me the out-of-sample conditional variance with re-estimated parameters//
    
    foreach var of varlist returnSP500 {
    /* Start log */
    log using “`output’GARCH_predict_returnSP500.log”, replace
    
    //Estimating the in-sample parameters.//
    arch returnSP500 in `start'/`end', arch(1/1) garch(1/1)
    
    //and getting the conditional variances and mean//
    predict v_returnSP500, variance
    predict r_returnSP500, xb
    
    //Generating the parameters//
    gen arch_returnSP500 = [ARCH]_b[L.arch]
    gen garch_returnSP500 = [ARCH]_b[L.garch]
    gen cons_returnSP500 = [ARCH]_b[_cons]
    gen capture_returnSP500 = 0
    
    //moving one period forward//
    local time_r_returnSP500 = `start’ + `frequency’
    
    //as long as I'm still in real time//
    while `time_r_returnSP500’ < 2018m1
    Thank you
    Last edited by Anders Emil Nedergaard; 29 Jan 2018, 17:10.

  • #2
    Well, I don't know anything about arch/garch/forecasting time series and I have no real sense of what you are trying to accomplish. But I can tell you that your local macro definitions are clearly unsuited to purpose.

    Let's review what the command -local- does. The next word in the command is a name, and a macro of that name will either be created (if none exists) or overwritten (if it already exists). The contents of that macro will be set to whatever text follows in the rest of the command, unless the third word is an equals sign, in which case whatever follows in the rest of the command will be evaluated as a Stata expression, and the result converted to a string and placed in the local macro. So after you execute those three first commands you will have three local macros with the following contents:

    local macro start will contain "1 month_id"
    local macro frequency will contain "5"
    local macro end will contain "if month_id==2005m1"

    Except for the last of these, there is nothing illegal about them. It all depends on how you will later use them. (The last is already destined to trip a syntax error wherever you use it because 2005m1 by itself is illegal.)

    The first use of these I see are in the command
    Code:
    arch returnSP500 in `start'/`end', arch(1/1) garch(1/1) //gtolerance(`gtol’)//
    Now, when Stata encounters `whatever' in a command it looks for a local macro named whatever. If it finds one, it substitutes the contents of that macro for `whatever'. If it finds none, it replaces `whatever' with an empty string. So Stata trands lates this command to:

    Code:
    arch returnSP500 in 1 month_id/if month_id==2005ml, arch(1/1) garch(1/1) //gtolerance()//
    I think you can see that this is very mangled syntax. Since the syntax of -in- clauses in Stata is of the form -in #/#-, I'm guessing that what you really want here is for `start' to contain some number (maybe 1?) and `end' to contain some other number (I have no clue what number that might be). I suppose perhaps you also intend to restrict this command to observations with month_id coded for January 2005. But in that case there is no reason to try to put it in a macro. And either way, you have to use -tm(2005m1)- to have a legal Stata expression.

    So I hope this perhaps point you in the general direction to get started.

    One other thing, that may or may not apply here. In Stata it is seldom necessary to write your own code to create rolling windows (which, I think, is what you are trying to do here.) There is the official -rolling- command to do that for you. And there are the user-written -rangestat- and -rangejoin- and -rangerun- commands (all from SSC), which often serve the purpose even more effectively for certain types of application.

    Comment


    • #3
      Hi Clyde

      Thank you very much. You are right. The macros should contain numbers. The numbers for the start and end of the sampling period, and then onwards into the out-off-sample period.
      I will try to look into the build in commands.

      I want to create something, that I can automatically loop, without having to plot in the sampling period every time. So I thought I should give the variable for time (the monht_id) a macro. Don't know if that's the right way to go though?
      Last edited by Anders Emil Nedergaard; 29 Jan 2018, 17:18.

      Comment

      Working...
      X