File: descr.t

package info (click to toggle)
libstatistics-descriptive-perl 2.6-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 76 kB
  • ctags: 26
  • sloc: perl: 338; makefile: 49
file content (143 lines) | stat: -rw-r--r-- 4,249 bytes parent folder | download | duplicates (4)
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
require 'Descriptive.pm';
use Benchmark;

print "1..14\n";

$testct = 1;

# test #1

$stat = Statistics::Descriptive::Full->new();
@result = $stat->least_squares_fit();
print ( (@result? 'not ': '' ) . 'ok ' . $testct++ . "\n" );

# test #2
# data are y = 2*x - 1

$stat->add_data( 1, 3, 5, 7 );
@result = $stat->least_squares_fit();
$ok = ( $result[0] == -1 ) && ( $result[1] == 2 );
print ( ($ok? '': 'not ' ) . 'ok ' . $testct++ . "\n" );

# test #3
# test error condition on harmonic mean : one element zero
$stat = Statistics::Descriptive::Full->new();
$stat->add_data( 1.1, 2.9, 4.9, 0.0 );
$result = $stat->harmonic_mean();
$ok = ! defined( $result );
print ( ($ok? '': 'not ' ) . 'ok ' . $testct++ . "\n" );

# test #4
# test error condition on harmonic mean : sum of elements zero
$stat = Statistics::Descriptive::Full->new();
$stat->add_data( 1.0, -1.0 );
$result = $stat->harmonic_mean();
$ok = ! defined( $result );
print ( ($ok? '': 'not ' ) . 'ok ' . $testct++ . "\n" );

# test #5
# test error condition on harmonic mean : sum of elements near zero
$stat = Statistics::Descriptive::Full->new();
$savetol = $Statistics::Descriptive::Tolerance;
$Statistics::Descriptive::Tolerance = 0.1;
$stat->add_data( 1.01, -1.0 );
$result = $stat->harmonic_mean();
$ok = ! defined( $result );
print ( ($ok? '': 'not ' ) . 'ok ' . $testct++ . "\n" );

$Statistics::Descriptive::Tolerance = $savetol;

# test #6
# test normal function of harmonic mean
$stat = Statistics::Descriptive::Full->new();
$stat->add_data( 1,2,3 );
$result = $stat->harmonic_mean();
$ok = defined( $result ) && abs( $result - 1.6363 ) < 0.001;
print ( ($ok? '': 'not ' ) . 'ok ' . $testct++ . "\n" );

# test #7
# test stringification of hash keys in frequency distribution
$stat = Statistics::Descriptive::Full->new();
$stat->add_data(0.1,
                0.15,
                0.16,
               1/3);
%f = $stat->frequency_distribution(2);

$ok = ($f{0.216666666666667} == 3) &&
      ($f{0.333333333333333} == 1);
print ( ($ok? '': 'not ' ) . 'ok ' . $testct++ . "\n" );

# test #8
##Test memorization of last frequency distribution
%g = $stat->frequency_distribution();
$ok = 1;
foreach $key (keys %f) {
  $ok = 0 if $f{$key} != $g{$key};
}
print ( ($ok? '': 'not ' ) . 'ok ' . $testct++ . "\n" );

# test #9
# test the frequency distribution with specified bins
$stat = Statistics::Descriptive::Full->new();
@freq_bins=(20,40,60,80,100);
$stat->add_data(23.92,
                32.30,
                15.27,
                39.89,
                8.96,
                40.71,
                16.20,
                34.61,
                27.98,
                74.40);
%f = $stat->frequency_distribution(\@freq_bins);

$ok = ($f{20} == 3) &&
      ($f{40} == 5) &&
      ($f{60} == 1) &&
      ($f{80} == 1) &&
      ($f{100} == 0);
print ( ($ok? '': 'not ' ) . 'ok ' . $testct++ . "\n" );

# test #10 and #11
# Test the percentile function and caching
$stat = Statistics::Descriptive::Full->new();
$stat->add_data(-5,-2,4,7,7,18);
##Check algorithm
$ok =  ( $stat->percentile(50) == 4 );
print ( ($ok? '': 'not ' ) . 'ok ' . $testct++ . "\n" );
$ok =  ( $stat->percentile(25) == -2 );
print ( ($ok? '': 'not ' ) . 'ok ' . $testct++ . "\n" );

# tests #12 and #13
# Check correct parsing of method parameters
$stat = Statistics::Descriptive::Full->new();
$stat->add_data(1,2,3,4,5,6,7,8,9,10);
$ok = ($stat->trimmed_mean(0.1,0.1) == $stat->trimmed_mean(0.1));
print ( ($ok? '': 'not ' ) . 'ok ' . $testct++ . "\n" );

$ok = ($stat->trimmed_mean(0.1,0) == 6);
print ( ($ok? '': 'not ' ) . 'ok ' . $testct++ . "\n" );

# tests #14
# Make sure that trimmed_mean caching works but checking execution times
# This test may fail on very fast machines but I'm not sure how to get better
# timing without requiring extra modules to be added.

$stat = Statistics::Descriptive::Full->new();
##Make this a really big array so that it takes some time to execute!
$stat->add_data((1,2,3,4,5,6,7,8,9,10,11,12,13) x 10000);

my ($t0,$t1,$td);
my @t = ();
foreach (0..1) {
  $t0 = new Benchmark;
  $stat->trimmed_mean(0.1,0.1);
  $t1 = new Benchmark;
  $td = timediff($t1,$t0);
  push @t, $td->cpu_p();
}

$ok = $t[1] < $t[0];
print ( ($ok? '': 'not ' ) . 'ok ' . $testct++ . "\n" );