creating time series

57 Views Asked by At

I want to generate a time series length 400. I used the following code

beta1=0
e=rnorm(1)
out=matrix(NA,400,1)
for (i in 1:400){
beta1[i+1]=((exp(0.3*beta1[i]+e)-1)/(1+exp(0.3*beta1[i]+e)))
out[i]=beta1[i]
}
beta1=out
beta1=ts(beta1)

but the series that is returned shows no variation. need some help on this.

1

There are 1 best solutions below

5
On

First, using = is not a nice practice. Use <- instead (or assign()).

Then format your script properly so as to look pretty: using spaces helps!

Also, having exponential and 'e' named variables can be confusing.

Using non-vectorized for loops is also not a good practice, since they are slow.

To answer the question, you do not have enough variance because the series is converging, because of its functional form, which is

$$ x_{t+1} = \dfrac{e^{0.3 \cdot x_t + \epsilon} - 1}{e^{0.3 \cdot x_t + \epsilon} + 1 }, \qquad x_0 = 0$$

Try this:

beta <- 0
out_matrix <- matrix(NA, 400, 1)
epsilon <- rnorm(1)
for (i in 1:400) { 
   ifelse( i == 1, 
              out_matrix[i] <- beta, 
              out_matrix[i] <- ( ( exp( 0.3 * out_matrix[i-1] + epsilon     ) - 1 ) / ( exp(0.3 * out_matrix[i-1] + epsilon ) + 1 ) )
   )
} 
out_matrix
plot.ts(out_matrix)