<br><br><div class="gmail_quote">On Thu, Jul 21, 2011 at 1:18 PM, Ginters Bušs <span dir="ltr"><<a href="mailto:ginters.buss@gmail.com">ginters.buss@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Dear all,<br><br>I am inspired by the recent Allan's comment that I can vectorize for-loops.  <br><br>Say, I have a for-loop for AR(2) process y(t+1)=f1*y(t)+f2*y(t-1)+epsilon(t+1) with a degenerate disturbance term:<br>

<br>f1=1.2; f2=-0.5;<br>n=100000;<br>c=1:n;<br>c(2)=f1*c(1)+c(2);<br>for i=3:n;<br>    c(i)=c(i)+f1*c(i-1)+f2*c(i-2);<br>end<br><br>Allan proposed trying<br><br>f1=1.2; f2=-0.5;<br>n=100000;<br>b=1:n;<br>b(2)=f1*b(1)+b(2);<br>

b(3:n) = b(3:n)+f1*b(2:n-1)+f2*b(1:n-2);<br><br><br>but the result is different.  Then, I thought I can try to use ode; say, for AR(1) process y(t+1) = a*y(t)+ epsilon(t+1) it would look like<br><br>deff("yp=a_function(k,y)","yp=a*y+sigma*u(k)")<br>

y0=0; <br>a=.9; <br>n=100000;<br>u=1:n;<br>y=ode("discrete",y0,1,1:n,a_function);<br><br>but it turns out that for-loop is a bit faster than this ode code. <br><br>Another idea would be using Wold representation of (only stationary) AR process by rewriting the above AR(2) process as <br>

<br>y(t+1) = ((1-f1*L - f2*L^2)^(-1))*epsilon(t+1)<br><br>where (1-f1*L - f2*L^2) is a lag polynomial, L defined as Ly(t)=y(t-1)<br><br>and trying to get the series representation of  (1-f1*L - f2*L^2)^(-1) but I'm stuck here, and this would work only for a stationary process.<br>

<br>Do you have any ideas/experience with rewriting for-loops for AR or other recursive processes more efficiently than the above first code?<br><br><br><br><br><br><br><br>
</blockquote></div>Just clarifying that I'm using a degenerate disturbance term here for easy comparison; in real application, it would be some stochastic process.<br>