Probability distribution | probability distribution | Method | data | |
---|---|---|---|---|
1 | Hypergeometric distribution | Hypergeometric distribution | scipy.stats.hypergeom | Discrete type |
2 | Bernoulli distribution | Bernoulli distribution | scipy.stats.bernoulli | Discrete type |
3 | Binomial distribution | binomial distribution | scipy.stats.binom | Discrete type |
4 | Poisson distribution | Poisson distribution | scipy.stats.poisson | Discrete type |
5 | Geometric distribution | Geometric Distribution | scipy.stats.geom | Discrete type |
6 | Negative binomial distribution | Negative Binomial Distribution | scipy.stats.nbinom | Discrete type |
7 | Uniform distribution | uniform distribution | scipy.stats.uniform | Discrete type |
8 | normal distribution | normal distribution | scipy.stats.norm | Continuous type |
9 | Exponential distribution | exponential distribution | scipy.stats.expon | Continuous type |
10 | Gamma distribution | gamma distribution | scipy.stats.gamma | Continuous type |
11 | Beta distribution | beta distribution | scipy.stats.betabinom | Continuous type |
12 | Cauchy distribution | Cauchy distribution | scipy.stats.cauchy | Continuous type |
13 | Lognormal distribution | lognormal distribution | scipy.stats.lognorm | Continuous type |
14 | Pareto distribution | Pareto distribution | scipy.stats.pareto | Continuous type |
15 | Weibull distribution | Weibull distribution | scipy.stats.dweibull | Continuous type |
scipy.stats.norm
** as an example.x
**, parameters ** expected value (mean value) loc
** and ** standard deviation scale
**, as well as ** elements. Number size
, ** Random number generation seedrandom_state = integer
, etc.#Library for numerical calculations
import numpy as np
from scipy import stats
#Library for graph drawing
import matplotlib.pyplot as plt
%matplotlib inline
#Module to make matplotlib support Japanese display
!pip install japanize-matplotlib
import japanize_matplotlib
rvs (loc = 0, scale = 1, size = 1, random_state = None)
#Generate pseudo-random numbers that follow a normal distribution with rvs
norm_rvs = stats.norm.rvs(loc=50, scale=20, size=1000, random_state=0) #Expected value=50,standard deviation=20,Quantity=1000
#Visualization (expressed in histogram)
plt.hist(norm_rvs, bins=10, alpha=0.3, ec='blue')
plt.xlabel("class", fontsize=13)
plt.ylabel("frequency", fontsize=13)
plt.show()
pdf (x, loc = 0, scale = 1)
#Generate arithmetic progression
X = np.arange(start=1, stop=7.1, step=0.1)
#Generate probability density function in pdf
norm_pdf = stats.norm.pdf(x=X, loc=4, scale=0.8) #Expected value=4,standard deviation=0.8
#Visualization
plt.plot(X, norm_pdf)
plt.xlabel("Random variable X", fontsize=13)
plt.ylabel("Probability density pdf", fontsize=13)
plt.show()
#Calculate the probability density when the random variable 5
x = 5
y = stats.norm.pdf(x=x, loc=4, scale=0.8) #Expected value=4,standard deviation=0.8
print("Random variable x=Probability density at 5:", y)
#Plot on the graph of probability density function
plt.plot(X, norm_pdf)
plt.plot(x, y, 'bo') #Place the blue dots
plt.vlines(x, 0.0, y, lw=2, linestyles='dashed') #Vertical line
plt.hlines(y, 1.0, x, lw=2, linestyles='dashed') #Horizon
plt.xlabel("Random variable X", fontsize=13)
plt.ylabel("Probability density pdf", fontsize=13)
plt.show()
pmf (k, n, p, loc = 0)
#Probability of success
p = 0.4
#Number of trials
n = 5
#Number of successes
k = np.arange(0, 5) # array([0, 1, 2, 3, 4])
#Calculate the probability for each success with pmf
binom_pmf = stats.binom.pmf(k, n, p) #Number of successes=0~4,Number of trials=5,Probability of success=0.4
#Visualization
plt.plot(k, binom_pmf, 'bo', ms=8)
plt.vlines(k, 0, binom_pmf, colors='b', lw=3, alpha=0.5)
plt.xticks(k) #x-axis scale
plt.xlabel("Random variable X", fontsize=13)
plt.ylabel("Probability mass function pmph", fontsize=13)
plt.show()
logpdf (x, loc = 0, scale = 1)
#Generate arithmetic progression
X = np.arange(start=1, stop=7.1, step=0.1)
#Generate logarithm of probability density function with logpdf
norm_logpdf = stats.norm.logpdf(x=X, loc=4, scale=0.8) #Expected value=4,standard deviation=0.8
#Visualization
plt.plot(X, norm_logpdf)
plt.xlabel("Random variable X", fontsize=13)
plt.ylabel("Log probability density function logpdf", fontsize=13)
plt.show()
#Get maximum log probability density
logpdf_max = np.max(norm_logpdf)
print('x=Log probability density of 4:', logpdf_max)
#Calculate probability density
x = 4
v_pdf = stats.norm.pdf(x=x, loc=4, scale=0.8) #Expected value=4,standard deviation=0.8
#Calculate the logarithm of probability density
v_pdf_log = np.log(v_pdf)
print('x=Logarithm of probability density of 4:', v_pdf_log)
#Visualization
plt.plot(X, norm_logpdf)
plt.plot(4, logpdf_max, 'bo') #Place the blue dots
logpdf_min = np.min(norm_logpdf) #Minimum log probability density(For drawing)
plt.vlines(x, logpdf_max, logpdf_min, lw=2, linestyles='dashed') #Vertical line
plt.hlines(logpdf_max, 1.0, x, lw=2, linestyles='dashed') #Horizon
plt.xlabel("Random variable X", fontsize=13)
plt.ylabel("Log probability density function logpdf", fontsize=13)
plt.show()
cdf (x, loc = 0, scale = 1)
#Generate x-axis arithmetic progression
X = np.linspace(start=1, stop=7, num=100)
#Generate cumulative distribution function with cdf
norm_cdf = stats.norm.cdf(x=X, loc=4, scale=0.8) #Expected value=4,standard deviation=0.8
#x with cdf=Calculate cumulative probabilities of 5 or less
under_5 = stats.norm.cdf(x=5, loc=4, scale=0.8) #Expected value=4,standard deviation=0.8
print('Probability of becoming 5 or less:', under_5)
#Visualization
plt.plot(X, norm_cdf)
plt.plot(5, under_5, 'bo') #Place the blue dots
plt.vlines(5, 0.0, under_5, lw=2, linestyles='dashed') #Vertical line
plt.hlines(under_5, 1.0, 5, lw=2, linestyles='dashed') #Horizon
plt.xlabel("Random variable X", fontsize=13)
plt.ylabel("Cumulative probability density cdf", fontsize=13)
plt.show()
sf (x, loc = 0, scale = 1)
#Generate x-axis arithmetic progression
X = np.linspace(start=1, stop=7, num=100)
#Generate survival function with sf
norm_sf = stats.norm.sf(x=X, loc=4, scale=0.8) #Expected value=4,standard deviation=0.8
#x with sf=Calculate cumulative probabilities of 5 or more
upper_5 = stats.norm.sf(x=5, loc=4, scale=0.8) #Expected value=4,standard deviation=0.8
print('Probability of 5 or more:', upper_5)
#Visualization
plt.plot(X, norm_sf)
plt.plot(5, upper_5, 'bo') #Place the blue dots
plt.vlines(5, 0.0, upper_5, lw=2, linestyles='dashed') #Vertical line
plt.hlines(upper_5, 1.0, 5, lw=2, linestyles='dashed') #Horizon
plt.xlabel("Random variable X", fontsize=13)
plt.ylabel("Survival function sf", fontsize=13)
plt.show()
ppf (q, loc = 0, scale = 1)
#Generate x-axis arithmetic progression
X = np.arange(start=1, stop=7, step=0.1)
#Generate probability density function in pdf
norm_pdf = stats.norm.pdf(x=X, loc=4, scale=0.8) #Expected value=4,standard deviation=0.8
#Get the variable corresponding to the cumulative distribution function 75% with ppf
q_75 = stats.norm.ppf(q=0.75, loc=4, scale=0.8) #Expected value=4,standard deviation=0.8
print('Cumulative distribution function 75% point random variable:', q_75)
#Get the probability density of the variable with pdf
v = stats.norm.pdf(x=q_75, loc=4, scale=0.8)
print('Cumulative distribution function 75% point probability density:', v)
#Visualization
plt.plot(X, norm_pdf)
plt.plot(q_75, v, 'bo') #Place the blue dots
plt.vlines(q_75, 0.0, v, lw=2, linestyles='dashed') #Vertical line
plt.hlines(v, 1.0, q_75, lw=2, linestyles='dashed') #Horizon
plt.xlabel("Random variable X", fontsize=13)
plt.ylabel("Probability density function pdf", fontsize=13)
plt.show()
#Generate x-axis arithmetic progression
X = np.arange(start=1, stop=7, step=0.1)
#Generate probability density function in pdf
norm_pdf = stats.norm.pdf(x=X, loc=4, scale=0.8) #Expected value=4,standard deviation=0.8
#Get the variable corresponding to the reverse survival function 25% with isf
q_25 = stats.norm.isf(q=0.25, loc=4, scale=0.8) #Expected value=4,standard deviation=0.8
print('Random variable with 25% reverse survival function:', q_25)
#Get the probability density of the variable with pdf
v = stats.norm.pdf(x=q_25, loc=4, scale=0.8)
print('Probability density of 25% point of inverse survival function:', v)
#Visualization
plt.plot(X, norm_pdf)
plt.plot(q_25, v, 'bo') #Place the blue dots
plt.vlines(q_25, 0.0, v, lw=2, linestyles='dashed') #Vertical line
plt.hlines(v, 1.0, q_25, lw=2, linestyles='dashed') #Horizon
plt.xlabel("Random variable X", fontsize=13)
plt.ylabel("Probability density function pdf", fontsize=13)
plt.show()
#Generate x-axis arithmetic progression
X = np.arange(start=1, stop=7, step=0.1)
#Generate probability density function in pdf
norm_pdf = stats.norm.pdf(x=X, loc=4, scale=0.8) #Expected value=4,standard deviation=0.8
#Get the variable corresponding to the confidence interval 95% with interval
lower, upper = stats.norm.interval(alpha=0.95, loc=4, scale=0.8) #Confidence factor=0.95,Expected value=4,standard deviation=0.8
print('Lower limit of 95% confidence interval:', lower)
print('Upper limit of 95% confidence interval:', upper)
#Get the probability density of each variable with pdf
v_lower = stats.norm.pdf(x=lower, loc=4, scale=0.8) #Expected value=4,standard deviation=0.8
v_upper = stats.norm.pdf(x=upper, loc=4, scale=0.8) #Expected value=4,standard deviation=0.8
#Visualization
plt.plot(X, norm_pdf)
plt.plot(lower, 0.0, 'k|')
plt.plot(upper, 0.0, 'k|')
plt.vlines(lower, 0.0, v_lower, lw=0.8) #Lower limit vertical line
plt.vlines(upper, 0.0, v_upper, lw=0.8) #Upper vertical line
plt.xlabel("Random variable X", fontsize=13)
plt.ylabel("Probability density function pdf", fontsize=13)
plt.show()
Recommended Posts