|
NAMEStatistics::Basic::LeastSquareFit - find the least square fit for two listsSYNOPSISA machine to calculate the Least Square Fit of given vectors x and y.The module returns the alpha and beta filling this 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 that "x_given_y()" can sometimes produce a divide-by-zero error since it has to divide by the $beta.) 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; } METHODSThis list of methods skips the methods inherited from Statistics::Basic::_TwoVectorBase (things like insert(), and ginsert()).
OVERLOADSThis object is overloaded. It tries to return an appropriate string for the calculation, but raises an error in numeric context.In boolean context, this object is always true (even when empty). AUTHORPaul Miller "<jettero@cpan.org>"COPYRIGHTCopyright 2012 Paul Miller -- Licensed under the LGPLSEE ALSOperl(1), Statistics::Basic, Statistics::Basic::_TwoVectorBase, Statistics::Basic::Vector
Visit the GSP FreeBSD Man Page Interface. |