| 1 | package Text::NumericData::Stat; | ||||||||||
| 2 | |||||||||||
| 3 | # TODO: Integrate into Text::NumericData::File already. | ||||||||||
| 4 | # Also, return something for single data sets; just set non-computable measures to something invalid. | ||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use Text::NumericData::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | # This is just a placeholder because of a past build system bug. | ||||||||||
| 11 | # The one and only version for Text::NumericData is kept in | ||||||||||
| 12 | # the Text::NumericData module itself. | ||||||||||
| 13 | 2 | our $VERSION = '1'; | |||||||||
| 14 | 2 | $VERSION = eval $VERSION; | |||||||||
| 15 | |||||||||||
| 16 | # Generate a statistics file out of given input file. | ||||||||||
| 17 | # Gives and takes Text::NumericData::File objects. | ||||||||||
| 18 | # This could be a method of Text::NumericData::File, but it doesn't have to. | ||||||||||
| 19 | sub generate | ||||||||||
| 20 | { | ||||||||||
| 21 | 1 | ▶ | my $in = shift; | ||||||||
|
Subroutine
generate: called CC=15 SCAR=27.3
Pod
undocumented
| |||||||||||
| 22 | 1 | my $out = Text::NumericData::File->new($in->{config}); | |||||||||
| 23 | 1 | my @mean; # arithmetic mean | |||||||||
| 24 | my @error; # standard error, sqrt(mean sq. error / N-1) | ||||||||||
| 25 | 1 | my @min; # minimal value | |||||||||
| 26 | 1 | my @max; # maximal value | |||||||||
| 27 | |||||||||||
| 28 | 1 | $out->{title} = 'Statistics'; | |||||||||
| 29 | 1 | ▶ | $out->{title} .= ' of '.$in->{title} if defined $in->{title}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 1 | $out->{titles} = ['column', 'name', 'mean', 'stderr', 'min', 'max']; | |||||||||
| 31 | |||||||||||
| 32 | 1 | my $N = @{$in->{data}}; | |||||||||
| 33 | 1 | ▶ | return $out if $N < 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 1 | my $S = @{$in->{data}[0]}; | |||||||||
| 35 | |||||||||||
| 36 | 1 | for my $i (0 .. $S-1) | |||||||||
| 37 | { | ||||||||||
| 38 | 2 | $max[$i] = $min[$i] = $mean[$i] = $in->{data}[0][$i]; | |||||||||
| 39 | } | ||||||||||
| 40 | 1 | ▶ | for(my $j=1; $j<@{$in->{data}}; ++$j) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | { | ||||||||||
| 42 | 9 | my $d = $in->{data}[$j]; | |||||||||
| 43 | 9 | for my $i (0 .. $S-1) | |||||||||
| 44 | { | ||||||||||
| 45 | 18 | $mean[$i] += $d->[$i]; | |||||||||
| 46 | 18 | ▶ | $min[$i] = $d->[$i] if $d->[$i] < $min[$i]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 18 | ▶ | $max[$i] = $d->[$i] if $d->[$i] > $max[$i]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | } | ||||||||||
| 49 | } | ||||||||||
| 50 | 1 | for my $i (0 .. $S-1) | |||||||||
| 51 | { | ||||||||||
| 52 | 2 | $mean[$i] /= $N; | |||||||||
| 53 | } | ||||||||||
| 54 | 1 | for my $d (@{$in->{data}}) | |||||||||
| 55 | { | ||||||||||
| 56 | 10 | for my $i (0 .. $S-1) | |||||||||
| 57 | { | ||||||||||
| 58 | 20 | $error[$i] += ($d->[$i]-$mean[$i])**2; | |||||||||
| 59 | } | ||||||||||
| 60 | } | ||||||||||
| 61 | 1 | for my $i (0 .. $S-1) | |||||||||
| 62 | { | ||||||||||
| 63 | 2 | ▶ | $error[$i] = $N > 1 ? sqrt($error[$i]/($N-1)) : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | } | ||||||||||
| 65 | 1 | for my $s (1 .. $S) | |||||||||
| 66 | { | ||||||||||
| 67 | 2 | my $name = $in->{titles}[$s-1]; | |||||||||
| 68 | 2 | ▶ | if(defined $name) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | { | ||||||||||
| 70 | 0 | $out->filter_text($name); # play safe since there is no quoting in data (yet?) | |||||||||
| 71 | } | ||||||||||
| 72 | else | ||||||||||
| 73 | { | ||||||||||
| 74 | 2 | $name = "col$s"; | |||||||||
| 75 | } | ||||||||||
| 76 | 2 | $out->{data}[$s-1] = [ $s, $name, $mean[$s-1], $error[$s-1], $min[$s-1], $max[$s-1] ]; | |||||||||
| 77 | } | ||||||||||
| 78 | 1 | return $out; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 2 | 1; | |||||||||
| 82 | |||||||||||
| 83 | __END__ | ||||||||||