I think I have a problem with Numpy and small numbers.
Can you help me finding a solution for the following problem:
import numpy as np
def gaussian(xx, mu=0, sigma=1):
return 1./(np.sqrt(2*np.pi)*sigma)*np.exp(-(mu-xx)**2/(2*sigma**2))
factors = (1., 0.1, 0.01, 0.001, 0.0001)
for factor in factors:
xx = np.linspace(5000, 5200, 1000)
yy = 1.-(factor*xx*(1.+gaussian(xx, 5100)))
step = xx[1] - xx[0]
print np.sum((1.-yy/(1.-factor*xx))*step)
This code should evaluate to -1 for all of the different factors.
But the output is :
1.0 -1.00019611689
0.1 -1.00196463662
0.01 -1.0200000008
0.001 -1.24390245353
0.0001 1.04081641153
So the problem is, that the smaller the factor is, the more I get into trouble because of what I think has to do with the precision of Numpy/Python.
What to do to evaluate the equation even for the small factors?
Thanks a lot for your help in advance.
yy-part to1-ax(1+g(x))and the then part inside the sum to1-yy/(1-ax) = 1-((1-ax(1+g(x))/(1-ax)) = 1-(1+g(x)) = -g(x). Sinceg(x)is a gaussian which has its peak at5100and a stddev of1and we are integrating over a range of200(from5000to5200) theg(x)will be 1 and thus the result will be-1. I think the mathematics is not the problem but the way Numpy/Python deals with the small numbers.axg(x) / (1 - ax), not to-g(x).yyas(1 - factor*xx)*(1+gaussian(xx, 5100))instead?