Debian Codegraph @

src/dr/stats/Regression.java

raw symbols SCIP
1 /*
2 * Regression.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.stats;
27
28 /**
29 * simple regression of two variables
30 *
31 * @author Andrew Rambaut
32 * @version $Id: Regression.java,v 1.5 2005/05/24 20:26:01 rambaut Exp $
33 */
34 public class Regression {
35 private Variate.D xData = null;
36 private Variate.D yData = null;
37
38 private boolean forceOrigin = false;
39 private boolean regressionKnown = false;
40
41 private double gradient;
42 private double intercept;
43 private double covariance;
44 private double sumResidualsSquared;
45 private double residualMeanSquared;
46 private double correlationCoefficient;
47
48 /**
49 * Constructor
50 */
51 public Regression() {
52 }
53
54 /**
55 * Constructor
56 */
57 public Regression(Variate xData, Variate yData) {
58 setData(xData, yData);
59 }
60
61 /**
62 * Constructor
63 */
64 public Regression(double[] xData, double[] yData) {
65 setData(xData, yData);
66 }
67
68 /**
69 * Constructor
70 */
71 public Regression(Variate xData, Variate yData, boolean forceOrigin) {
72 setData(xData, yData);
73 setForceOrigin(forceOrigin);
74 }
75
76 /**
77 * Constructor
78 */
79 public Regression(double[] xData, double[] yData, boolean forceOrigin) {
80 setData(xData, yData);
81 setForceOrigin(forceOrigin);
82 }
83
84 /**
85 * Set data
86 */
87 public void setData(double[] xData, double[] yData) {
88 Variate.D xd = new Variate.D();
89 Variate.D yd = new Variate.D();
90
91 for (int i = 0; i < xData.length; i++) {
92 xd.add(xData[i]);
93 yd.add(yData[i]);
94 }
95
96 this.xData = xd;
97 this.yData = yd;
98
99 regressionKnown = false;
100 }
101
102 /**
103 * Set data
104 */
105 public void setData(Variate xData, Variate yData) {
106 this.xData = (Variate.D) xData;
107 this.yData = (Variate.D) yData;
108
109 regressionKnown = false;
110 }
111
112 public void setForceOrigin(boolean forceOrigin) {
113 this.forceOrigin = forceOrigin;
114
115 regressionKnown = false;
116 }
117
118 public double getGradient() {
119 if (!regressionKnown)
120 calculateRegression();
121 return gradient;
122 }
123
124 public double getIntercept() {
125 if (!regressionKnown)
126 calculateRegression();
127 return intercept;
128 }
129
130 public double getYIntercept() {
131 return getIntercept();
132 }
133
134 public double getXIntercept() {
135 return -getIntercept() / getGradient();
136 }
137
138 public double getCovariance() {
139 if (!regressionKnown)
140 calculateRegression();
141 return covariance;
142 }
143
144 public double getResidualMeanSquared() {
145 if (!regressionKnown)
146 calculateRegression();
147 return residualMeanSquared;
148 }
149
150 public double getSumResidualsSquared() {
151 if (!regressionKnown)
152 calculateRegression();
153 return sumResidualsSquared;
154 }
155
156 public double getCorrelationCoefficient() {
157 if (!regressionKnown) {
158 calculateRegression();
159 }
160 return correlationCoefficient;
161 }
162
163 public double getRSquared() {
164 if (!regressionKnown) {
165 calculateRegression();
166 }
167 return correlationCoefficient * correlationCoefficient;
168 }
169
170 public double getResidual(final double x, final double y) {
171 return y - ((getGradient() * x) + getIntercept());
172 }
173
174 public double getX(final double y) {
175 return (y - getIntercept()) / getGradient();
176 }
177
178 public double getY(final double x) {
179 return x * getGradient() + getIntercept();
180 }
181
182 public Variate.N getXData() {
183 return xData;
184 }
185
186 public Variate.N getYData() {
187 return yData;
188 }
189
190 public Variate getYResidualData() {
191 Variate.D rd = new Variate.D();
192
193 for (int i = 0; i < xData.getCount(); i++) {
194 rd.add(getResidual(xData.get(i), yData.get(i)));
195 }
196
197 return rd;
198 }
199
200 private void calculateRegression() {
201 int i, n = xData.getCount();
202 double meanX = 0.0, meanY = 0.0;
203
204 if (!forceOrigin) {
205 meanX = xData.getMean();
206 meanY = yData.getMean();
207 }
208
209 //Calculate sum of products & sum of x squares
210
211 double sumProducts = 0.0;
212 double sumSquareX = 0.0;
213 double sumSquareY = 0.0;
214 double x1, y1;
215 for (i = 0; i < n; i++) {
216 x1 = xData.get(i) - meanX;
217 y1 = yData.get(i) - meanY;
218 sumProducts += x1 * y1;
219 sumSquareX += x1 * x1;
220 sumSquareY += y1 * y1;
221 }
222
223 //Calculate gradient and intercept of regression line. Calculate covariance.
224
225 gradient = sumProducts / sumSquareX; // Gradient
226 intercept = meanY - (gradient * meanX); // Intercept
227 covariance = sumProducts / (n - 1); // Covariance
228
229 correlationCoefficient = sumProducts / Math.sqrt(sumSquareX * sumSquareY);
230
231 //Calculate residual mean square
232
233 sumResidualsSquared = 0;
234 double residual;
235 for (i = 0; i < n; i++) {
236 residual = yData.get(i) - ((gradient * xData.get(i)) + intercept);
237 sumResidualsSquared += residual * residual;
238 }
239
240 residualMeanSquared = sumResidualsSquared / (n - 2);// Residual Mean Square
241
242 regressionKnown = true;
243 }
244
245
246 }