# 马尔科夫链及转移矩阵简单案例及Python实现

Please refresh the page if equations are not rendered correctly.
---------------------------------------------------------------

## 一、马尔科夫链和转移矩阵的介绍

created: 2023-06-16T18:01:20 (UTC +08:00)
tags: [马尔科夫链转移矩阵怎么求]
source: https://blog.csdn.net/m0_45161766/article/details/107235866

### 1、马尔科夫链(Markov chain)概述

\mathrm{P}\left(\mathrm{x}_{\mathrm{t}+1} \mid \ldots, \mathrm{x}_{\mathrm{t}-2,} \mathrm{x}_{\mathrm{t}-1}, \mathrm{x}_{\mathrm{t}}\right)=\mathrm{P}\left(\mathrm{x}_{\mathrm{t}+1} \mid \mathrm{x}_{\mathrm{t}}\right)

### 2、转移概率矩阵(Transition Probability Matrix)

A=\left[\begin{array}{ccc}
0.9&0.075&0.025 \\
0.15&0.8&0.05 \\
0.25&0.25&0.5
\end{array}\right]

## 二、举例说明

### 1、问题描述

A=\left[\begin{array}{ccc}
0.9&0.075&0.025 \\
0.15&0.8&0.05 \\
0.25&0.25&0.5
\end{array}\right]

### 2、代码实现

import numpy as np
import matplotlib.pyplot as plt

def calanddraw(p0, p, ax, c, n):
"""
Calculate and draw the probability distribution of the Markov chain:
p0 -> p0 * p -> p0 * p * p -> ...

Parameters
----------
p0 : array_like
Initial probability distribution.
p : array_like
Transition matrix.
ax : matplotlib.axes._subplots.AxesSubplot
Axes on which to plot.
c : list
A list of colors for plotting.
n : int
Number of iterations.

Returns
-------
None
"""
# Draw the initial probability distribution
for j in range(len(p0)):
ax.scatter(0, p0[j], c=c[j], s=2)

for i in range(n):
# Calculate the probability distribution of step i
p0 = np.mat(p0) * np.mat(p)

# Draw the probability distribution of step i for each state
for j in range(len(np.array(p0)[0])):
ax.scatter(i+1, p0[0, j], c=c[j], s=2)  # 确定画点属性

if __name__ == '__main__':
""" Make up some data for Markov chain analysis """
# Initial probability distribution. The sum of each row is 1.
p01 = np.array([0.5, 0.2, 0.3])
p02 = np.array([0.1, 0.4, 0.5])

# Transition matrix. The sum of each row is 1.
A = np.array([[0.9, 0.075, 0.025],
[0.15, 0.8, 0.05],
[0.25, 0.25, 0.5]
])

""" Calculate and draw the probability distribution of the Markov chain """
n = 30  # Iteration times
c = np.array(['r', 'g', 'b'])  # Color of each state

fig = plt.figure(figsize=(15, 6))

calanddraw(p01, A, ax1, c, n)
calanddraw(p02, A, ax2, c, n)
plt.show()


### 3、运行结果

Everything not saved will be lost.