Next | Welcome to my ~/bin | 38 |
The code itself is quite simple:
There's a loop to read the input and gather basic statistics:
while (<>) { chomp; my ($x, $y) = split; ($x, $y) = ($., $x) if not defined $y; $X += $x; $Y += $y; $X2 += $x*$x; $Y2 += $y*$y; $XY += $x*$y; $N += 1; }
(If there is only one column, then a column with 1, 2, 3... is inferred)
Then these statistics are used to generate the values of interest:
my $Xm = $X / $N;
my $Ym = $Y / $N;
my $Xv = $X2 / $N - $Xm*$Xm;
my $Yv = $Y2 / $N - $Ym*$Ym;
my $Xs = sqrt($Xv);
my $Ys = sqrt($Yv);
my $m = $X*$X == $N * $X2 ? "Inf" : ($X*$Y/$N - $XY) / ($X*$X/$N - $X2);
my $b = $m eq "Inf" ? "None" : $Ym - $m * $Xm;
my $r = $Ys ? $m * $Xs/$Ys : "Inf";
Note the special-casing in the division-by-zero places
Next | Menu | Copyright © 2005 M. J. Dominus |