March 05, 2021, 06:47:34 AM

Author Topic: [bmx] Number from a poisson distribution by Warpy [ 1+ years ago ]  (Read 477 times)

Offline BlitzBot

Title : Number from a poisson distribution
Author : Warpy
Posted : 1+ years ago

Description : 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.


Code :
Code: BlitzMax
  1. Function poisson(lambda!)
  2.         If lambda>500 Return poisson(lambda/2)+poisson(lambda/2)
  3.         k=0
  4.         u!=Rnd(0,1)
  5.         fact=1
  6.         p!=Exp(-lambda)
  7.         u:-p
  8.         While u>0
  9.                 k:+1
  10.                 fact:*k
  11.                 p:*lambda/k
  12.                 u:-p
  13.         Wend
  14.         Return k
  15. End Function
  16.  
  17.  
  18. '  example - draws a graph of the proability density function
  19. Local counts[20]
  20. For i=0 To 1000
  21.         p=poisson(5)
  22.         If p<20
  23.                 counts[p]:+1
  24.         EndIf
  25. Next
  26.  
  27. Graphics 800,800,0
  28. For i=0 To 19
  29.         DrawRect i*40,0,40,counts[i]
  30.         Print counts[i]
  31. Next
  32. Flip
  33. WaitKey()


Comments :


Warpy(Posted 1+ years ago)

 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


Floyd(Posted 1+ years ago)

 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.


Warpy(Posted 1+ years ago)

 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.


plash(Posted 1+ years ago)

 <div class="quote"> 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. </div>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... ;)


Warpy(Posted 1+ years ago)

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


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal