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$.
In [1]:
# May need to start notebook with
# jupyter notebook --NotebookApp.iopub_data_rate_limit=10000000000
using PlotlyJS
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]:
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 [ ]: