Poisson Distributions¶
In [1]:
Copied!
from scipy.stats import poisson, expon, weibull_min
from scipy.stats import poisson, expon, weibull_min
Let's say we have a customer service, and incoming customer service calls average two per minute, this code will simulate 100 minutes, returning the number of calls in each of those 100 minutes.
In [2]:
Copied!
poisson.rvs(2, size=100)
poisson.rvs(2, size=100)
Out[2]:
array([1, 1, 3, 2, 4, 1, 1, 2, 2, 0, 6, 5, 1, 2, 4, 2, 2, 1, 4, 0, 1, 2,
1, 1, 5, 2, 2, 0, 1, 1, 1, 3, 1, 2, 0, 0, 1, 7, 3, 1, 1, 1, 4, 2,
1, 2, 5, 2, 0, 1, 2, 0, 1, 2, 4, 1, 0, 2, 0, 1, 3, 1, 1, 2, 2, 2,
1, 1, 4, 0, 2, 1, 2, 4, 2, 0, 2, 0, 4, 4, 1, 1, 3, 1, 4, 4, 0, 5,
3, 3, 2, 2, 2, 2, 0, 1, 7, 2, 3, 1])
Exponential Distribution¶
This code would generate 100 random numbers from an exponential distribution where the mean number of events per time period is 0.2. So you could use it to simulate 100 intervals, in minutes, between service calls, where the average rate of incoming calls is 0.2 per minute.
In [3]:
Copied!
expon.rvs(0.2, size=100)
expon.rvs(0.2, size=100)
Out[3]:
array([0.397774 , 1.78669585, 0.24388101, 0.80225463, 0.20678341,
1.53308992, 0.33835323, 0.27865364, 0.78411232, 1.27610884,
0.34070167, 0.51838856, 1.21307718, 0.40966295, 3.72126624,
1.53866206, 0.42577755, 0.46400747, 0.27649895, 0.56874221,
1.0162171 , 0.22036323, 1.13960613, 0.28667656, 0.33314455,
0.27517873, 0.466853 , 0.45978772, 0.77067044, 0.409914 ,
0.6826922 , 1.22251748, 1.01517777, 5.39617828, 0.52922812,
1.04665629, 0.41208094, 1.33731307, 0.26893765, 3.04276089,
1.47654124, 1.05684195, 3.77709693, 2.26164743, 1.44931822,
2.78645578, 0.80889253, 1.31996855, 1.11839537, 2.45195781,
0.20513845, 1.8384235 , 3.31120134, 1.45418781, 0.97768708,
0.36544842, 4.10542212, 0.25421171, 1.31268027, 0.71089734,
1.00286943, 1.43124045, 0.35220972, 1.92664618, 0.229928 ,
1.05097538, 0.33855224, 0.47075289, 1.73579147, 2.14039468,
0.58785016, 1.81556284, 1.65771544, 2.56798087, 1.14281195,
0.47736291, 1.09663642, 0.7019216 , 0.57361099, 2.90051122,
0.93066009, 2.03915797, 0.46182773, 0.58194612, 1.2476486 ,
2.12676086, 0.67349041, 2.48237518, 1.16436066, 2.054739 ,
2.30215589, 0.26248143, 1.33438137, 3.65004807, 0.78888892,
0.26735973, 0.78166068, 0.29987045, 2.53629116, 0.89861057])
Weibull Distribution¶
In [4]:
Copied!
simulation_size = 20_000_000
lifespans = weibull_min.rvs(c=1.5, scale=5000, size=simulation_size)
print(lifespans)
count = 0
for life in lifespans:
if life < 4000:
count += 1
print(f"Percentage of failures after 4000 hours of usage: {count/simulation_size}")
simulation_size = 20_000_000
lifespans = weibull_min.rvs(c=1.5, scale=5000, size=simulation_size)
print(lifespans)
count = 0
for life in lifespans:
if life < 4000:
count += 1
print(f"Percentage of failures after 4000 hours of usage: {count/simulation_size}")
[11606.04092257 2732.85042492 4005.60362697 ... 2677.00225402 3748.06515772 1510.6641351 ]
Percentage of failures after 4000 hours of usage: 0.51097385