List of usage examples for org.apache.commons.math3.stat.regression OLSMultipleLinearRegression setNoIntercept
public void setNoIntercept(boolean noIntercept)
From source file:modelcreation.ModelCreation.java
/** * @param args the command line arguments *///from w ww. j av a 2 s . c o m public static void main(String[] args) { int size = writeDataIntoFile(); double[][] x = new double[size][2]; double[] y = new double[size]; readDataFromFile(x, y); // TTest tTest = new TTest(); // System.out.println("p value for home value = " + tTest.tTest(x[0], y)); // System.out.println("p value for away value = " + tTest.tTest(x[1], y)); // System.out.println("Average mean squared error: " + apply10FoldCrossValidation(x, y)); // double[] predictions = new double[size]; // for (int i = 0; i < size; i++) { // predictions[i] = 0.5622255342802198 + (1.0682845275289186E-9 * x[i][0]) + (-9.24614306976538E-10 * x[i][1]); // // //System.out.print("Actual: " + y[i]); // //System.out.println(" Predicted: " + predicted); // } // // System.out.println(calculateMeanSquaredError(y, predictions)); OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression(); regression.newSampleData(y, x); regression.setNoIntercept(true); printRegressionStatistics(regression); //Team[] teams2014 = getTeams(354); //Team[] teams2015 = getTeams(398, 2015); //Team[] teams = concatTeams(teams2014, teams2015); // HashMap<Integer, ArrayList<Integer>> marketValueGoalsDataset = createMarketValueGoalsDataset(teams2014); // // SimpleRegression regression = new SimpleRegression(); // // Set<Integer> marketValues = marketValueGoalsDataset.keySet(); // for (Integer marketValue:marketValues) { // ArrayList<Integer> goals = marketValueGoalsDataset.get(marketValue); // int totalGoals = 0; // for(Integer goal:goals) { // regression.addData(marketValue, goal); // totalGoals += goal; // } // double avg = (double) totalGoals / goals.size(); // System.out.println("Team Value: " + marketValue + ", Goal Average: " + avg); // } // // System.out.println("Intercept: " + regression.getIntercept()); // System.out.println("Slope: " + regression.getSlope()); // System.out.println("R^2: " + regression.getRSquare()); //LinearRegression.calculateLinearRegression(marketValueGoalsDataset); }
From source file:modelcreation.ModelCreation.java
public static double apply10FoldCrossValidation(double[][] x, double[] y) { int subSize = y.length / 10; ArrayList<Integer> indeces = new ArrayList(); for (int i = 0; i < y.length; i++) { indeces.add(i);// www . j a v a 2s .c om } Collections.shuffle(indeces); double[] meanSquaredErrors = new double[10]; int count = 0; for (int i = 0; i < 10; i++) { System.out.println("-------------Fold " + i + "--------------"); double[][] subXTest = new double[subSize][2]; double[] subYTest = new double[subSize]; double[][] subXTraining = new double[y.length - subSize][2]; double[] subYTraining = new double[y.length - subSize]; for (int j = 0; j < i * subSize; j++) { int index = indeces.get(count); count++; subXTraining[j][0] = x[index][0]; subXTraining[j][1] = x[index][1]; subYTraining[j] = y[index]; } for (int j = 0; j < subSize; j++) { int index = indeces.get(count); count++; subXTest[j][0] = x[index][0]; subXTest[j][1] = x[index][1]; subYTest[j] = y[index]; } for (int j = i * subSize; j < y.length - subSize; j++) { int index = indeces.get(count); count++; subXTraining[j][0] = x[index][0]; subXTraining[j][1] = x[index][1]; subYTraining[j] = y[index]; } count = 0; OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression(); regression.newSampleData(subYTraining, subXTraining); regression.setNoIntercept(true); meanSquaredErrors[i] = evaluateModel(regression, subXTest, subYTest); } double sum = 0; for (int i = 0; i < meanSquaredErrors.length; i++) { sum += meanSquaredErrors[i]; } return (double) sum / meanSquaredErrors.length; }
From source file:org.hawkular.datamining.forecast.stats.AugmentedDickeyFullerTest.java
private void calculateStatistics(double[] x) { double[] differences = TimeSeriesDifferencing.differencesAtLag(x, 1); double[] dependedVariable = Arrays.copyOfRange(differences, maxLag, differences.length); double[][] explanatoryVariables = explanatoryVariables(x, differences); // OLS model/*from w w w . j a va2 s.c om*/ OLSMultipleLinearRegression olsMultipleLinearRegression = new OLSMultipleLinearRegression(); olsMultipleLinearRegression.setNoIntercept(type == Type.NoInterceptNoTimeTrend); olsMultipleLinearRegression.newSampleData(dependedVariable, explanatoryVariables); double[] parameters = olsMultipleLinearRegression.estimateRegressionParameters(); double[] standardErrors = olsMultipleLinearRegression.estimateRegressionParametersStandardErrors(); // first parameter is intercept, second gamma*(lagged xt) int gammaIndex = (type == Type.NoInterceptNoTimeTrend) ? 0 : 1; adfStat = parameters[gammaIndex] / standardErrors[gammaIndex]; }
From source file:org.meteoinfo.math.fitting.OLSTrendLine.java
@Override public void setValues(Array y, Array x) { if (x.getSize() != y.getSize()) { throw new IllegalArgumentException(String .format("The numbers of y and x values must be equal (%d != %d)", y.getSize(), x.getSize())); }//from ww w .j a va2s . c o m double[][] xData = new double[(int) x.getSize()][]; for (int i = 0; i < x.getSize(); i++) { // the implementation determines how to produce a vector of predictors from a single x xData[i] = xVector(x.getDouble(i)); } double[] yy = new double[(int) y.getSize()]; if (logY()) { // in some models we are predicting ln y, so we replace each y with ln y for (int i = 0; i < yy.length; i++) { if (i < x.getSize()) yy[i] = Math.log(y.getDouble(i)); else yy[i] = y.getDouble(i); } } else { for (int i = 0; i < yy.length; i++) { yy[i] = y.getDouble(i); } } // double[] yy = (double[])y.copyTo1DJavaArray(); // if(logY()) { // in some models we are predicting ln y, so we replace each y with ln y // yy = Arrays.copyOf(yy, yy.length); // user might not be finished with the array we were given // for (int i = 0; i < x.getSize(); i++) { // yy[i] = Math.log(yy[i]); // } // } OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression(); ols.setNoIntercept(true); // let the implementation include a constant in xVector if desired ols.newSampleData(yy, xData); // provide the data to the model coef = MatrixUtils.createColumnRealMatrix(ols.estimateRegressionParameters()); // get our coefs rs = ols.calculateRSquared(); }
From source file:org.spotter.ext.detection.trendlines.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)); }// w w w .j av a2 s . c om 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:uk.ac.cam.cl.dtg.teaching.programmingtest.java.FittedCurve.java
private double fit(List<Point> mapped, double proportion) { Collections.shuffle(mapped);/*from w w w .j a v a 2 s. c o m*/ int total = (int) (mapped.size() * proportion); double[][] x = new double[total][]; double[] y = new double[total]; Iterator<Point> step = mapped.iterator(); for (int i = 0; i < total; ++i) { Point next = step.next(); x[i] = next.xcoeff; y[i] = next.yvalue; } OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression(); ols.setNoIntercept(true); ols.newSampleData(y, x); RealMatrix coef = MatrixUtils.createColumnRealMatrix(ols.estimateRegressionParameters()); double rsqTotal = 0.0; for (Point p : mapped) { double yhat = coef.preMultiply(p.xcoeff)[0]; rsqTotal += unmapY(Math.pow(p.yvalue - yhat, 2.0)); } if (DEBUG) { List<Point> newMaps = mapValues(this.xs, this.ys); LinkedList<Double> xs = new LinkedList<>(); LinkedList<Double> ys = new LinkedList<>(); int i = 0; for (Point p : newMaps) { double yhat = unmapY(coef.preMultiply(p.xcoeff)[0]); ys.addLast(yhat); xs.addLast(this.xs[i++]); } System.out.println("x=[" + Joiner.on(",").join(xs) + "]"); System.out.println("y=[" + Joiner.on(",").join(ys) + "]"); } return rsqTotal; }
From source file:uk.ac.sanger.mig.analysis.maths.trendline.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 ww . j a 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 }