随机过程是随机变量的聚集: { X t , t ∈ I } , \{X_t,t \in I \}, {Xt,t∈I},其中: X t X_t Xt是时间 t t t的随机变量集, I I I是过程的索引集。
我们将在本教程中重点介绍的离散时间随机过程是随机变量序列。 1.1模拟赌徒的毁灭Gambler’s Ruin
我们可以用闻名的赌徒破产的例子,这是一个随机过程。
在我们的例子中,一个赌徒从$50开始。为了简朴起见,他们只能以$1的增量下注。他们每次下注只能赢或输$1。他们会继续赌博,直到输光所有的钱(带着$0脱离)或者赢了$100。我们可以用以下符号将其形式化:
$\begin{equation}
X_{g} =
\begin{cases}
$+1, & \text{with a probability of 50%}\
$-1, & \text{with a probability of 50%}\
\end{cases}
\end{equation}$ 式中: X g X_g Xg为以美元计的赌博结果.
Source: Introduction to Stochastic Processes with R by Robert P. Dobrow
Source: Introduction to Stochastic Processes with R by Robert P. Dobrow 2.马尔可夫链(Markov Chains)
马尔可夫链是一种随机过程。 马尔可夫链是随机变量( X t X_t Xt)的聚集,其中未来状态 ( j j j)仅取决于当前状态 ( i i i)。马尔可夫链可以是离散的或连续的。 对于马尔可夫链转移矩阵(表示为 P P P):
每一行和为1,即: ∑ j P i j = 1 \sum\limits_{j}P_{ij} = 1 j∑Pij=1.
概率必须黑白负的,即: P i j ≥ 0 ∀ i , j P_{ij} \geq 0 \ \ \forall \ \ i,j Pij≥0 ∀ i,j
Source: Markov Chain from Wolfram MathWorld 2.1马尔可夫链模拟
马尔可夫链可以由初始分布和转移矩阵模拟。
例子中,初始状态是纽约市New York。从最初的状态,我们可以观光到:巴黎Paris,开罗Cairo,首尔Seoul,乃至纽约市New York City. 转移矩阵包罗从一个状态移到另一个状态的一步转移概率。
mc
_example = {'NYC': [.25,0,.75,1],
'Paris': [.25,.25,0,0],
'Cairo': [.25,.25,.25,0],
'Seoul': [.25,.5,0,0]}
mc
= pd.DataFrame(data = mc
_example, index = ['NYC', 'Paris', 'Cairo', 'Seoul'])
复制代码
2.2马尔可夫转移概率图
我们可以用以下符号将马尔可夫链在初始起点(纽约市)的运动数理化:
P ( X 1 = New York ∣ X 0 = New York ) = P ( X 1 = Paris ∣ X 0 = New York ) = P ( X 1 = Cairo ∣ X 0 = New York ) = P ( X 1 = Seoul ∣ X 0 = New York ) = 25 % P(X_1 = \text{New York}|X_0 = \text{New York})=P(X_1 = \text{Paris}|X_0 = \text{New York})=P(X_1 = \text{Cairo}|X_0 = \text{New York}) = P(X_1 = \text{Seoul}|X_0 = \text{New York}) = 25\% P(X1=New York∣X0=New York)=P(X1=Paris∣X0=New York)=P(X1=Cairo∣X0=New York)=P(X1=Seoul∣X0=New York)=25%
travel_sim = []
travel_sim.append(mc
.iloc[0].index[0])
city = np.random.choice(mc
.iloc[0].index, p = mc
.iloc[0])
travel_sim.append(city)
while len(travel_sim) < 25:
city = np.random.choice(mc
.iloc[mc
.index.get_loc(city)].index, p = mc
.iloc[mc
.index.get_loc(city)])
travel_sim.append(city)
travel_sim
复制代码
['NYC',
'Cairo',
'NYC',
'Seoul',
'NYC',
'Paris',
'Paris',
'Paris',
'Seoul',
'NYC',
'Cairo',
'Cairo',
'NYC',
'NYC',
'NYC',
'Cairo',
'NYC',
'NYC',
'Paris',
'Cairo',
'NYC',
'NYC',
'Paris',
'Cairo',
'Cairo']
复制代码
mc
复制代码
NYC Paris Cairo Seoul
NYC 0.25 0.25 0.25 0.25
Paris 0.00 0.25 0.25 0.50
Cairo 0.75 0.00 0.25 0.00
Seoul 1.00 0.00 0.00 0.00
复制代码
2.3无记忆性:给定现在,未来独立于已往
马尔可夫链的一个告急特征是它们是无记忆的。看例子可知,唯一告急的状态是当前状态。 假如我们的从纽约开始 ( X 0 = N Y C X_0 = NYC X0=NYC) 去了巴黎 ( X 1 = P a r i s X_1 = Paris X1=Paris), 然后到下一个城市 ( X 2 X_2 X2)只取决于从巴黎出发的可能性. 最初在纽约开始出发这一究竟并不影响到 X 2 . X_2. X2. 2.4 n n n 步转移矩阵
for i in range(1,11,1): print(f'n Step Transition Matrix at the nth step {i}\n', matrix_power(mc
2.to_numpy(), i),'\n')
复制代码
n Step Transition Matrix at the nth step 1
[[0.2 0.6 0.2]
[0.3 0. 0.7]
[0.5 0. 0.5]]
n Step Transition Matrix at the nth step 2
[[0.32 0.12 0.56]
[0.41 0.18 0.41]
[0.35 0.3 0.35]]
n Step Transition Matrix at the nth step 3
[[0.38 0.192 0.428]
[0.341 0.246 0.413]
[0.335 0.21 0.455]]
n Step Transition Matrix at the nth step 4
[[0.3476 0.228 0.4244]
[0.3485 0.2046 0.4469]
[0.3575 0.201 0.4415]]
n Step Transition Matrix at the nth step 5
[[0.35012 0.20856 0.44132]
[0.35453 0.2091 0.43637]
[0.35255 0.2145 0.43295]]
n Step Transition Matrix at the nth step 6
[[0.353252 0.210072 0.436676]
[0.351821 0.212718 0.435461]
[0.351335 0.21153 0.437135]]
n Step Transition Matrix at the nth step 7
[[0.35201 0.2119512 0.4360388]
[0.3519101 0.2110926 0.4369973]
[0.3522935 0.210801 0.4369055]]
n Step Transition Matrix at the nth step 8
[[0.35200676 0.211206 0.43678724]
[0.35220845 0.21114606 0.43664549]
[0.35215175 0.2113761 0.43647215]]
n Step Transition Matrix at the nth step 9
[[0.35215677 0.21120406 0.43663917]
[0.35210825 0.21132507 0.43656668]
[0.35207925 0.21129105 0.43662969]]
n Step Transition Matrix at the nth step 10
[[0.35211216 0.21129406 0.43659378]
[0.35210251 0.21126495 0.43663254]
[0.35211801 0.21124755 0.43663443]]
复制代码
initial_dist = np.asarray([0,1,0])for i in range(1,11,1): print(f'start pizza then n Step Transition Vector at the nth step {i}\n', np.dot(initial_dist,matrix_power(mc
2.to_numpy(), i)),'\n')
复制代码
start pizza then n Step Transition Vector at the nth step 1
[0.3 0. 0.7]
start pizza then n Step Transition Vector at the nth step 2
[0.41 0.18 0.41]
start pizza then n Step Transition Vector at the nth step 3
[0.341 0.246 0.413]
start pizza then n Step Transition Vector at the nth step 4
[0.3485 0.2046 0.4469]
start pizza then n Step Transition Vector at the nth step 5
[0.35453 0.2091 0.43637]
start pizza then n Step Transition Vector at the nth step 6
[0.351821 0.212718 0.435461]
start pizza then n Step Transition Vector at the nth step 7
[0.3519101 0.2110926 0.4369973]
start pizza then n Step Transition Vector at the nth step 8
[0.35220845 0.21114606 0.43664549]
start pizza then n Step Transition Vector at the nth step 9
[0.35210825 0.21132507 0.43656668]
start pizza then n Step Transition Vector at the nth step 10