-1

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 Answers1

0

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)
  • thank you. just one more question what is non vectorized for loop? – mindrani Jun 15 '15 at 00:59
  • Check http://www.noamross.net/blog/2014/4/16/vectorization-in-r--why.html and especially the resources. Obviously you do not have performance penalties for such small series. But for something more complicated the difference can be huge. – Konstantinos Jun 15 '15 at 01:06
  • Also: http://www.noamross.net/blog/2013/4/25/faster-talk.html – Konstantinos Jun 15 '15 at 01:12
  • I tried ur code. Can you tell me why it is generating values greater than 1 althoguh this functional form is supposed to create values between 1 and -1 – mindrani Jun 15 '15 at 01:20
  • It doesn't generate values outside $[-1,1]$. – Konstantinos Jun 15 '15 at 17:26