Announcement

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

  • Generate a time series.

    Dear all,

    I want to simulate a time series.

    Given x_0, y_0, z_0,

    x_t = a*y_t-1 + b*x_t-1;
    y_t = c*x_t;

    For example, a = 0.5, b = 0.8, c = 0.6; x_0 = 12, y_0 = 2; iterate the equations for t = 1...20.

    I tried that,
    x[_n] = a*y[_n-1] + b*x[_n-1];
    y[_n]= c*x[_n]

    It is wrong and returns an error, "weights not allowed". How to solve it?

    Thank you a lot.
    Thank you very much.

  • #2
    I tried that,
    x[_n] = a*y[_n-1] + b*x[_n-1];
    y[_n]= c*x[_n]

    It is wrong and returns an error, "weights not allowed". How to solve it?
    No, either that is not what you tried, or that is not what you got. If you actually tried that code, the error that Stata would complain about is "command x is unrecognized." I think I know what you actually did, and will give you some advice below. But in posting here it is important to show exactly what you did and exactly what you got, and not leave it to others to guess.

    I imagine you tried to run:
    Code:
    gen x[_n] = a*y[_n-1] + b*x[_n-1];
    gen y[_n]= c*x[_n]
    This will produce the error you refer to, because the subscripting [_n] is not permitted on the left side of = in Stata syntax. In addition, because of the nature of the recursion involved in your equations, you need to be sure that the value of y is updated in the n-th observation before you go on to update x in the n+1'th observation. You can't do that with consecutive commands on the entire data set, because each command is carried out on the entire data set before the next command is begun. So this is one of those rare situations in Stata where you need to loop over observations.

    To do what you want, you need the following:

    Code:
    clear*
    scalar a = 0.5
    scalar b = 0.8
    scalar c = 0.6
    
    set obs 21
    gen x = 12 in 1
    gen y = 2 in 1
    
    forvalues i = 2/`=_N' {
        replace x = a*y[_n-1] + b*x[_n-1] in `i'
        replace y = c*x in `i'
    }

    Comment


    • #3
      Dear Prof. Clyde,


      Thank you for your help and suggestion. I'll pay attention to it next time.
      For your sample code,
      I also post my try here,

      clear* // Why should we add a * here? What's the difference between it with "clear"?
      scalar a = 0.5
      scalar b = 0.8
      scalar c = 0.6

      set obs 21
      scalar x = 12 in 1
      scalar y = 2 in 1

      forvalues i = 2/21 {
      scalar x = a*y + b*x in `i'
      scalar y = c*x in `i'
      scalar list // How can we save the scalar into a file or transform it into variable?
      }

      Btw, is it correct to replace
      `=_N' by 21 in your code? What's the purpose of using the back quote and single quote mean here? Why do we use scalar here? Sorry for my long questions here. Thank you for your help and patience again.

      Best,

      Last edited by John Bai; 01 Oct 2018, 21:45.
      Thank you very much.

      Comment


      • #4
        It makes no sense to say -scalar whatever in `i'-. A scalar is not a Stata variable: it is a constant and constructs like -in `i'- or [_n] etc. do not apply to it. As it happens, -scalar whatever in `i'- is legal syntax, but it has no effect different from just -scalar whatever-.

        Your code will end up with x and y containing the final values of the process, but all the other values along the way will have been lost. Maybe you only need the final results, but you originally spoke of needing to create the time series. You created it, but you also destroyed it along the way and were left with only the endpoints.

        If you want x and y as variables, as you seem to imply at the end, use the code I showed in #2. It does exactly that.

        As for the * in clear*, it's not strictly necessary. -clear- just clears the data set in memory. clear* also clears value labels, matrices, scalars, constraints, postutil, programs timers, mata, and closes all open files. I have gotten into the habit of starting most of my code with -clear*- so that I don't mistakenly write a program that only works when some previous things were done or created. I like my programs to be self-contained and self-documenting so that anything that is needed for the calculation to work is contained right there in the program. I don't like programs that rely on hidden pre-requisites. So that's why I almost always use -clear*-. But the particular code here will run just fine without the * and it in no way depends on pre-existing structures of any kind.

        Comment


        • #5
          Dear Prof.
          Clyde,

          Thank you for your great help and patience. It seems very impossible to apply the method in my post to create a variable.
          Thank you very much.

          Comment


          • #6
            Originally posted by Clyde Schechter View Post
            No, either that is not what you tried, or that is not what you got. If you actually tried that code, the error that Stata would complain about is "command x is unrecognized." I think I know what you actually did, and will give you some advice below. But in posting here it is important to show exactly what you did and exactly what you got, and not leave it to others to guess.

            I imagine you tried to run:
            Code:
            gen x[_n] = a*y[_n-1] + b*x[_n-1];
            gen y[_n]= c*x[_n]
            This will produce the error you refer to, because the subscripting [_n] is not permitted on the left side of = in Stata syntax. In addition, because of the nature of the recursion involved in your equations, you need to be sure that the value of y is updated in the n-th observation before you go on to update x in the n+1'th observation. You can't do that with consecutive commands on the entire data set, because each command is carried out on the entire data set before the next command is begun. So this is one of those rare situations in Stata where you need to loop over observations.

            To do what you want, you need the following:

            Code:
            clear*
            scalar a = 0.5
            scalar b = 0.8
            scalar c = 0.6
            
            set obs 21
            gen x = 12 in 1
            gen y = 2 in 1
            
            forvalues i = 2/`=_N' {
            replace x = a*y[_n-1] + b*x[_n-1] in `i'
            replace y = c*x in `i'
            }

            Dear Clyde,

            I have a similar issue. I try to produce three time series variables that are dependent on each others observations in the following way:

            fv[n] = fv[n-1] - amort[n]
            amort[n] = pmt - c[n]
            c[n] = fv[n-1]*r

            given that pmt and r are constants/scalars and the first line of observations are available and defined. I found the above code very much related to my issue but unfortunately when I tried running it in stata it only produces the first observation of each x and y as 12 and 2, the rest is missing values. I would highly appreciate your guidance and comments regarding my similar issue. I have tried many different variations of loops which all turned out to do wrong calculations.

            Looking forward,
            Shadi

            Comment


            • #7
              Your question is not very clearly posed. You refer to observations of x and y as 12 and 2. What is the connection between this x and this y and the variables fv, amort, and c?

              In any case, to do this there are two things. You need a set of initial values for the first observation. I'll illustrate with code that starts with fv = 12, amort = 2, and c = 5. You also don't say what the values of the constants pmt and r are. I'll illustrate in the code with pmt = 7 and r = 0.03

              Next, you have to update the equations in the right order. If you try to do them in the order you have written them in, you will get just missing values. Look carefully at them. In order to update fv, you must already know the current value of amort. And in order to obtain the present value of amort you must know the present value of c. c, however, can be computed entirely from past values of fv (and the constant r). So you have to update these equations in the reverse order from how you wrote them.

              Code:
              clear*
              
              set obs 100
              
              // INITIAL VALUES
              gen fv = 12 in 1
              gen amort = 2 in 1
              gen c = 5 in 1
              
              // CONSTANTS
              local pmt = 7
              local r = .03
              
              // ITERATE THE TIME SERIES
              forvalues i = 2/`=_N' {
                  replace c = fv[_n-1]*`r' in `i'
                  replace amort = `pmt' - c in `i'
                  replace fv = fv[_n-1] - amort in `i'
              }

              Comment


              • #8
                Originally posted by Clyde Schechter View Post
                Your question is not very clearly posed. You refer to observations of x and y as 12 and 2. What is the connection between this x and this y and the variables fv, amort, and c?

                In any case, to do this there are two things. You need a set of initial values for the first observation. I'll illustrate with code that starts with fv = 12, amort = 2, and c = 5. You also don't say what the values of the constants pmt and r are. I'll illustrate in the code with pmt = 7 and r = 0.03

                Next, you have to update the equations in the right order. If you try to do them in the order you have written them in, you will get just missing values. Look carefully at them. In order to update fv, you must already know the current value of amort. And in order to obtain the present value of amort you must know the present value of c. c, however, can be computed entirely from past values of fv (and the constant r). So you have to update these equations in the reverse order from how you wrote them.

                Code:
                clear*
                
                set obs 100
                
                // INITIAL VALUES
                gen fv = 12 in 1
                gen amort = 2 in 1
                gen c = 5 in 1
                
                // CONSTANTS
                local pmt = 7
                local r = .03
                
                // ITERATE THE TIME SERIES
                forvalues i = 2/`=_N' {
                replace c = fv[_n-1]*`r' in `i'
                replace amort = `pmt' - c in `i'
                replace fv = fv[_n-1] - amort in `i'
                }
                Thank you very much, I learned something valuable from your post. It worked for me.

                Comment

                Working...
                X