The Geometric Distribution gives the chance of success at trial n. The success at the first trial is p.
The success at at n is (1-p)^(n-1)*p. In Python, the ^ operation is performed by **.
The formula is contrasted to scipy.stas.geom.pmf, which should be the same.
# ex19.py
from __future__ import print_function, division
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import geom
prob = 0.2
N = 12
p = np.zeros(N)
d = np.zeros(N)
for k in range(1,N+1):
p[k-1] = (1-prob)**(k-1)*prob
d[k-1] = geom.pmf(k,prob)
plt.bar(range(1,13),p, color = 'gray')
plt.xlim(1,13)
plt.xlabel('n')
plt.ylabel('p(n)')
plt.title('First Success at n')
plt.show()
print('Probability success after', N ,'trials is',
(1-sum(p)),'\naccording to formula.\n')
print('Probability success after', N ,'trials is',
(1-sum(d)),'\naccording to geom.pmf')
# Probability success after 12 trials is 0.068719476736
# according to formula.
# Probability success after 12 trials is 0.068719476736
# according to geom.pmf
Output:
No comments:
Post a Comment