【python讲概率】S05E06 多元正态分布及其性质

# 1.再谈相关性：基于多元正态分布

## 1.1.二元标准正态分布

import numpy as np
import matplotlib.pyplot as plt
import seaborn
seaborn.set()

mean = np.array([0, 0])
conv = np.array([[1, 0],
[0, 1]])

x, y = np.random.multivariate_normal(mean=mean, cov=conv, size=5000).T
plt.figure(figsize=(6, 6))
plt.plot(x, y, 'ro', alpha=0.2)
plt.gca().axes.set_xlim(-4, 4)
plt.gca().axes.set_ylim(-4, 4)
plt.show()

## 1.2.二元一般正态分布

import numpy as np
import matplotlib.pyplot as plt
import seaborn
seaborn.set()

mean = np.array([0, 0])
conv_1 = np.array([[1, 0],
[0, 1]])

conv_2 = np.array([[4, 0],
[0, 0.25]])

x_1, y_1 = np.random.multivariate_normal(mean=mean, cov=conv_1, size=3000).T
x_2, y_2 = np.random.multivariate_normal(mean=mean, cov=conv_2, size=3000).T
plt.plot(x_1, y_1, 'ro', alpha=0.05)
plt.plot(x_2, y_2, 'bo', alpha=0.05)

plt.gca().axes.set_xlim(-6, 6)
plt.gca().axes.set_ylim(-6, 6)
plt.show()

import numpy as np
import matplotlib.pyplot as plt
import seaborn
seaborn.set()

fig, ax = plt.subplots(2, 2)
mean = np.array([0,0])

conv_1 = np.array([[1, 0],
[0, 1]])

conv_2 = np.array([[1, 0.3],
[0.3, 1]])

conv_3 = np.array([[1, 0.85],
[0.85, 1]])

conv_4 = np.array([[1, -0.85],
[-0.85, 1]])

x_1, y_1 = np.random.multivariate_normal(mean=mean, cov=conv_1, size=3000).T
x_2, y_2 = np.random.multivariate_normal(mean=mean, cov=conv_2, size=3000).T
x_3, y_3 = np.random.multivariate_normal(mean=mean, cov=conv_3, size=3000).T
x_4, y_4 = np.random.multivariate_normal(mean=mean, cov=conv_4, size=3000).T

ax[0][0].plot(x_1, y_1, 'bo', alpha=0.05)
ax[0][1].plot(x_2, y_2, 'bo', alpha=0.05)
ax[1][0].plot(x_3, y_3, 'bo', alpha=0.05)
ax[1][1].plot(x_4, y_4, 'bo', alpha=0.05)

plt.show()

# 2.聚焦相关系数

`
import numpy as np
import matplotlib.pyplot as plt
import seaborn
seaborn.set()