1 package Math::GSL::Statistics::Test;
2 use base q{Test::Class};
3 use Test::More tests => 28;
4 use Math::GSL::Test qw/:all/;
5 use Math::GSL::Sort qw/:all/;
6 use Math::GSL::Statistics qw/:all/;
9 sub make_fixture : Test(setup) {
25 sub teardown : Test(teardown) {
28 sub GSL_STATS_MEAN : Tests {
29 ok_similar( gsl_stats_mean([2 .. 5], 1, 4), 3.5 );
32 sub GSL_STATS_VARIANCE : Tests {
33 ok_similar( gsl_stats_variance([2 .. 5], 1, 4), 5/3);
36 sub GSL_STATS_SD : Tests {
38 ok_similar( gsl_stats_sd($self->{data}, 1, 20) , 3.79750610685209);
41 sub GSL_STATS_ABSDEV : Tests {
43 my $x = gsl_stats_absdev($self->{data},1, 20);
47 sub GSL_STATS_SKEW : Tests {
49 my $x = gsl_stats_skew($self->{data}, 1, 20);
50 ok_similar($x, -0.909355923168064);
53 sub GSL_STATS_KURTOSIS : Tests {
55 my $x = gsl_stats_kurtosis($self->{data}, 1, 20);
56 ok_similar_relative($x, -0.233692524908094);
59 sub GSL_STATS_COVARIANCE : Tests {
61 my $x = gsl_stats_covariance($self->{data}, 1, $self->{datab}, 1, 20);
62 ok_similar_relative($x, 14.5263157894737);
65 sub GSL_STATS_CORRELATION : Tests {
67 my $x = gsl_stats_correlation($self->{data}, 1, $self->{datab}, 1, 20);
68 ok_similar_relative($x, 0.793090350710101);
71 sub GSL_STATS_PVARIANCE : Tests {
73 my $x = gsl_stats_pvariance($self->{data}, 1, 20, $self->{datab}, 1, 20);
74 ok_similar_relative($x, 18.8421052631579);
77 sub GSL_STATS_TTEST : Tests {
79 my $x = gsl_stats_ttest($self->{data}, 1, 20, $self->{datab}, 1, 20);
80 ok_similar_relative($x, -1.45701922702927);
83 sub GSL_STATS_MAX : Tests {
85 my $x = gsl_stats_max($self->{data}, 1, 20);
86 ok_similar_relative($x, 22);
89 sub GSL_STATS_MIN : Tests {
91 my $x = gsl_stats_min($self->{data}, 1, 20);
92 ok_similar_relative($x, 8);
95 sub GSL_STATS_MINMAX : Tests {
97 my ($min, $max) = gsl_stats_minmax($self->{data}, 1, 20);
98 ok_similar_relative($max, 22);
99 ok_similar_relative($min, 8);
102 sub GSL_STATS_MAX_INDEX : Tests {
104 my $x = gsl_stats_max_index($self->{data}, 1, 20);
105 ok_similar_relative($x, 9);
108 sub GSL_STATS_MIN_INDEX : Tests {
110 my $x = gsl_stats_min_index($self->{data}, 1, 20);
111 ok_similar_relative($x, 12);
114 sub GSL_STATS_MINMAX_INDEX : Tests {
116 my ($min, $max) = gsl_stats_minmax_index($self->{data}, 1, 20);
117 ok_similar_relative($min, 12);
118 ok_similar_relative($max, 9);
121 sub GSL_STATS_MEDIAN_FROM_SORTED_DATA_EVEN : Tests {
123 my $sorted = gsl_sort($self->{data}, 1, 20);
124 my $x = gsl_stats_median_from_sorted_data($sorted, 1, 20);
125 ok_similar_relative($x, 18);
128 sub GSL_STATS_MEDIAN_FROM_SORTED_DATA_ODD : Tests {
130 my $sorted = gsl_sort($self->{data}, 1, 20);
131 my $x = gsl_stats_median_from_sorted_data($sorted, 1, 19);
132 ok_similar_relative($x, 18);
135 sub GSL_STATS_QUANTILE_FROM_SORTED_DATA_0 : Tests {
137 my $sorted = gsl_sort($self->{data}, 1, 20);
138 my $x = gsl_stats_quantile_from_sorted_data($sorted, 1, 20, 0);
139 ok_similar_relative($x, 8);
142 sub GSL_STATS_QUANTILE_FROM_SORTED_DATA_100 : Tests {
144 my $sorted = gsl_sort($self->{data}, 1, 20);
145 my $x = gsl_stats_quantile_from_sorted_data($sorted, 1, 20, 1);
146 ok_similar_relative($x, 22);
149 sub GSL_STATS_QUANTILE_FROM_SORTED_DATA_50_EVEN : Tests {
151 my $sorted = gsl_sort($self->{data}, 1, 20);
152 my $x = gsl_stats_quantile_from_sorted_data($sorted, 1, 20, 0.5);
153 ok_similar_relative($x, 18);
156 sub GSL_STATS_QUANTILE_FROM_SORTED_DATA_50_ODD : Tests {
158 my $sorted = gsl_sort($self->{data}, 1, 20);
159 my $x = gsl_stats_quantile_from_sorted_data($sorted, 1, 20, 0.5);
160 ok_similar_relative($x, 18);
163 sub GSL_STATS_LAG1_AUTOCORRELATION : Tests {
167 $numacc3->[0] = 1000000.2 ;
168 for ($i = 1 ; $i < 1000 ; $i += 2) {
169 $numacc3->[$i] = 1000000.1 ;
170 $numacc3->[$i+1] = 1000000.3 ;
172 my $mean = gsl_stats_mean ($numacc3, 1, $nacc3);
173 my $sd = gsl_stats_sd ($numacc3, 1, $nacc3);
174 my $lag1 = gsl_stats_lag1_autocorrelation ($numacc3, 1, $nacc3);
176 my $expected_mean = 1000000.2;
177 my $expected_sd = 0.1;
178 my $expected_lag1 = -0.999;
180 ok_similar_relative($mean, $expected_mean);
181 ok_similar_relative($sd, $expected_sd);
182 ok_similar_relative($lag1, $expected_lag1);
185 sub GSL_STATS_WMEAN : Tests {
186 my $w = [4, 4, 2, 2];
187 my $data = [2, 3, 4, 5];
188 my $mean = gsl_stats_wmean($w, 1, $data, 1, 4);
189 ok_similar ($mean, 19/6);
192 Test::Class->runtests;