[PYTHON] Defeat the probability density function of the normal distribution

Synopsis

f(x)=\frac{1}{\sqrt{2\piσ^2}}e^{-\frac{(x-μ)^2}{2σ^2}}

The normal distribution is one of the typical distributions and is a frequent distribution in the field of statistics. However, the probability density function of the normal distribution is very complicated like the ↑ formula, and has buried many beginners. I also remember having a headache and dizziness when I first saw this formula when I started studying statistics.

In this article, the coefficient part ($ \ frac {1} {\ sqrt {2 \ piσ ^ 2}} ) and the exponent part (-\ frac {(x-μ) ^ 2} {2σ ^ 2) of the equation ↑ } $) and describe why it was shaped like this.

Index part

Many events in the world have the highest probability of taking an average value, and the probability of taking that value decreases as the distance from the average value increases.

An expression that simply expresses this is

f(x)=e^{-x^2}

is. In a graph,

import numpy as np
import matplotlib.pyplot as plt

def normal_dist(x, ave = 0, disp=1):
    return np.exp((-(x-ave)**2)/(2*disp**2))

x = np.linspace(-3, 3)
y = normal_dist(x)

plt.plot(x,y)
plt.grid(axis="both")
plt.show()

image.png It looks like a mountain.

This formula is inferior in versatility because only one graph can be drawn. Therefore, ① Move left and right of the graph ② Change the graph width Add the function of.

Move left and right of the graph

By changing the $ x $ part to $ x-μ $, you can shift the position of $ x $, which takes the maximum value.

f(x)=e^{-(x-μ)^2}

It becomes such a formula. Let's change the value of $ μ $ and see the change in the graph.

color = ["b", "g", "r", "c", "m"]
for i, col in enumerate(color):
    y = normal_dist(x, i)
    plt.plot(x, y, color=col)
    
plt.show()

image.png The graph shifted to the right as $ μ $ increased.

Change graph width

You can change the width by multiplying the exponent part by $ \ frac {1} {2σ ^ 2} $.

f(x)=e^{-\frac{x^2}{2σ^2}}
x = np.linspace(-10, 10)
color = ["b", "g", "r", "c", "m"]
for i, col in enumerate(color):
    y = normal_dist(x, 0, i+1)
    plt.plot(x, y, color=col)
    
plt.show()

image.png

I succeeded in changing the width. The square of σ is to handle both positive and negative, and the multiplication of $ 2 $ is to make it easier to calculate.

Coefficient part

Since it is a probability density function, the total area must be $ 1 $. So multiply the function by a convenient value. Put a convenient factor as $ c $ and

\int_{-\infty}^{\infty}ce^{-\frac{(x-μ)^2}{2σ^2}}dx=1

To calculate $ c $.

At first glance it may seem like a complicated calculation,

\int_{-\infty}^{\infty}e^{-ax^2}dx=\sqrt{\frac{\pi}{a}}

Using this Gaussian integral formula, if $ a = \ frac {1} {2σ ^ 2} $

c=\frac{1}{\sqrt{2\piσ^2}}

And it is easy to calculate. The solution is now the coefficient of the formula at the top.

After all, this formula was a formula that defines the probability density function by making $ f (x) = e ^ {-x ^ 2} $ versatile and adjusting the coefficients. I'm happy.

Referenced site

https://to-kei.net/distribution/normal-distribution/density-function-derivation/ Semantic understanding of the density function of the normal distribution

https://mathtrain.jp/gauss Two proofs of the Gaussian integral formula

https://bellcurve.jp/statistics/course/7797.html 14-1. Normal distribution

Recommended Posts

Defeat the probability density function of the normal distribution
Try transcribing the probability mass function of the binomial distribution in Python
Explain the nature of the multivariate normal distribution graphically
Verification of normal distribution
[Python] Note: A self-made function that finds the area of the normal distribution
Steps to calculate the likelihood of a normal distribution
Tweet the probability of precipitation as part of the function of the bot
Check the asymptotic nature of the probability distribution in Python
Understanding the meaning of complex and bizarre normal distribution formulas
PRML Diagram Drawing Exercise 1.4 Nonlinear Transformation of Probability Density Function
Is the probability of precipitation correct?
Test the goodness of fit of the distribution
Embedding method DensMAP that reflects the density of distribution of high-dimensional data
Carefully derive the interquartile range of the standard normal distribution from the beginning
Precautions when drawing the probability density function and the histogram on top of each other in matplotlib
[Python3] Rewrite the code object of the function
About the arguments of the setup function of PyCaret
About the Normal Equation of Linear Regression
Text mining: Probability density distribution on the hypersphere and text clustering in KMeans
I tried the pivot table function of pandas
Get the caller of a function in Python
Match the distribution of each group in Python
Concept of Bayesian reasoning (2) ... Bayesian estimation and probability distribution
Fix the argument of the function used in map
Calculate the probability of outliers on a boxplot
Predict the distribution of continuous values ​​other than the normal distribution with ordinary PyTorch or TensorFlow
[CodeIQ] I wrote the probability distribution of dice (from CodeIQ math course for machine learning [probability distribution])
Bivariate normal distribution
Find the cumulative distribution function by sorting (Python version)
Plot and understand the multivariate normal distribution in Python
# Function that returns the character code of a string
Drawing on Jupyter using the plot function of pandas
I examined the argument class_weight of Chainer's softmax_cross_entropy function.
Python: Diagram of 2D data distribution (kernel density estimation)
How to hit the document of Magic Function (Line Magic)
Check the type and version of your Linux distribution
How to find the average amount of information (entropy) of the original probability distribution from a sample