Create a standard normal distribution graph in Python

1. What is a standard normal distribution?

Normal distribution with mean μ = 0, variance σ = 1, That is, it is a function expressed by the following formula.

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

2. Create your own standard normal distribution

If you define the above f (x) yourself and try [-5.0, -4.999, -4.998, ..., 4.998, 4.999, 5.0] and the sequence of ~ 5.0 ~ 5.0, You will get a graph that is commonly called a normal distribution.

norm_original.py


# coding: utf-8
import numpy as np
import math

# f(x)Write by yourself
f = lambda x: (math.exp(-x**2/2)) / math.sqrt(2*math.pi)

#Vector x[-5.0, ..., 5.0]Created in the section of
n = np.linspace(-5.0, 5.0, 10000)

# f(x)Get the result of
p = []
for i in range(len(n)):
    p.append(f(n[i]))

#Display on graph
plt.scatter(n, p)
plt.show()

plt_norm_orig.png

I got a beautiful bell, but it's a little annoying to define it one by one. Above all, it takes time when you want to change the mean or variance.

3. Use scipy

With the scipy.stats module In the above code (norm_original.py), the formula defined by the lambda expression can be called from the function.

norm_usescipy.py


# coding: utf-8
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm

#Vector x[-5.0, ..., 5.0]Created in the section of
n = np.linspace(-5.0, 5.0, 10000)

#Average 0,Find the probability of x in a normal distribution with a standard deviation of 1
p = []
for i in range(len(n)):
    p.append(norm.pdf(x=n[i], loc=0, scale=1))

#Create a graph of standard normal distribution by showing the characteristics of random numbers-probabilities in a scatter plot.
plt.scatter(n, p)
plt.show()

pdf_norm_scipy.png

Since you can specify the mean and variance as arguments, you can easily obtain a different form of normal distribution.

4. Look at the simultaneous entropy from the cumulative distribution function

In the previous chapter, we used the probability density function of the normal distribution from scipy.stats.norm.pdf. Now let's play around with it a bit and use the cumulative distribution function of the normal distribution from scipy.stats.norm.cdf.

Wikipedia's [Joint Entropy](https://ja.wikipedia.org/wiki/%E7%B5%90%E5%90%88%E3%82%A8%E3%83%B3%E3%83%88% E3% 83% AD% E3% 83% 94% E3% 83% BC) I will write it referring to the description.

The simultaneous entropy (joint entropy) H (X) represents the uncertainty of the value of the random variable X. When the probability that event x belonging to X occurs is p (x), the entropy of X is

H(X) = - \sum_{x} p_x log_2 (p_x)

It is represented by.

Here, assuming that the probability is on the standard normal distribution, if the entropy is represented in a graph,

norm_usescipy.py


---p.append(norm.pdf(x=n[i], loc=0, scale=1))
+++p.append(norm.cdf(x=n[i], loc=0, scale=1))

cdf_norm.png

You can get H (X) like this.

Recommended Posts

Create a standard normal distribution graph in Python
Create a function in Python
Create a DI Container in Python
Mixed normal distribution implementation in python
Create a binary file in Python
Create a Kubernetes Operator in Python
Create a random string in Python
Create a simple GUI app in Python
Create a JSON object mapper in Python
[GPS] Create a kml file in Python
Create a Vim + Python test environment in 1 minute
Draw a graph of a quadratic function in Python
Create a GIF file using Pillow in Python
Logistic distribution in Python
Create a Python module
Create SpatiaLite in Python
I want to create a window in Python
How to create a JSON file in Python
Graph drawing in python
Create a virtual environment with conda in Python
Create a simple momentum investment model in Python
Create a new page in confluence with Python
Create a datetime object from a string in Python (Python 3.3)
Create a package containing global commands in Python
Create a Python environment
Create a MIDI file in Python using pretty_midi
Create a loop antenna pattern in Python in KiCad
Draw graph in python
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Create a data collection bot in Python using Selenium
[LINE Messaging API] Create a rich menu in Python
Until drawing a 3D graph in Python on windows10
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
Plot and understand the multivariate normal distribution in Python
In Python, create a decorator that dynamically accepts arguments Create a decorator
[Python] Create a Tkinter program distribution file with cx_Freeze
Create a fake Minecraft server in Python with Quarry
Take a screenshot in Python
Create a Wox plugin (Python)
Create gif video in Python
Transposed matrix in Python standard
Generate U distribution in Python
Make a bookmarklet in Python
Create a python numpy array
Draw a heart in Python
Create a directory with python
Create a list in Python with all followers on twitter
Change the standard output destination to a file in Python
Create a child account for connect with Stripe in Python
Let's create a script that registers with Ideone.com in Python.
A memo when creating a directed graph using Graphviz in Python
A standard way to develop and distribute packages in Python
Create code that outputs "A and pretending B" in python
Create a tool to check scraping rules (robots.txt) in Python
Graph the Poisson distribution and the Poisson cumulative distribution in Python and Java, respectively.
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[python] Manage functions in a list
Hit a command in Python (Windows)
Create a CSV reader in Flask