Exercise 2 (1 point):

Consider the following stochastic process (random walk)

$X_t =αX_{t−1}+ε_t,$ for 𝛼=1.

Assume that $t≥1$, $X_0=0$ and that the sequence ${ε_t}$ is a zero mean Gaussian whitenoise process with variance $𝜎^2$.

Questions

  1. Sample R = 100 realizations (sample paths) of this process, using $𝜎^2 = 2, t = 1,2,...,T , T = 1000$. Compute and plot the sample ensemble mean and variance functions of the process. Is the process mean and variance stationary?
  2. Repeat the above for the case of the random walk with a drift (use 𝜇 = 0.1) $$X_t =μ+αX_{t−1}+ε_t$$.

In [1]:
# May need to start notebook with
# jupyter notebook --NotebookApp.iopub_data_rate_limit=10000000000

using PlotlyJS


Plotly javascript loaded.

To load again call

init_notebook(true)

Question 1:

  • Sample R = 100 realizations (sample paths) of this process, using $𝜎^2 = 2, t = 1,2,...,T , T = 1000$.
  • Compute and plot the sample ensemble mean and variance functions of the process.
  • Is the process mean and variance stationary?

    R: The mean is stationary, the variance is not


In [2]:
T = 1000
ntrials = 100
σ = sqrt(2) 
α = 1
X = zeros(Float64, ntrials, T)

for t=2:T
    X[:, t] = α*X[:, t-1] + σ*randn(ntrials);
end

In [3]:
# Plot the paths - remove ";" in last line to see individual plots
layout = Layout(;title="Sample Paths for Randow Walk",
                 legend=attr(family="Arial, sans-serif", size=20,
                             color="grey"))
#plot
p1 = plot(X', layout,  line_width=0.5);

#Plot the mean
layout = Layout(;title="Mean of Random Process",
                     legend=attr(family="Arial, sans-serif", size=20,
                                 color="grey"))
p2 = plot(vec(mean(X, 1)), layout,  line_width=0.5);

#Plot the variance
layout = Layout(;title="Variance of Random Process",
                     legend=attr(family="Arial, sans-serif", size=20,
                                 color="grey"))
p3 = plot(vec(var(X, 1)), layout,  line_width=0.8);

[p1 p2 p3]


Out[3]:

Question2

  • Repeat the above for the case of the random walk with a drift (use 𝜇 = 0.1) $$X_t =μ+αX_{t−1}+ε_t$$.

R: In this case the plots show that the mean is also clearly drifting


In [4]:
μ = 0.1
X_drift = zeros(Float64, ntrials, T)

for t=2:T
    X_drift[:, t] = α*X_drift[:, t-1] + σ*randn(ntrials) + μ;
end

In [5]:
# Plot the paths - remove ";" in last line to see individual plots
layout = Layout(;title="Randow Walk with Drift (0.1)",
                 legend=attr(family="Arial, sans-serif", size=20,
                             color="grey"))
#plot
p1 = plot(X_drift', layout,  line_width=0.5);

#Plot the mean
layout = Layout(;title="Mean of Random Process",
                     legend=attr(family="Arial, sans-serif", size=20,
                                 color="grey"))
p2 = plot(vec(mean(X_drift, 1)), layout,  line_width=0.5);

#Plot the variance
layout = Layout(;title="Variance of Random Process",
                     legend=attr(family="Arial, sans-serif", size=20,
                                 color="grey"))
p3 = plot(vec(var(X_drift, 1)), layout,  line_width=0.8);

[p1 p2 p3]


Out[5]:

In [ ]: