def plot(self): p = 0.5 mean, var, skew, kurt = geom.stats(p, moments='mvsk') x = np.arange(geom.ppf(0.01, p), geom.ppf(0.99, p)) ax.plot(x, geom.pmf(x, p), 'bo', ms=8, label='geom pmf') ax.vlines(x, 0, geom.pmf(x, p), colors='b', lw=5, alpha=0.5) rv = geom(p) ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1, label='frozen pmf') ax.legend(loc='best', frameon=False) plt.show()
def test_geom(self): from scipy.stats import geom import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) p = 0.5 mean, var, skew, kurt = geom.stats(p, moments='mvsk') x = np.arange(geom.ppf(0.01, p), geom.ppf(0.99, p)) ax.plot(x, geom.pmf(x, p), 'bo', ms=8, label='geom pmf') ax.vlines(x, 0, geom.pmf(x, p), colors='b', lw=5, alpha=0.5) rv = geom(p) ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1, label='frozen pmf') ax.legend(loc='best', frameon=False) self.assertEqual(str(ax), "AxesSubplot(0.125,0.11;0.775x0.77)")
def test_geometric_max_draw_list(self, n, p, maxv): """ NB: slight mismatches on arrays possibly due to rounding error in C """ array_c = covid19.intArray(n) covid19.geometric_max_draw_list(array_c, n, p, maxv) array_c = np.array(c_array_as_python_list(array_c, n)) array_np = geom.ppf(np.linspace(1 / n, 1 - 1 / n, n), p) - 1 array_np = np.minimum(array_np, maxv) np.testing.assert_almost_equal(np.mean(array_c), np.mean(array_np), decimal=2)
def generate_graph_data(self): ageGroup = self.tableModel.data[self.selected_item_index.row()][0] parameter = self.tableModel.data[self.selected_item_index.row()][1] p1 = self.temporaryParametersDict[ageGroup][parameter]["p1"] p2 = self.temporaryParametersDict[ageGroup][parameter]["p2"] distributionType = self.temporaryParametersDict[ageGroup][parameter][ "distributionType"] xyDict = {"x": [], "y": []} try: if distributionType == 'Binomial': xyDict["x"] = np.arange(binom.ppf(0.01, int(p1), p2 / 100), binom.ppf(0.99, int(p1), p2 / 100)) xyDict["y"] = binom.pmf(xyDict["x"], int(p1), p2 / 100) elif distributionType == 'Geometric': xyDict["x"] = np.arange(geom.ppf(0.01, p1 / 100), geom.ppf(0.99, p1 / 100)) xyDict["y"] = geom.pmf(xyDict["x"], p1 / 100) if p2 != 0: self.tableModel.setData( self.selected_item_index.sibling( self.selected_item_index.row(), 3), 0, Qt.EditRole) elif distributionType == 'Laplacian': xyDict["x"] = np.arange(dlaplace.ppf(0.01, p1 / 100), dlaplace.ppf(0.99, p1 / 100)) xyDict["y"] = dlaplace.pmf(xyDict["x"], p1 / 100) if p2 != 0: self.tableModel.setData( self.selected_item_index.sibling( self.selected_item_index.row(), 3), 0, Qt.EditRole) elif distributionType == 'Logarithmic': xyDict["x"] = np.arange(logser.ppf(0.01, p1 / 100), logser.ppf(0.99, p1 / 100)) xyDict["y"] = logser.pmf(xyDict["x"], p1 / 100) if p2 != 0: self.tableModel.setData( self.selected_item_index.sibling( self.selected_item_index.row(), 3), 0, Qt.EditRole) elif distributionType == 'Neg. binomial': xyDict["x"] = np.arange(nbinom.ppf(0.01, p1, p2 / 100), nbinom.ppf(0.99, p1, p2 / 100)) xyDict["y"] = nbinom.pmf(xyDict["x"], p1, p2 / 100) elif distributionType == 'Planck': xyDict["x"] = np.arange(planck.ppf(0.01, p1 / 100), planck.ppf(0.99, p1 / 100)) xyDict["y"] = planck.pmf(xyDict["x"], p1 / 100) if p2 != 0: self.tableModel.setData( self.selected_item_index.sibling( self.selected_item_index.row(), 3), 0, Qt.EditRole) elif distributionType == 'Poisson': xyDict["x"] = np.arange(poisson.ppf(0.01, p1), poisson.ppf(0.99, p1)) xyDict["y"] = poisson.pmf(xyDict["x"], p1) if p2 != 0: self.tableModel.setData( self.selected_item_index.sibling( self.selected_item_index.row(), 3), 0, Qt.EditRole) elif distributionType == 'Uniform': if p1 - 0.5 * p2 < 0: p2 = p1 min = p1 - 0.5 * p2 max = p1 + 0.5 * p2 xyDict["x"] = np.arange(randint.ppf(0.01, min, max), randint.ppf(0.99, min, max)) xyDict["y"] = randint.pmf(xyDict["x"], min, max) elif distributionType == 'Zipf (Zeta)': xyDict["x"] = np.arange(zipf.ppf(0.01, p1), zipf.ppf(0.99, p1)) xyDict["y"] = zipf.pmf(xyDict["x"], p1) if p2 != 0: self.tableModel.setData( self.selected_item_index.sibling( self.selected_item_index.row(), 3), 0, Qt.EditRole) self.update_graph(xyDict) except Exception as E: log.error(E)
rv = nbinom(n, p) ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1, label='frozen pmf') ax.legend(loc='best', frameon=False) # ============================================= # # ================= GEOMETRIQUE =============== # # ============================================= # fig, ax = plt.subplots(1, 1) p = 0.3 mean, var, skew, kurt = geom.stats(p, moments='mvsk') x = np.arange(geom.ppf(0.01, p), geom.ppf(0.99, p)) ax.plot(x, geom.pmf(x, p), 'bo', ms=8, label='geom pmf') ax.vlines(x, 0, geom.pmf(x, p), colors='b', lw=5, alpha=0.5) rv = geom(p) ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1, label='frozen pmf') ax.legend(loc='best', frameon=False) # ============================================= # # =================== POISSON ================= # # ============================================= # fig, ax = plt.subplots(1, 1) mu = 20
plt.title("Cumulative Density Function of Binomial(n=10,p=0.4) Distribution", fontsize=16) plt.xticks(np.arange(-2, 9, 1)) plt.yticks(np.arange(0, 1.1, 0.1)) plt.legend(loc='upper left', shadow=True) plt.show() # ### Geometric Distribution # In[4]: #Geometric Distribution from scipy.stats import geom p = 0.6 x = np.arange(geom.ppf(0.01, p), geom.ppf( 0.99, p)) #Percent Point Function (inverse of cdf — percentiles) print("Mean : ", geom.stats(p, moments='m')) print("Variance : ", geom.stats(p, moments='v')) print("Prob. Mass Func. : ", geom.pmf(x, p)) print("Cum. Density Func.: ", geom.cdf(x, p)) CDF = geom.cdf(x, p) fig = plt.figure(figsize=(20, 10)) plt.subplot(221) plt.plot(x, geom.pmf(x, p), 'go', ms=8, label='PMF') plt.vlines(x, 0, geom.pmf(x, p), colors='g', lw=5, alpha=0.5) plt.xlabel("Sample Space of Geometric Distribution", fontsize=14) plt.ylabel("PMF", fontsize=14)
from scipy.stats import geom import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) # Calculate a few first moments: p = 0.5 mean, var, skew, kurt = geom.stats(p, moments='mvsk') # Display the probability mass function (``pmf``): x = np.arange(geom.ppf(0.01, p), geom.ppf(0.99, p)) ax.plot(x, geom.pmf(x, p), 'bo', ms=8, label='geom pmf') ax.vlines(x, 0, geom.pmf(x, p), colors='b', lw=5, alpha=0.5) # Alternatively, the distribution object can be called (as a function) # to fix the shape and location. This returns a "frozen" RV object holding # the given parameters fixed. # Freeze the distribution and display the frozen ``pmf``: rv = geom(p) ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1, label='frozen pmf') ax.legend(loc='best', frameon=False) plt.show()
import numpy as np from scipy.stats import geom import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) p = 0.5 mean, var, skew, kurt = geom.stats(p, moments='mvsk') x = np.arange(geom.ppf(0.01, p),geom.ppf(0.99, p)) ax.plot(x, geom.pmf(x, p), 'bo', ms=8, label='geom pmf') ax.vlines(x, 0, geom.pmf(x, p), colors='b', lw=5, alpha=0.5) plt.show()
def testGeom(): # {{{ """ Geometric Distribution (discrete) Notes ----- 伯努利事件进行k次, 第一次成功的概率 为什么是几何分布呢, 为什么不叫几毛分布? 与几何数列有关 (乘积倍数) p: 成功的概率 q: 失败的概率(1-p) k: 第一次成功时的经历的次数 (前k-1次是失败的) geom.pmf(k, p), (1-p)**(k-1)*p) """ # 准备数据: 已知 p. # X轴: 第k次才成功 # Y轴: 概率 p = 0.4 xs = np.arange(geom.ppf(0.01, p), geom.ppf(0.99, p), step=1) # E(X) = 1/p, D(X) = (1-p)/p**2 mean, var, skew, kurt = geom.stats(p, moments='mvsk') print("mean: %.2f, var: %.2f, skew: %.2f, kurt: %.2f" % (mean, var, skew, kurt)) fig, axs = plt.subplots(2, 2) # 显示pmf1 ys = geom.pmf(xs, p) axs[0][0].plot(xs, ys, 'bo', markersize=5, label='geom pmf') axs[0][0].vlines(xs, 0, ys, colors='b', linewidth=5, alpha=0.5, label='vline pmf') axs[0][0].legend(loc='best', frameon=False) # 显示pmf2 ys = (1 - p)**(xs - 1) * p axs[0][1].plot(xs, ys, 'bo', markersize=5, label='geom pmf') axs[0][1].vlines(xs, 0, ys, colors='b', linewidth=5, alpha=0.5, label='vline pmf') axs[0][1].legend(loc='best', frameon=False) axs[0][1].set_title('ys = (1-p)**(xs-1)*p') # 显示cdf P(X<=x) ys = geom.cdf(xs, p) axs[1][0].plot(xs, ys, 'bo', markersize=5, label='geom cdf') axs[1][0].legend(loc='best', frameon=False) print(np.allclose(xs, geom.ppf(ys, p))) # ppf:y-->x cdf:x-->y # 生成随机数据(random variables) data = geom.rvs(p, size=1000) import sys sys.path.append("../../thinkstats") import Pmf pmf = Pmf.MakePmfFromList(data) xs, ys = pmf.Render() axs[1][1].plot(xs, ys, 'bo', markersize=5, label='rvs-pmf') plt.show()