Skip to content

Commit 567194f

Browse files
authored
Merge pull request #12 from farshidbalan/master
Replace depreciated bivariate_normal from matplotlib.mlab
2 parents 941af50 + 1c81b06 commit 567194f

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

PythonCodes/Chapter 01/Fig01_02.py

+30-31
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,63 @@
1-
#%%
2-
"""
3-
Created on Thu Nov 28 2018
4-
Bivariate normal PDF
5-
@author: Lech A. Grzelak
6-
"""
71
import numpy as np
82
import matplotlib.pyplot as plt
9-
from matplotlib.mlab import bivariate_normal
3+
from scipy.stats import multivariate_normal
104

11-
def BivariateNormalPDFPlot():
125

6+
def BivariateNormalPDFPlot():
137
# Number of points in each direction
148

15-
n = 40;
16-
9+
n = 40;
10+
1711
# Parameters
1812

19-
mu_1 = 0;
20-
mu_2 = 0;
13+
mu_1 = 0;
14+
mu_2 = 0;
2115
sigma_1 = 1;
2216
sigma_2 = 0.5;
23-
rho1 = 0.0
24-
rho2 = -0.8
25-
rho3 = 0.8
26-
17+
rho1 = 0.0
18+
rho2 = -0.8
19+
rho3 = 0.8
20+
2721
# Create a grid and a multivariate normal
2822

29-
x = np.linspace(-3.0,3.0,n)
30-
y = np.linspace(-3.0,3.0,n)
31-
X, Y = np.meshgrid(x,y)
32-
Z = lambda rho: bivariate_normal(X,Y,sigma_1,sigma_2,mu_1,mu_2,rho*sigma_1*sigma_2)
33-
23+
x = np.linspace(-3.0, 3.0, n)
24+
y = np.linspace(-3.0, 3.0, n)
25+
X, Y = np.meshgrid(x, y)
26+
pos = np.empty(X.shape + (2,))
27+
pos[:, :, 0] = X
28+
pos[:, :, 1] = Y
29+
Z = lambda rho: multivariate_normal([mu_1, mu_2], [[sigma_1, rho],
30+
[rho, sigma_2]])
31+
3432
# Make a 3D plot- rho = 0.0
3533

36-
fig= plt.figure(1)
34+
fig = plt.figure(1)
3735
ax = fig.gca(projection='3d')
38-
ax.plot_surface(X, Y, Z(rho1),cmap='viridis',linewidth=0)
36+
ax.plot_surface(X, Y, Z(rho1).pdf(pos), cmap='viridis', linewidth=0)
3937
ax.set_xlabel('X axis')
4038
ax.set_ylabel('Y axis')
4139
ax.set_zlabel('Z axis')
4240
plt.show()
43-
41+
4442
# Make a 3D plot- rho = -0.8
4543

46-
fig= plt.figure(2)
44+
fig = plt.figure(2)
4745
ax = fig.gca(projection='3d')
48-
ax.plot_surface(X, Y, Z(rho2),cmap='viridis',linewidth=0)
46+
ax.plot_surface(X, Y, Z(rho2).pdf(pos), cmap='viridis', linewidth=0)
4947
ax.set_xlabel('X axis')
5048
ax.set_ylabel('Y axis')
5149
ax.set_zlabel('Z axis')
5250
plt.show()
53-
51+
5452
# Make a 3D plot- rho = 0.8
5553

56-
fig= plt.figure(3)
54+
fig = plt.figure(3)
5755
ax = fig.gca(projection='3d')
58-
ax.plot_surface(X, Y, Z(rho3),cmap='viridis',linewidth=0)
56+
ax.plot_surface(X, Y, Z(rho3).pdf(pos), cmap='viridis', linewidth=0)
5957
ax.set_xlabel('X axis')
6058
ax.set_ylabel('Y axis')
6159
ax.set_zlabel('Z axis')
6260
plt.show()
63-
64-
BivariateNormalPDFPlot()
61+
62+
63+
BivariateNormalPDFPlot()

0 commit comments

Comments
 (0)