File: Lite.pm

package info (click to toggle)
libstatistics-lite-perl 3.62-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 116 kB
  • ctags: 17
  • sloc: perl: 288; makefile: 2
file content (332 lines) | stat: -rwxr-xr-x 8,348 bytes parent folder | download
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package Statistics::Lite;
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
require Exporter;

$VERSION = '3.62';
@ISA = qw(Exporter);
@EXPORT = ();
@EXPORT_OK = qw(min max range sum count mean median mode variance stddev variancep stddevp statshash statsinfo frequencies);
%EXPORT_TAGS=
(
	all   => [ @EXPORT_OK ],
	funcs => [qw<min max range sum count mean median mode variance stddev variancep stddevp>],
	stats => [qw<statshash statsinfo>],
);

sub definedvals
{
	return grep{defined}@_;
}

sub count
{
	return scalar definedvals @_;
}

sub min
{
	my @data = definedvals @_;
	return unless @data;
	return $data[0] unless @data > 1;
	my $min= shift @data;
	foreach(@data) { $min= $_ if $_ < $min; }
	return $min;
}

sub max
{
	my @data = definedvals @_;
	return unless @data;
	return $data[0] unless @data > 1;
	my $max= shift @data;
	foreach(@data) { $max= $_ if $_ > $max; }
	return $max;
}

sub range
{
	my @data = definedvals @_;
	return unless @data;
	return 0 unless @data > 1;
	return abs($data[1]-$data[0]) unless @data > 2;
	my $min= shift @data; my $max= $min;
	foreach(@data) { $min= $_ if $_ < $min; $max= $_ if $_ > $max; }
	return $max - $min;
}

sub sum
{
	my @data = definedvals @_;
	return unless @data;
	return $data[0] unless @data > 1;
	my $sum;
	foreach(@data) { $sum+= $_; }
	return $sum;
}

sub mean
{
	my @data = definedvals @_;
	return unless @data;
	return $data[0] unless @data > 1;
	return sum(@data)/scalar(@data);
}

