Probability Density Function (PDF) and Cumulative Distribution Function (CDF) and scipy.stats.norm use

foreword

In the process of doing the algorithm, you may encounter knowledge such as probability density distribution. Here is a supplement to the use of the corresponding scipy piece.

Mainly from scipy.stats import norm

The corresponding is also a normal distribution (Gaussian distribution)

  • https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html

The normal distribution formula is:

0cf67a5018b85a6866b129b9fff3066f.png

The graph looks like this:

26fcd5cc8924268aed4aadb20be79314.png

This graph is the probability density

say a few nouns here

  • Random variables (including discrete random variables + continuous random variables)

  • Probability Density Functions (Probability Density Functions - PDF)

  • Cumulative Distribution Functions (CDF)

For details, you can read this article, which is well understood with some small examples (toss a coin) https://blog.csdn.net/neweastsun/article/details/124191560

Probability Density Function (PDF) and Cumulative Distribution Function (CDF) and scipy.stats.norm use

the code

The corresponding pdf and cdf functions are provided in from scipy.stats import norm

from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 2)

x = np.linspace(norm.ppf(0.0001),
                norm.ppf(0.9999), 100)
print(x)
rv = norm()
ax[0].bar(x, rv.pdf(x), width=0.01, label='pdf')
ax[0].set_xticks([-3, -2, -1, 0, 1, 2, 3])
ax[0].set_yticks([0.0, 0.1, 0.2, 0.3, 0.4])
ax[0].set_title("pdf")
ax[1].plot(x, rv.cdf(x), '-^', linewidth=1, label='cdf', markersize=2)
ax[1].set_xticks([-3, -2, -1, 0, 1, 2, 3])
ax[1].set_yticks(np.linspace(0.0, 1.0, 5))
ax[1].set_title("cdf")
plt.show()

result:

8e7f0efbdc8df00c25186e99f392a664.png

The left side is the probability density distribution map (the peak is around 0, and the two sides are descending successively, and it is highly symmetrical), and the right side is the cumulative distribution map (the maximum value is 1, and the range is 0-1)

In order to draw this picture, we need the original data (in fact, the x-axis data of the pdf and cdf images), which can be obtained by ppf. ppf is the reverse calculation of cdf, which is equivalent to giving a cumulative quantile point (0 to 1, 0 is close to the leftmost side of the pdf picture, and 1 is close to the far right side of the pdf picture), and the corresponding x value is returned.

Recommended reading:

My 2022 Internet School Recruitment Sharing

My 2021 Summary

Talking about the difference between algorithm post and development post

Internet school recruitment research and development salary summary

The 2022 Internet job hunting status, gold 9 silver 10 will soon become copper 9 iron 10! !

Public number: AI snail car

Stay humble, stay disciplined, keep improving

01a92ec21f34c7ef0604e4654a7d8859.jpeg

Send [Snail] to get a copy of "Hands-on AI Project" (written by AI Snail Car)

Send [1222] to get a good leetcode brushing notes

Send [AI Four Classics] Get four classic AI e-books

Guess you like

Origin blog.csdn.net/qq_33431368/article/details/131820488