I am trying to plot a Poisson distribution with lambda = 100, but when the lambda value is high, part of the distribution is getting cut off and I cant figure out why.
set xrange [ 0: 150]
lambda = 100
poisson(x) = lambda**x / int(x)! * exp(-lambda)
plot poisson(x) with boxes
1st thing to fix, with your code above, did you notice that even lambda = 10 does not produce the correct results. This problem is with gnuplot's default sampling for functions. It is 100. you have 151 points (0 through 150, integer values) you want to plot. So although you use the int() function for the factorial, lambda**x is not giving you the correct results. Start by setting the sampling to exactly the number of points you want:
set sample 151
There seems to be a bug in gnuplot's division. If you simply print poisson(88)
you get NaN. Yet, you can print 100**88
and get 1e+176
and you can print 88!
and get 1.85482642257398e+134
. But if you print 1e+176 / 1.85482642257398e+134
you get inf.0
. This should not happen because there is no overflow here.
I got it to work properly by taking log(lambda**x) and log(int(x)!) and bringing everything into the exp() function using properties of logarithms. This eliminates crazy big numbers in the division and multiplication. Try:
set xrange [ 0: 150]
set sample 151
lambda = 100
poisson(x) = exp(log(lambda**x) - log(int(x)!) - lambda)
plot poisson(x) with boxes