[Distribution genealogy] The relationship between uniform distribution and triangular distribution

Uniform and Triangular Distributions

The uniform distribution is the easiest continuous random distribution to understand, and it is actually a continuous distribution with equal probability. Its PDF is

f ( x ) = 1 b − a , x ∈ ( a , b ) f(x)=\frac{1}{b-a}, x\in (a,b) f(x)=ba1,x(a,b)

Its samples are at ( a , b ) (a,b)(a,b ) In the interval, almost a rectangle is formed, so it is called rectangular distribution.

Correspondingly, PDF is a triangular distribution, which is a triangular distribution. For lower bound aaa , the upper limit isbbb , the mode of which isccThe triangular distribution of c , its PDF is

f ( x ) = { 2 ( x − a ) ( b − a ) ( c − a ) a ⩽ x ⩽ c 2 ( b − x ) ( b − a ) ( b − c ) c < x ⩽ b f(x)=\left\{\begin{aligned} \frac{2(x-a)}{(b-a)(c-a)}\quad a\leqslant x\leqslant c\\ \frac{2(b-x)}{(b-a)(b-c)}\quad c<x\leqslant b \end{aligned}\right. f(x)= (ba)(ca)2(xa)axc(ba)(bc)2(bx)c<xb

In scipy.stats, uniformit is the uniform distribution class; triangit is the triangular distribution class. uniformThe input parameters are the left endpoint and length; triangcompared with the uniform distribution, there is one more mode position, and the value range is [ 0 , 1 ] [0,1][0,1 ] as shown below.

from scipy.stats import uniform, triang

fig = plt.figure()
ax = fig.add_subplot(121)
ax.hist(uniform(1,5).rvs(10000), bins=50)
ax = fig.add_subplot(122)
ax.hist(triang(0.5, 1, 5).rvs(10000), bins=50)
plt.show()

The result is

insert image description here

Uniformly distributed addition

Two uniformly distributed random variables X 1 , X 2 X_1, X_2X1,X2, where the samples can be added or subtracted to obtain a triangular distribution, and the lower limit and upper limit of X1 are a 1 , b 1 a_1,b_1a1,b1, the lower limit and upper limit of X2 are respectively a 2 , b 2 a_2,b_2a2,b2, from the characteristics of this triangular distribution, we can see that the lower limit and upper limit of the triangular distribution are a 1 + a 2 , b 1 + b 2 a_1+a_2, b_1+b_2 respectivelya1+a2,b1+b2, whose mode is a 1 + b 1 + a 2 + b 2 2 \frac{a_1+b_1+a_2+b_2}{2}2a1+b1+a2+b2

a1, d1 = 1,5
a2, d2 = 3,6
x1 = uniform(a1,d1).rvs(10000)
x2 = uniform(a2,d2).rvs(10000)
plt.hist(x1+x2, density=True, bins=100)


st = a1+a2
D = d1 + d2
rv = triang(0.5, st, D)
xs = np.linspace(st, st+D, 200)
plt.plot(xs, rv.pdf(xs))

plt.show()

The result is

insert image description here

log uniform distribution

scipy.statsloguniformThe distribution class is also provided in , whose probability density function is

f ( x , a , b ) = 1 x log ⁡ ( a b ) f(x,a,b)=\frac{1}{x\log(\frac{a}{b})} f(x,a,b)=xlog(ba)1

It is conceivable that its probability density curve is an inverse function. If the logarithm of the random variable is taken, it can be transformed into the interval [ log ⁡ a , log ⁡ ba ] [\log a, \log\frac{b}{a}][loga,logab] Uniform distribution, next test

import numpy as np
from scipy.stats import loguniform
import matplotlib.pyplot as plt

a, b = 1, 5

rs = loguniform(a,b).rvs(10000)
plt.hist(np.log10(rs), density=True, bins=100)

rv = uniform(np.log10(a), np.log10(b/a/a))
xs = np.linspace(rv.ppf(0.01), rv.ppf(0.99), 100)
plt.plot(xs, rv.pdf(xs))
plt.show()

insert image description here

Guess you like

Origin blog.csdn.net/m0_37816922/article/details/130699831