TAGraph { static double[] getRegressionWithSigma(XYDataset data, int series) { int n = data.getItemCount(series); if (n < 2) { throw new IllegalArgumentException("Not enough data."); } double sumX = 0; double sumY = 0; for (int i = 0; i < n; i++) { double x = data.getXValue(series, i); double y = data.getYValue(series, i); sumX += x; sumY += y; } double xMean = sumX / n; double sumT2 = 0; double sumB = 0; for (int i = 0; i < n; i++) { double x = data.getXValue(series, i); double y = data.getYValue(series, i); double t = x - xMean; sumT2 += t * t; sumB += t * y; } double b = sumB / sumT2; double a = (sumY - sumX * b) / n; double sdeva = Math.sqrt((1.0 + sumX * sumX / (n * sumT2)) / n); double sdevb = Math.sqrt(1.0 / sumT2); double sumChiSqr = 0.0; for (int i = 0; i < n; i++) { double x = data.getXValue(series, i); double y = data.getYValue(series, i); double chi = y - a - b * x; sumChiSqr += chi * chi; } double sdevdat = Math.sqrt(sumChiSqr / (n - 2)); sdeva = sdeva * sdevdat; sdevb = sdevb * sdevdat; return new double[]{a, b, sdeva, sdevb}; } TAGraph(TAResult taResult); JFreeChart createAnomalyGraph(String regionName, int featureIndex); JFreeChart createYearlyCyclGaph(String regionName, int featureIndex); JFreeChart createTimeseriesGaph(String regionName, int featureIndex); JFreeChart createTimeseriesSigmaGraph(String regionName, int featureIndex, int sigmaIndex); static void writeChart(JFreeChart chart, OutputStream outputStream); static void main(String[] args); static final String DATE_PATTERN; static final DateFormat DATE_FORMAT; }
@Test public void testGetRegressionWithSigma() throws Exception { double[][] data = { {-3.20, 4.49, -1.66, 0.64, -2.43, -0.89, -0.12, 1.41, 2.95, 2.18, 3.72, 5.26}, {-7.14, -1.30, -4.26, -1.90, -6.19, -3.98, -2.87, -1.66, -0.78, -2.61, 0.31, 1.74} }; DefaultXYDataset xyDataset = new DefaultXYDataset(); xyDataset.addSeries("", data); double[] coeffs = TAGraph.getRegressionWithSigma(xyDataset, 0); assertNotNull(coeffs); assertEquals(4, coeffs.length); assertEquals(-3.44596, coeffs[0], 1E-6); assertEquals(0.867329, coeffs[1], 1E-6); assertEquals(0.282225, coeffs[2], 1E-6); assertEquals(0.099150, coeffs[3], 1E-6); }