1 45 46 package org.jfree.data.statistics; 47 48 import org.jfree.data.xy.XYDataset; 49 50 53 public abstract class Regression { 54 55 64 public static double[] getOLSRegression(double[][] data) { 65 66 int n = data.length; 67 if (n < 2) { 68 throw new IllegalArgumentException ("Not enough data."); 69 } 70 71 double sumX = 0; 72 double sumY = 0; 73 double sumXX = 0; 74 double sumXY = 0; 75 for (int i = 0; i < n; i++) { 76 double x = data[i][0]; 77 double y = data[i][1]; 78 sumX += x; 79 sumY += y; 80 double xx = x * x; 81 sumXX += xx; 82 double xy = x * y; 83 sumXY += xy; 84 } 85 double sxx = sumXX - (sumX * sumX) / n; 86 double sxy = sumXY - (sumX * sumY) / n; 87 double xbar = sumX / n; 88 double ybar = sumY / n; 89 90 double[] result = new double[2]; 91 result[1] = sxy / sxx; 92 result[0] = ybar - result[1] * xbar; 93 94 return result; 95 96 } 97 98 108 public static double[] getOLSRegression(XYDataset data, int series) { 109 110 int n = data.getItemCount(series); 111 if (n < 2) { 112 throw new IllegalArgumentException ("Not enough data."); 113 } 114 115 double sumX = 0; 116 double sumY = 0; 117 double sumXX = 0; 118 double sumXY = 0; 119 for (int i = 0; i < n; i++) { 120 double x = data.getXValue(series, i); 121 double y = data.getYValue(series, i); 122 sumX += x; 123 sumY += y; 124 double xx = x * x; 125 sumXX += xx; 126 double xy = x * y; 127 sumXY += xy; 128 } 129 double sxx = sumXX - (sumX * sumX) / n; 130 double sxy = sumXY - (sumX * sumY) / n; 131 double xbar = sumX / n; 132 double ybar = sumY / n; 133 134 double[] result = new double[2]; 135 result[1] = sxy / sxx; 136 result[0] = ybar - result[1] * xbar; 137 138 return result; 139 140 } 141 142 151 public static double[] getPowerRegression(double[][] data) { 152 153 int n = data.length; 154 if (n < 2) { 155 throw new IllegalArgumentException ("Not enough data."); 156 } 157 158 double sumX = 0; 159 double sumY = 0; 160 double sumXX = 0; 161 double sumXY = 0; 162 for (int i = 0; i < n; i++) { 163 double x = Math.log(data[i][0]); 164 double y = Math.log(data[i][1]); 165 sumX += x; 166 sumY += y; 167 double xx = x * x; 168 sumXX += xx; 169 double xy = x * y; 170 sumXY += xy; 171 } 172 double sxx = sumXX - (sumX * sumX) / n; 173 double sxy = sumXY - (sumX * sumY) / n; 174 double xbar = sumX / n; 175 double ybar = sumY / n; 176 177 double[] result = new double[2]; 178 result[1] = sxy / sxx; 179 result[0] = Math.pow(Math.exp(1.0), ybar - result[1] * xbar); 180 181 return result; 182 183 } 184 185 195 public static double[] getPowerRegression(XYDataset data, int series) { 196 197 int n = data.getItemCount(series); 198 if (n < 2) { 199 throw new IllegalArgumentException ("Not enough data."); 200 } 201 202 double sumX = 0; 203 double sumY = 0; 204 double sumXX = 0; 205 double sumXY = 0; 206 for (int i = 0; i < n; i++) { 207 double x = Math.log(data.getXValue(series, i)); 208 double y = Math.log(data.getYValue(series, i)); 209 sumX += x; 210 sumY += y; 211 double xx = x * x; 212 sumXX += xx; 213 double xy = x * y; 214 sumXY += xy; 215 } 216 double sxx = sumXX - (sumX * sumX) / n; 217 double sxy = sumXY - (sumX * sumY) / n; 218 double xbar = sumX / n; 219 double ybar = sumY / n; 220 221 double[] result = new double[2]; 222 result[1] = sxy / sxx; 223 result[0] = Math.pow(Math.exp(1.0), ybar - result[1] * xbar); 224 225 return result; 226 227 } 228 229 } 230 | Popular Tags |