sub median
{
	my @data = definedvals @_;
	return unless @data;
	return $data[0] unless @data > 1;
	@data= sort{$a<=>$b}@data;
	return $data[$#data/2] if @data&1;
	my $mid= @data/2;
	return ($data[$mid-1]+$data[$mid])/2;
}

sub mode
{
	my @data = definedvals @_;
	return unless @data;
	return $data[0] unless @data > 1;
	my %count;
	foreach(@data) { $count{$_}++; }
	my $maxhits= max(values %count);
	foreach(keys %count) { delete $count{$_} unless $count{$_} == $maxhits; }
	return mean(keys %count);
}

sub variance
{
	my @data = definedvals @_;
	return unless @data;
	return 0 unless @data > 1;
	my $mean= mean @data;
	return (sum map { ($_ - $mean)**2 } @data) / $#data;
}

sub variancep
{
	my @data = definedvals @_;
	return unless @data;
	return 0 unless @data > 1;
	my $mean= mean @data;
	return (sum map { ($_ - $mean)**2 } @data) / ( $#data +1 );
}

sub stddev
{
	my @data = definedvals @_;
	return unless @data;
	return 0 unless @data > 1;
	return sqrt variance @data;
}

sub stddevp
{
	my @data = definedvals @_;
	return unless @data;
	return 0 unless @data > 1;
	return sqrt variancep @data;
}

sub statshash
{
	my @data = definedvals @_;
	return unless @data;
	return
	(
		count     => 1,
		min       => $data[0],
		max       => $data[0],
		range     => 0,
		sum       => $data[0],
		mean      => $data[0],
		median    => $data[0],
		mode      => $data[0],
		variance  => 0,
		stddev    => 0,
		variancep => 0,
		stddevp   => 0
	) unless @data > 1;
	my $count= scalar(@data);
	@data= sort{$a<=>$b}@data;
	my $median;
	if(@data&1) { $median= $data[$#data/2]; }
	else { my $mid= @data/2; $median= ($data[$mid-1]+$data[$mid])/2; }
	my $sum= 0;
	my %count;
	foreach(@data) { $sum+= $_; $count{$_}++; }
	my $mean= $sum/$count;
	my $maxhits= max(values %count);
	foreach(keys %count)
	{ delete $count{$_} unless $count{$_} == $maxhits; }
	return
	(
		count     => $count,
		min       => $data[0],
		max       => $data[-1],
		range     => ($data[-1] - $data[0]),
		sum       => $sum,
		mean      => $mean,
		median    => $median,
		mode      => mean(keys %count),
		variance  => variance(@data),
		stddev    => stddev(@data),
		variancep => variancep(@data),
		stddevp   => stddevp(@data)
	);
}

sub statsinfo
{
	my %stats= statshash(@_);
	return <<".";
min       = $stats{min}
max       = $stats{max}
range     = $stats{range}
sum       = $stats{sum}
count     = $stats{count}
mean      = $stats{mean}
median    = $stats{median}
mode      = $stats{mode}
variance  = $stats{variance}
stddev    = $stats{stddev}
variancep = $stats{variancep}
stddevp   = $stats{stddevp}
.
}

sub frequencies
{
	my @data = definedvals @_;
	return unless @data;
	return ( $data[0], 1 ) unless @data > 1;
	my %count;
	foreach(@data) { $count{$_}++; }
	return %count;
}

1;
__END__

=head1 NAME

Statistics::Lite - Small stats stuff.

=head1 SYNOPSIS

	use Statistics::Lite qw(:all);

	$min= min @data;
	$mean= mean @data;

	%data= statshash @data;
	print "sum= $data{sum} stddev= $data{stddev}\n";

	print statsinfo(@data);

=head1 DESCRIPTION

This module is a lightweight, functional alternative to larger, more complete,
object-oriented statistics packages.
As such, it is likely to be better suited, in general, to smaller data sets.

This is also a module for dilettantes.

When you just want something to give some very basic, high-school-level statistical values,
without having to set up and populate an object first, this module may be useful.

=head2 NOTE

This module implements standard deviation and variance calculated by both the unbiased and biased estimators.

=head1 FUNCTIONS

=over 4

=item C<min(@data)>, C<max(@data)>, C<range(@data)>, C<sum(@data)>, C<count(@data)>

Returns the minimum value, maximum value, range (max - min),
sum, or count of values in C<@data>. Undefined values are ignored.

C<count(@data)> simply returns C<scalar(@data)>.

B<Please note> that this module does B<not> ignore undefined values in your
data; instead, those are B<treated as zero>.

=item C<mean(@data)>, C<median(@data)>, C<mode(@data)>

Calculates the mean, median, or mode average of the values in C<@data>. Undefined values are ignored.
(In the event of ties in the mode average, their mean is returned.)

=item C<variance(@data)>, C<stddev(@data)>

Returns the standard deviation or variance of C<@data> for a sample (same as Excel's STDEV).
This is also called the Unbiased Sample Variance and involves dividing the
sample's squared deviations by N-1 (the sample count minus 1).
The standard deviation is just the square root of the variance.

=item C<variancep(@data)>, C<stddevp(@data)>

Returns the standard deviation or variance of C<@data> for the population (same as Excel's STDEVP).
This involves dividing the squared deviations of the population by N (the population size).
The standard deviation is just the square root of the variance.

=item C<statshash(@data)>

Returns a hash whose keys are the names of all the functions listed above,
with the corresponding values, calculated for the data set.

=item C<statsinfo(@data)>

Returns a string describing the data set, using the values detailed above.

=item C<frequencies(@data)>

Returns a hash. The keys are the distinct values in the data set,
and the values are the number of times that value occurred in the data set.

=back

=head2 Import Tags

The C<:all> import tag imports all exportable functions from this module into
the current namespace (use with caution). More specifically, these functions
are the following: C<min>, C<max>, C<range>, C<sum>, C<count>, C<mean>,
C<median>, C<mode>, C<variance>, C<stddev>, C<variancep>, C<stddevp>,
C<statshash>, C<statsinfo>, and C<frequencies>.

To import the statistical functions, use the import tag C<:funcs>.  This
imports all of the above-mentioned functions, except for C<statshash>,
C<statsinfo>, and C<frequencies>.

Use C<:stats> to import C<statshash(@data)> and C<statsinfo(@data)>.

=head1 REPOSITORY

L<https://github.com/brianary/Statistics-Lite>

=head1 AUTHOR

Brian Lalonde E<lt>brian@webcoder.infoE<gt>,
C<stddev(@data)>, C<stddevp(@data)>, C<variance(@data)>, C<variancep(@data)>,
additional motivation by Nathan Haigh, with kind support from Alexander Zangerl.

The project lives at https://github.com/brianary/Statistics-Lite

=head1 COPYRIGHT AND LICENSE

Copyright 2000 Brian Lalonde E<lt>brian@webcoder.infoE<gt>, Nathan Haigh,
Alexander Zangerl, and Ton Voon.

This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

=head1 SEE ALSO

perl(1).

=cut