def test_expon(self): from scipy.stats import expon import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) mean, var, skew, kurt = expon.stats(moments='mvsk') x = np.linspace(expon.ppf(0.01), expon.ppf(0.99), 100) ax.plot(x, expon.pdf(x), 'r-', lw=5, alpha=0.6, label='expon pdf') rv = expon() ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') vals = expon.ppf([0.001, 0.5, 0.999]) np.allclose([0.001, 0.5, 0.999], expon.cdf(vals)) self.assertEqual(str(ax), "AxesSubplot(0.125,0.11;0.775x0.77)")
def main(): fix, ax = plt.subplots(1, 1) mean, var, skew, kurt = expon.stats(moments='mvsk') x = exp_dist(0.9) #c=0.9 ax.plot(x, expon.pdf(x), 'r-', lw=5, alpha=0.6, label='expon pdf') #freeze rv = expon() ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') #generate random num r = expon.rvs(size=1000) # compare #ax.hist(r, density=True, histtype='stepfilled', alpha=0.2) ax.legend(loc='best', frameon=False) plt.show()
def get_population_average_estimate(param, shape): size = 100000 if shape == 'normal' or shape == 'contaminated normal': mu = 0 elif shape == 'lognormal': mu = lognorm.stats(param, moments='m') elif shape == 'contaminated chi-squared': # mu = chi2.stats(4, 0, param, moments='m') data = chi2.rvs(4, size=size) contam_inds = np.random.randint(size, size=int(param * size)) data[contam_inds] *= 10 mu = np.mean(data) elif shape == 'exponential': mu = expon.stats(0, param, moments='m') return mu
'r-', lw=5, alpha=0.6, label='gamma cdf') ax2.set_title('gamma cdf') # logistic b = 0.5 mean, var, skew, kurt = logistic.stats(b, moments = 'mvsk') x = np.linspace(logistic.ppf(0.01, b), logistic.ppf(0.99, b), 100) ax3.plot(x, logistic.pdf(x, b), 'g-', lw=5, alpha=0.6, label='gamma pdf') ax3.set_title('logistic pdf') ax4.plot(x, logistic.cdf(x, b), 'g-', lw=5, alpha=0.6, label='gamma cdf') ax4.set_title('logistic cdf') # exponential a = 1.99 mean, var, skew, kurt = expon.stats(a, moments = 'mvsk') x = np.linspace(expon.ppf(0.01, a), expon.ppf(0.99, a), 100) ax5.plot(x, expon.pdf(x, a), 'b-', lw=5, alpha=0.6, label='gamma pdf') ax5.set_title('exponential pdf') ax6.plot(x, expon.cdf(x, a), 'b-', lw=5, alpha=0.6, label='gamma cdf') ax6.set_title('exponential cdf') plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0) 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]))
plt.legend(loc='upper left', shadow=True) plt.show() # ### Exponential Distribution # In[7]: #Exponential Distribution from scipy.stats import expon loc, scale = 0, 0.67 # Scale is 1/Lambda x = np.linspace(expon.ppf(0.01, loc, scale), expon.ppf(0.99, loc, scale), 25) #Percent Point Function (inverse of cdf — percentiles) print("Mean : ", expon.stats(loc, scale, moments='m')) print("Variance : ", expon.stats(loc, scale, moments='v')) print("Prob. Dens. Func. : ", expon.pdf(x, loc, scale)) print("Cum. Density Func.: ", expon.cdf(x, loc, scale)) CDF = expon.cdf(x, loc, scale) fig = plt.figure(figsize=(20, 10)) plt.subplot(221) plt.plot(x, expon.pdf(x, loc, scale), 'g', ms=8, label='PDF') plt.xlabel("Sample Space of Exponential Distribution", fontsize=14) plt.ylabel("PDF", fontsize=14) plt.title("Probability Distribution of Exponential(λ=1.5) Distribution", fontsize=16) plt.xticks(np.arange(0, 5, 1)) plt.yticks(np.arange(0, 1.7, 0.1))
def main(): parser = argparse.ArgumentParser() parser.add_argument('-s', '--stages', type=int, required=False, help='Etapas de la distribución') parser.add_argument('-l', '--lambdap', type=float, required=True, nargs='+', help='Parámetro lambda de cada distribución') parser.add_argument('-r', '--runs', type=int, required=True, help='Ejecuciones a realizar por cada simulación') parser.add_argument('-o', '--output', type=str, required=False, help='Archivo de salida para la grafica') parser.add_argument('-d', '--dist', type=str, required=True, choices=['erlang', 'expon', 'hyperexp'], help='Distribución a emplear para la simulación') parser.add_argument('--no-graph', required=False, help='Suprime la salida como gráfica', dest='graph', action='store_false') parser.add_argument('--graph', required=False, help='Habilita la salida como gráfica (usar con [-o])', dest='graph', action='store_true') parser.add_argument('-p', '--probability', required=False, type=float, help='Probabilidad para la distribución Hiperexp.') parser.set_defaults(graph=True) args = parser.parse_args() # msg = 'Distribución {3} con {0} etapas (lambda={1}) en {2} ejecuciones' # print msg.format(args.stages, args.lambdap, args.runs, args.dist) fig, ax = plt.subplots(1, 1) if args.dist in 'erlang': if args.stages <= 0: print 'Error: se necesita un número válido de etapas' sys.exit(1) lambdap = args.lambdap[0] mean, var, skew, kurt = erlang.stats(args.stages, scale=lambdap, moments='mvsk') x = np.linspace(erlang.ppf(0.00001, args.stages, scale=lambdap), erlang.ppf(0.99999, args.stages, scale=lambdap), num=1000) rv = erlang(args.stages, scale=lambdap) ax.plot(x, rv.pdf(x), 'r-', lw=5, alpha=0.6, label='Erlang PDF') # Generate random numbers with this distribution r = erlang.rvs(args.stages, scale=lambdap, size=args.runs) ax.hist(r, bins=20, normed=True, histtype='stepfilled', alpha=0.4, label='Experimental values') meanexp = np.mean(r) varexp = np.var(r) print 'Mediaexperimental: {0} MediaAnalitica: {1}'.format(meanexp, mean) print 'Sigma2_exp: {0} Sigma2_a: {1}'.format(varexp, var) print 'CoV_exp: {0} CoV_a: {1}'.format(np.sqrt(varexp)/meanexp, np.sqrt(var)/mean) elif args.dist in 'expon': lambdap = args.lambdap[0] mean, var, skew, kurt = expon.stats(scale=lambdap, moments='mvsk') x = np.linspace(expon.ppf(0.00001, scale=lambdap), expon.ppf(0.99999, scale=lambdap), num=1000) rv = expon(scale=lambdap) ax.plot(x, rv.pdf(x), 'r-', lw=5, alpha=0.6, label='Exponential PDF') # Generate random numbers with this distribution r = expon.rvs(scale=lambdap, size=args.runs) ax.hist(r, bins=20, normed=True, histtype='stepfilled', alpha=0.4, label='Experimental values') meanexp = np.mean(r) varexp = np.var(r) print 'Mediaexperimental: {0} MediaAnalitica: {1}'.format(meanexp, mean) print 'Sigma2_exp: {0} Sigma2_a: {1}'.format(varexp, var) print 'CoV_exp: {0} CoV_a: {1}'.format(np.sqrt(varexp)/meanexp, np.sqrt(var)/mean) elif args.dist in 'hyperexp': rv = hyperexp(args.probability, args.lambdap[0], args.lambdap[1]) x = np.linspace(0.00000001, 10.99999, num=1000) ax.plot(x, rv.pdf(x), 'r-', lw=5, alpha=0.6, label='HyperExp PDF') # ax.plot(x, rv.cdf(x), 'b-', lw=2, alpha=0.6, label='HyperExp CDF') r = rv.rvs(size=args.runs) ax.hist(r, normed=True, bins=100, range=(0, 11), histtype='stepfilled', alpha=0.4, label='Experimental values') meanexp = np.mean(r) varexp = np.var(r) mean = rv.mean() var = rv.standard_dev()**2 print 'Mediaexperimental: {0} MediaAnalitica: {1}'.format(meanexp, mean) print 'Sigma2_exp: {0} Sigma2_a: {1}'.format(varexp, var) print 'CoV_exp: {0} CoV_a: {1}'.format(np.sqrt(varexp)/meanexp, rv.CoV()) if args.graph: ax.legend(loc='best', frameon=False) plt.show()
def testExponential(betta, size): values = [lhw.rnexp(betta) for i in range(size)] mean, var = expon.stats(0, betta, moments='mv') startWork(values, mean, var, "exponential") pass
beta_est[k]=np.dot(ganma[:,k],x) / sample_sum_of[k] print(np.abs(beta_true[k]- beta_est[k])) total_sample=sum(sample_sum_of) for k in range(K): alpha_est[k]=sample_sum_of[k]/total_sample #print(np.sum(np.abs(alpha_true-alpha_est)),'',np.sum(np.abs(beta_true - beta_est))) print(' '.join(map(str,np.abs(alpha_true-alpha_est))),' '.join(map(str,np.abs(beta_true - beta_est)))) #reusult print('#alpha_true=',alpha_true) print('#alpah_est =',alpha_est) print('#beta_ture',beta_true) print('#beta',beta_est) fig,ax=plt.subplots(1,1) mean, var, skew ,kurt=expon.stats(moments='mvsk') X=np.linspace(min(x),max(x),) for k in range(K): ax.plot(X,0.5*sample*expon.pdf(X,loc=0,scale=beta_true[k]),'r--',lw=2,label='frozen pdf') ax.plot(X,0.5*sample*expon.pdf(X,loc=0,scale=beta_est[k]),'b--',lw=2,label='frozen pdf') num_bin=10 hist,bins=np.histogram(x,bins=num_bin) width=0.6*(bins[1]-bins[0]) center=(bins[:-1]+bins[1:])/2 ax.hist(x,bins,range=(min(x),max(x)),facecolor='b',alpha=0.4) plt.show()
aVec = norm.stats(loc=[0, 1, 2], scale=[1, 2, 4], moments="mv") print('vectorized: ') print(aVec) # random numbers from multivariate normal # does not give Multivariate normal! print('MULTI?! Does not give multivariate Normal!') rvs = norm.rvs(loc=np.array([0, 1]), scale=np.array([[1, 0], [0, 1]])) print(rvs) print('\n\n') ############################################################# ############################################################# # In general the standardized distribution for a random variable X is # obtained through the transformation (X - loc) / scale. The # default values are loc = 0 and scale = 1. print('exponential dist') print(expon.stats()) # for exponential dist scale=1/lambda print(expon.mean(scale=3)) print('UNIFORM!!!:') # This distribution is constant between loc and loc + scale. from scipy.stats import uniform print(uniform.cdf([0, 1, 2, 34, 5], loc=1, scale=4)) print(np.mean(norm.rvs(5, size=500))) # shape parameters: from scipy.stats import gamma print('GAMMA') print(gamma(a=1, scale=2).stats(moments="mv")) print(gamma.stats(a=1, scale=2, moments="mv")) ###################################################
def testExpon(): # {{{ """ Exponential Distribution (指数分布[又叫负指数分布]: continuous) 连续概率分布,用于表示独立随机事件发生的时间间隔。 比如旅客进入机场的时间间隔,打进客服中心电话的时间间隔,中文维基百科新条目出现的时间间隔等等. lambda: rate parameter pdf = lambda * exp(-lambda * x) E(X) = 1/lambda (如果你平均每个小时接到2次电话,那么你预期等待每一次电话的时间是半个小时.) """ # 准备数据: 已知lam, 某件事件单位事件发生的次数(频率) # X轴: 间隔时间 # Y轴: 某时间点上的密度(可以大于1) lam = 0.5 theta = 1 / lam xs = np.linspace(expon.ppf(0.01, scale=theta), expon.ppf(0.99, scale=theta), num=1000) # E(X) = 1/lam 即 (theta), D(X) = 1/(lam**2) 即 (theta**2) mean, var, skew, kurt = expon.stats(loc=0, scale=theta, moments='mvsk') print("mean: %.2f, var: %.2f, skew: %.2f, kurt: %.2f" % (mean, var, skew, kurt)) fig, axs = plt.subplots(2, 2) # fig.set_figheight(10) # fig.set_figwidth(14) # print(fig.get_dpi(), fig.get_figheight(), fig.get_figwidth()) # 显示pdf (使用expon.pdf) ys = expon.pdf(xs, scale=theta) axs[0][0].plot(xs, ys, 'bo', markersize=5, label='expon pdf') axs[0][0].legend() axs[0][0].set_title('lambda = %.2f' % lam) axs[0][0].set_xlabel(u"间隔时间") axs[0][0].set_ylabel(u"概率密度") # 显示pdf (使用np.exp) ys = lam * np.exp(-lam * xs) axs[0][1].plot(xs, ys, 'bo', markersize=5, label='np exp') axs[0][1].legend() axs[0][1].set_title('lambda = %.2f' % lam) # 显示cdf ys = expon.cdf(xs, scale=theta) axs[1][0].plot(xs, ys, 'bo', markersize=5, label='expon cdf') axs[1][0].legend() axs[1][0].set_title('lambda = %.2f' % lam) # 随机变量RVS data = expon.rvs(scale=theta, size=1000) data = np.around(data, decimals=1) 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') axs[1][1].scatter(xs, ys, label='rvs pmf') axs[1][1].legend() axs[1][1].set_xlabel(u"间隔时间") axs[1][1].set_ylabel(u"量化后的概率") plt.show()
def main(): parser = argparse.ArgumentParser() parser.add_argument("-s", "--stages", type=int, required=False, help="Etapas de la distribución") parser.add_argument( "-l", "--lambdap", type=float, required=True, nargs="+", help="Parámetro lambda de cada distribución" ) parser.add_argument("-r", "--runs", type=int, required=True, help="Ejecuciones a realizar por cada simulación") parser.add_argument("-o", "--output", type=str, required=False, help="Archivo de salida para la grafica") parser.add_argument( "-d", "--dist", type=str, required=True, choices=["erlang", "expon", "hyperexp"], help="Distribución a emplear para la simulación", ) parser.add_argument( "--no-graph", required=False, help="Suprime la salida como gráfica", dest="graph", action="store_false" ) parser.add_argument( "--graph", required=False, help="Habilita la salida como gráfica (usar con [-o])", dest="graph", action="store_true", ) parser.set_defaults(graph=True) args = parser.parse_args() msg = "Distribución {3} con {0} etapas (lambda={1}) en {2} ejecuciones" print msg.format(args.stages, args.lambdap, args.runs, args.dist) fig, ax = plt.subplots(1, 1) if args.dist in "erlang": if args.stages <= 0: print "Error: se necesita un número válido de etapas" sys.exit(1) lambdap = args.lambdap[0] mean, var, skew, kurt = erlang.stats(args.stages, scale=lambdap, moments="mvsk") print "E[X]={0}, var(X)={1}".format(mean, var) x = np.linspace( erlang.ppf(0.00001, args.stages, scale=lambdap), erlang.ppf(0.99999, args.stages, scale=lambdap), num=1000 ) rv = erlang(args.stages, scale=lambdap) ax.plot(x, rv.pdf(x), "r-", lw=5, alpha=0.6, label="Erlang PDF") # Generate random numbers with this distribution r = erlang.rvs(args.stages, scale=lambdap, size=args.runs) ax.hist(r, bins=20, normed=True, histtype="stepfilled", alpha=0.2) meanexp = np.mean(r) varexp = np.var(r) print "Mediaexperimental: {0} MediaAnalitica: {1}".format(meanexp, mean) print "Sigma2_exp: {0} Sigma2_a: {1}".format(varexp, var) print "CoV_exp: {0} CoV_a: {1}".format(np.sqrt(varexp) / meanexp, np.sqrt(var) / mean) elif args.dist in "expon": lambdap = args.lambdap[0] mean, var, skew, kurt = expon.stats(scale=lambdap, moments="mvsk") print "E[X]={0}, var(X)={1}".format(mean, var) x = np.linspace(expon.ppf(0.00001, scale=lambdap), expon.ppf(0.99999, scale=lambdap), num=1000) rv = expon(scale=lambdap) ax.plot(x, rv.pdf(x), "r-", lw=5, alpha=0.6, label="Exponential PDF") # Generate random numbers with this distribution r = expon.rvs(scale=lambdap, size=args.runs) ax.hist(r, bins=20, normed=True, histtype="stepfilled", alpha=0.2) meanexp = np.mean(r) varexp = np.var(r) print "Mediaexperimental: {0} MediaAnalitica: {1}".format(meanexp, mean) print "Sigma2_exp: {0} Sigma2_a: {1}".format(varexp, var) print "CoV_exp: {0} CoV_a: {1}".format(np.sqrt(varexp) / meanexp, np.sqrt(var) / mean) elif args.dist in "hyperexp": print "HyperExponential RV" rv = hyperexp(0.1, args.lambdap[0], args.lambdap[1]) x = np.linspace(0.00000001, 10.99999, num=1000) ax.plot(x, rv.pdf(x), "r-", lw=5, alpha=0.6, label="HyperExp PDF") # ax.plot(x, rv.cdf(x), 'b-', lw=2, alpha=0.6, label='HyperExp CDF') r = rv.rvs(size=args.runs) ax.hist(r, normed=True, bins=100, range=(0, 11), histtype="stepfilled", alpha=0.2) meanexp = np.mean(r) varexp = np.var(r) print "Mediaexperimental: {0} MediaAnalitica: {1}".format(meanexp, mean) print "Sigma2_exp: {0} Sigma2_a: {1}".format(varexp, var) print "CoV_exp: {0} CoV_a: {1}".format(np.sqrt(varexp) / meanexp, np.sqrt(var) / mean) if args.graph: plt.show()