Example normalizing and analyzing continuous probability

Initialize python


In [50]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import quad

Define probability function


In [51]:
def phif(T):
    return np.exp(-(T-20.)**2/50.)

Plot out function


In [52]:
T = np.linspace(-20,50,1000)
phi = phif(T)
plt.plot(T,phi)
plt.xlabel('Temperature (C)')
plt.ylabel('Relative probability (1/C)')


Out[52]:
Text(0, 0.5, 'Relative probability (1/C)')

Bell shaped curve!

Normalize by numerical integration


In [53]:
N,err = quad(phif,-20,50)

def phif_norm(T):
    return phif(T)/N

plt.plot(T,phif_norm(T))
plt.xlabel('Temperature (C)')
plt.ylabel('Relative probability (1/C)')


Out[53]:
Text(0, 0.5, 'Relative probability (1/C)')

Most probable value of $T$????

Expectation value of $T$:


In [54]:
def Tphif(T):
    return T*phif_norm(T)
Tbar,err = quad(Tphif,-20,50)
print("Expectation value of T = {:4.1f} deg C".format(Tbar))

plt.plot(T,phif_norm(T))
plt.xlabel('Temperature (C)')
plt.ylabel('Relative probability (1/C)')
plt.plot([Tbar,Tbar],[0,0.09],ls='-')


Expectation value of T = 20.0 deg C
Out[54]:
[<matplotlib.lines.Line2D at 0x151e1ef3d0>]

Variance and standard deviation


In [55]:
def T2phif(T):
    return T*T*phif_norm(T)
T2bar,err = quad(T2phif,-20,50)
print("Expectation value of T**2 = {:4.1f} deg C**2".format(T2bar))

Trms=np.sqrt(T2bar)
print("Room mean square T ={:4.1f} deg C".format(Trms))

variance = T2bar-Tbar*Tbar
print("Variance ={:4.1f} deg C**2".format(variance))

stddev = np.sqrt(variance)
print("Standard deviation ={:4.1f} deg C".format(stddev))


Expectation value of T**2 = 425.0 deg C**2
Room mean square T =20.6 deg C
Variance =25.0 deg C**2
Standard deviation = 5.0 deg C

Probability for $T$ to be within +/- 1 $\sigma$


In [56]:
prob,err = quad(phif_norm,Tbar-stddev,Tbar+stddev)
print("Probability = {:4.3f}".format(prob))


Probability = 0.683

Unitless!!!


In [ ]: