Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ntp / scripts / monitoring / lr.pl
blob1ab3e3fe4fa3503785b9552db7cc3776d12c2583
1 ;#
2 ;# lr.pl,v 3.1 1993/07/06 01:09:08 jbj Exp
3 ;#
4 ;#
5 ;# Linear Regression Package for perl
6 ;# to be 'required' from perl
7 ;#
8 ;# Copyright (c) 1992
9 ;# Frank Kardel, Rainer Pruy
10 ;# Friedrich-Alexander Universitaet Erlangen-Nuernberg
12 ;# Copyright (c) 1997 by
13 ;# Ulrich Windl <Ulrich.Windl@rz.uni-regensburg.de>
14 ;# (Converted to a PERL 5.004 package)
16 ;#############################################################
18 package lr;
21 ## y = A + Bx
23 ## B = (n * Sum(xy) - Sum(x) * Sum(y)) / (n * Sum(x^2) - Sum(x)^2)
25 ## A = (Sum(y) - B * Sum(x)) / n
29 ## interface
31 ;# init(tag); initialize data set for tag
32 ;# sample(x, y, tag); enter sample
33 ;# Y(x, tag); compute y for given x
34 ;# X(y, tag); compute x for given y
35 ;# r(tag); regression coefficient
36 ;# cov(tag); covariance
37 ;# A(tag);
38 ;# B(tag);
39 ;# sigma(tag); standard deviation
40 ;# mean(tag);
41 #########################
43 sub init
45 my $self = shift;
47 $self->{n} = 0;
48 $self->{sx} = 0.0;
49 $self->{sx2} = 0.0;
50 $self->{sxy} = 0.0;
51 $self->{sy} = 0.0;
52 $self->{sy2} = 0.0;
55 sub sample($$)
57 my $self = shift;
58 my($_x, $_y) = @_;
60 ++($self->{n});
61 $self->{sx} += $_x;
62 $self->{sy} += $_y;
63 $self->{sxy} += $_x * $_y;
64 $self->{sx2} += $_x**2;
65 $self->{sy2} += $_y**2;
68 sub B()
70 my $self = shift;
72 return 1 unless ($self->{n} * $self->{sx2} - $self->{sx}**2);
73 return ($self->{n} * $self->{sxy} - $self->{sx} * $self->{sy})
74 / ($self->{n} * $self->{sx2} - $self->{sx}**2);
77 sub A()
79 my $self = shift;
81 return ($self->{sy} - B() * $self->{sx}) / $self->{n};
84 sub Y()
86 my $self = shift;
88 return A() + B() * $_[$[];
91 sub X()
93 my $self = shift;
95 return ($_[$[] - A()) / B();
98 sub r()
100 my $self = shift;
102 my $s = ($self->{n} * $self->{sx2} - $self->{sx}**2)
103 * ($self->{n} * $self->{sy2} - $self->{sy}**2);
105 return 1 unless $s;
107 return ($self->{n} * $self->{sxy} - $self->{sx} * $self->{sy}) / sqrt($s);
110 sub cov()
112 my $self = shift;
114 return ($self->{sxy} - $self->{sx} * $self->{sy} / $self->{n})
115 / ($self->{n} - 1);
118 sub sigma()
120 my $self = shift;
122 return 0 if $self->{n} <= 1;
123 return sqrt(($self->{sy2} - ($self->{sy} * $self->{sy}) / $self->{n})
124 / ($self->{n}));
127 sub mean()
129 my $self = shift;
131 return 0 if $self->{n} <= 0;
132 return $self->{sy} / $self->{n};
135 sub new
137 my $class = shift;
138 my $self = {
139 (n => undef,
140 sx => undef,
141 sx2 => undef,
142 sxy => undef,
143 sy => undef,
144 sy2 => undef)
146 bless $self, $class;
147 init($self);
148 return $self;