-
Notifications
You must be signed in to change notification settings - Fork 87
/
markov_chain_example.py
65 lines (53 loc) · 1.62 KB
/
markov_chain_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#===============================================================================
#
# Copyright (c) 2017 <> All Rights Reserved
#
#
# File: ch3/markov/markov_chain_example.py
# Author: Hai Liang Wang
# Date: 2018-05-30:18:22:33
#
#===============================================================================
from __future__ import print_function
from __future__ import division
__copyright__ = "Copyright (c) 2017 . All Rights Reserved"
__author__ = "Hai Liang Wang"
__date__ = "2018-05-30:18:22:33"
import os
import sys
curdir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(curdir)
if sys.version_info[0] < 3:
reload(sys)
sys.setdefaultencoding("utf-8")
# raise "Must be using Python 3"
else:
unicode = str
import numpy as np
from matplotlib import pyplot
P = np.matrix([[1, 0, 0, 0, 0, 0],
[1/4, 1/2, 0, 1/4, 0, 0],
[0, 0, 0, 1, 0, 0],
[1/16, 1/4, 1/8, 1/4, 1/4, 1/16],
[0, 0, 0, 1/4, 1/2, 1/4],
[0, 0, 0, 0, 0, 1]])
v = np.matrix([[0, 0, 1, 0, 0, 0]])
# 数学运算
plot_data = []
for step in range(5):
result = v * P**step
plot_data.append(np.array(result).flatten())
# Convert the data format
plot_data = np.array(plot_data)
# 绘制图形
pyplot.figure(1)
pyplot.xlabel('Steps')
pyplot.ylabel('Probability')
lines = []
for i, shape in zip(range(6), ['x', 'h', 'H', 's', '8', 'r+']):
line, = pyplot.plot(plot_data[:, i], shape, label="S%i" % (i+1))
lines.append(line)
pyplot.legend(handles=lines, loc=1)
pyplot.savefig(os.path.join(curdir, "markov-predict.png"))