]> mj.ucw.cz Git - netgrind.git/commitdiff
Added a utility for linear fitting.
authorMartin Mares <mj@ucw.cz>
Sat, 28 Jun 2003 13:58:45 +0000 (13:58 +0000)
committerMartin Mares <mj@ucw.cz>
Sat, 28 Jun 2003 13:58:45 +0000 (13:58 +0000)
post/linear-fit [new file with mode: 0755]

diff --git a/post/linear-fit b/post/linear-fit
new file mode 100755 (executable)
index 0000000..9535226
--- /dev/null
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+# Simple linear fitting using the Least Squares method
+# (c) 2003 Martin Mares <mj@ucw.cz>, GPL'ed
+
+use strict;
+use warnings;
+
+my $n = 0;
+my $sx = 0;
+my $sy = 0;
+my $sxy = 0;
+my $sxx = 0;
+my $syy = 0;
+while (<STDIN>) {
+       chomp;
+       my ($x, $y) = split /\s+/;
+       $n++;
+       $sx += $x;
+       $sy += $y;
+       $sxy += $x * $y;
+       $sxx += $x * $x;
+       $syy += $y * $y;
+}
+$n || die "No data found";
+
+my $b = ($n*$sxy - $sx*$sy) / ($n*$sxx - $sx*$sx);
+my $a = ($sy - $sx*$b) / $n;
+my $d = $syy - 2*$sy*$a - 2*$sxy*$b + $a*$a*$n + 2*$sx*$a*$b + $sxx*$b*$b;
+my $var = $d / $n;
+my $stdd = sqrt($var);
+
+printf "Number of records:     %d\n", $n;
+printf "a:                     %.9f\n", $a;
+printf "b:                     %.9f\n", $b;
+printf "1/b:                   %.3f\n", 1/$b;
+printf "Variance:              %.3f\n", $var;
+printf "Std. deviation:                %.3f\n", $stdd;