Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- # poisson.pl by Konslow. Calculates the probability of observation k given an expected value lambda.
- # Program uses stirling's approximation of the factorial to calculate the factorial of k.
- # Tested on Ubuntu 14.04.
- # Type ./poisson.pl lambda k where lambda and k are non-negative integers to use this program.
- use strict; use warnings;
- my ($lambda, $k) = @ARGV;
- sub ln_fucktorial( $k ){
- (0.5 * log(2 * 3.14159265358979))
- + ($k + 0.5) * log($k)
- - $k + 1 / (12 * $k)
- - 1 / (360 * ($k ** 3));
- }
- sub k_fucktorial($k){
- 2.71828 ** ln_fucktorial($k);
- }
- my $i = 0;
- if ($lambda <= 0) {
- print "lambda must be greater than 0!\n";
- $i++;
- }
- if ($lambda != int($lambda)) {
- print "lambda must be an integer!\n";
- $i++;
- }
- if ($k < 0) {
- print "k must be equal to or greater than 0!\n";
- $i++;
- }
- if ($k != int($k)) {
- print "k must be an integer!\n";
- $i++;
- }
- if ($i == 0) {
- print "\nPoisson's probability of k = $k occuring in lambda = $lambda is ", (($lambda ** $k) / k_fucktorial($k)) * (2.71828 ** (-$lambda)), "\n\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement