line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::Stat; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
868
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
42
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
567
|
|
5
|
1
|
|
|
1
|
|
8
|
use vars qw( @ISA @EXPORT_OK ); |
|
1
|
|
|
|
|
22
|
|
|
1
|
|
|
|
|
228
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
1
|
|
|
1
|
|
1169
|
use AutoLoader qw(AUTOLOAD); |
|
1
|
|
|
|
|
2033
|
|
|
1
|
|
|
|
|
7
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @ISA = qw( Exporter ); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=pod |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Math::Stat - Perform Sample Statistics on Arrays |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSYS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=over 4 |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use Math::Stat; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my $stat = Math::Stat->new(\@data, { Autoclean => 1 }); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
$stat->median(); # returns median of data points in data |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
$stat->average(); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$stat->stddev(); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
$stat->skewness(); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
$stat->kurtosis(); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$stat->moment($N); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=back |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=cut |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub new { |
45
|
0
|
|
|
0
|
0
|
|
my($self, $data_ref) = @_; |
46
|
0
|
|
0
|
|
|
|
my $class = ref($self) || $self; |
47
|
0
|
|
|
|
|
|
my %struct; |
48
|
0
|
|
|
|
|
|
my $nobj = bless \%struct, $class; |
49
|
0
|
|
|
|
|
|
$nobj->{'Data'} = $data_ref; |
50
|
0
|
|
|
|
|
|
$nobj->{'DataCT'} = scalar(@{$data_ref}); |
|
0
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
$nobj; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub average { |
55
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
56
|
0
|
0
|
|
|
|
|
return 0 unless $self->{'DataCT'}; |
57
|
0
|
|
|
|
|
|
my $sum = 0; |
58
|
0
|
|
|
|
|
|
for my $el (@{$self->{'Data'}}) { |
|
0
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
$sum += $el; |
60
|
|
|
|
|
|
|
} |
61
|
0
|
|
|
|
|
|
return $sum/$self->{'DataCT'}; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub median { |
65
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
66
|
0
|
|
|
|
|
|
my @sorted = sort {$a <=> $b} @{$self->{'Data'}}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
67
|
0
|
0
|
|
|
|
|
return 0 unless scalar(@sorted); |
68
|
0
|
|
|
|
|
|
return $sorted[int(scalar(@sorted)/2)]; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub variance { |
72
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
73
|
0
|
|
|
|
|
|
return $self->moment(2); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub stddev { |
77
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
78
|
0
|
|
|
|
|
|
return sqrt($self->variance()); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub skewness { |
82
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
83
|
0
|
|
|
|
|
|
return $self->moment(3)/($self->moment(2)**1.5); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub kurtosis { |
87
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
88
|
0
|
|
|
|
|
|
return $self->moment(4)/($self->moment(2)**2) - 3; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub moment { |
92
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
93
|
0
|
|
|
|
|
|
my $moment = shift; |
94
|
0
|
|
|
|
|
|
my @array = @_; |
95
|
0
|
0
|
|
|
|
|
return 0 unless $self->{'DataCT'} > ($moment - 1); |
96
|
0
|
|
|
|
|
|
my $avg = $self->average(); |
97
|
0
|
|
|
|
|
|
my $l3 = 0; |
98
|
0
|
|
|
|
|
|
for my $e (@{$self->{'Data'}}) { |
|
0
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
$l3 += ($e - $avg) ** $moment; |
100
|
|
|
|
|
|
|
} |
101
|
0
|
|
|
|
|
|
return $l3/(scalar(@{$self->{'Data'}}) - 1); |
|
0
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub _min { |
105
|
0
|
|
|
0
|
|
|
my ($a, $b) = @_; |
106
|
0
|
0
|
|
|
|
|
return ($a < $b)?$a:$b; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub _max { |
110
|
0
|
|
|
0
|
|
|
my ($a, $b) = @_; |
111
|
0
|
0
|
|
|
|
|
return ($a > $b)?$a:$b; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub min { |
115
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
116
|
0
|
|
|
|
|
|
my $min; |
117
|
0
|
|
|
|
|
|
my $seen = 0; |
118
|
0
|
|
|
|
|
|
foreach my $el (@{$self->{'Data'}}) { |
|
0
|
|
|
|
|
|
|
119
|
0
|
0
|
|
|
|
|
if($seen) { |
120
|
0
|
|
|
|
|
|
$min = _min($min, $el); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
else { |
123
|
0
|
|
|
|
|
|
$min = $el; |
124
|
0
|
|
|
|
|
|
$seen = 1; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
} |
127
|
0
|
|
|
|
|
|
return $min; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub max { |
131
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
132
|
0
|
|
|
|
|
|
my $max; |
133
|
0
|
|
|
|
|
|
my $seen = 0; |
134
|
0
|
|
|
|
|
|
foreach my $el (@{$self->{'Data'}}) { |
|
0
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
|
print "$el\n"; |
136
|
0
|
0
|
|
|
|
|
if($seen) { |
137
|
0
|
|
|
|
|
|
$max = _max($max, $el); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
else { |
140
|
0
|
|
|
|
|
|
$max = $el; |
141
|
0
|
|
|
|
|
|
$seen = 1; |
142
|
|
|
|
|
|
|
} |
143
|
0
|
|
|
|
|
|
print "$max\n"; |
144
|
|
|
|
|
|
|
} |
145
|
0
|
|
|
|
|
|
return $max; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
1; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=pod |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 AUTHOR |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
George Schlossnagle |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=cut |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
__END__ |