Get normal distribution from data

Fit a normal distribution using a list of data

ALso calculate the probability of a value range. i.e. The CDF


from scipy.stats import norm

import numpy as np

import matplotlib.pyplot as plt


data = np.random.normal(size=1000)

mean, std = norm.fit(data)


fig = plt.figure(figsize=(6,4))

plt.hist(data, bins=100, density=True, alpha=0.6, color='g')


xmin, xmax = plt.xlim()

x = np.linspace(xmin, xmax, 100)

p = norm.pdf(x, mean, std)

plt.plot(x, p, 'b', linewidth=2, label='Normal distribution')

plt.show()


#get cumurrative probability of an interval

range_a = -0.5

range_b = 0.5

prob = norm.cdf(range_b, mean, std) - norm.cdf(range_a, mean, std)

print(f'probability of value between {range_a} and {range_b} is {prob}')