1Statistics::Basic::LeasUtsSeqruaCroenFtirti(b3u)ted PerlStDaotciusmteinctsa:t:iBoansic::LeastSquareFit(3)
2
3
4
6 Statistics::Basic::LeastSquareFit - find the least square fit for two
7 lists
8
10 A machine to calculate the Least Square Fit of given vectors x and y.
11
12 The module returns the alpha and beta filling this formula:
13
14 $y = $beta * $x + $alpha
15
16 for a given set of x and y co-ordinate pairs.
17
18 Say you have the set of Cartesian coordinates:
19
20 my @points = ( [1,1], [2,2], [3,3], [4,4] );
21
22 The simplest way to find the LSF is as follows:
23
24 my $lsf = lsf()->set_size(int @points);
25 $lsf->insert(@$_) for @points;
26
27 Or this way:
28
29 my $xv = vector( map {$_->[0]} @points );
30 my $yv = vector( map {$_->[1]} @points );
31 my $lsf = lsf($xv, $yv);
32
33 And then either query the values or print them like so:
34
35 print "The LSF for $xv and $yv: $lsf\n";
36 my ($yint, $slope) =
37 my ($alpha, $beta) = $lsf->query;
38
39 LSF is meant for finding a line of best fit. $beta is the slope of the
40 line and $alpha is the y-offset. Suppose you want to draw the line.
41 Use these to calculate the "x" for a given "y" or vice versa:
42
43 my $y = $lsf->y_given_x( 7 );
44 my $x = $lsf->x_given_y( 7 );
45
46 (Note that x_given_y() can sometimes produce a divide-by-zero error
47 since it has to divide by the $beta.)
48
49 Create a 20 point "moving" LSF like so:
50
51 use Statistics::Basic qw(:all nofill);
52
53 my $sth = $dbh->prepare("select x,y from points where something");
54 my $len = 20;
55 my $lsf = lsf()->set_size($len);
56
57 $sth->execute or die $dbh->errstr;
58 $sth->bind_columns( my ($x, $y) ) or die $dbh->errstr;
59
60 my $count = $len;
61 while( $sth->fetch ) {
62 $lsf->insert( $x, $y );
63 if( defined( my ($yint, $slope) = $lsf->query ) {
64 print "LSF: y= $slope*x + $yint\n";
65 }
66
67 # This would also work:
68 # print "$lsf\n" if $lsf->query_filled;
69 }
70
72 This list of methods skips the methods inherited from
73 Statistics::Basic::_TwoVectorBase (things like insert(), and
74 ginsert()).
75
76 new()
77 Create a new Statistics::Basic::LeastSquareFit object. This
78 function takes two arguments -- which can either be arrayrefs or
79 Statistics::Basic::Vector objects. This function is called when
80 the leastsquarefirt() shortcut-function is called.
81
82 query()
83 LSF is meant for finding a line of best fit. $beta is the slope of
84 the line and $alpha is the y-offset.
85
86 my ($alpha, $beta) = $lsf->query;
87
88 y_given_x()
89 Automatically calculate the y-value on the line for a given
90 x-value.
91
92 my $y = $lsf->y_given_x( 7 );
93
94 x_given_y()
95 Automatically calculate the x-value on the line for a given
96 y-value.
97
98 my $x = $lsf->x_given_y( 7 );
99
100 x_given_y() can sometimes produce a divide-by-zero error since it
101 has to divide by the $beta. This might be helpful:
102
103 if( defined( my $x = eval { $lsf->x_given_y(7) } ) ) {
104 warn "there is no x value for 7";
105
106 } else {
107 print "x (given y=7): $x\n";
108 }
109
110 query_vector1()
111 Return the Statistics::Basic::Vector for the first vector used in
112 the computation of alpha and beta.
113
114 query_vector2()
115 Return the Statistics::Basic::Vector object for the second vector
116 used in the computation of alpha and beta.
117
118 query_mean1()
119 Returns the Statistics::Basic::Mean object for the first vector
120 used in the computation of alpha and beta.
121
122 query_variance1()
123 Returns the Statistics::Basic::Variance object for the first vector
124 used in the computation of alpha and beta.
125
126 query_covariance()
127 Returns the Statistics::Basic::Covariance object used in the
128 computation of alpha and beta.
129
131 This object is overloaded. It tries to return an appropriate string
132 for the calculation, but raises an error in numeric context.
133
134 In boolean context, this object is always true (even when empty).
135
137 Paul Miller "<jettero@cpan.org>"
138
140 Copyright 2012 Paul Miller -- Licensed under the LGPL
141
143 perl(1), Statistics::Basic, Statistics::Basic::_TwoVectorBase,
144 Statistics::Basic::Vector
145
146
147
148perl v5.36.0 2023-01-2S0tatistics::Basic::LeastSquareFit(3)