Test
郝伟 2022/0/0

简介

累积分布函数 CDF

累积分布函数(Cumulative Distribution Function, CDF),又叫分布函数,是概率密度函数的积分,能完整描述一个实随机变量X的概率分布。

Demo1

import random
import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1,1,figsize=(16,9))
x = np.linspace(norm.ppf(0.01), norm.ppf(0.99), 200)

lines = [
    (0, 2.00, 'r-'),
    (0, 1.00, 'r-'),
    (0, 0.25, 'y-'),
    (0, 0.49, 'b-'),
    (0, 0.09, 'g-'),
    (1, 0.16, 'k-'),
]

for mu, x_scale, x_style in lines:
    ax.plot(x, norm.cdf(x, loc=mu, scale=x_scale), x_style, lw=2, alpha=0.6, label=f'scale = {x_scale:.2f}, loc = {mu}')
ax.set_title('Norm CDF')
ax.grid()
ax.legend()
plt.show()

Demo2

import random
import numpy as np
from scipy.stats import norm
# %matplotlib inline
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1,1,figsize=(8,4))
x = np.linspace(norm.ppf(0.01), norm.ppf(0.99), 200)
ax.plot(x, norm.pdf(x),'r-', lw=2, alpha=0.6, label='scale = 1, loc = 0')
ax.plot(x, norm.pdf(x,scale=0.25),'k-', lw=2, alpha=0.6, label='scale = 0.25, loc = 0')
ax.plot(x, norm.pdf(x,scale=0.49),'g-', lw=2, alpha=0.6, label='scale = 0.49, loc = 0')
ax.plot(x, norm.pdf(x,scale=0.09),'b-', lw=2, alpha=0.6, label='scale = 0.09, loc = 0')
ax.plot(x, norm.pdf(x,loc = 1, scale=0.16),'y-', lw=2, alpha=0.6, label='scale = 0.16, loc = 1')
ax.set_title('norm pdf')
ax.grid()
ax.legend()
plt.show()

PDF, CDF, PPF, SF and RF

import numpy as np
from scipy.stats import norm
mu = 0     # pos
sigma = 1  # scale
xs = np.linspace(norm.ppf(0.01), norm.ppf(0.99), 100)
ys = norm.cdf(xs, loc=mu, scale=sigma)

print(' Probability Distribution Function '.center(80, '-'))
for x in [-1, 0, 1]:
    print(f'pdf({x:5.2f}): {norm.pdf(x):8.5f}')

print(' Cumulative Distribution Function '.center(80, '-'))
for x in [-1, 0, 1]:
    print(f'pdf({x:5.2f}): {norm.cdf(x):8.5f}')
for x in [-1, 0, 1, 2, 3]:
    print(f'pdf({x:5.2f}, mu=1, sigma=2): {norm.cdf(x, loc=1, scale=2):8.5f}')

print('[3sigma theory]')
for i in range(1, 4):
    print(f'{i} sigma: {norm.cdf(i, loc=mu, scale=sigma) - norm.cdf(-i, loc=mu, scale=sigma):10.6%}')

print(' Cumulative Distribution Function '.center(80, '-'))
for y in [0.15866, 0.50000, 0.84134  ]:
    print(f'norm.ppf({y:.5f}): {norm.ppf(y):8.4f}')

print(' Survial Function & Risk Function '.center(80, '-'))
xs = np.linspace(norm.ppf(0.01),norm.ppf(0.99), 10)
ys = norm.sf(xs)
# rf = Risk Function
print('X            CDF         SF          RF')
for x, y in zip(xs, ys):
    rf = norm.pdf(x)/norm.sf(x)
    print(f'{x:8.5f}:   {norm.cdf(x):8.5f}    {y:8.5f}    {rf:8.5f}')

输出

---------------------- Probability Distribution Function -----------------------
pdf(-1.00):  0.24197
pdf( 0.00):  0.39894
pdf( 1.00):  0.24197
----------------------- Cumulative Distribution Function -----------------------
pdf(-1.00):  0.15866
pdf( 0.00):  0.50000
pdf( 1.00):  0.84134
pdf(-1.00, mu=1, sigma=2):  0.15866
pdf( 0.00, mu=1, sigma=2):  0.30854
pdf( 1.00, mu=1, sigma=2):  0.50000
pdf( 2.00, mu=1, sigma=2):  0.69146
pdf( 3.00, mu=1, sigma=2):  0.84134
[3sigma theory]
1 sigma: 68.268949%
2 sigma: 95.449974%
3 sigma: 99.730020%
----------------------- Cumulative Distribution Function -----------------------
norm.ppf(0.15866):  -1.0000
norm.ppf(0.50000):   0.0000
norm.ppf(0.84134):   1.0000
----------------------- Survial Function & Risk Function -----------------------
X            CDF         SF          RF
-2.32635:    0.01000     0.99000     0.02692
-1.80938:    0.03520     0.96480     0.08046
-1.29242:    0.09811     0.90189     0.19189
-0.77545:    0.21904     0.78096     0.37819
-0.25848:    0.39802     0.60198     0.64094
 0.25848:    0.60198     0.39802     0.96939
 0.77545:    0.78096     0.21904     1.34839
 1.29242:    0.90189     0.09811     1.76402
 1.80938:    0.96480     0.03520     2.20551
 2.32635:    0.99000     0.01000     2.66521