/usr/lib/WigeoN/PerlLib/BHStats.pm is in wigeon 20101212+dfsg1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | package BHStats;
use strict;
sub binomial_probability_sum_k_to_n {
my ($n,$k,$p) = @_;
my $sum = 0;
for (my $i = $k; $i <= $n; $i++) {
my $binProb = binomial_probability($n,$i,$p);
$sum += $binProb;
}
return ($sum);
}
sub binomial_probability_sum_k_to_0 {
my ($n,$k,$p) = @_;
my $sum = 0;
for (my $i = $k; $i >= 0; $i--) {
my $binProb = binomial_probability($n,$i,$p);
$sum += $binProb;
}
return ($sum);
}
sub binomial_probability {
my ($n_observations, $k_successes, $p_probability) = @_;
my ($n, $k, $p) = ($n_observations, $k_successes, $p_probability);
### Given B(n,p), find P(X=k)
my $binomial_prob = binomial_coefficient($n,$k) * ($p**$k) * (1-$p)**($n-$k);
return ($binomial_prob);
}
sub binomial_coefficient {
my ($n_things, $k_at_a_time) = @_;
my $number_of_k_arrangements = (factorial($n_things)) / ( factorial($k_at_a_time) * factorial($n_things-$k_at_a_time) );
return ($number_of_k_arrangements);
}
sub factorial {
my $x = shift;
$x = int($x);
my $factorial = 1;
while ($x > 1) {
$factorial *= $x;
$x--;
}
return ($factorial);
}
sub stDev {
# standard deviation calculation
my @nums = @_;
@nums = sort {$a<=>$b} @nums;
my $avg = avg(@nums);
my $count_eles = scalar(@nums);
## sum up the sqr of diff from avg
my $sum_avg_diffs_sqr = 0;
foreach my $num (@nums) {
my $diff = $num - $avg;
my $sqr = $diff**2;
$sum_avg_diffs_sqr += $sqr;
}
my $stdev = sqrt ($sum_avg_diffs_sqr/($count_eles-1));
return ($stdev);
}
sub median {
my @nums = @_;
@nums = sort {$a<=>$b} @nums;
my $count = scalar (@nums);
if ($count %2 == 0) {
## even number:
my $half = $count / 2;
return (avg ($nums[$half-1], $nums[$half]));
}
else {
## odd number. Return middle value
my $middle_index = int($count/2);
return ($nums[$middle_index]);
}
}
sub avg {
my @nums = @_;
my $total = $#nums + 1;
my $sum = 0;
foreach my $num (@nums) {
$sum += $num;
}
my $avg = $sum/$total;
return ($avg);
}
sub CorrelationCoeff {
my ($x_aref, $y_aref) = @_;
my @x = @$x_aref;
my @y = @$y_aref;
my $total = $#x + 1;
my $avg_x = avg(@x);
my $avg_y = avg(@y);
my $stdev_x = stDev(@x);
my $stdev_y = stDev(@y);
# sum part of equation
my $summation = 0;
for (my $i = 0; $i < $total; $i++) {
my $x_val = $x[$i];
my $y_val = $y[$i];
my $x_part = ($x_val - $avg_x)/$stdev_x;
my $y_part = ($y_val - $avg_y)/$stdev_y;
$summation += ($x_part * $y_part);
}
my $cor = (1/($total-1)) * $summation;
return ($cor);
}
####
sub geometric_mean {
my @entries = @_;
my $num_entries = scalar (@entries);
unless ($num_entries) {
return (undef);
}
## All entries must be > 0
my $logsum = 0;
foreach my $entry (@entries) {
unless ($entry > 0) {
return (undef);
}
$logsum += log ($entry);
}
my $geo_mean = exp ( (1/$num_entries) * $logsum);
return ($geo_mean);
}
####
sub min {
my @vals = @_;
@vals = sort {$a<=>$b} @vals;
my $min_val = shift @vals;
return ($min_val);
}
####
sub max {
my @vals = @_;
@vals = sort {$a<=>$b} @vals;
my $max_val = pop @vals;
return ($max_val);
}
####
sub sum {
my @vals = @_;
my $x = 0;
foreach my $val (@vals) {
$x += $val;
}
return ($x);
}
1; #EOM
|