6 Ways Automation Can Transform Your SMB’s Employee Management with Cost Efficiency
Dmytro Spilka·5 min

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from scipy import stats
sns.set(color_codes=True)
#Set random number for reproducibility
np.random.seed(123)
#simulate normal dist (Std normal: loc = 1 and scale = 1)
x = np.random.normal(size=500, loc = 0, scale = 1)
#Plot
fig = plt.figure(figsize=(10,6))
ax = sns.distplot(x, fit = norm, axlabel = "values",
kde_kws={"color": "r", "lw": 3, "label": "KDE"},
fit_kws={"color": "black", "lw": 3, "label": "StdNormal"})
plt.legend(labels=['Kernel Density','Standard Normal Dist'])
plt.title("Standard Normal Distribution")
plt.show(block=False)
np.random.seed(123)
x = np.random.binomial(size = 100, n = 10, p = 0.5)
#Plot
fig = plt.figure(figsize=(10,6))
ax = sns.distplot(x, axlabel = "Values",
kde_kws={"color": "r", "lw": 3, "label": "KDE"})
for p in ax.patches:
ax.annotate("%.2f" % p.get_height(), (p.get_x() + p.get_width() / 2., p.get_height()),
ha='center', va='center', fontsize=11, color='black', xytext=(0, 20),
textcoords='offset points')
plt.legend(labels=['Kernel Density','Binomial Distribution'])
plt.ylim(0, 0.6)
plt.title("Binomial Distribution")
plt.show(block=False)
In the plot above we have flipped a coin 10 times and performed this experiment 100 times. For instance, the probability that the coin lands on heads (denoted as a success) exactly 5 times for all these experiments is 0.30. Note that this is an empirical distribution and not theoretical. Also, as n (number of flips) gets larger, the binomial distribution can be somewhat approximated by the normal distribution. Notice the kernel density (red line), it closely resembles the normal distribution. Have a look at Khan Academy for a detailed explanation of the distribution.
np.random.seed(123)
x = np.random.poisson(lam = 3, size = 10000)
fig = plt.figure(figsize=(10,6))
ax = sns.distplot(x, axlabel = "values", kde = False)
plt.title("Poisson Distribution")
plt.ylabel("frequency")
plt.show(block=False)
Note that in this simulation we plot the frequency and not the density of the distribution. To plot with the density on the y-axis, you'd only need to change 'kde = False' to 'kde = True' in the code above.
np.random.seed(123)
x = np.random.exponential(size=500, scale = 1.5)
fig = plt.figure(figsize=(10,6))
ax = sns.distplot(x, fit = stats.expon, axlabel = "values",
kde_kws={"color": "r", "lw": 3, "label": "KDE"},
fit_kws={"color": "black", "lw": 3, "label": "exp"})
plt.legend(labels=['Kernel Density','Exponential Dist'])
plt.title("Exponential Distribution")
plt.show(block=False)
Have a look here for a detailed explanation of the Exponential distribution and its applications.Instantly repurpose any DDI article into a professionally produced short-form video.
Try DDI Media →
As a current Masters in Statistics student, Luka is eager to simplify complex topics and provide big-data solutions to real-world problems. He also has an educational background in actuarial and financial engineering. In his spare time, Luka enjoys traveling, writing on machine learning topics and taking part in data science competitions.