Python implementation probability distribution

Source: Internet
Author: User

1. Two-item distribution (discrete)
ImportNumPy as NP fromSciPyImportStatsImportMatplotlib.pyplot as Plt" "# Two distributions (binomial distribution) # Prerequisites: Independent repetition Test, back-up, only two results # Two distributions indicate that the probability of event A in a random experiment is p, then the probability of K event A in repeated n trials is: # f (n,k,p) = Choose (n, k) * P**k * (1-p) * * (N-K)" "#① Defining the basic information for two distributionsP = 0.4#event a probability 0.4n = 5#repeat experiment 5 timesK = Np.arange (n+1)#6 Possible results#k = Np.linspace (STATS.BINOM.PPF (0.01,n,p), STATS.BINOM.PPF (0.99,n,p), n+1) #另一种方式#② Calculating the probability mass distribution of two distributions (probability mass function)#is called mass because of discrete points, the default volume (that is, width) is 1#P (x=x)---is the probabilityProbs =STATS.BINOM.PMF (k, N, p)#Array ([0.07776, 0.2592, 0.3456, 0.2304, 0.0768, 0.01024])#Plt.plot (k, probs)#③ Calculating the cumulative probability of two distributions (cumulative density function)#P (x<=x)--and probabilityCumsum_probs =STATS.BINOM.CDF (k, N, p)#Array ([0.07776, 0.33696, 0.68256, 0.91296, 0.98976, 1. ])#④ According to the cumulative probability to get the corresponding K, here lazy, directly using the above Cumsum_probsK2 =STATS.BINOM.PPF (Cumsum_probs, N, p)#Array ([0, 1, 2, 3, 4, 5])#⑤ forgery of random variables conforming to two-item distributions (randomness variates)X = Stats.binom.rvs (n,p,size=20)#Array ([2, 3, 1, 2, 2, 2, 1, 2, 2, 3, 3, 0, 1, 1, 1, 2, 3, 4, 0, 3])#⑧ makes the frequency histogram above that satisfies the two-item distribution random variable (similar to group by)plt.hist (X)#⑨ make a histogram of frequency distributions that meet the two distribution random variables abovePlt.hist (X, normed=True) plt.show ()

2. Normal distribution (continuous)

" "standard Normal distribution density function: f (x) = exp (-X**2/2)/sqrt (2*PI)" "x= Np.linspace (STATS.NORM.PPF (0.01), STATS.NORM.PPF (0.99), 100)#probability density distribution functions (probability density function)#This is called density because of successive points, with a default volume of 0#f (X)---not probabilityProbs =norm.pdf (x)#plt.plot (x, probs, ' R ', lw=5, alpha=0.6, label= ' Norm pdf ')#cumulative probability density functions cumulative density function#definite integral ∫_-oo^a f (x) DX----is the probabilityCumsum_probs =stats.norm.cdf (x)#forgery of random variables with normal distribution x#the LOC and scale parameters allow you to specify the offset and scaling parameters of a random variable. For random variables with normal distributions, these two parameters are equivalent to specifying their expected and standard deviations:X = Stats.norm.rvs (loc=1.0, scale=2.0, size=1000)#⑨ frequency distribution histogram of random variables with normal distribution abovePlt.hist (X, Normed=true, histtype='stepfilled', alpha=0.2) plt.legend (Loc=' Best', frameon=False) plt.show ()#parameters are estimated for the given data. It's lazy here, just use the x above.Mean, std =Stats.norm.fit (X)#Array (1.01810091), Array (2.00046946)

Python implementation probability distribution

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.