<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">;#
;# lr.pl,v 3.1 1993/07/06 01:09:08 jbj Exp
;#
;#
;# Linear Regression Package for perl
;# to be 'required' from perl
;#
;#  Copyright (c) 1992 
;#  Frank Kardel, Rainer Pruy
;#  Friedrich-Alexander Universitaet Erlangen-Nuernberg
;#
;#  Copyright (c) 1997 by
;#  Ulrich Windl &lt;Ulrich.Windl@rz.uni-regensburg.de&gt;
;#  (Converted to a PERL 5.004 package)
;#
;#############################################################

package lr;

##
## y = A + Bx
##
## B = (n * Sum(xy) - Sum(x) * Sum(y)) / (n * Sum(x^2) - Sum(x)^2)
##
## A = (Sum(y) - B * Sum(x)) / n
##

##
## interface
##
;# init(tag);		initialize data set for tag
;# sample(x, y, tag);	enter sample
;# Y(x, tag);		compute y for given x 
;# X(y, tag);		compute x for given y
;# r(tag);		regression coefficient
;# cov(tag);		covariance
;# A(tag);   
;# B(tag);
;# sigma(tag);		standard deviation
;# mean(tag);
#########################

sub init
{
    my $self = shift;

    $self-&gt;{n}   = 0;
    $self-&gt;{sx}  = 0.0;
    $self-&gt;{sx2} = 0.0;
    $self-&gt;{sxy} = 0.0;
    $self-&gt;{sy}  = 0.0;
    $self-&gt;{sy2} = 0.0;
}

sub sample($$$)
{
    my $self = shift;
    my($_x, $_y) = @_;

    ++($self-&gt;{n});
    $self-&gt;{sx}  += $_x;
    $self-&gt;{sy}  += $_y;
    $self-&gt;{sxy} += $_x * $_y;
    $self-&gt;{sx2} += $_x**2;
    $self-&gt;{sy2} += $_y**2;
}

sub B($)
{
    my $self = shift;

    return 1 unless ($self-&gt;{n} * $self-&gt;{sx2} - $self-&gt;{sx}**2);
    return ($self-&gt;{n} * $self-&gt;{sxy} - $self-&gt;{sx} * $self-&gt;{sy})
	/ ($self-&gt;{n} * $self-&gt;{sx2} - $self-&gt;{sx}**2);
}

sub A($)
{
    my $self = shift;

    return ($self-&gt;{sy} - B($self) * $self-&gt;{sx}) / $self-&gt;{n};
}

sub Y($$)
{
    my $self = shift;

    return A($self) + B($self) * $_[$[];
}

sub X($$)
{
    my $self = shift;

    return ($_[$[] - A($self)) / B($self);
}

sub r($)
{
    my $self = shift;

    my $s = ($self-&gt;{n} * $self-&gt;{sx2} - $self-&gt;{sx}**2)
	  * ($self-&gt;{n} * $self-&gt;{sy2} - $self-&gt;{sy}**2);

    return 1 unless $s;
    
    return ($self-&gt;{n} * $self-&gt;{sxy} - $self-&gt;{sx} * $self-&gt;{sy}) / sqrt($s);
}

sub cov($)
{
    my $self = shift;

    return ($self-&gt;{sxy} - $self-&gt;{sx} * $self-&gt;{sy} / $self-&gt;{n})
	/ ($self-&gt;{n} - 1);
}

sub sigma($)
{
    my $self = shift;

    return 0 if $self-&gt;{n} &lt;= 1;
    return sqrt(($self-&gt;{sy2} - ($self-&gt;{sy} * $self-&gt;{sy}) / $self-&gt;{n})
		/ ($self-&gt;{n}));
}

sub mean($)
{
    my $self = shift;

    return 0 if $self-&gt;{n} &lt;= 0;
    return $self-&gt;{sy} / $self-&gt;{n};
}

sub new
{
    my $class = shift;
    my $self = {
	(n =&gt; undef,
	 sx =&gt; undef,
	 sx2 =&gt; undef,
	 sxy =&gt; undef,
	 sy =&gt; undef,
	 sy2 =&gt; undef)
    };
    bless $self, $class;
    init($self);
    return $self;
}

1;
</pre></body></html>