List of usage examples for org.apache.commons.math.stat.regression OLSMultipleLinearRegression OLSMultipleLinearRegression
OLSMultipleLinearRegression
From source file:dfs.OLSTrendLine.java
@Override public void setValues(double[] y, double[] x) { if (x.length != y.length) { throw new IllegalArgumentException( String.format("The numbers of y and x values must be equal (%d != %d)", y.length, x.length)); }//from w w w . ja v a 2 s .c o m double[][] xData = new double[x.length][]; for (int i = 0; i < x.length; i++) { // the implementation determines how to produce a vector of predictors from a single x xData[i] = xVector(x[i]); } if (logY()) { // in some models we are predicting ln y, so we replace each y with ln y y = Arrays.copyOf(y, y.length); // user might not be finished with the array we were given for (int i = 0; i < x.length; i++) { y[i] = Math.log(y[i]); } } OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression(); ols.setNoIntercept(true); // let the implementation include a constant in xVector if desired ols.newSampleData(y, xData); // provide the data to the model coef = MatrixUtils.createColumnRealMatrix(ols.estimateRegressionParameters()); // get our coefs }
From source file:com.ibm.streamsx.transportation.sfpark.ParkingFill.java
public ParkingFill aggregate(Iterable<ParkingOccupancy> items) { Mean mean = new Mean(); int count = 0; for (ParkingOccupancy occupancy : items) { ospid = occupancy.getOspid();/*from w ww .java 2 s .co m*/ // maintain the last values, as that's all // that matters for parking now! occ = occupancy.getOcc(); oper = occupancy.getOper(); setTs(occupancy.getTs()); if (oper == 0) continue; count++; double fill = ((double) occ) / ((double) oper); mean.increment(fill); } if (ospid == null || oper == 0) { return null; } if (count > 5) { double[] values = new double[count * 2]; int i = 0; for (ParkingOccupancy occupancy : items) { int occl = occupancy.getOcc(); int operl = occupancy.getOper(); long tsl = occupancy.getTs(); if (operl == 0) continue; // y, then x // spaces (y) vs time (x) values[i++] = occl; values[i++] = tsl; } OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression(); ols.newSampleData(values, count, 1); double[] coe = ols.estimateRegressionParameters(); if (coe.length >= 2) setTrend(coe[1] * 1000.0 * 60.0); // cars per minute } fill = (int) (mean.getResult() * 100.0); if (fill > 100) fill = 100; else if (fill < 0) fill = 0; return this; }
From source file:ca.uwaterloo.iss4e.algorithm.PAR.java
public static Double[][] computeParameters(final Double[] readings, int order, int seasons) { Double[][] parameters = new Double[seasons][]; try {//w ww. j ava 2 s . c om double[][] series = makeSeries(readings, seasons); for (int season = 0; season < series.length; ++season) { double[] serie = series[season]; Pair pair = makePair(serie, order); double[] Y = (double[]) pair.getKey(); double[][] X = (double[][]) pair.getValue(); OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression(); // regression.setNoIntercept(true); regression.newSampleData(Y, X); double[] params = regression.estimateRegressionParameters(); Double[] bigParams = new Double[params.length]; for (int i = 0; i < params.length; ++i) { bigParams[i] = Double.valueOf(params[i]); } parameters[season] = bigParams; } } catch (Exception e) { e.printStackTrace(); } return parameters; }
From source file:ca.uwaterloo.iss4e.algorithm.PAR.java
public static List<ArrayListWritable<DoubleWritable>> computeParameters(final List<DoubleWritable> readings, int order, int seasons) { List<ArrayListWritable<DoubleWritable>> parameters = new ArrayList<ArrayListWritable<DoubleWritable>>(); try {/*from w ww . j a v a 2s.c o m*/ double[][] series = makeSeries(readings, seasons); for (int season = 0; season < series.length; ++season) { double[] serie = series[season]; Pair pair = makePair(serie, order); double[] Y = (double[]) pair.getKey(); double[][] X = (double[][]) pair.getValue(); OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression(); // regression.setNoIntercept(true); regression.newSampleData(Y, X); double[] params = regression.estimateRegressionParameters(); //Double[] bigParams = new Double[params.length]; ArrayListWritable<DoubleWritable> bigParams = new ArrayListWritable<DoubleWritable>(); for (int i = 0; i < params.length; ++i) { bigParams.add(new DoubleWritable(params[i])); } parameters.add(bigParams); } } catch (Exception e) { e.printStackTrace(); } return parameters; }
From source file:ca.uwaterloo.iss4e.algorithm.PARX.java
public static double[] computePARModel(final List<Double> readings, final List<Double> temperatures, int currentIndex, int trainingSize, int order, int seasons) throws SMASException { Pair pair = PARX.prepareAllVariables(readings, temperatures, currentIndex, trainingSize, order, seasons); double[] Y = (double[]) pair.getKey(); double[][] X = (double[][]) pair.getValue(); OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression(); regression.setNoIntercept(true);/* ww w . jav a 2 s. co m*/ regression.newSampleData(Y, X); double[] beta = regression.estimateRegressionParameters(); return beta; }
From source file:ca.uwaterloo.iss4e.algorithm.PARX.java
public static double[] getLoadByTemperature(final List<Double> readings, final List<Double> temperatures, int order) throws SMASException { int seasons = 24; Pair pair = PARX.prepareAllVariables(readings, temperatures, readings.size() - 1, readings.size(), order, seasons);//from w w w . j a v a 2 s . co m double[] Y = (double[]) pair.getKey(); double[][] X = (double[][]) pair.getValue(); OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression(); regression.setNoIntercept(true); regression.newSampleData(Y, X); double[] beta = regression.estimateRegressionParameters(); double[] loadByTemp = new double[Y.length * seasons]; int startIndex = readings.size() - Y.length * seasons; for (int i = 0; i < loadByTemp.length; ++i) { double t = temperatures.get(i + startIndex); loadByTemp[i] = beta[order + 0] * (t > 20 ? (t - 20) : 0) + beta[order + 1] * ((5 <= t && t < 16) ? (16 - t) : 0) + beta[order + 2] * (t < 5 ? (5 - t) : 0); } return loadByTemp; }
From source file:ca.uwaterloo.iss4e.algorithm.PAR.java
public static List<ArrayListWritable<DoubleWritable>> computeParameters2(final List<Double> readings, int order, int seasons) { List<ArrayListWritable<DoubleWritable>> parameters = new ArrayList<ArrayListWritable<DoubleWritable>>(); try {/* w w w .j a va2 s . c o m*/ double[][] series = makeSeries2(readings, seasons); for (int season = 0; season < series.length; ++season) { double[] serie = series[season]; Pair pair = makePair(serie, order); double[] Y = (double[]) pair.getKey(); double[][] X = (double[][]) pair.getValue(); OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression(); // regression.setNoIntercept(true); regression.newSampleData(Y, X); double[] params = regression.estimateRegressionParameters(); //Double[] bigParams = new Double[params.length]; ArrayListWritable<DoubleWritable> bigParams = new ArrayListWritable<DoubleWritable>(); for (int i = 0; i < params.length; ++i) { bigParams.add(new DoubleWritable(params[i])); } parameters.add(bigParams); } } catch (Exception e) { e.printStackTrace(); } return parameters; }
From source file:ca.uwaterloo.iss4e.algorithm.PAR.java
public static double[][] computeParameters3(final List<Double> readings, int order, int seasons) { double[][] parameters = new double[seasons][]; try {/*from w w w . j a v a 2 s . c om*/ double[][] series = makeSeries2(readings, seasons); for (int season = 0; season < series.length; ++season) { double[] serie = series[season]; Pair pair = makePair(serie, order); double[] Y = (double[]) pair.getKey(); double[][] X = (double[][]) pair.getValue(); OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression(); // regression.setNoIntercept(true); regression.newSampleData(Y, X); double[] params = regression.estimateRegressionParameters(); parameters[season] = params; } } catch (Exception e) { e.printStackTrace(); } return parameters; }
From source file:org.apache.flink.statistics.regression.OLSTrendLine.java
@Override public void setValues(double[] y, double[] x) { if (x.length != y.length) { throw new IllegalArgumentException( String.format("The numbers of y and x values must be equal (%d != %d)", y.length, x.length)); }/*from www .ja v a 2s . c o m*/ double[][] xData = new double[x.length][]; for (int i = 0; i < x.length; i++) { // the implementation determines how to produce a vector of // predictors from a single x xData[i] = xVector(x[i]); } if (logY()) { // in some models we are predicting ln y, so we replace // each y with ln y y = Arrays.copyOf(y, y.length); // user might not be finished with // the array we were given for (int i = 0; i < x.length; i++) { y[i] = Math.log(y[i]); } } OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression(); ols.newSampleData(y, xData); // provide the data to the model coef = MatrixUtils.createColumnRealMatrix(ols.estimateRegressionParameters()); // get our coefs }
From source file:ru.algorithmist.jquant.math.GrangerTest.java
/** * Returns p-value for Granger causality test. * * @param y - predictable variable/*from w ww.j a v a2 s .c o m*/ * @param x - predictor * @param L - lag, should be 1 or greater. * @return p-value of Granger causality */ public static double granger(double[] y, double[] x, int L) { OLSMultipleLinearRegression h0 = new OLSMultipleLinearRegression(); OLSMultipleLinearRegression h1 = new OLSMultipleLinearRegression(); double[][] laggedY = createLaggedSide(L, y); double[][] laggedXY = createLaggedSide(L, x, y); int n = laggedY.length; h0.newSampleData(strip(L, y), laggedY); h1.newSampleData(strip(L, y), laggedXY); double rs0[] = h0.estimateResiduals(); double rs1[] = h1.estimateResiduals(); double RSS0 = sqrSum(rs0); double RSS1 = sqrSum(rs1); double ftest = ((RSS0 - RSS1) / L) / (RSS1 / (n - 2 * L - 1)); System.out.println(RSS0 + " " + RSS1); System.out.println("F-test " + ftest); FDistribution fDist = new FDistributionImpl(L, n - 2 * L - 1); try { double pValue = 1.0 - fDist.cumulativeProbability(ftest); System.out.println("P-value " + pValue); return pValue; } catch (MathException e) { throw new RuntimeException(e); } }