<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://eclr.humanities.manchester.ac.uk/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=AS</id>
		<title>ECLR - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://eclr.humanities.manchester.ac.uk/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=AS"/>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php/Special:Contributions/AS"/>
		<updated>2026-04-26T22:36:29Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.30.1</generator>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Temp&amp;diff=4209</id>
		<title>Temp</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Temp&amp;diff=4209"/>
				<updated>2017-10-06T12:07:27Z</updated>
		
		<summary type="html">&lt;p&gt;AS: AS moved page Monte-Carlo to Temp: AH request&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Monte Carlo Basics =&lt;br /&gt;
&lt;br /&gt;
Quite often we would like to know which of the proposed estimators is “better” and under what conditions. The exact answer (for any sample size) to these questions is known only for a quite limited class of estimators, distributions of error terms and regression functional forms. We can address these questions using various Central Limit Theorems. However, the applicability of our results relies on a “sufficiently large” number of observations for asymptotics to kick in. The question “When &amp;#039;&amp;#039;sufficiently large&amp;#039;&amp;#039; is &amp;#039;&amp;#039;large enough&amp;#039;&amp;#039;?” is a very interesting one, but it lies well beyond the scope of this discussion. In small samples asymptotics does not work. Unfortunately, this is exactly the area where all real-world applications belong to.&lt;br /&gt;
&lt;br /&gt;
Monte Carlo method can somewhat address this issue. However, it has its own problems. Assume that we would like to know how the conditional quasi-maximum likelihood estimator (estimator A) performs compared to the unconditional quasi-maximum likelihood estimator (estimator B) for AR(1) process with AR coefficient &amp;lt;math&amp;gt;\alpha=.5&amp;lt;/math&amp;gt;, sample size &amp;lt;math&amp;gt;T=50&amp;lt;/math&amp;gt; and t-distributed error terms with 10 degrees of freedom. On one hand, using MC method, we can reliably test the hypotheses that, for example, estimator A has smaller bias and/or smaller variance than estimator B. However, this answer, strictly speaking, can be applied &amp;#039;&amp;#039;&amp;#039;only&amp;#039;&amp;#039;&amp;#039; for the case of &amp;lt;math&amp;gt;\alpha=.5&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;T=50&amp;lt;/math&amp;gt;, and t-disributed error term with 10 degrees of freedom. Once the sample size, autoregressive coefficient, degrees of freedom or distribution is changed, results might change significantly as well.&lt;br /&gt;
&lt;br /&gt;
The idea behind the Monte Carlo Method is very simple. Assume for a moment that you can draw somehow observations from an (un)known distribution of interest. Then, by drawing sufficiently large number of observations from this distribution, you can approximate with an arbitrary level of precision:&lt;br /&gt;
&lt;br /&gt;
# finite population moments of any functions of random variables&lt;br /&gt;
# CDF and/or pdf and thus quantiles of interest&lt;br /&gt;
&lt;br /&gt;
As an example, for the case when the distribution is known and random number generator is readily available, we can compute&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;E(|X|),E(|X|^3) \text{ for } X\sim N(0,1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Formally, taking integrals we’ll get:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty} |x|e^{-x^2/2}dx=\frac{2}{\sqrt{2\pi}}\int_{0}^{\infty} e^{-x^2/2}d(x^2/2)=\sqrt{\frac{2}{\pi}}\approx 0.7979&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty} |x|^3e^{-x^2/2}dx=\frac{2}{\sqrt{2\pi}}\int_{0}^{\infty} x^2e^{-x^2/2}d(x^2/2)=2\sqrt{\frac{2}{\pi}}\approx  1.5958&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively, the Monte-Carlo algorithm to estimate &amp;lt;math&amp;gt;E(|X|)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;E(|X|^3)&amp;lt;/math&amp;gt; is:&lt;br /&gt;
&lt;br /&gt;
# Draw a large number (say, 10000000) of observations from a normal distribution &amp;lt;math&amp;gt;N(0,1)&amp;lt;/math&amp;gt; and compute &amp;lt;math&amp;gt;|x|&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;|x|^3&amp;lt;/math&amp;gt; for each of them.&lt;br /&gt;
# Take averages and report results&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;rng(1);x=randn(10000000,1);&lt;br /&gt;
absx=abs(x);&lt;br /&gt;
absx3=absx.^3;&lt;br /&gt;
disp([mean(absx) mean(absx3)])&lt;br /&gt;
     0.7982    1.5973&amp;lt;/source&amp;gt;&lt;br /&gt;
