-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineRegression.java
More file actions
148 lines (136 loc) · 2.76 KB
/
LineRegression.java
File metadata and controls
148 lines (136 loc) · 2.76 KB
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
/**
* Class used to calculate the line of regression. Used to find feed forward for
* shooter velocity controller.
*
* @author Bryan
*/
public class LineRegression {
private int N;
private double[] x;
private double[] y;
private static double xSum = 0;
private static double ySum = 0;
private static double xSquaredSum = 0;
private static double xAverage = 0;
private static double yAverage = 0;
private static double slope;
private static double intercept;
/**
* Instantiates a new line regression.
*/
public LineRegression() {
}
/**
* Instantiates a new line regression.
*
* @param x
* Array of x values
* @param y
* Array of y values
*/
public LineRegression(double[] x, double[] y) {
this.setValues(x, y);
}
/**
* Sets the values.
*
* @param x
* the x
* @param y
* the y
*/
public void setValues(double[] x, int[] y) {
double[] temp = new double[y.length];
for (int i = 0; i < y.length; i++) {
temp[i] = (double) y[i];
}
this.setValues(x, temp);
}
/**
* Sets the values.
*
* @param x
* the x
* @param y
* the y
*/
public void setValues(int[] x, double[] y) {
double[] temp = new double[x.length];
for (int i = 0; i < x.length; i++) {
temp[i] = (double) x[i];
}
this.setValues(temp, y);
}
/**
* Sets the values.
*
* @param x
* the x
* @param y
* the y
*/
public void setValues(int[] x, int[] y) {
double[] temp = new double[x.length];
for (int i = 0; i < x.length; i++) {
temp[i] = (double) x[i];
}
this.setValues(temp, y);
}
/**
* Sets the values.
*
* @param x
* the x
* @param y
* the y
*/
public void setValues(double[] x, double[] y) {
if (x.length != y.length) {
throw new IllegalArgumentException("array lengths are not equal");
}
N = x.length;
this.x = x;
this.y = y;
xSum = 0;
ySum = 0;
xSquaredSum = 0;
for (int i = 0; i < N; i++) {
xSum = xSum + x[i];
ySum = ySum + y[i];
xSquaredSum = xSquaredSum + Math.pow(x[i], 2);
}
xAverage = xSum / N;
yAverage = ySum / N;
bestLine();
}
/**
* Best line.
*/
private void bestLine() {
// second pass: compute summary statistics
double xxbar = 0.0, yybar = 0.0, xybar = 0.0;
for (int i = 0; i < N; i++) {
xxbar += (x[i] - xAverage) * (x[i] - xAverage);
yybar += (y[i] - yAverage) * (y[i] - yAverage);
xybar += (x[i] - xAverage) * (y[i] - yAverage);
}
slope = xybar / xxbar;
intercept = yAverage - slope * xAverage;
}
/**
* Gets the slope.
*
* @return the slope
*/
public double getSlope() {
return slope;
}
/**
* Gets the intercept.
*
* @return the intercept
*/
public double getIntercept() {
return intercept;
}
}