/** * Used for calculating top part of simple regression for * beta 1 * * @param vector the x coordinates * @param vector2 the y coordinates * @return the sum of mean differences for the input vectors */ public static double sumOfMeanDifferences(double[] vector, double[] vector2) { double mean = sum(vector) / vector.length; double mean2 = sum(vector2) / vector2.length; double ret = 0; for (int i = 0; i < vector.length; i++) { double vec1Diff = vector[i] - mean; double vec2Diff = vector2[i] - mean2; ret += vec1Diff * vec2Diff; } return ret; }//end sumOfMeanDifferences
/** * This returns the minimized loss values for a given vector. * It is assumed that the x, y pairs are at * vector[i], vector[i+1] * * @param vector the vector of numbers to getFromOrigin the weights for * @return a double array with w_0 and w_1 are the associated indices. */ public static double[] weightsFor(double[] vector) { /* split coordinate system */ List<double[]> coords = coordSplit(vector); /* x vals */ double[] x = coords.get(0); /* y vals */ double[] y = coords.get(1); double meanX = sum(x) / x.length; double meanY = sum(y) / y.length; double sumOfMeanDifferences = sumOfMeanDifferences(x, y); double xDifferenceOfMean = sumOfMeanDifferencesOnePoint(x); double w_1 = sumOfMeanDifferences / xDifferenceOfMean; double w_0 = meanY - (w_1) * meanX; double[] ret = new double[vector.length]; ret[0] = w_0; ret[1] = w_1; return ret; }//end weightsFor
/** * This returns the minimized loss values for a given vector. * It is assumed that the x, y pairs are at * vector[i], vector[i+1] * @param vector the vector of numbers to getFromOrigin the weights for * @return a double array with w_0 and w_1 are the associated indices. */ public static double[] weightsFor(double[] vector) { /* split coordinate system */ List<double[]> coords=coordSplit(vector); /* x vals */ double[] x=coords.get(0); /* y vals */ double[] y=coords.get(1); double meanX=sum(x)/x.length; double meanY=sum(y)/y.length; double sumOfMeanDifferences=sumOfMeanDifferences(x,y); double xDifferenceOfMean=sumOfMeanDifferencesOnePoint(x); double w_1=sumOfMeanDifferences/xDifferenceOfMean; double w_0=meanY - (w_1) * meanX; double[] ret = new double[vector.length]; ret[0]=w_0; ret[1]=w_1; return ret; }//end weightsFor
/** * Used for calculating top part of simple regression for * beta 1 * @param vector the x coordinates * @param vector2 the y coordinates * @return the sum of mean differences for the input vectors */ public static double sumOfMeanDifferences(double[] vector,double[] vector2) { double mean=sum(vector)/vector.length; double mean2=sum(vector2)/vector2.length; double ret=0; for(int i=0;i<vector.length;i++) { double vec1Diff=vector[i]-mean; double vec2Diff=vector2[i]-mean2; ret+=vec1Diff * vec2Diff; } return ret; }//end sumOfMeanDifferences
/** * This returns the minimized loss values for a given vector. * It is assumed that the x, y pairs are at * vector[i], vector[i+1] * * @param vector the vector of numbers to getFromOrigin the weights for * @return a double array with w_0 and w_1 are the associated indices. */ public static double[] weightsFor(List<Double> vector) { /* split coordinate system */ List<double[]> coords = coordSplit(vector); /* x vals */ double[] x = coords.get(0); /* y vals */ double[] y = coords.get(1); double meanX = sum(x) / x.length; double meanY = sum(y) / y.length; double sumOfMeanDifferences = sumOfMeanDifferences(x, y); double xDifferenceOfMean = sumOfMeanDifferencesOnePoint(x); double w_1 = sumOfMeanDifferences / xDifferenceOfMean; double w_0 = meanY - (w_1) * meanX; //double w_1=(n*sumOfProducts(x,y) - sum(x) * sum(y))/(n*sumOfSquares(x) - Math.pow(sum(x),2)); // double w_0=(sum(y) - (w_1 * sum(x)))/n; double[] ret = new double[vector.size()]; ret[0] = w_0; ret[1] = w_1; return ret; }//end weightsFor
/** * How much of the variance is explained by the regression * @param residuals error * @param targetAttribute data for target attribute * @return the sum squares of regression */ public static double ssReg(double[] residuals,double[] targetAttribute) { double mean=sum(targetAttribute)/targetAttribute.length; double ret=0; for(int i=0;i<residuals.length;i++) { ret+=Math.pow(residuals[i]-mean,2); } return ret; }
/** * This returns the minimized loss values for a given vector. * It is assumed that the x, y pairs are at * vector[i], vector[i+1] * @param vector the vector of numbers to getFromOrigin the weights for * @return a double array with w_0 and w_1 are the associated indices. */ public static double[] weightsFor(List<Double> vector) { /* split coordinate system */ List<double[]> coords=coordSplit(vector); /* x vals */ double[] x=coords.get(0); /* y vals */ double[] y=coords.get(1); double meanX=sum(x)/x.length; double meanY=sum(y)/y.length; double sumOfMeanDifferences=sumOfMeanDifferences(x,y); double xDifferenceOfMean=sumOfMeanDifferencesOnePoint(x); double w_1=sumOfMeanDifferences/xDifferenceOfMean; double w_0=meanY - (w_1) * meanX; //double w_1=(n*sumOfProducts(x,y) - sum(x) * sum(y))/(n*sumOfSquares(x) - Math.pow(sum(x),2)); // double w_0=(sum(y) - (w_1 * sum(x)))/n; double[] ret = new double[vector.size()]; ret[0]=w_0; ret[1]=w_1; return ret; }//end weightsFor
/** * Used for calculating top part of simple regression for * beta 1 * @param vector the x coordinates * @return the sum of mean differences for the input vectors */ public static double sumOfMeanDifferencesOnePoint(double[] vector) { double mean=sum(vector)/vector.length; double ret=0; for(int i=0;i<vector.length;i++) { double vec1Diff=Math.pow(vector[i]-mean,2); ret+=vec1Diff; } return ret; }//end sumOfMeanDifferences
/** * How much of the variance is explained by the regression * * @param residuals error * @param targetAttribute data for target attribute * @return the sum squares of regression */ public static double ssReg(double[] residuals, double[] targetAttribute) { double mean = sum(targetAttribute) / targetAttribute.length; double ret = 0; for (int i = 0; i < residuals.length; i++) { ret += Math.pow(residuals[i] - mean, 2); } return ret; }
/** * Used for calculating top part of simple regression for * beta 1 * * @param vector the x coordinates * @return the sum of mean differences for the input vectors */ public static double sumOfMeanDifferencesOnePoint(double[] vector) { double mean = sum(vector) / vector.length; double ret = 0; for (int i = 0; i < vector.length; i++) { double vec1Diff = Math.pow(vector[i] - mean, 2); ret += vec1Diff; } return ret; }//end sumOfMeanDifferences
public static double[] normalizeToOne(double[] doubles) { normalize(doubles, sum(doubles)); return doubles; }
public static double[] normalizeToOne( double[] doubles ) { normalize( doubles , sum( doubles ) ); return doubles; }