Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # poisson.pl by Konslow. Calculates the probability of observation k given an expected value lambda.
  4. # Program uses stirling's approximation of the factorial to calculate the factorial of k.
  5. # Tested on Ubuntu 14.04.
  6. # Type ./poisson.pl lambda k where lambda and k are non-negative integers to use this program.
  7. use strict; use warnings;
  8.  
  9. my ($lambda, $k) = @ARGV;
  10.  
  11. sub ln_fucktorial( $k ){
  12. (0.5 * log(2 * 3.14159265358979))
  13. + ($k + 0.5) * log($k)
  14. - $k + 1 / (12 * $k)
  15. - 1 / (360 * ($k ** 3));
  16. }
  17.  
  18. sub k_fucktorial($k){
  19. 2.71828 ** ln_fucktorial($k);
  20. }
  21.  
  22. my $i = 0;
  23.  
  24. if ($lambda <= 0) {
  25. print "lambda must be greater than 0!\n";
  26. $i++;
  27. }
  28. if ($lambda != int($lambda)) {
  29. print "lambda must be an integer!\n";
  30. $i++;
  31. }
  32. if ($k < 0) {
  33. print "k must be equal to or greater than 0!\n";
  34. $i++;
  35. }
  36. if ($k != int($k)) {
  37. print "k must be an integer!\n";
  38. $i++;
  39. }
  40. if ($i == 0) {
  41. print "\nPoisson's probability of k = $k occuring in lambda = $lambda is ", (($lambda ** $k) / k_fucktorial($k)) * (2.71828 ** (-$lambda)), "\n\n";
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement