Code archives/Algorithms/Number from a poisson distribution

This code has been declared by its author to be Public Domain code.

Download source code

Number from a poisson distribution by Warpy2008
quoth wikipedia,
"In probability theory and statistics, the Poisson distribution is a discrete probability distribution that expresses the probability of a number of events occurring in a fixed period of time if these events occur with a known average rate and independently of the time since the last event. The Poisson distribution can also be used for the number of events in other specified intervals such as distance, area or volume.
....
The parameter lambda is not only the mean number of occurrences, but also its variance. "


This is a little function that returns a random number from a poisson distribution with mean value that you provide. You might want to use this for things such as randomly emitting objects, population growth, or anything that either might happen or might not happen at any instant in time.
Function poisson(lambda!)
	If lambda>500 Return poisson(lambda/2)+poisson(lambda/2)
	k=0
	u!=Rnd(0,1)
	fact=1
	p!=Exp(-lambda)
	u:-p
	While u>0
		k:+1
		fact:*k
		p:*lambda/k
		u:-p
	Wend
	Return k
End Function


'  example - draws a graph of the proability density function
Local counts[20]
For i=0 To 1000
	p=poisson(5)
	If p<20
		counts[p]:+1
	EndIf
Next

Graphics 800,800,0
For i=0 To 19
	DrawRect i*40,0,40,counts[i]
	Print counts[i]
Next
Flip
WaitKey()

Comments

Warpy2008
edit: added a recursion thing at the start of the function to cope with precision errors: exp(-lambda) gets rounded to zero somewhere around lambda=750


Floyd2008
This is getting a little esoteric for the Blitz forums.

For the "very big lambda" case you might instead implement a function to return a random number from a Normal distribution, with given mean and variance. That's a Gaussian distribution for the physicists in the crowd.

Then a Normal distribution with mean and variance both equal to a large lambda should a very good approximation of Poisson. If you do implement this you might want to test that assertion. I think it's true, but it has been rather a long time since I studied this stuff.


Warpy2008
Good point, I'd forgotten about that. And I think poisson approximates a binomial distribution, if I can remember what little stats I did at uni.
I think I'll write up a series of these, each calling the next one up when the numbers get too big.


plash2008
This is a little function that returns a random number from a poisson distribution with mean value that you provide. You might want to use this for things such as randomly emitting objects, population growth, or anything that either might happen or might not happen at any instant in time.
But it will still have to rely on a random number in the first place, as it will output the same values for the same mean each time.

We have to seed our random function, and even if we seed it with another random number to a trillion it would still have a pattern.

If only random was truly random... ;)


Warpy2008
... what?

Are you saying I need to say pseudorandom? Let it go, man.


Code Archives Forum