Statis...uareFit(3pm)
β¨ NAME β¨
okay bestie, gather round π«Ά this is Statistics::Basic::LeastSquareFit β the module that finds the least square fit for two lists. iconic behavior, no cap. π
πΈ SYNOPSIS πΈ
It's literally a little machine π€ that calculates the Least Square Fit of given vectors x and y.
The module hands you back alpha and beta, and they fill in this exact formula:
$y = $beta * $x + $alpha
for a given set of x and y co-ordinate pairs.
Say you have the set of Cartesian coordinates:
my @points = ( [1,1], [2,2], [3,3], [4,4] );
The simplest way to find the LSF is as follows:
my $lsf = lsf()->set_size(int @points);
$lsf->insert(@$_) for @points;
Or this way:
my $xv = vector( map {$_->[0]} @points );
my $yv = vector( map {$_->[1]} @points );
my $lsf = lsf($xv, $yv);
And then either query the values or print them like so:
print "The LSF for $xv and $yv: $lsf\n";
my ($yint, $slope) =
my ($alpha, $beta) = $lsf->query;
LSF is meant for finding a line of best fit. $beta is the slope of the line and $alpha is the y-offset. Suppose you want to draw the line. Use these to calculate the "x" for a given "y" or vice versa:
my $y = $lsf->y_given_x( 7 );
my $x = $lsf->x_given_y( 7 );
(Note thatx_given_y()can sometimes produce a divide-by-zero error since it has to divide by the$beta.) π girl math can be brutal
Create a 20 point "moving" LSF like so:
use Statistics::Basic qw(:all nofill);
my $sth = $dbh->prepare("select x,y from points where something");
my $len = 20;
my $lsf = lsf()->set_size($len);
$sth->execute or die $dbh->errstr;
$sth->bind_columns( my ($x, $y) ) or die $dbh->errstr;
my $count = $len;
while( $sth->fetch ) {
$lsf->insert( $x, $y );
if( defined( my ($yint, $slope) = $lsf->query ) {
print "LSF: y= $slope*x + $yint\n";
}
# This would also work:
# print "$lsf\n" if $lsf->query_filled;
}
π METHODS π
Heads up bestie π«Ά this list skips the methods inherited from Statistics::Basic::_TwoVectorBase (things like insert(), and ginsert()). Those still exist, they're just not re-explained here.
new()
Create a new Statistics::Basic::LeastSquareFit object. π
This function takes two arguments β which can either be arrayrefs or Statistics::Basic::Vector objects. This function is called when the leastsquarefirt() shortcut-function is called.
query()
LSF is meant for finding a line of best fit. $beta is the slope of the line and $alpha is the y-offset.
my ($alpha, $beta) = $lsf->query;
y_given_x()
Automatically calculate the y-value on the line for a given x-value. β¨
my $y = $lsf->y_given_x( 7 );
x_given_y()
Automatically calculate the x-value on the line for a given y-value. β¨
my $x = $lsf->x_given_y( 7 );
β οΈx_given_y()can sometimes produce a divide-by-zero error since it has to divide by the$beta. This might be helpful:
if( defined( my $x = eval { $lsf->x_given_y(7) } ) ) {
warn "there is no x value for 7";
} else {
print "x (given y=7): $x\n";
}
query_vector1()
Return the Statistics::Basic::Vector for the first vector used in the computation of alpha and beta. π¦
query_vector2()
Return the Statistics::Basic::Vector object for the second vector used in the computation of alpha and beta. π¦
query_mean1()
Returns the Statistics::Basic::Mean object for the first vector used in the computation of alpha and beta. π
query_variance1()
Returns the Statistics::Basic::Variance object for the first vector used in the computation of alpha and beta. π
query_covariance()
Returns the Statistics::Basic::Covariance object used in the computation of alpha and beta. π
π OVERLOADS π
This object is overloaded. It tries to return an appropriate string for the calculation, but raises an error in numeric context. π so don't try to do math on the string, bestie.
In boolean context, this object is always true (even when empty). periodt. π
π AUTHOR π
Paul Miller <jettero@cpan.org>
π COPYRIGHT π
Copyright 2012 Paul Miller -- Licensed under the LGPL
π SEE ALSO π
perl(1), Statistics::Basic, Statistics::Basic::_TwoVectorBase, Statistics::Basic::Vector
and that's the whole thing, bestie π«Ά this page came from perl v5.36.0, dated 2022-11-19 β go forth and fit those squares, you absolute icon πβ¨
π¦ where this page comes from
- debian bookworm: libstatistics-basic-perl 1.6611-3 Β· LGPL-2.1-only
- debian trixie: libstatistics-basic-perl 1.6611-3 Β· LGPL-2.1-only
- ubuntu noble: libstatistics-basic-perl 1.6611-3 Β· LGPL-2.1-only
- ubuntu resolute: libstatistics-basic-perl 1.6611-3 Β· LGPL-2.1-only
- ubuntu stonking: libstatistics-basic-perl 1.6611-3 Β· LGPL-2.1-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms