SoFunction
Updated on 2024-10-30

Examples of probability distribution operations implemented in Python

This article example describes the operation of probability distribution operations implemented in Python. Shared for your reference, as follows:

1. Binomial distribution (discrete)

import numpy as np
from scipy import stats
import  as plt
'''
# Binomial distribution
# Prerequisites: independently repeated trials, with playbacks, only two outcomes
# The binomial distribution states that the probability of an event A occurring on a randomized trial, if p, is the probability of an event A occurring k times in n repetitions of the trial:
# f(n,k,p) = choose(n, k) * p**k * (1-p)**(n-k)
'''
# ① Define the basic information of the binomial distribution
p = 0.4 # Event A probability 0.4
n = 5  # Repeat the experiment 5 times
k = (n+1) # 6 possible outcomes
#k = ((0.01,n,p), (0.99,n,p), n+1) #anotherway
# ② Calculate the probability mass function of the binomial distribution.
# The reason it's called mass is that discrete points with a default volume (i.e. width) of 1
# P(X=x) --> is the probability that
probs = (k, n, p)
#array([ 0.07776, 0.2592 , 0.3456 , 0.2304 , 0.0768 , 0.01024])
#(k, probs)
# ③ Calculate the cumulative density function of the binomial distribution.
# P(X<=x) --> also probabilities
cumsum_probs = (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 use the above cumsum_probs
k2 = (cumsum_probs, n, p)
#array([0, 1, 2, 3, 4, 5])
# ⑤ Fake random variates with binomial distribution.
X = (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])
#⑧ Make a histogram of the frequency of random variables that satisfy the binomial distribution above (similar to group by).
(X)
#9 Make a histogram of the frequency distribution of the random variable satisfying the binomial distribution above.
(X, normed=True)
()

2. Normal (continuous) distribution

'''
Standard normal distribution
Density function: f(x) = exp(-x**2/2)/sqrt(2*pi)
'''
x = ((0.01), (0.99), 100)
# Probability density function
# It's called a density because of the continuous points, which have a default volume of 0
# f(x) --> not probability
probs = (x)
# (x, probs, 'r-', lw=5, alpha=0.6, label='norm pdf')
# Cumulative density function Cumulative density function
# Definite integral ∫_-oo^a f(x)dx --> is the probability of
cumsum_probs = (x)
# Falsify a random variable X that conforms to a normal distribution
# The loc and scale parameters allow you to specify the offset and scaling parameters of a random variable. For a normally distributed random variable, these two parameters are equivalent to specifying its expected value and standard deviation:
X = (loc=1.0, scale=2.0, size=1000)
#9 Make a histogram of the frequency distribution of the normally distributed random variable above.
(X, normed=True, histtype='stepfilled', alpha=0.2)
(loc='best', frameon=False)
()
# Parameter estimation for the given data. Being lazy here, I'll just use the above X
mean, std = (X)
#array(1.01810091), array(2.00046946)

P.S. NumPy, SciPy & MatPlotLib modules are available for download:

NumPy: /projects/numpy/files/NumPy/1.9.2/
SciPy: /projects/scipy/files/scipy/0.15.1/
MatPlotLib: /

Readers interested in more Python related content can check out this site's topic: theSummary of Python mathematical operations techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques

I hope that what I have said in this article will help you in Python programming.