-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunningRegression.java
97 lines (89 loc) · 2.96 KB
/
RunningRegression.java
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
package frc.robot;
// from http://www.johndcook.com/running_regression.html
/*
Computing linear regression in one pass
The RunningRegression class is the analog of the RunningStats class described here and uses that class.
You add pairs of (x, y) values by using the Push. At any point along the way you can call the Slope,
Intercept, or Correlation functions to see the current value of these statistics.
You can also combine two RunningRegression objects by using the + and += operators. For example, you might
accrue data on several different threads in parallel then add their RunningRegression objects together.
*/
/* EXAMPLE
RunningRegression aLine = new RunningRegression(); // construct regression object
Loop:
System.out.printf("%d, %f\n",i, xm); // print data point - assumes xm was set above here
aLine.Push(xm, 1.); // add data point to regression routine - this example assumes data are flat line at 1 (y = 1.) otherwise enter a y value
System.out.print(aLine); // print regression
*/
public class RunningRegression
{
public RunningRegression()
{
Clear();
}
public final void Clear()
{
x_stats.Clear();
y_stats.Clear();
S_xy = 0.0;
n = 0;
}
public final void copyFrom(RunningRegression r)
{
x_stats.copyFrom(r.x_stats);
y_stats.copyFrom(r.y_stats);
S_xy = r.S_xy;
n = r.n;
}
public final void Push(double x, double y)
{
S_xy += (x_stats.Mean() - x) * (y_stats.Mean() - y) * (double)n / (double)(n + 1);
x_stats.Push(x);
y_stats.Push(y);
n++;
}
public final int NumDataValues()
{
return n;
}
public final double Slope()
{
double S_xx = x_stats.Variance() * (n - 1.0);
return S_xy / S_xx;
}
public final double Intercept()
{
return y_stats.Mean() - Slope() * x_stats.Mean();
}
public final double Correlation()
{
double t = x_stats.StandardDeviation() * y_stats.StandardDeviation();
return S_xy / ((n - 1) * t);
}
public String toString()
{
String content;
content =
String.format("\nNumDataValues: %d\n", NumDataValues()) +
String.format("y = A + Bx ==> y = %g %+gx\n", Intercept(), Slope()) +
String.format("Slope: %f\n", Slope()) +
String.format("Intercept: %f\n", Intercept()) +
String.format("r^2 Correlation: %f\n", Correlation());
return content;
}
public RunningRegression add (RunningRegression b)
{
RunningRegression combined = new RunningRegression();
combined.x_stats.copyFrom(this.x_stats.add(b.x_stats));
combined.y_stats.copyFrom(this.x_stats.add(b.y_stats));
combined.n = this.n + b.n;
double delta_x = b.x_stats.Mean() - this.x_stats.Mean();
double delta_y = b.y_stats.Mean() - this.y_stats.Mean();
combined.S_xy = this.S_xy + b.S_xy + (double)(this.n * b.n) * delta_x * delta_y / (double)combined.n;
return combined;
}
private RunningStats x_stats = new RunningStats();
private RunningStats y_stats = new RunningStats();
private double S_xy;
private int n;
}