def binomial_distribution(*args): try: n, p = int(args[0]), float(args[1]) except (ValueError, TypeError): return {'is_valid': False} if n <= 0 or not 0 < p < 1: return {'is_valid': False} t = Symbol('t') mean, var = binom.stats(n, p) cf = (1 - p + p * exp(I * t))**n d_cf = diff(cf, t) dd_cf = diff(d_cf, t) mean2 = round(float(lambdify(t, -dd_cf)(0)), 2) return { 'n': n, 'p': p, 'q': round(1 - p, 2), 'mean': round(float(mean), 2), 'mean2': mean2, 'var': round(float(var), 2), 'g': latex(cf), 'g1': latex(d_cf), 'g2': latex(dd_cf), 'type': type, 'is_valid': True, }
def testBinom(): # {{{ """ Binomial Distribution (二项分布 discrete) 二项分布的例子:抛掷10次硬币,恰好两次正面朝上的概率是多少? 事件要么发生, 要么不发生 """ # 准备数据: 已知 n(伯努利实验次数), p(某件事件发生的概率) # X轴: n次实验中事件出现k次 # Y轴: 概率 n = 100 # 当n很大(np > 5 && nq > 5) 近似 X ~ N(np, npq) p = 0.5 xs = np.arange(binom.ppf(0.01, n, p), binom.ppf(0.99, n, p)) # E(X) = np, D(X) = np(1-p) mean, var, skew, kurt = binom.stats(n, p, loc=0, moments='mvsk') print("mean: %.2f, var: %.2f, skew: %.2f, kurt: %.2f" % (mean, var, skew, kurt)) fig, axs = plt.subplots(1, 3) # 显示pmf ys = binom.pmf(xs, n, p) axs[0].plot(xs, ys, 'bo', markersize=5, label='binom pmf') axs[0].legend() # 显示cdf ys = binom.cdf(xs, n, p) axs[1].plot(xs, ys, 'bo', markersize=5, label='binom cdf') axs[1].legend() # 随机变量RVS data = binom.rvs(n, p, size=1000) import sys sys.path.append("../../thinkstats") import Pmf pmf = Pmf.MakePmfFromList(data) xs, ys = pmf.Render() axs[2].plot(xs, ys, 'bo', markersize=5, label='rvs pmf') axs[2].legend() plt.show()
def do_statistics(histogram, latex=False): data = gen_data(histogram) emean, evar, eskew, ekurt = binom.stats(TEXT_SIZE * 8, 0.5, moments='mvsk') estd = binom.std(TEXT_SIZE * 8, 0.5) mean = s.mean(data) median = s.median(data) # We will use sample versions because we don't have all the population stdev = s.stdev(data) variance = s.variance(data) sk = skew(data) ku = kurtosis(data) mean_err = abs(emean - mean) / emean std_err = abs(estd - stdev) / estd var_err = abs(evar - variance) / evar kurt_err = abs(ekurt - ku) / ekurt print("Media:\t\t{}\t\t\t(Err) {}".format(mean, mean_err)) print("Mediana:\t{}".format(median)) print("Desv. Std.:\t{}\t(Err) {}".format(stdev, std_err)) print("Varianza.:\t{}\t(Err) {}".format(variance, var_err)) print("Simetría:\t{}".format(sk)) print("Kurtosis:\t{}\t(Err) {}".format(ku, kurt_err)) if latex: print("\n\nLaTeX\n") print( " &".join("{:>11}".format(x) for x in [ "Muestra", "Media", "Mediana", "Desviación", "Variance", "Asimetría", "Curtosis" ]), "\\\\ \\hline") print( " &".join( "{:>11.2f}".format(x) for x in [STUDY_SIZE, mean, median, stdev, variance, sk, ku]), "\\\\") print("\n\n") print(list(enumerate(histogram))) print("\n\n")
def test_binom(self): from scipy.stats import binom import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) n, p = 5, 0.4 mean, var, skew, kurt = binom.stats(n, p, moments='mvsk') x = np.arange(binom.ppf(0.01, n, p), binom.ppf(0.99, n, p)) ax.plot(x, binom.pmf(x, n, p), 'bo', ms=8, label='binom pmf') ax.vlines(x, 0, binom.pmf(x, n, p), colors='b', lw=5, alpha=0.5) rv = binom(n, p) ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1, label='frozen pmf') ax.legend(loc='best', frameon=False) self.assertEqual("AxesSubplot(0.125,0.11;0.775x0.77)", str(ax))
def inom(): #二项分布的概率分布图 fig, ax = plt.subplots(1, 1) n = 100 p = 0.5 #平均值, 方差, 偏度, 峰度 mean, var, skew, kurt = binom.stats(n, p, moments='mvsk') # print(mean,var,skew,kurt) #ppf:累积分布函数的反函数。q=0.01时,ppf就是p(X<x)=0.01时的x值。 #这一步是构建累计图的横坐标,当然不这么做 x = np.arange(binom.ppf(0.01, n, p), binom.ppf(0.99, n, p)) # x=np.arange(0,100) binom.pmf(x, n, p) # ax.plot(x, binom.pmf(x, n, p),'o') # plt.show() #1次试验成功的概率是0.06,没有不合格品概率 print(binom.pmf(0, 5, 0.06)) #5次试验成功的概率是0.06,没一个不合格品概率 print(binom.pmf(1, 5, 0.06)) #有3个及3个一下不合格品 print(binom.cdf(3, 5, 0.06))
from scipy.stats import binom def ncr(n, r): f = math.factorial return f(n) / f(r) / f(n - r) # setting the values # of n and p beta = 10 p = 1.0 / (4.3 * 30 * 24) #-math.exp(-1/(4.3)) print p # obtaining the mean and variance mean, var = binom.stats(beta, p) # list of pmf values # printing mean and variance print("mean = " + str(mean)) print("variance = " + str(var)) for beta in [10]: print "beta={}".format(beta) for rho in [1, 3, 5, 10]: availability = 0.0 for k in range(0, beta + 1): a = 0 if beta - k >= rho: a = float(ncr(beta - k, rho)) / float(ncr(beta, rho)) failure = binom.pmf(k, beta, p) availability += a * failure
#程序文件Pex4_3.py from scipy.stats import binom n, p=20, 0.8 print("期望和方差分布为:", binom.stats(n,p))
def binomial_distribution(self, DendriteList, ModifyRangeMap): # setting the values of n and p # defining the list of k values n = len(DendriteList) p = 1 / n r_values = list(range(n + 1)) # obtaining the mean and variance mean, var = binom.stats(n, p) # list of pmf values # dist = [(binom.pmf(k, n, p)) * n for k in r_values] dist = [] for k in r_values: ans = (binom.pmf(k, n, p)) * n if float(ans) > 0.05: dist.append(int(math.ceil(ans))) # printing the table print( '\n', "<--------------- Binomial distribution simulation: --------------->", '\n') print(len(dist)) print("k\tp(k)") # for index_ in r_values: # print(str(r_values[index_]) + "\t" + "{:.2f}".format(float(dist[index_]))) # # printing mean and variance # print("mean = " + str(mean)) # print("variance = " + str(var)) parallel_histlist = [] for key, value in ModifyRangeMap.items(): parallel_histlist.append(len(value) + 1) # print(parallel_histlist) values_ = histogram(parallel_histlist) # -------------------fig2 --------------------- if max(values_) > max(dist[2:len(dist)]): y_group_range = max(values_) else: y_group_range = max(dist[2:len(dist)]) plt.figure( "Classification of dendritic branch parallel growth vs random simulation" ) plt.subplot(1, 2, 1) plt.bar(range(2, len(values_) + 2), height=values_, color="blue", width=0.35) # plt.yticks(range(0, max(values_))) plt.ylim([0, y_group_range]) # plt.yticks(np.arange(0, max(values_),step=2)) plt.xticks(fontsize=20, rotation=45) plt.yticks(fontsize=20) plt.ylabel('frequency', fontsize=25, fontweight='bold') # plt.xlabel('parallel groups', fontsize=10) plt.xlabel('parallel groups', fontsize=25, fontweight='bold') plt.title('Measured', fontsize=26) ax = plt.gca() ax.xaxis.set_major_locator(MaxNLocator(integer=True)) plt.tight_layout() plt.subplot(1, 2, 2) plt.bar(r_values[2:len(dist)], height=dist[2:len(dist)], color="blue", width=0.35) plt.ylim([0, y_group_range]) # plt.yticks(np.arange(0, max(values_), step=2)) plt.xticks(fontsize=20, rotation=45) plt.yticks(fontsize=20) plt.ylabel('frequency', fontsize=25, fontweight='bold') plt.xlabel('parallel groups', fontsize=25, fontweight='bold') plt.title('Simulation', fontsize=26) # plt.yticks(range(0, max(values_))) ax = plt.gca() ax.xaxis.set_major_locator(MaxNLocator(integer=True)) plt.tight_layout() plt.show()
from scipy.stats import binom, expon import matplotlib.pyplot as plt import numpy as np # 离散型的,二项分布 plt.subplot(311) n = 100 p = 0.5 mean, var, skew, kurt = binom.stats(n, p, moments='mvsk') # ppf:累积分布函数的反函数。q=0.01时,ppf就是p(X<x)=0.01时的x值 x = np.arange(binom.ppf(0.01, n, p), binom.ppf(0.99, n, p)) # 离散的 # 分布律 plt.plot(x, binom.pmf(x, n, p), 'o') # 连续型的,指数分布 plt.subplot(312) plt.title('概率密度') lambdax = 1 / 5 loc = 0 scale = 1 / lambdax means, vars, skews, kurts = expon.stats(loc, scale, moments='mvsk') xx = np.linspace(expon.ppf(0.01, loc, scale), expon.ppf(0.99, loc, scale), 100) #概率密度 plt.plot(xx, expon.pdf(xx, loc, scale), label='expon') plt.subplot(313) plt.title('分布函数') plt.plot(xx, 1 - np.exp(-lambdax * xx), '*') plt.show() print(xx[-1], 1 - np.exp(-lambdax * x[-1]))
from scipy.stats import binom p = 0.5 k= 100 #'mvsk': 'mean', 'variance'm 'skewness', 'kurtosis' mean, var, _ , _ = binom.stats(k ,p, moments='mvsk') print("Mean: %.3f, variance: %.3f" % (mean, var)) ## code-runne.run not found issue
from scipy.stats import binom n, p = 20, 0.8 mean, variance, skewness, kurtosis = binom.stats(n, p, moments='mvsk') print("所求的数字特征为:", binom.stats(n, p, moments='mvsk'))
# 平均値です。 mu_A = n_A * p_A mu_B = n_B * p_B # 標準偏差を計算しましょう。 sigma_A = (n_A * p_A * (1 - p_A))**0.5 sigma_B = (n_B * p_B * (1 - p_B))**0.5 print('プレイヤーAは1試合で、平均{:0.1f}回±{:0.1f}シュートを決めます。'.format(mu_A, sigma_A)) print('\n') print('プレイヤーBは1試合で、平均{:0.1f}回±{:0.1f}シュートを決めます。'.format(mu_B, sigma_B)) """ scipyを利用できます """ mean, var = binom.stats(n_A, p_A) print(mean) print(var**0.5) """ 確率質量関数も求められます コインを10回投げて裏と表を出すことを考えてみましょう。 """ # 10回と、表の確率0.5をセットします。 n = 10 p = 0.5 x = range(n + 1) # 二項分布の確率質量関数をから、実際の確率を計算できます。
plt.yticks(np.arange(0, 1.1, 0.1)) plt.legend(loc='upper left', shadow=True) plt.show() # ### Binomial Distribution # In[3]: #Binomial Distribution from scipy.stats import binom n, p = 10, 0.4 x = np.arange(binom.ppf(0.01, n, p), binom.ppf( 0.99, n, p)) #Percent Point Function (inverse of cdf — percentiles) print("Mean : ", binom.stats(n, p, moments='m')) print("Variance : ", binom.stats(n, p, moments='v')) print("Prob. Mass Func. : ", binom.pmf(x, n, p)) print("Cum. Density Func.: ", binom.cdf(x, n, p)) CDF = binom.cdf(x, n, p) fig = plt.figure(figsize=(20, 10)) plt.subplot(221) plt.plot(x, binom.pmf(x, n, p), 'go', ms=8, label='PMF') plt.vlines(x, 0, binom.pmf(x, n, p), colors='g', lw=5, alpha=0.5) plt.xlabel("Sample Space of Binomial Distribution", fontsize=14) plt.ylabel("PMF", fontsize=14) plt.title("Probability Distribution of Binomial(n=10,p=0.4) Distribution", fontsize=16) plt.xticks(np.arange(0, 8, 1))
plt.show() #Binomial distribution #Example1 #https://data-flair.training/blogs/python-probability-distributions/ #Describes variation in the number of one outcome from replicate to replicate import seaborn as sb import matplotlib.pyplot as plt from scipy.stats import binom n=20 p=0.8 binom.rvs(size=10,n=n,p=p) moments = binom.stats(n, p, moments='mvsk') data_binom = binom.rvs(n=20,p=0.8,loc=0,size=1000) ax = sb.distplot(data_binom,kde=True,color='blue',hist_kws={"linewidth": 25,'alpha':1}) ax.set(xlabel='Binomial', ylabel='Frequency') plt.show() print('mean,var,skew,kurt =', moments) #Example2 #Describes variation in the number of one outcome from replicate to replicate from scipy.stats import binom import matplotlib.pyplot as plt import seaborn as sb n = 484382 #number of of particular set of trials (e.g. male births) N = 938223 #total number ep = (n-1)/(N+2) #estimated probability of occurence of each trial size = shape of returned array
# Distribucion Binomial usando scipy.stats from scipy.stats import binom import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) # Calculamos los primeros momentos: n, p = 5, 0.4 mean, var, skew, kurt = binom.stats(n, p, moments='mvsk') # Mostramos el pmf de la variable aleatoria (``pmf``): x = np.arange(binom.ppf(0.01, n, p), binom.ppf(0.99, n, p)) ax.plot(x, binom.pmf(x, n, p), 'bo', ms=8, label='pmf binomial') ax.vlines(x, 0, binom.pmf(x, n, p), colors='b', lw=5, alpha=0.5) ax.legend(loc='best', frameon=False) # Comprobar la exactitud del ``cdf`` y ``ppf``: prob = binom.cdf(x, n, p) np.allclose(x, binom.ppf(prob, n, p)) # Generamos numeros aleatorios r = binom.rvs(n, p, size=1000) plt.show()
import matplotlib.pyplot as plt import numpy as np from scipy.stats import binom, skew np.random.seed(5) # Binomial Distribution Parameters n = 10 p = 0.2 # Descriptive Stats for Original Probability Distribution pop_mean, pop_var = binom.stats(n, p, loc=0, moments='mv') pop_std = binom.std(n, p) print('Population Mean: ', pop_mean) print('Population Variance: ', pop_var) # Random samples x = np.arange(0, n + 1) pmf = binom(n, p).pmf(x) # Sample statistics mean_of_means = [] variances = [] skews = [] samp_sizes = [5, 10, 50, 100] for i, samp_size in enumerate(samp_sizes, start=1): means = [] for sample in range(10000): pts = binom.rvs(n, p, size=samp_size)
import matplotlib.pyplot as plt from scipy.stats import binom # n is the number of trials n = int(input("Input the number of trials: ")) # p is the probability of success p = float(input("Input the success probability: ")) # While probabilities should be <= to 1 while p > 1: print("p value should be <= 1") p = float(input("Input the success probability: ")) # defining the list of r values r_values = list(range(n + 1)) # mean and variance mean, var = binom.stats(n, p) # list of pmf values dist = [binom.pmf(r, n, p) for r in r_values] # printing the table print("r\tp(r)") for i in range(n + 1): print(str(r_values[i]) + "\t" + str(dist[i])) # Plot values plt.bar(r_values, dist) plt.show() # Display MEAN, VARIANCE print("mean = " + str(mean)) print("variance = " + str(var))