xxxxxxxxxx
1
1
using Plots
xxxxxxxxxx
1
1
using LinearAlgebra
xxxxxxxxxx
3
1
md"""
2
[A Brief Introduction to Markov Chains](https://office.fxrobot.kr/Products/Files/DocEditor.aspx?fileid=2258)
3
"""
3×1 Adjoint{Float64,Array{Float64,2}}:
0.0
0.0
1.0
xxxxxxxxxx
18
1
# Finite state-space markov chain example
2
3
# Transition Operator
4
# S F R
5
# U O A
6
# N G I
7
# N G N
8
# Y Y Y
9
10
begin
11
P = [0.8 0.15 0.05; # Sunny
12
0.4 0.5 0.1; # Foggy
13
0.1 0.3 0.6]' # Rainy
14
nWeeks = 25
15
16
# Initial state is rainy
17
X = [ .0 .0 1.]'
18
end
3×3 Adjoint{Float64,Array{Float64,2}}:
0.8 0.4 0.1
0.15 0.5 0.3
0.05 0.1 0.6
xxxxxxxxxx
1
1
P
0.596489
0.263159
0.140353
xxxxxxxxxx
10
1
# Run markov chain
2
begin
3
Xₖ = Array{Float64,2}(undef,(3,nWeeks))
4
Xₖ[:,1] = X
5
for k in 2:nWeeks
6
Xₖ[:,k]=P*Xₖ[:,k-1]
7
end
8
Xₖ[:,end]
9
end
10
xxxxxxxxxx
8
1
# Display
2
begin
3
plot(1:nWeeks,Xₖ[1,:],linecolor=:red,label="Sunny",linewidth=2,
4
xaxis="Week", yaxis="p(Weather)")
5
plot!(1:nWeeks,Xₖ[2,:],linecolor=:black,label="Foggy",linewidth=2)
6
plot!(1:nWeeks,Xₖ[3,:],linecolor=:blue,label="Rainy",linewidth=2)
7
plot!([15, 15],[0, 1],linecolor=:green, linestyle=:dash,label="Burn In",linewidth=2)
8
end