代码
import numpy as np
mean = [0, 0]
sigam1 = 1
sigam2 = 1
rho = 0
cov12 = rho*sigam1*sigam2
cov = [
[sigam1**2, cov12],
[cov12, sigam2**2]
]
x1, y1 = np.random.multivariate_normal(
mean,
cov,
5000
).T
rho = 0.5
cov12 = rho*sigam1*sigam2
cov = [
[sigam1**2, cov12],
[cov12, sigam2**2]
]
x2, y2 = np.random.multivariate_normal(
mean,
cov,
5000
).T
rho = 1
cov12 = rho*sigam1*sigam2
cov = [
[sigam1**2, cov12],
[cov12, sigam2**2]
]
x3, y3 = np.random.multivariate_normal(
mean,
cov,
5000
).T
import matplotlib.pyplot as plt
plt.plot(x1, y1, '.', label='rho=0')
plt.plot(x2, y2, 'x', label='rho=0.5')
plt.plot(x3, y3, '1', label='rho=1')
plt.axis('equal')
plt.legend()
plt.show()
结果图示

see also
- https://numpy.org/doc/stable/reference/random/generated/numpy.random.multivariate_normal.html