The Bayesian update contains a multiplication of the Prior and the Likelihood. The area under the bell curve of a gaussian sums up to 1. We know that this only holds if we integrate from -Inf to +Inf.
This is not feasible on a computer, so I tried to somehow approximate the normalization step after a multiplication of two gaussians.
First, I create one gaussian using the quasi-continuous space between -50.0 and +50.0:
from scipy.stats import norm import numpy as np x = np.linspace(-50.0, 50.0, 10000) y1 = norm.pdf(x)
When I now sum over all values of y1, I get 99.99 - as the sum does not take into account that I have a point each 1/100 of a full step.
> print y1.sum() > 99.99
When I'd do a Riemann-like integration, like multiplying each x distance with the y value, then it will some up to appx. one:
> print np.multiply(y1, 0.01).sum() > 0.9999
Now I create another gaussian, slightly shifted:
y2 = np.norm(x+2)
When I plot these two curves, I see the desired and well-known picture:
import matplotlib.pyplot as plt %matplotlib inline plt.ylim(0.0,0.5) plt.xlim(-6.0,6.0) plt.plot(x, y1) plt.plot(x, y2)
So, now let's assume that I multiply these two gaussians:
y3 = (y1 * y2)
When I would add this y3 to the plot, of course the values would be too small as the y3 are not normalized yet - but, I also cannot normalize the values to sum up to 1 - as seen above, my original PDF-values of y1 and y2 have either summed up to 99.99 due to the sampling. So, I do an approximation:
y3 = ((y1.sum() + y2.sum()) / 2.0) * (y3.astype(float) / y3.astype(float).sum())
which is kind of a scaled normalization. When I now plot the y3 along with the other two gaussians, I get an image that does not look to be totally wrong:
plt.ylim(0.0,.7) plt.xlim(-8.0,6.0) plt.plot(x, y1) plt.plot(x, y2) plt.plot(x, y3, '--')
Now, my questions are:
- Is there any other (more pythonic way) to multiply and normalize two gaussians? Or more generally, two unknown PDFs?
- Is the approach that I have implemented methodically o.k.? If not, what are the weaknesses?