-
Notifications
You must be signed in to change notification settings - Fork 4
/
README
306 lines (241 loc) · 13.1 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
Contents:
NAME
SYNOPSIS
DESCRIPTION
ALGORITHM
LIMITATIONS
EXAMPLES
METHODS
SEE ALSO
AUTHOR
LICENSE
DISCLAIMER
NAME
LineFit - Least squares line fit, weighted or unweighted
SYNOPSIS
require 'linefit'
lineFit = LineFit.new
lineFit.setData(x,y)
intercept, slope = lineFit.coefficients
rSquared = lineFit.rSquared
meanSquaredError = lineFit.meanSqError
durbinWatson = lineFit.durbinWatson
sigma = lineFit.sigma
tStatIntercept, tStatSlope = lineFit.tStatistics
predictedYs = lineFit.predictedYs
residuals = lineFit.residuals
varianceIntercept, varianceSlope = lineFit.varianceOfEstimates
DESCRIPTION
The LineFit class does weighted or unweighted least-squares
line fitting to two-dimensional data (y = a + b * x). (This is also
called linear regression.) In addition to the slope and y-intercept, the
class can return the square of the correlation coefficient (R squared),
the Durbin-Watson statistic, the mean squared error, sigma, the t
statistics, the variance of the estimates of the slope and y-intercept,
the predicted y values and the residuals of the y values. (See the
METHODS section for a description of these statistics.)
The class accepts input data in separate x and y arrays or a single 2-D
array (an array of two arrays). The optional weights are input in a
separate array. The module can optionally verify that the input data and
weights are valid numbers. If weights are input, the line fit minimizes
the weighted sum of the squared errors and the following statistics are
weighted: the correlation coefficient, the Durbin-Watson statistic, the
mean squared error, sigma and the t statistics.
The class is state-oriented and caches its results. Once you call the
setData() method, you can call the other methods in any order or call a
method several times without invoking redundant calculations.
The decision to use or not use weighting could be made using your a
prior knowledge of the data or using supplemental data. If the data is
sparse or contains non-random noise, weighting can degrade the solution.
Weighting is a good option if some points are suspect or less relevant
(e.g., older terms in a time series, points that are known to have more
noise).
ALGORITHM
The least-square line is the line that minimizes the sum of the squares
of the y residuals:
Minimize SUM((y[i] - (a + b * x[i])) ** 2)
Setting the parial derivatives of a and b to zero yields a solution that
can be expressed in terms of the means, variances and covariances of x
and y:
b = SUM((x[i] - meanX) * (y[i] - meanY)) / SUM((x[i] - meanX) ** 2)
a = meanY - b * meanX
Note that a and b are undefined if all the x values are the same.
If you use weights, each term in the above sums is multiplied by the
value of the weight for that index. The program normalizes the weights
(after copying the input values) so that the sum of the weights equals
the number of points. This minimizes the differences between the
weighted and unweighted equations.
LineFit uses equations that are mathematically equivalent to
the above equations and computationally more efficient. The module runs
in O(N) (linear time).
LIMITATIONS
The regression fails if the input x values are all equal or the only
unequal x values have zero weights. This is an inherent limit to fitting
a line of the form y = a + b * x. In this case, the class issues an
error message and methods that return statistical values will return
undefined values. You can also use the return value of the regress()
method to check the status of the regression.
As the sum of the squared deviations of the x values approaches zero,
the class's results become sensitive to the precision of floating
point operations on the host system.
If the x values are not all the same and the apparent "best fit" line is
vertical, the class will fit a horizontal line. For example, an input
of (1, 1), (1, 7), (2, 3), (2, 5) returns a slope of zero, an intercept
of 4 and an R squared of zero. This is correct behavior because this
line is the best least-squares fit to the data for the given
parameterization (y = a + b * x).
On a 32-bit system the results are accurate to about 11 significant
digits, depending on the input data. Many of the installation tests will
fail on a system with word lengths of 16 bits or fewer. (You might want
to upgrade your old 80286 IBM PC.)
EXAMPLES
require 'linefit'
lineFit = LineFit.new
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
y = [4039,4057,4052,4094,4104,4110,4154,4161,4186,4195,4229,4244,4242,4283,4322,4333,4368,4389]
lineFit.setData(x,y)
intercept, slope = lineFit.coefficients
rSquared = lineFit.rSquared
meanSquaredError = lineFit.meanSqError
durbinWatson = lineFit.durbinWatson
sigma = lineFit.sigma
tStatIntercept, tStatSlope = lineFit.tStatistics
predictedYs = lineFit.predictedYs
residuals = lineFit.residuals
varianceIntercept, varianceSlope = lineFit.varianceOfEstimates
print "Slope: #{slope} Y-Intercept: #{intercept}\n"
print "r-Squared: #{rSquared}\n"
print "Mean Squared Error: #{meanSquaredError}\n"
print "Durbin Watson Test: #{durbinWatson}\n"
print "Sigma: #{sigma}\n"
print "t Stat Intercept: #{tStatIntercept} t Stat Slope: #{tStatSlope}\n\n"
print "Predicted Ys: #{predictedYs.inspect}\n\n"
print "Residuals: #{residuals.inspect}\n\n"
print "Variance Intercept: #{varianceIntercept} Variance Slope: #{varianceSlope}\n"
print "\n"
newX = 24
newY = lineFit.forecast(newX)
print "New X: #{newX}\nNew Y: #{newY}\n"
METHODS
The class is state-oriented and caches its results. Once you call the
setData() method, you can call the other methods in any order or call a
method several times without invoking redundant calculations.
The regression fails if the x values are all the same. In this case, the
module issues an error message and methods that return statistical
values will return undefined values. You can also use the return value
of the regress() method to check the status of the regression.
new() - create a new LineFit object
linefit = LineFit.new
linefit = LineFit.new(validate)
linefit = LineFit.new(validate, hush)
validate = 1 -> Verify input data is numeric (slower execution)
= 0 -> Don't verify input data (default, faster execution)
hush = 1 -> Suppress error messages
= 0 -> Enable error messages (default)
coefficients() - Return the slope and y intercept
intercept, slope = linefit.coefficients
The returned list is undefined if the regression fails.
durbinWatson() - Return the Durbin-Watson statistic
durbinWatson = linefit.durbinWatson
The Durbin-Watson test is a test for first-order autocorrelation in the
residuals of a time series regression. The Durbin-Watson statistic has a
range of 0 to 4; a value of 2 indicates there is no autocorrelation.
The return value is undefined if the regression fails. If weights are
input, the return value is the weighted Durbin-Watson statistic.
meanSqError() - Return the mean squared error
meanSquaredError = linefit.meanSqError
The return value is undefined if the regression fails. If weights are
input, the return value is the weighted mean squared error.
predictedYs() - Return the predicted y values array
predictedYs = linefit.predictedYs
The returned list is undefined if the regression fails.
forecast() - Return the independent (Y) value, by using a dependent (X) value.
forecasted_y = linefit.forecast(x_value)
Will use the slope and intercept to calculate the Y value along the line
at the x value. Note: value returned only as good as the line fit.
regress() - Do the least squares line fit (if not already done)
linefit.regress
You don't need to call this method because it is invoked by the other
methods as needed. After you call setData(), you can call regress() at
any time to get the status of the regression for the current data.
residuals() - Return predicted y values minus input y values
residuals = linefit.residuals
The returned list is undefined if the regression fails.
rSquared() - Return the square of the correlation coefficient
rSquared = linefit.rSquared
R squared, also called the square of the Pearson product-moment
correlation coefficient, is a measure of goodness-of-fit. It is the
fraction of the variation in Y that can be attributed to the variation
in X. A perfect fit will have an R squared of 1; fitting a line to the
vertices of a regular polygon will yield an R squared of zero. Graphical
displays of data with an R squared of less than about 0.1 do not show a
visible linear trend.
The return value is undefined if the regression fails. If weights are
input, the return value is the weighted correlation coefficient.
setData() - Initialize (x,y) values and optional weights
lineFit.setData(x, y)
lineFit.setData(x, y, weights)
lineFit.setData(xy)
lineFit.setData(xy, weights)
xy is an array of arrays; x values are xy[i][0], y values are
xy[i][1]. The method identifies the difference between the first
and fourth calling signatures by examining the first argument.
The optional weights array must be the same length as the data array(s).
The weights must be non-negative numbers; at least two of the weights
must be nonzero. Only the relative size of the weights is significant:
the program normalizes the weights (after copying the input values) so
that the sum of the weights equals the number of points. If you want to
do multiple line fits using the same weights, the weights must be passed
to each call to setData().
The method will return false if the array lengths don't match, there are
less than two data points, any weights are negative or less than two of
the weights are nonzero. If the new() method was called with validate =
1, the method will also verify that the data and weights are valid
numbers. Once you successfully call setData(), the next call to any
method other than new() or setData() invokes the regression.
sigma() - Return the standard error of the estimate
sigma = linefit.sigma
Sigma is an estimate of the homoscedastic standard deviation of the
error. Sigma is also known as the standard error of the estimate.
The return value is undefined if the regression fails. If weights are
input, the return value is the weighted standard error.
tStatistics() - Return the t statistics
tStatIntercept, tStatSlope = linefit.tStatistics
The t statistic, also called the t ratio or Wald statistic, is used to
accept or reject a hypothesis using a table of cutoff values computed
from the t distribution. The t-statistic suggests that the estimated
value is (reasonable, too small, too large) when the t-statistic is
(close to zero, large and positive, large and negative).
The returned list is undefined if the regression fails. If weights are
input, the returned values are the weighted t statistics.
varianceOfEstimates() - Return variances of estimates of intercept, slope
varianceIntercept, varianceSlope = linefit.varianceOfEstimates
Assuming the data are noisy or inaccurate, the intercept and slope
returned by the coefficients() method are only estimates of the true
intercept and slope. The varianceofEstimate() method returns the
variances of the estimates of the intercept and slope, respectively. See
Numerical Recipes in C, section 15.2 (Fitting Data to a Straight Line),
equation 15.2.9.
The returned list is undefined if the regression fails. If weights are
input, the returned values are the weighted variances.
SEE ALSO
Mendenhall, W., and Sincich, T.L., 2003, A Second Course in Statistics:
Regression Analysis, 6th ed., Prentice Hall.
Press, W. H., Flannery, B. P., Teukolsky, S. A., Vetterling, W. T., 1992,
Numerical Recipes in C : The Art of Scientific Computing, 2nd ed.,
Cambridge University Press.
AUTHOR
Eric Cline, escline(at)gmail(dot)com
Richard Anderson
LICENSE
This program is free software; you can redistribute it and/or modify it
under the same terms as Ruby itself.
The full text of the license can be found in the LICENSE file included
in the distribution and available in the RubyForge listing for
LineFit (see rubyforge.org).
DISCLAIMER
To the maximum extent permitted by applicable law, the author of this
module disclaims all warranties, either express or implied, including
but not limited to implied warranties of merchantability and fitness for
a particular purpose, with regard to the software and the accompanying
documentation.