File Coverage

blib/lib/Text/NumericData/Stat.pm
Criterion Covered Total %
statement 35 36 97.2
branch 3 6 50.0
condition n/a
subroutine 3 3 100.0
pod 0 1 0.0
total 41 46 89.1


line stmt bran cond sub pod time code
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 soemthing invalid.
5              
6 2     2   1538 use Text::NumericData::File;
  2         8  
  2         83  
7              
8 2     2   107 use strict;
  2         8  
  2         993  
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             our $VERSION = '1';
14             $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     1 0 2 my $in = shift;
22 1         4 my $out = Text::NumericData::File->new($in->{config});
23 1         3 my @mean; # arithmetic mean
24             my @error; # standard error, sqrt(mean sq. error / N-1)
25              
26 1         2 my $N = @{$in->{data}};
  1         3  
27 1 50       4 return $out if $N < 2;
28 1         2 my $S = @{$in->{data}[0]};
  1         3  
29              
30 1         2 for my $d (@{$in->{data}})
  1         2  
31             {
32 10         16 for my $i (0 .. $S-1)
33             {
34 20         33 $mean[$i] += $d->[$i];
35             }
36             }
37 1         3 for my $i (0 .. $S-1)
38             {
39 2         5 $mean[$i] /= $N;
40             }
41 1         2 for my $d (@{$in->{data}})
  1         3  
42             {
43 10         16 for my $i (0 .. $S-1)
44             {
45 20         76 $error[$i] += ($d->[$i]-$mean[$i])**2;
46             }
47             }
48 1         3 for my $i (0 .. $S-1)
49             {
50 2         5 $error[$i] = sqrt($error[$i]/($N-1));
51             }
52 1         3 $out->{title} = 'Statistics';
53 1 50       3 $out->{title} .= ' of '.$in->{title} if defined $in->{title};
54 1         3 $out->{titles} = ['column', 'name', 'mean', 'stderr'];
55 1         4 for my $s (1 .. $S)
56             {
57 2         5 my $name = $in->{titles}[$s-1];
58 2 50       8 if(defined $name)
59             {
60 0         0 $out->filter_text($name); # play safe since there is no quoting in data (yet?)
61             }
62             else
63             {
64 2         4 $name = "col$s";
65             }
66 2         8 $out->{data}[$s-1] = [ $s, $name, $mean[$s-1], $error[$s-1] ];
67             }
68 1         4 return $out;
69             }
70              
71             1;
72              
73             __END__