Skip to content

Commit 87d34b8

Browse files
authored
Update and rename basic_statistical.py to bstats.py
1 parent 714b80f commit 87d34b8

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

basic_statistical.py renamed to bstats.py

+42-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Show statistical information
2+
Show statistical information
33
44
Le, Phong D. - le.duc.phong@gmail.com
55
Date: Sep 19, 2020
@@ -36,6 +36,40 @@ def corshow(X, Y):
3636
print('Kendall tau Correlation between X and Y is \n{}'.format(kendalltau(X, Y)[0]))
3737

3838

39+
def visualize2D(X):
40+
'''
41+
Show histogram of the variable X
42+
'''
43+
44+
# Calculate the number of bins using Sturges' rule
45+
# Formula: bins = ceil(1 + log2(n))
46+
bins = np.ceil(1 + np.log2(len(X))).astype('int')
47+
plt.hist(X, bins=bins, density=True)
48+
49+
def visualize3D(X, Y):
50+
'''
51+
Show 3D histogram of joint probabily of X and Y
52+
'''
53+
# Determine the number of bins using Sturges' rule
54+
binX = np.ceil(1 + np.log2(len(X))).astype('int')
55+
binY = np.ceil(1 + np.log2(len(Y))).astype('int')
56+
57+
# Create 2d histogram
58+
Hist, _, _ = np.histogram2d(X, Y, bins=[binX, binY])
59+
60+
# Define 3d axes
61+
fig = plt.figure()
62+
ax = fig.add_subplot(111, projection='3d')
63+
64+
# Create an X-Y mesh of the 2D data
65+
x_data, y_data = np.meshgrid(np.arange(Hist.shape[1]), np.arange(Hist.shape[0]))
66+
67+
# Flatten out the arrays to pass to bar3d
68+
x_data = x_data.flatten()
69+
y_data = y_data.flatten()
70+
z_data = Hist.flatten()
71+
ax.bar3d(x_data, y_data, np.zeros(len(z_data)), 1, 1, z_data)
72+
3973

4074
if __name__ == '__main__':
4175
# X, a random Gaussian distribution
@@ -52,7 +86,11 @@ def corshow(X, Y):
5286
# Show correlation information between X and Y
5387
corshow(X, Y)
5488

55-
# Plot histogram of the variable
56-
plt.hist(X)
57-
plt.hist(Y)
89+
# Plot 2D histogram of the variable
90+
visualize2D(X)
91+
visualize2D(Y)
92+
plt.show()
93+
94+
# Plot 3D histogram of the two variables
95+
visualize3D(X, Y)
5896
plt.show()

0 commit comments

Comments
 (0)