SciPy Library

s used for running scientific and mathematical computations. You are specifically going to use a part of the library that implements Gaussian distributions.

Demo: Probability Density Function

from scipy.stats import norm
import numpy as np

# our solution to calculate the probability density function
def gaussian_density(x, mu, sigma):
    return (1/np.sqrt(2*np.pi*np.power(sigma, 2.))) * np.exp(-np.power(x - mu, 2.) / (2 * np.power(sigma, 2.)))

print("Probability density function our solution: mu = 50, sigma = 10, x = 50")
print(gaussian_density(50, 50, 10))
print("\nProbability density function SciPy: mu = 50, sigma = 10, x = 50")
print(norm(loc = 50, scale = 10).pdf(50))

Calculating Probability

Here are a few more examples of the cdf method. The code cell below prints out the probability that the temperature is between:

  • -infinity and 25

  • -infinity and 75

  • -infinity and 125

  • -infinity and +infinity

print(norm(loc = 50, scale = 10).cdf(25))
print(norm(loc = 50, scale = 10).cdf(75))
print('%.20f' % norm(loc = 50, scale = 10).cdf(125)) # '%.20f' prints out 20 decimal places
print(norm(loc = 50, scale = 10).cdf(float('inf')))
0.00620966532578
0.993790334674
0.99999999999996813660
1.0

Last updated