Which is quite close to the analytically evaluated ones. The more observations you take, the closer your results to the analytical ones will be.&lt;br /&gt;
&lt;br /&gt;
However, in reality, the random number generator for the distribution of interest is not readily available. Quite often you have to construct it first.&lt;br /&gt;
&lt;br /&gt;
= OLS properties of AR(1) process estimator =&lt;br /&gt;
&lt;br /&gt;
Consider a very simple example: (un)biasedness of OLS estimator while applying it to AR(1) process&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;y_t=\alpha y_{t-1}+e_t,\ e_t\sim N(0,\sigma^2)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Recall that the OLS estimator for regression&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;y=X\beta+e, e\sim (0,\sigma^2_e I)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;math&amp;gt;I&amp;lt;/math&amp;gt; is an identity matrix, is &amp;lt;math&amp;gt;\hat \beta=(X&amp;#039;X)^{-1}X&amp;#039;y=\beta+(X&amp;#039;X)^{-1}X&amp;#039;e&amp;lt;/math&amp;gt; Unbiasedness of OLS estimator, assuming &amp;lt;math&amp;gt;(X&amp;#039;X)^{-1}&amp;lt;/math&amp;gt; exists, is satisfied as long as &amp;lt;math&amp;gt;E[(X&amp;#039;X)^{-1}X&amp;#039;e]=0&amp;lt;/math&amp;gt;. Sufficient condition for this to hold is &amp;lt;math&amp;gt;E(e|X)=0&amp;lt;/math&amp;gt;. While this is quite often the case for cross-sectional applications, this is definitely not the case for Time Series. The condition &amp;lt;math&amp;gt;E(e|X)=0&amp;lt;/math&amp;gt; has to be understood in the following way: &amp;#039;&amp;#039;any&amp;#039;&amp;#039; error term &amp;lt;math&amp;gt;e_s&amp;lt;/math&amp;gt; in this regression has to be independent from &amp;#039;&amp;#039;any&amp;#039;&amp;#039; &amp;lt;math&amp;gt;x_t&amp;lt;/math&amp;gt;. For AR processes, for obvious reasons, this is not the case. For example, &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; is not independent from &amp;lt;math&amp;gt;x_{t+1}&amp;lt;/math&amp;gt; as &amp;lt;math&amp;gt;x_{t+1}&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;y_t&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;Cov(x_{t+1},e_t)=\sigma_e^2&amp;lt;/math&amp;gt;. Moreover, &amp;lt;math&amp;gt;Cov(x_{t+s},e_t)=\alpha^s\sigma_e^2&amp;lt;/math&amp;gt; and, thus the larger &amp;lt;math&amp;gt;|\alpha|&amp;lt;/math&amp;gt;, the larger, presumably, has to be the bias. On the other hand, for consistency, it is sufficient that&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\label{consistency}&lt;br /&gt;
(X&amp;#039;X/T)^{-1} \Rightarrow Q,\ X&amp;#039;e/T \Rightarrow 0&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
• where &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; is a full-rank finite matrix. Since these conditions are satisfied for covariance-stationary processes with gaussian increments, we expect that the bias will become smaller and smaller as the sample size increases.&lt;br /&gt;
&lt;br /&gt;
Let’s sketch the algorithm:&lt;br /&gt;
&lt;br /&gt;
# Generate &amp;lt;math&amp;gt;y_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;T=20&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\alpha=0&amp;lt;/math&amp;gt; using the following data generating process: &amp;lt;math&amp;gt;y_t=\alpha y_{t-1}+e_t&amp;lt;/math&amp;gt;&lt;br /&gt;
# Estimate and save &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; using OLS (Please note, these steps correspond to the first step from the previous algorithm “draw a large number of observations from a distribution of interest)&lt;br /&gt;
# Repeat pp 1-2 1000 times, compute &amp;lt;math&amp;gt;\bar{\hat{\alpha}}&amp;lt;/math&amp;gt; and bias&lt;br /&gt;
# Repeat pp 1-3 for &amp;lt;math&amp;gt;\alpha=0.1, 0.2, ... , 0.9, 0.99&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat pp 1-4 for &amp;lt;math&amp;gt;T=50,100,200,1000&amp;lt;/math&amp;gt;&lt;br /&gt;
# Plot results for different sample sizes&lt;br /&gt;
&lt;br /&gt;
Before implementing the algorithm, we have to take care of one additional issue, i.e. sample simulation (p. 1). If we know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;, we can generate &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt; drawing an observation from a normal distribution (using normal random number generator &amp;lt;code&amp;gt;randn&amp;lt;/code&amp;gt;) and proceed to &amp;lt;math&amp;gt;y_T&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1&amp;amp;=\alpha y_{0}+e_1\\&lt;br /&gt;
y_2&amp;amp;=\alpha y_{1}+e_2\\&lt;br /&gt;
\ldots\\&lt;br /&gt;
y_T&amp;amp;=\alpha y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
• Unfortunately, &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is unknown. &amp;#039;&amp;#039;Please note: The whole discussion regarding &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; below applies for processes with finite memory only. Compare with Random Walk.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
One way to handle this problem is &amp;#039;&amp;#039;to pretend that &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is known&amp;#039;&amp;#039;, i.e. to assume that &amp;lt;math&amp;gt;y_0=E(y)=0&amp;lt;/math&amp;gt;. Then, &amp;lt;math&amp;gt;y_1=e_1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;y_2=\alpha e_1+e_2&amp;lt;/math&amp;gt;, etc. However, this approach has an additional issue. For AR(1) process &amp;lt;math&amp;gt;Var(y_t)=\frac{\sigma_e^2}{1-\alpha^2}&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;t&amp;lt;/math&amp;gt;. If we assume that &amp;lt;math&amp;gt;y_0=0&amp;lt;/math&amp;gt;, however, &amp;lt;math&amp;gt;Var(y_1)=Var(e_1)=\sigma_e^2&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;Var(y_2)=\sigma_e^2(1+\alpha^2)&amp;lt;/math&amp;gt;, and for arbitrary &amp;lt;math&amp;gt;t&amp;gt;0&amp;lt;/math&amp;gt;: &amp;lt;math&amp;gt;Var(y_t)=\sigma_e^2\sum_{i=0}^{t-1}\alpha^{2(t-1)}=\sigma^2_e\frac{1-\alpha^{2t}}{1-\alpha^2}&amp;lt;/math&amp;gt;. For a reasonably large &amp;lt;math&amp;gt;t&amp;lt;/math&amp;gt;: &amp;lt;math&amp;gt;V(y_t)\approx\frac{\sigma^2_e}{1-\alpha^2}&amp;lt;/math&amp;gt;. Thus, if we decide to implement this algorithm, we have to start recording &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; not from the first observation, but from some non-zero &amp;lt;math&amp;gt;T_0&amp;lt;/math&amp;gt; s.t. &amp;lt;math&amp;gt;\alpha^{2T_0}\approx 0&amp;lt;/math&amp;gt;. Obviously, &amp;lt;math&amp;gt;T_0&amp;lt;/math&amp;gt; depends on &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt;. The larger &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt;, the larger &amp;lt;math&amp;gt;T_0&amp;lt;/math&amp;gt; should be. The thrown away part is sometimes called a “burn-in” period.&lt;br /&gt;
&lt;br /&gt;
Alternatively, we can take into account the fact that &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is not known and generate &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt; directly with &amp;lt;math&amp;gt;E(y_1)=0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;Var(y_1)=\frac{\sigma^2_e}{1-\alpha^2}&amp;lt;/math&amp;gt;, i.e. &amp;lt;math&amp;gt;y_1=e_1/\sqrt{1-\alpha^2}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For stationary processes the first approach does not require any analytical derivations, i.e. any knowledge of unconditional and various conditional population moments of the process. The second one runs faster. However, the second method has its own issues. It is easy to implement it for AR(1) process. For ARMA(p,q) the amount of effort increases significantly. For stochastic volatility models it might become unbearable.&lt;br /&gt;
&lt;br /&gt;
I will focus on the first version. &amp;lt;math&amp;gt;T_0=200&amp;lt;/math&amp;gt; is sufficiently large for almost all values of &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; considered. The p.1 of the algorithm becomes:&lt;br /&gt;
&lt;br /&gt;
# Generate &amp;lt;math&amp;gt;y_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;T=20&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\alpha=0&amp;lt;/math&amp;gt;&lt;br /&gt;
## Draw &amp;lt;math&amp;gt;T+T_0&amp;lt;/math&amp;gt; observations from &amp;lt;math&amp;gt;N(0,1)&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_0=200&amp;lt;/math&amp;gt;, save them in a vector &amp;lt;math&amp;gt;e&amp;lt;/math&amp;gt; and assume &amp;lt;math&amp;gt;z_0=0&amp;lt;/math&amp;gt;&lt;br /&gt;
## Construct &amp;lt;math&amp;gt;z_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1&amp;lt;/math&amp;gt;: &amp;lt;math&amp;gt;z_t=\alpha z_{t-1}+e_t&amp;lt;/math&amp;gt;&lt;br /&gt;
## Repeat 1.b) for &amp;lt;math&amp;gt;t=2,3,\ldots,T+T_0&amp;lt;/math&amp;gt;&lt;br /&gt;
## Save the last T observations as vector &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To make the code easier to understand, we will write a set of functions for these steps. (Please see the section about functions). Also, we will use &amp;lt;code&amp;gt;for .. end&amp;lt;/code&amp;gt; loop here. Please see the section about loops.&lt;br /&gt;
&lt;br /&gt;
Lets implement the first step, i.e. generation of a sample of size &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;. This function has to have three parameters: alpha, &amp;lt;math&amp;gt;T_0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;. We call it dgp, i.e. data generating process.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;function y=dgp(alpha,T0,T)&lt;br /&gt;
Tt=T+T0;&lt;br /&gt;
e=randn(Tt,1); %1 a)&lt;br /&gt;
z=zeros(Tt,1);&lt;br /&gt;
z(1)=e(1); %since z_0=0&lt;br /&gt;
for t=2:Tt&lt;br /&gt;
    z(t)=alpha*z(t-1)+e(t);&lt;br /&gt;
end&lt;br /&gt;
y=z(end-T+1:end); %dropping first T0 observations&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
The next step is to construct an estimate for &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;function alpha=myar1(y)&lt;br /&gt;
X=y(1:end-1);%specifying X&lt;br /&gt;
y=y(2:end);%specifying y&lt;br /&gt;
alpha=inv(X&amp;#039;*X)*X&amp;#039;*y; %Estimating AR(1) parameter&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
The next step is to repeat these steps 1000 times, compute mean, bias and standard errors of &amp;lt;math&amp;gt;\hat\alpha&amp;lt;/math&amp;gt;. For the sake of generality, this function will depend on &amp;lt;math&amp;gt;T_0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; - number of replication, in our case 1000.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;function [meanalpha biasalpha stdalpha]=MC(alpha,T0,T,N)&lt;br /&gt;
 for i=N:-1:1&lt;br /&gt;
 %This way we don&amp;#039;t need to initialize alphahat vector prior to the cycle&lt;br /&gt;
    y=dgp(alpha,T0,T);&lt;br /&gt;
    alphahat(i,1)=myar1(y);&lt;br /&gt;
 end&lt;br /&gt;
meanalpha=mean(alphahat);&lt;br /&gt;
biasalpha=mean(alphahat-alpha);&lt;br /&gt;
stdalpha=std(alphahat);&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
The next step is to repeat our simulations for different sample sizes and different alphas. The easiest way is to create two vectors of values, one for different sample sizes, one for different alphas, and loop over all possible combinations of them:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;alphavec=[0:.1:.9 .99];&lt;br /&gt;
Tvec=[20 50 100 200 1000];&lt;br /&gt;
T0=200; %burn-in period&lt;br /&gt;
N=1000; %number of iterations&lt;br /&gt;
rng(1); %for results replicability&lt;br /&gt;
for Tind=1:length(Tvec)&lt;br /&gt;
  T=Tvec(Tind); %picking the right T&lt;br /&gt;
  for alphaind=1:length(alphavec)&lt;br /&gt;
      alpha=alphavec(alphaind); %picking the right alpha&lt;br /&gt;
      [meanalpha(alphaind,Tind) biasalpha(alphaind,Tind) stdalpha(alphaind,Tind)]=MC(alpha,T0,T,N);&lt;br /&gt;
  end&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
And the last, but not the least, step is to plot our results:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;plot(alphavec,biasalpha)&lt;br /&gt;
title(&amp;#039;Bias as a function of sample size and $\alpha$&amp;#039;,&amp;#039;interpreter&amp;#039;,&amp;#039;latex&amp;#039;)&lt;br /&gt;
xlabel(&amp;#039;$\alpha$&amp;#039;,&amp;#039;interpreter&amp;#039;,&amp;#039;latex&amp;#039;)&lt;br /&gt;
ylabel(&amp;#039;Bias of $\hat\alpha$&amp;#039;,&amp;#039;interpreter&amp;#039;,&amp;#039;latex&amp;#039;)&lt;br /&gt;
legend({&amp;#039;T=20&amp;#039;,&amp;#039;T=50&amp;#039;,&amp;#039;T=100&amp;#039;, &amp;#039;T=200&amp;#039;, &amp;#039;T=1000&amp;#039;})&lt;br /&gt;
figure&lt;br /&gt;
plot(alphavec,stdalpha)&lt;br /&gt;
title(&amp;#039;Standard error of $\hat\alpha$ as a function of sample size and $\alpha$&amp;#039;,&amp;#039;interpreter&amp;#039;,&amp;#039;latex&amp;#039;)&lt;br /&gt;
xlabel(&amp;#039;$\alpha$&amp;#039;,&amp;#039;interpreter&amp;#039;,&amp;#039;latex&amp;#039;)&lt;br /&gt;
ylabel(&amp;#039;Std of $\hat\alpha$&amp;#039;,&amp;#039;interpreter&amp;#039;,&amp;#039;latex&amp;#039;)&lt;br /&gt;
legend({&amp;#039;T=20&amp;#039;,&amp;#039;T=50&amp;#039;,&amp;#039;T=100&amp;#039;, &amp;#039;T=200&amp;#039;, &amp;#039;T=1000&amp;#039;})&amp;lt;/source&amp;gt;&lt;br /&gt;
The next two figures are generated using the code above. To create a smoother picture, we ran simulations for &amp;lt;math&amp;gt;N=10000&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\alpha=0,0.05,0.1,0,15,\ldots,0.95,0.99&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:bias.png|image|thumb|center|upright=3.0|alt=Bias of AR(1) Estimation.|Dependence of Bias on $\alpha$.]]&lt;br /&gt;
&lt;br /&gt;
[[Image:std.png|image|thumb|center|upright=4.0|alt=St. deviation of AR(1) Estimation.|Dependence of std on $\alpha$.]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some of the findings coincide with our predictions, some of them do not. In particular,&lt;br /&gt;
&lt;br /&gt;
# Bias of &amp;lt;math&amp;gt;\alpha=0&amp;lt;/math&amp;gt; is very close to 0&lt;br /&gt;
# Bias of &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; increases (in absolute value) up to some point&lt;br /&gt;
# Standard errors decrease (Figure 2) as sample size increases.&lt;br /&gt;
&lt;br /&gt;
One outcome that contradicts our “intuition” is the fact that bias in absolute value &amp;#039;&amp;#039;decreases&amp;#039;&amp;#039; after some point. This point depends on the sample size. The intuition for this behavior lies in the notion of non-stationarity. As &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; further and further increases, stationary series start to behave more and more like non-stationary ones. For non-stationary time series &amp;lt;math&amp;gt;X&amp;#039;X/T^2=\sum y_t^2 /T^2 \Rightarrow C(B(t))&amp;lt;/math&amp;gt;, while &amp;lt;math&amp;gt;X&amp;#039;e/T = \sum y_{t-1}e_t/T \Rightarrow C&amp;#039;(B(t))&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;C(B(t)),C&amp;#039;(B(t))&amp;lt;/math&amp;gt; – finite functionals of Brownian motions. Thus, for &amp;lt;math&amp;gt;|\rho|\rightarrow 1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;(X&amp;#039;X)^{-1}&amp;lt;/math&amp;gt; will dominate &amp;lt;math&amp;gt;X&amp;#039;y&amp;lt;/math&amp;gt; and the bias will decrease. Moreover, for &amp;lt;math&amp;gt;\rho=1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\hat\alpha&amp;lt;/math&amp;gt; is a consistent estimator of &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; even in the case of &amp;lt;math&amp;gt;Cov(x_t,e_t)\ne 0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= On a notion of Conditional Distribution =&lt;br /&gt;
&lt;br /&gt;
Repeat simulation using a slightly modified version of the main code, i.e. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;alphavec=[0:.1:.9 .99];&lt;br /&gt;
Tvec=[20 50 100 200 1000];&lt;br /&gt;
T0=200; %burn-in period&lt;br /&gt;
N=1000; %number of iterations&lt;br /&gt;
%rng(1); %for results replicability&lt;br /&gt;
for Tind=1:length(Tvec)&lt;br /&gt;
  T=Tvec(Tind); %picking the right T&lt;br /&gt;
  for alphaind=1:length(alphavec)&lt;br /&gt;
      rng(1); %this is the new line &lt;br /&gt;
      alpha=alphavec(alphaind); %picking the right alpha&lt;br /&gt;
      [meanalpha(alphaind,Tind) biasalpha(alphaind,Tind) stdalpha(alphaind,Tind)]=MC(alpha,T0,T,N);&lt;br /&gt;
  end&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare the smoothness of the graphs. Explain the differences.&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Monte-Carlo&amp;diff=4210</id>
		<title>Monte-Carlo</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Monte-Carlo&amp;diff=4210"/>
				<updated>2017-10-06T12:07:27Z</updated>
		
		<summary type="html">&lt;p&gt;AS: AS moved page Monte-Carlo to Temp: AH request&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[Temp]]&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=MATLAB&amp;diff=4208</id>
		<title>MATLAB</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=MATLAB&amp;diff=4208"/>
				<updated>2017-10-06T11:14:43Z</updated>
		
		<summary type="html">&lt;p&gt;AS: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== &amp;lt;div id=&amp;quot;Essential&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;The Essential MATLAB Programming Techniques ==&lt;br /&gt;
&lt;br /&gt;
In this section we will introduce a number of basic and intermediate programming techniques. Whatever language you program in you will encounter these techniques, although the details will, of course, vary. We recommend that you ensure that you are familiar with these before you progress to [[#SpecEcmtrTopics| Special Econometric Topics ]].&lt;br /&gt;
&lt;br /&gt;
=== Basic Programming ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Basics and&amp;lt;br&amp;gt;Matrices&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Loading Data and&amp;lt;br&amp;gt;Date Formats&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Program Flow and&amp;lt;br&amp;gt;Logicals&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Functions&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Saving Data and&amp;lt;br&amp;gt;Screen Output&lt;br /&gt;
|-&lt;br /&gt;
| [[Discussion]] &amp;lt;br&amp;gt; [http://www.youtube.com/watch?v=av5MgVpybT0&amp;amp;feature=youtu.be&amp;amp;hd=1 Example Clip]&lt;br /&gt;
| [[LoadingData|Discussion]] &amp;lt;br/&amp;gt;[http://youtu.be/jyb68zGM2ik?hd=1 ExampleClip]&lt;br /&gt;
| [[Program Flow and Logicals|Discussion]]&lt;br /&gt;
| [[Function|Discussion]] &amp;lt;br/&amp;gt; [[FctExampleCode|Example Code]] &amp;lt;br/&amp;gt; [[media:OLSexample.xls|OLSexample.xls]] &amp;lt;br&amp;gt; [http://youtu.be/FPw9DH8pfiU?hd=1 Example Clip]&lt;br /&gt;
| [[SavingData|Discussion]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
After having gone through these basic techniques you may want to test your newly acquired skills with the following examples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Example 1&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Example 2&lt;br /&gt;
|-&lt;br /&gt;
| [[Example 1]]&lt;br /&gt;
| [[Example 2|Example2a]]&amp;lt;br&amp;gt;[[Example 2b|Example2b]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Intermediate Programming ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Statistical&amp;lt;br&amp;gt;Functions&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Arrays and&amp;lt;br&amp;gt;Structures&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Debugging&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Graphing Data&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Function Handlers&amp;lt;br&amp;gt; Anonymous Functions&lt;br /&gt;
|-&lt;br /&gt;
| [[StatFunct|Discussion]]&lt;br /&gt;
| [[ArrayStructures|Discussion]]&lt;br /&gt;
| coming soon&lt;br /&gt;
| [[Graphing|Discussion]]&lt;br /&gt;
| [[Anonym|Discussion]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Advanced Programming ===&lt;br /&gt;
&lt;br /&gt;
Sorry, but this cannot be taught! It will come with experience. Find someone who has experience in MATLAB programming and let him or her look over your code.&lt;br /&gt;
&lt;br /&gt;
== Nonlinear Optimisation ==&lt;br /&gt;
&lt;br /&gt;
The optimal parameters in a linear econometric model (assuming certain assumptions) can be found analytically. We call them the Ordinary Least Squares (OLS) estimates and they are easily calculated with a certain formula (see the [[FctExampleCode#OLSestm|OLSest.m]] function). When econometric models do not have such an analytical solution, an alternative parameter estimation strategy is required. In essence it is a clever &amp;quot;trial and error&amp;quot; strategy. This is often called nonlinear optimisation.&lt;br /&gt;
&lt;br /&gt;
Nonlinear optimisation is a very important, but also a very tricky area of econometric computing. It certainly helps to understand some of the underlying theory and therefore we have below separate sections on the theory and implementation.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Theory&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Implementation&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Constrained &amp;lt;br&amp;gt;Optimisation&lt;br /&gt;
|-&lt;br /&gt;
| [[NonlinOptTheory| Discussion]]&lt;br /&gt;
| [[NonlinOptImp| Discussion]]&lt;br /&gt;
| [[ConNonlinOptImp| Discussion]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;div id=&amp;quot;SpecEcmtrTopics&amp;quot;&amp;gt;&amp;lt;/div&amp;gt; Special Econometric Topics ==&lt;br /&gt;
&lt;br /&gt;
Topics in this Section will assume that you have mastered all the techniques covered in the [[#Essential| Essential Programming Section ]]&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Robust standard&amp;lt;br&amp;gt;errors&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Univariate&amp;lt;br&amp;gt;Time Series&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Unit Root and&amp;lt;br&amp;gt;Stationarity Testing&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Forecasting&lt;br /&gt;
|-&lt;br /&gt;
| [[RobInf|Discussion]]&amp;lt;br&amp;gt;[[ExampleCodeOLShac|Example Code]]&lt;br /&gt;
| [[UniTS|Discussion]]&amp;lt;br&amp;gt;[[media:FXrateUSEU.xls|FXrateUSEU.xls]]&amp;lt;br&amp;gt;[[media:USGDP.xls|USGDP.xls]]&lt;br /&gt;
| coming soon&lt;br /&gt;
| [[Forecasting|Discussion]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Maximum&amp;lt;br&amp;gt;Likelihood&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Generalized&amp;lt;br&amp;gt;Methods of Moments&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Instrumental&amp;lt;br&amp;gt;Variables&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Bayesian&amp;lt;br&amp;gt;Estimation&lt;br /&gt;
|-&lt;br /&gt;
| [[MaxLik|Discussion]]&amp;lt;br&amp;gt;[[MaxLikCode|Example Code]]&lt;br /&gt;
| coming soon&lt;br /&gt;
| [[IV|Discussion]]&amp;lt;br&amp;gt;[[ExampleCodeIV|Example Code]]&lt;br /&gt;
| [[Bayes|Discussion]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Monte-Carlo/&amp;lt;br&amp;gt;Simulation Techniques&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Binary Response&amp;lt;br&amp;gt;Models&lt;br /&gt;
! scope=&amp;quot;col&amp;quot;| Handling High&amp;lt;br&amp;gt;Frequency Data&lt;br /&gt;
|-&lt;br /&gt;
| coming soon&lt;br /&gt;
| coming soon&lt;br /&gt;
| coming soon&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Other useful MATLAB resources ==&lt;br /&gt;
&lt;br /&gt;
=== The MATLAB Software ===&lt;br /&gt;
&lt;br /&gt;
The software is available on University of Manchester Computer Labs. If you make regular use of MATLAB you should consider purchasing your own software. The Student Version of MATLAB is available, for instance, from [http://www.amazon.co.uk/MATLAB-Simulink-Student-Version-R2014a/dp/0989614026/ref=sr_1_1?s=software&amp;amp;ie=UTF8&amp;amp;qid=1411983990&amp;amp;sr=1-1&amp;amp;keywords=matlab+2014 Amazon] for £66. This is a real bargain, considering that the equivalent non-discounted package would come in at about £4,000.&lt;br /&gt;
&lt;br /&gt;
=== Freely available toolboxes ===&lt;br /&gt;
&lt;br /&gt;
The following toolboxes are freely available and contain extremely useful procedures&lt;br /&gt;
&lt;br /&gt;
* Spatial Econometrics by James P. LeSage [http://www.spatial-econometrics.com/]. This toolbox contains a wide variety of useful econometrics functions. It also contains an excellent documentation. In addition to quite general econometric functions you will, as the name suggests, find a huge list of functions relevant if you are working with spatial data.&lt;br /&gt;
* &amp;lt;div id=&amp;quot;MFEtoolbox&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;Oxford MFE toolbox by Kevin Sheppard [https://bitbucket.org/kevinsheppard/mfe_toolbox]. Use the download link in the box on the right that starts with &amp;quot;Owner: Kevin Sheppard&amp;quot;. This toolbox contains many useful functions for uni- and multivariate volatility models.&lt;br /&gt;
&lt;br /&gt;
You need to copy these toolboxes into your MATLAB toolbox folder and add the respective path to the MATLAB list of folders it searches for functions. (In the main menu select FILE and then SET PATH where you can add the folders you added.) If you work on a computer for which you have no administrator rights, this strategy may not work. This [http://youtu.be/_32OqcW9WoY?hd=1 Example Clip] demonstrates what to do in that case. It is just a matter of adding one line into your code! Piece of cake.&lt;br /&gt;
&lt;br /&gt;
=== Literature and other learning resources ===&lt;br /&gt;
* [http://www.kevinsheppard.com/wiki/MFE_Toolbox: Kevin Sheppard&amp;#039;s MATLAB introduction].&lt;br /&gt;
* Martin V., Hurn S. and Harris D. (2012) Econometric Modelling with Time Series: Specification, Estimation and Testing (Themes in Modern Econometrics).[http://www.amazon.co.uk/Econometric-Modelling-Time-Specification-Econometrics/dp/0521196604/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1345214275&amp;amp;sr=1-1] This book contains an extensive library of relevant MATLAB codes.&lt;br /&gt;
* Higham, D.J. and Higham, N.J. (2005) MATLAB Guide, Society for Industrial and Applied Mathematics [http://www.amazon.co.uk/MATLAB-Guide-Desmond-J-Higham/dp/0898715784/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1347377409&amp;amp;sr=1-1]&lt;br /&gt;
This website does not cover any theoretical ground and is no substitute for any Econometric Textbook. There is a wide range of very good Econometric Textbooks available. If you are concerned about programming in MATLAB than you are likely to appreciate textbooks that use matrix notation. Here are two very good books that fit that bill:&lt;br /&gt;
* Heij C., de Boer P., Franses P.H., Kloek T. and van Dijk H.K (2004) Econometric Methods with Applications in Business and Economics, Oxford University Press, New York.[http://www.amazon.co.uk/Econometric-Methods-Applications-Business-Economics/dp/0199268010/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1354473313&amp;amp;sr=1-1]&lt;br /&gt;
* Greene W.H. (2012) Econometric Analysis, Pearson, Harlow.[http://www.amazon.co.uk/Econometric-Analysis-William-H-Greene/dp/0273753568/ref=sr_1_1?ie=UTF8&amp;amp;qid=1354473593&amp;amp;sr=8-1]&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3170</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3170"/>
				<updated>2013-10-11T13:25:57Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* while end loop */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&amp;lt;source&amp;gt;  &lt;br /&gt;
  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Set $i=1$. &lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y0+e(i)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Increase $i$ by one, i.e. $i=i+1$ (please note, in programming this is a valid statement)&lt;br /&gt;
# Check whether &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;. If yes, proceed. Else go to line 11.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; &lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; &lt;br /&gt;
# Repeat lines 7 $-$ 9&lt;br /&gt;
# ... (continuing the script)&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
    y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
    y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3169</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3169"/>
				<updated>2013-10-11T13:25:11Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* while end loop */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&amp;lt;source&amp;gt;  &lt;br /&gt;
  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Set $i=1$. &lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y0+e(i)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Increase $i$ by one, i.e. $i=i+1$ (please note, in programming this is a valid statement)&lt;br /&gt;
# Check whether &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;. If yes, proceed. Else go to line 11.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; &lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; &lt;br /&gt;
# Repeat lines 7 $-$ 9&lt;br /&gt;
# ... (continuing the script)&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3168</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3168"/>
				<updated>2013-10-11T13:23:50Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* while end loop */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&amp;lt;source&amp;gt;  &lt;br /&gt;
  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Set $i=1$. &lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y0+e(i)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Increase $i$ by one, i.e. $i=i+1$ (please note, in programming this is a valid statement)&lt;br /&gt;
# Check whether &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;. If yes, proceed. Else skip the loop.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; &lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; &lt;br /&gt;
# Repeat lines 7 $-$ 9&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3167</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3167"/>
				<updated>2013-10-11T13:22:18Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* while end loop */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&amp;lt;source&amp;gt;  &lt;br /&gt;
  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Set $i=1$. &lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y0+e(i)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Increase $i$ by one $i=i+1$&lt;br /&gt;
# Check whether &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;. If yes, proceed. Else skip the loop.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; &lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; (please note, in programming this is a correct statement),&lt;br /&gt;
# Repeat lines 7 $-$ 9&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3166</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3166"/>
				<updated>2013-10-11T13:22:05Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* while end loop */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&amp;lt;source&amp;gt;  &lt;br /&gt;
  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Set $i=1$. &lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y0+e(i)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Increase $i$ by one $i=i+1$&lt;br /&gt;
# Check whether &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;. If yes, proceed. Else skip the loop.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; &lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; (please note, in programming this is a correct statement),&lt;br /&gt;
# Repeat lines 7 -- 9&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3165</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3165"/>
				<updated>2013-10-11T13:16:17Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* while end loop */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&amp;lt;source&amp;gt;  &lt;br /&gt;
  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; (please note, in programming this is a correct statement),&lt;br /&gt;
# Check whether &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;. If yes, repeat lines 5 - 6. Else stop.&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3164</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3164"/>
				<updated>2013-10-11T13:14:56Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* while end loop */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&amp;lt;source&amp;gt;  &lt;br /&gt;
  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; (please note, in programming this is a correct statement),&lt;br /&gt;
# Repeat lines 5 - 6 while &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3163</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3163"/>
				<updated>2013-10-11T13:14:26Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* while end loop */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&amp;lt;source&amp;gt;  &lt;br /&gt;
  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; (please note, in programming this is an important statement),&lt;br /&gt;
# Repeat lines 5 - 6 while &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3162</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3162"/>
				<updated>2013-10-11T13:12:55Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* while end loop */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&amp;lt;source&amp;gt;  &lt;br /&gt;
  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; (please note, in programming this is an important statement),&lt;br /&gt;
# Repeat line 4 while &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3161</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3161"/>
				<updated>2013-10-11T13:12:40Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* if else end or if end */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&amp;lt;source&amp;gt;  &lt;br /&gt;
  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; (please note, in programming this is an important statement),&lt;br /&gt;
# Repeat line 4 while &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3160</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3160"/>
				<updated>2013-10-11T13:12:00Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* while end loop */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&amp;lt;source&amp;gt;  &lt;br /&gt;
  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; (please note, in programming this is an important statement),&lt;br /&gt;
# Repeat line 4 while &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3159</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3159"/>
				<updated>2013-10-11T13:11:37Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* if else end or if end */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&amp;lt;source&amp;gt;  &lt;br /&gt;
  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; (please note, in programming this is an important statement),&lt;br /&gt;
# Repeat line 4 while &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3158</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3158"/>
				<updated>2013-10-11T13:09:15Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* for ... end loop */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&amp;lt;source&amp;gt;  &lt;br /&gt;
  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; (please note, in programming this is an important statement),&lt;br /&gt;
# Repeat line 4 while &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3157</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3157"/>
				<updated>2013-10-11T13:07:14Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* for ... end loop */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created (for the sake of certainty, &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; (please note, in programming this is an important statement),&lt;br /&gt;
# Repeat line 4 while &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3156</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3156"/>
				<updated>2013-10-11T12:54:33Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* for ... end loop */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created. For example, if $e\sim N(0,4)$ with $1000$ observations: &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)*2&amp;lt;/source&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; (please note, in programming this is an important statement),&lt;br /&gt;
# Repeat line 4 while &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3155</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3155"/>
				<updated>2013-10-11T12:52:52Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* for ... end loop */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created, say &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)*2&amp;lt;/source&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; (please note, in programming this is an important statement),&lt;br /&gt;
# Repeat line 4 while &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3154</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3154"/>
				<updated>2013-10-11T12:52:20Z</updated>
		
		<summary type="html">&lt;p&gt;AS: /* for ... end loop */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created, say &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)*2&amp;lt;/source&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; (please note, in programming this is an important statement),&lt;br /&gt;
# Repeat line 4 while &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(indminus,l);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>AS</name></author>	</entry>

	</feed>