@@ -1,17 +1,14 @@ |
package datadownload; |
- |
import java.io.IOException; |
import java.util.ArrayList; |
import java.util.Arrays; |
import java.util.Comparator; |
-import java.util.Iterator; |
-import java.util.List; |
import org.apache.commons.math.ArgumentOutsideDomainException; |
import org.apache.commons.math.analysis.interpolation.SplineInterpolator; |
import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction; |
|
/** |
-* Last Updated: 23-June-2014 |
+* Last Updated: 13-July-2015 |
* @author Tyler Wible |
* @since 21-June-2012 |
*/ |
@@ -26,6 +23,13 @@ |
} |
} |
public class DoubleMath{ |
+ private double[] convertArray(ArrayList<Double> array){ |
+ double[] newArray = new double[array.size()]; |
+ for(int i=0; i<array.size(); i++){ |
+ newArray[i] = array.get(i); |
+ } |
+ return newArray; |
+ } |
/** |
* Finds the maximum value of a double[] array |
* @param array the double[] array |
@@ -50,32 +54,8 @@ |
* @return the maximum of the above array as a double |
*/ |
public double max(ArrayList<Double> array){ |
- double maximum = -9999; |
- for(int i=0; i<array.size(); i++){ |
- if(i == 0){ |
- maximum = array.get(i); |
- }else{ |
- if(Double.compare(maximum, array.get(i)) < 0){ |
- maximum = array.get(i); |
- } |
- } |
- } |
- return maximum; |
- } |
- /** |
- * Finds the maximum value of a double[][] array |
- * @param array the double[][] array |
- * @return the maximum of the above array as a double |
- */ |
- public double max(double[][] array){ |
- double maximum = -9999; |
- for(int i=0; i<array.length; i++){ |
- for(int j=0; j<array[i].length; j++){ |
- if(Double.compare(maximum, array[i][j]) < 0){ |
- maximum = array[i][j]; |
- } |
- } |
- } |
+ double[] newArray = convertArray(array); |
+ double maximum = max(newArray); |
return maximum; |
} |
/** |
@@ -102,58 +82,31 @@ |
* @return the minimum of the above array as a double |
*/ |
public double min(ArrayList<Double> array){ |
- double minimum = -9999; |
- for(int i=0; i<array.size(); i++){ |
- if(i == 0){ |
- minimum = array.get(i); |
- }else{ |
- if(Double.compare(minimum, array.get(i)) > 0){ |
- minimum = array.get(i); |
- } |
- } |
- } |
- return minimum; |
- } |
- /** |
- * Finds the minimum value of a double[][] array |
- * @param array the double[][] array |
- * @return the minimum of the above array as a double |
- */ |
- public double min(double[][] array){ |
- double minimum = array[0][0]; |
- for(int i=0; i<array.length; i++){ |
- for(int j=0; j<array[i].length; j++){ |
- if(Double.compare(minimum, array[i][j]) > 0){ |
- minimum = array[i][j]; |
- } |
- } |
- } |
+ double[] newArray = convertArray(array); |
+ double minimum = min(newArray); |
return minimum; |
} |
/** |
* Calculates the sum of a double[] array |
- * @param dataArray the array to be summed |
+ * @param array the array to be summed |
* @return the sum of the array |
*/ |
- public double sum(double[] dataArray){ |
+ public double sum(double[] array){ |
double sum = 0; |
//Calculate the sum of the array |
- for(int i=0; i<dataArray.length; i++){ |
- sum = sum + dataArray[i]; |
+ for(int i=0; i<array.length; i++){ |
+ sum = sum + array[i]; |
} |
return sum; |
} |
/** |
* Calculates the sum of a ArrayList<Double> |
- * @param dataArray the array to be summed |
+ * @param array the array to be summed |
* @return the sum of the array |
*/ |
- public double sum(ArrayList<Double> dataArray){ |
- double sum = 0; |
- //Calculate the sum of the array |
- for(int i=0; i<dataArray.size(); i++){ |
- sum = sum + dataArray.get(i); |
- } |
+ public double sum(ArrayList<Double> array){ |
+ double[] newArray = convertArray(array); |
+ double sum = sum(newArray); |
return sum; |
} |
/** |
@@ -173,28 +126,28 @@ |
/** |
* Rounds the double[] array to the number of decimal places "n" where roundingValue = 10^n |
* For example if the roundingValue is equal to 10, then the returned matrix would contain the original matrix with 1 decimal place |
- * @param originalColumn the original double[] matrix to be rounded |
+ * @param array the original double[] matrix to be rounded |
* @param roundingValue is equal to the 10^number of decimal places desired (aka 2 decimal places has a roundingValue = 10) |
* @return a double[] array of the rounded values of the original data |
*/ |
- public double[] roundColumn(double[] originalColumn, double roundingValue){ |
- double[] roundedColumn = new double[originalColumn.length]; |
- for(int i=0; i<originalColumn.length; i++){ |
- double tempValue = Math.round(originalColumn[i]*roundingValue); |
- roundedColumn[i] = tempValue/roundingValue; |
+ public double[] roundColumn(double[] array, double roundingValue){ |
+ double[] roundedArray = new double[array.length]; |
+ for(int i=0; i<array.length; i++){ |
+ double tempValue = Math.round(array[i]*roundingValue); |
+ roundedArray[i] = tempValue/roundingValue; |
} |
|
- return roundedColumn; |
+ return roundedArray; |
} |
/** |
- * Calculates the average of the provided double[] array |
- * @param data the double[] array of which the average is desired |
- * @return the double value of the average of the data array |
+ * Calculates the arithmetic mean of the provided double[] array |
+ * @param array the double[] array of which the arithmetic mean is desired |
+ * @return the double value of the arithmetic mean of the data array |
*/ |
- public double Average(double[] data){ |
+ public double meanArithmetic(double[] array){ |
//Calculates the average of a double array |
- double sum = sum(data); |
- double count = data.length; |
+ double sum = sum(array); |
+ double count = array.length; |
if(count == 0){//fix divide by zero errors |
count = 1; |
} |
@@ -203,71 +156,94 @@ |
return average; |
} |
/** |
- * Calculates the average of the provided ArrayList<Double> |
- * @param data the ArrayList<Double> of which the average is desired |
- * @return the double value of the average of the data array |
+ * Calculates the arithmetic mean of the provided ArrayList<Double> |
+ * @param array the ArrayList<Double> of which the arithmetic mean is desired |
+ * @return the double value of the arithmetic mean of the data array |
*/ |
- public double Average(ArrayList<Double> data){ |
- //Calculates the average of a double array |
- double sum = sum(data); |
- double count = data.size(); |
- double average = sum/count; |
- |
+ public double meanArithmetic(ArrayList<Double> array){ |
+ double[] newArray = convertArray(array); |
+ double average = meanArithmetic(newArray); |
return average; |
} |
/** |
+ * Calculates the harmonic mean of the provided double[] array, |
+ * only calculates for real positive values. |
+ * |
+ * Note that the final estimate of the harmonic mean is a weighted average |
+ * of the harmonic mean of the non-zero elements and zero. |
+ * @param array the double[] array of which the harmonic mean is desired |
+ * @return the double value of the harmonic mean of the data array |
+ */ |
+ public double meanHarmonic(double[] array){ |
+ //Calculate properties of harmonic mean |
+ double reciprocalSum = 0, nZeros = 0, nData = 0; |
+ for(int i=0; i<array.length; i++){ |
+ if(array[i] > 0){ |
+ reciprocalSum = reciprocalSum + (1/array[i]);//sum of reciprocals |
+ }else{ |
+ nZeros++; |
+ } |
+ nData++; |
+ } |
+ |
+ //Compute harmonic mean (with correction for the number of zero items in the array) |
+ double meanHarmonic = (nData - nZeros) / (reciprocalSum * ((nData - nZeros)/nData)); |
+ return meanHarmonic; |
+ } |
+ /** |
+ * Calculates the harmonic mean of the provided ArrayList<Double> array, |
+ * only calculates for real positive values. |
+ * |
+ * Note that the final estimate of the harmonic mean is a weighted average |
+ * of the harmonic mean of the non-zero elements and zero. |
+ * @param array the ArrayList<Double> array of which the harmonic mean is desired |
+ * @return the double value of the harmonic mean of the data array |
+ */ |
+ public double meanHarmonic(ArrayList<Double> array){ |
+ double[] newArray = convertArray(array); |
+ double meanHarmonic = meanHarmonic(newArray); |
+ return meanHarmonic; |
+ } |
+ /** |
* Finds the average of the Log10 of a double[] array |
- * @param data the double[] array that the average is desired for |
+ * @param array the double[] array that the average is desired for |
* @return the double value of the average of the Log10 of the data array |
*/ |
- public double Average_Log10(double[] data){ |
+ public double Average_Log10(double[] array){ |
//Matlab code: Xmean = mean(log10(data)); |
- |
- //Finds the average of the Log10 of a double array |
- double averageLog10 = 0; |
- |
- double sum = 0; |
- double ctr = data.length; |
- if(ctr == 0){//fix divide by zero errors |
- ctr = 1; |
- } |
- for(int i=0; i<data.length; i++){//Loop through rows and get average of Log10 of rows |
- sum = sum + Math.log10(data[i]); |
- } |
- |
- averageLog10 = sum/ctr; |
- |
+ double[] log10array = Log10(array); |
+ double averageLog10 = meanArithmetic(log10array); |
return averageLog10; |
} |
/** |
* Calculates and returns the Java Math.Log10 value of each element in the double[] data array |
- * @param data the double[] array containing the pre-Log10 values |
+ * @param array the double[] array containing the pre-Log10 values |
* @return the resulting double[] array containing the Log10 values of the "data" array |
*/ |
- public double[] Log10(double[] data){ |
+ public double[] Log10(double[] array){ |
//Matlab code: X = log10(data); |
|
//Returns an array containing the Log10 value of each element in the data array |
- double[] log10Array = new double[data.length]; |
- for(int i=0; i<data.length; i++){//Loop through rows |
- log10Array[i] = Math.log10(data[i]); |
+ double[] log10Array = new double[array.length]; |
+ for(int i=0; i<array.length; i++){//Loop through rows |
+ log10Array[i] = Math.log10(array[i]); |
} |
|
return log10Array; |
} |
/** |
- * Calculates and returns the Java Math.Log10 value of the second column in the double[all][column] data array |
- * @param data the double[][] array containing the pre-Log10 values |
+ * Calculates and returns the Java Math.Log10 value of the specified column in the double[all][column] data array |
+ * @param array the double[][] array containing the pre-Log10 values |
* @param column the column of data to be calculated with |
* @return the resulting double[] array containing the Log10 values of the "data" array |
*/ |
- public double[] Log10(double[][] data, int column){ |
+ public double[] Log10(double[][] array, int column){ |
//Matlab code: X = log10(data); |
|
//Returns an array containing the Log10 value of each element in the data array |
- double[] log10Array = new double[data.length]; |
- for(int i=0; i<data.length; i++){//Loop through rows |
- log10Array[i] = Math.log10(data[i][column]); |
+ double[] log10Array = new double[array.length]; |
+ for(int i=0; i<array.length; i++){//Loop through rows |
+ log10Array[i] = Math.log10(array[i][column]); |
} |
|
return log10Array; |
@@ -281,18 +257,10 @@ |
* @throws ArgumentOutsideDomainException |
*/ |
public double[] splineInterpolation(double[] Xarray, double[] Yarray, double[] xarray) throws ArgumentOutsideDomainException{ |
- //Set up the spline interplator |
- SplineInterpolator splineInterp2 = new SplineInterpolator(); |
- PolynomialSplineFunction splineFunction2 = splineInterp2.interpolate(Xarray, Yarray); |
- |
//Interpolate each y point for each provided x point using the above interpolatoer |
double[] yarray = new double[xarray.length]; |
for(int i=0; i<xarray.length; i++){ |
- try{ |
- yarray[i] = splineFunction2.value(xarray[i]); |
- }catch(ArgumentOutsideDomainException e){ |
- yarray[i] = 0; |
- } |
+ yarray[i] = splineInterpolation(Xarray, Yarray, xarray[i]); |
} |
return yarray; |
} |
@@ -321,9 +289,8 @@ |
* @return a double[] array of y points corresponding to the array of x points of the xArray after a linear interpolation between Xarray and Yarray |
*/ |
public double[] linearInterpolation(double[] Xarray, double[] Yarray, double[] xArray){ |
+ //Interpolate a y value for each x value in xArray |
double[] yArray = new double[xArray.length]; |
- |
- //Interpolate a y value for each x value in xArray |
for(int i=0; i<xArray.length; i++){ |
yArray[i] = linearInterpolation(Xarray, Yarray, xArray[i]); |
} |
@@ -358,7 +325,7 @@ |
if(i == 0 && xValue < Xarray[0]){ |
//If xValue is smaller than the first Xarray value, extrapolate based on the slope bewteen the first two points in Xarray and Yarray |
yValue = ((Yarray[i+1] - Yarray[i])/(Xarray[i+1] - Xarray[i]))*(xValue - Xarray[i]) + Yarray[i]; |
- System.err.println("The x value: " + xValue + " is smaller than the smallest provided X value:" + Xarray[0] + ". Therefore it's corresponding y value: " + yValue + " was extrapolated from the dataset."); |
+ System.err.println("The x value: " + xValue + " is smaller than the smallest provided X value: " + Xarray[0] + ". Therefore it's corresponding y value: " + yValue + " was extrapolated from the dataset."); |
break; |
|
}else if(xValue == Xarray[i]){ |
@@ -381,48 +348,48 @@ |
} |
/** |
* Sub-function to calculate a percentile of a dataset |
- * @param dataList input list data for percentile (double[]) |
+ * @param array input list data for percentile (double[]) |
* @param percentile_type which percentile value is desired 0.25, or 0.95, etc. |
* @return percentile of the dataset. |
*/ |
- public double Percentile_function (double[] dataList, double percentile_type){ |
+ public double Percentile_function (double[] array, double percentile_type){ |
//Sort Data |
- Arrays.sort(dataList); |
+ Arrays.sort(array); |
double percentile = -9999; |
//return quartiles for small datasets early, if not small, then continue to find which percentile is asked for |
- if(dataList.length == 0){ |
+ if(array.length == 0){ |
percentile = -9999; |
- }else if(dataList.length == 1){ |
- percentile = dataList[0]; |
+ }else if(array.length == 1){ |
+ percentile = array[0]; |
}else if(Double.compare(percentile_type, 0.5) == 0){ |
//Find median of dataset |
- percentile = Median(dataList); |
+ percentile = Median(array); |
}else{ |
//Find rank of the desired percentile |
- double rank = percentile_type*(dataList.length + 1);//ex: 0.95*(n + 1) = rank of 95th percentile |
+ double rank = percentile_type*(array.length + 1);//ex: 0.95*(n + 1) = rank of 95th percentile |
try{ |
int rank_int = Integer.parseInt(String.valueOf(rank)); |
//If the rank is an integer find the value corresponding to it |
- for(int i=0; i<dataList.length; i++){ |
+ for(int i=0; i<array.length; i++){ |
if(i+1 == rank_int){//i+1 is to compensate for Java's zero-based indexing system |
- percentile = dataList[i]; |
+ percentile = array[i]; |
break; |
} |
} |
|
}catch(NumberFormatException e){ |
//If the rank is not an integer average it between the two nearest ranks |
- for(int i=0; i<dataList.length; i++){ |
+ for(int i=0; i<array.length; i++){ |
if(i+1 < rank && i+2 >= rank){//i+1 is to compensate for Java's zero-based indexing system |
try{ |
- percentile = (dataList[i] + dataList[i+1])/2; |
+ percentile = (array[i] + array[i+1])/2; |
}catch(IndexOutOfBoundsException err){ |
- percentile = dataList[i]; |
+ percentile = array[i]; |
System.err.println("Insufficient data to calculate the desired percentile accurately"); |
} |
break; |
- }else if(i+1 == dataList.length){ |
- percentile = dataList[i]; |
+ }else if(i+1 == array.length){ |
+ percentile = array[i]; |
System.err.println("Insufficient data to calculate the desired percentile accurately"); |
} |
} |
@@ -432,139 +399,80 @@ |
} |
/** |
* Sub-function to calculate a percentile of a dataset |
- * @param input input list data for percentile (List<Double>) |
+ * @param array input list data for percentile (List<Double>) |
* @param percentile_type which percentile value is desired 0.25, or 0.95, etc. |
* @return percentile of the dataset. |
*/ |
- public double Percentile_function (List<Double> input, double percentile_type){ |
- //Create a double[] list of data from the input ArrayList |
- Iterator<Double> iterate = input.iterator(); |
- double[] dataList = new double[input.size()]; |
- int ctr = 0; |
- while(iterate.hasNext()){ |
- dataList[ctr] = (Double) iterate.next(); |
- ctr++; |
- } |
- |
- //Call the percentile function |
- double percentile = Percentile_function(dataList, percentile_type); |
+ public double Percentile_function (ArrayList<Double> array, double percentile_type){ |
+ double[] newArray = convertArray(array); |
+ double percentile = Percentile_function(newArray, percentile_type); |
return percentile; |
} |
/** |
* Sub-function to calculate the median of a dataset |
- * @param input input list data for median |
+ * @param array input list data for median |
* @return median of the dataset. |
*/ |
- public double Median(double[] input){ |
+ public double Median(double[] array){ |
//sort dataset before calculating median |
- Arrays.sort(input); |
+ Arrays.sort(array); |
|
//Find the median |
- int midpoint = (input.length)/2; |
+ int midpoint = (array.length)/2; |
double median = 0; |
- if(input.length == 0){ |
+ if(array.length == 0){ |
median = -9999; |
- }else if(input.length%2 == 1){ |
- median = input[midpoint]; |
+ }else if(array.length%2 == 1){ |
+ median = array[midpoint]; |
}else{ |
- median = (input[midpoint-1] + input[midpoint])/2; |
+ median = (array[midpoint-1] + array[midpoint])/2; |
} |
return median; |
} |
/** |
* Sub-function to calculate the median of a dataset |
- * @param input input list data for median |
+ * @param array input list data for median |
* @return median of the dataset. |
*/ |
- public double Median(ArrayList<Double> input){ |
- Iterator<Double> iterate = input.iterator(); |
- double[] dataList = new double[input.size()]; |
- int ctr = 0; |
- while(iterate.hasNext()){ |
- dataList[ctr] = (Double) iterate.next(); |
- ctr++; |
- } |
- |
- //Call the median function |
- double median = Median(dataList); |
+ public double Median(ArrayList<Double> array){ |
+ double[] newArray = convertArray(array); |
+ double median = Median(newArray); |
return median; |
} |
/** |
- * Sub-function to find minimum or maximum of a dataset |
- * @param dataList dataset of which the minimum or maximum is desired. |
- * @param maxTrue if true the maximum value is calculated, otherwise the minimum is calculated. |
- * @return the minimum or maximum value |
- */ |
- public double Min_Max(double[] dataList, boolean maxTrue){ |
- double minMax = 0; |
- //sort array |
- Arrays.sort(dataList); |
- if(dataList.length ==0){ |
- //If no data return zero |
- minMax = 0; |
- }else if(maxTrue){ |
- //If maximum, return last value |
- minMax = dataList[dataList.length-1]; |
- }else{ |
- //Otherwise return return first value, minimum |
- minMax = dataList[0]; |
- } |
- return minMax; |
- } |
- /** |
- * Sub-function to find minimum or maximum of a dataset |
- * @param input dataset of which the minimum or maximum is desired. |
- * @param maxTrue if true the maximum value is calculated, otherwise the minimum is calculated. |
- * @return the minimum or maximum value |
- */ |
- public double Min_Max(List<Double> input, boolean maxTrue){ |
- //Create a double[] list of data from the input ArrayList |
- Iterator<Double> iterate = input.iterator(); |
- double[] dataList = new double[input.size()]; |
- int ctr = 0; |
- while(iterate.hasNext()){ |
- dataList[ctr] = (Double) iterate.next(); |
- ctr++; |
- } |
- double minMax = Min_Max(dataList, maxTrue); |
- return minMax; |
- } |
- /** |
* Sub-function to calculate the standard deviation of the population |
- * @param dataList input list data for standard deviation |
+ * @param array input list data for standard deviation |
* @return the population standard deviation of the dataList. |
*/ |
- public double StandardDeviationSample(double[] dataList){ |
- double variance = VarianceSample(dataList); |
+ public double StandardDeviationSample(double[] array){ |
+ double variance = VarianceSample(array); |
double standardDeviation = Math.pow(variance,0.5); |
- |
return standardDeviation; |
} |
/** |
* Sub-function to calculate the standard deviation of a dataset |
- * @param dataList input list data for standard deviation |
+ * @param array input list data for standard deviation |
* @return standard deviation of the dataset. |
*/ |
- public double StandardDeviationSample(ArrayList<Double> dataList){ |
- double variance = VarianceSample(dataList); |
- double standardDeviation = Math.pow(variance,0.5); |
- |
+ public double StandardDeviationSample(ArrayList<Double> array){ |
+ double[] newArray = convertArray(array); |
+ double standardDeviation = StandardDeviationSample(newArray); |
return standardDeviation; |
} |
/** |
* Sub-function to calculate the variance of the population |
- * @param dataList data array for which the variance is to be found |
+ * @param array data array for which the variance is to be found |
* @return variance of the data array |
*/ |
- public double VariancePopulation(double[] dataList){ |
- double average = Average(dataList); |
+ public double VariancePopulation(double[] array){ |
+ double average = meanArithmetic(array); |
//Calculate sum of differences |
double sum = 0; |
- for(int i=0; i<dataList.length; i++){ |
- sum = sum + Math.pow((dataList[i] - average), 2); |
+ for(int i=0; i<array.length; i++){ |
+ sum = sum + Math.pow((array[i] - average), 2); |
} |
//Calculate total number in the set |
- double ctr = (double) dataList.length; |
+ double ctr = (double) array.length; |
if(ctr == 0){//fix divide by zero errors |
ctr = 1; |
} |
@@ -574,45 +482,34 @@ |
} |
/** |
* Sub-function to calculate the variance of the population |
- * @param dataList data array for which the variance is to be found |
+ * @param array data array for which the variance is to be found |
* @return variance of the data array |
*/ |
- public double VariancePopulation(ArrayList<Double> dataList){ |
- double average = Average(dataList); |
- //Calculate sum of differences |
- double sum = 0; |
- for(int i=0; i<dataList.size(); i++){ |
- sum = sum + Math.pow((dataList.get(i) - average), 2); |
- } |
- //Calculate total number in the set |
- double ctr = (double) dataList.size(); |
- if(ctr == 0){//fix divide by zero errors |
- ctr = 1; |
- } |
- double variance = sum/ctr; |
- |
+ public double VariancePopulation(ArrayList<Double> array){ |
+ double[] newArray = convertArray(array); |
+ double variance = VariancePopulation(newArray); |
return variance; |
} |
/** |
* Sub-function to calculate the variance of the population, variance = [1/(n-1)] * sum[(value - average)^2] |
- * @param dataList data array for which the variance is to be found |
+ * @param array data array for which the variance is to be found |
* @return variance of the data array |
*/ |
- public double VarianceSample(double[] dataList){ |
- double average = Average(dataList); |
+ public double VarianceSample(double[] array){ |
+ double average = meanArithmetic(array); |
//Calculate sum of differences |
double sum = 0; |
- for(int i=0; i<dataList.length; i++){ |
- sum = sum + Math.pow((dataList[i] - average), 2); |
+ for(int i=0; i<array.length; i++){ |
+ sum = sum + Math.pow((array[i] - average), 2); |
} |
|
//Calculate total number in the set |
double denominator = 0; |
//fix divide by zero errors |
- if((dataList.length-1) <= 0){ |
+ if((array.length-1) <= 0){ |
denominator = 1; |
}else{ |
- denominator = dataList.length - 1; |
+ denominator = array.length - 1; |
} |
|
//Calculate variance |
@@ -622,51 +519,35 @@ |
} |
/** |
* Sub-function to calculate the variance of the population |
- * @param dataList data array for which the variance is to be found |
+ * @param array data array for which the variance is to be found |
* @return variance of the data array |
*/ |
- public double VarianceSample(ArrayList<Double> dataList){ |
- double average = Average(dataList); |
- //Calculate sum of differences |
- double sum = 0; |
- for(int i=0; i<dataList.size(); i++){ |
- sum = sum + Math.pow((dataList.get(i) - average), 2); |
- } |
- //Calculate total number in the set |
- double denominator = 0; |
- if(dataList.size()-1 <= 0){//fix divide by zero errors |
- denominator = 1; |
- }else{ |
- denominator = dataList.size() - 1; |
- } |
- double variance = sum/denominator; |
- |
+ public double VarianceSample(ArrayList<Double> array){ |
+ double[] newArray = convertArray(array); |
+ double variance = VarianceSample(newArray); |
return variance; |
} |
/** |
* Sub-function to calculate the coefficient of variation of a dataset (Standard deviation / average) |
- * @param dataList input list data for coefficient of variation |
+ * @param array input list data for coefficient of variation |
* @return coefficient of variation of the dataset. |
*/ |
- public double CoefficientOfVariation(double[] dataList){ |
+ public double CoefficientOfVariation(double[] array){ |
//Calculate the average and standard deviation for the coefficient of varience |
- double average = Average(dataList); |
- double stDev = StandardDeviationSample(dataList); |
+ double average = meanArithmetic(array); |
+ double stDev = StandardDeviationSample(array); |
double coefVar = stDev/average; |
|
return coefVar; |
} |
/** |
* Sub-function to calculate the coefficient of variation of a dataset (Standard deviation / average) |
- * @param dataList input list data for coefficient of variation |
+ * @param array input list data for coefficient of variation |
* @return coefficient of variation of the dataset. |
*/ |
- public double CoefficientOfVariation(ArrayList<Double> dataList){ |
- //Calculate the average and standard deviation for the coefficient of varience |
- double average = Average(dataList); |
- double stDev = StandardDeviationSample(dataList); |
- double coefVar = stDev/average; |
- |
+ public double CoefficientOfVariation(ArrayList<Double> array){ |
+ double[] newArray = convertArray(array); |
+ double coefVar = CoefficientOfVariation(newArray); |
return coefVar; |
} |
/** |
@@ -685,8 +566,8 @@ |
double N = xData.length; |
|
//Calculate the average of the two datasets |
- double xBar = Average(xData); |
- double yBar = Average(yData); |
+ double xBar = meanArithmetic(xData); |
+ double yBar = meanArithmetic(yData); |
|
//Sum (Xi - Xbar) * (Yi - Ybar) |
double sum = 0; |
@@ -699,89 +580,65 @@ |
} |
/** |
* Sub-function to calculate the covariance of two datasets |
- * @param xData input list of x data |
- * @param yData input list of y data |
+ * @param xArray input list of x data |
+ * @param yArray input list of y data |
* @return covariance of variation of the dataset. |
* @throws IOException |
*/ |
- public double Covariance(ArrayList<Double> xData, ArrayList<Double> yData) throws IOException{ |
- //Check if arrays are the same size |
- if(xData.size() != yData.size()){ |
- throw(new IOException("Data arrays must be the same size to perform this statistic. X data size:\t" + |
- xData.size() + "\tY data size:\t" + yData.size())); |
- } |
- double N = xData.size(); |
- |
- //Calculate the average of the two datasets |
- double xBar = Average(xData); |
- double yBar = Average(yData); |
- |
- //Sum (Xi - Xbar) * (Yi - Ybar) |
- double sum = 0; |
- for(int i=0; i<N; i++){ |
- sum = sum + ((xData.get(i) - xBar) * (yData.get(i) - yBar)); |
- } |
- double covariance = sum / (N - 1); |
- |
+ public double Covariance(ArrayList<Double> xArray, ArrayList<Double> yArray) throws IOException{ |
+ double[] newXarray = convertArray(xArray); |
+ double[] newYarray = convertArray(yArray); |
+ double covariance = Covariance(newXarray, newYarray); |
return covariance; |
} |
/** |
* Sub-function to calculate the skewness of a dataset |
- * @param dataList input list data for skewness |
+ * @param array input list data for skewness |
* @return skewness of the dataset. |
*/ |
- public double SkewnessPopulation(double[] dataList){ |
+ public double SkewnessPopulation(double[] array){ |
//Get the average and standard deviation for use in the skewness formula |
- double average = Average(dataList); |
- double stDev = StandardDeviationSample(dataList); |
+ double average = meanArithmetic(array); |
+ double stDev = StandardDeviationSample(array); |
|
//Create list of values to compute the expected value (average) of in order to get the skewness |
- double[] list = new double[dataList.length]; |
- for(int i=0; i<dataList.length; i++){ |
- list[i] = Math.pow(((dataList[i] - average)/stDev), 3); |
+ double[] list = new double[array.length]; |
+ for(int i=0; i<array.length; i++){ |
+ list[i] = Math.pow(((array[i] - average)/stDev), 3); |
} |
- double skewness = Average(list); |
+ double skewness = meanArithmetic(list); |
|
return skewness; |
} |
/** |
* Sub-function to calculate the skewness of a dataset |
- * @param dataList input list data for skewness |
+ * @param array input list data for skewness |
* @return skewness of the dataset. |
*/ |
- public double SkewnessPopulation(ArrayList<Double> dataList){ |
- //Get the average and standard deviation for use in the skewness formula |
- double average = Average(dataList); |
- double stDev = StandardDeviationSample(dataList); |
- |
- //Create list of values to compute the expected value (average) of in order to get the skewness |
- double[] list = new double[dataList.size()]; |
- for(int i=0; i<dataList.size(); i++){ |
- list[i] = Math.pow(((dataList.get(i) - average)/stDev), 3); |
- } |
- double skewness = Average(list); |
- |
+ public double SkewnessPopulation(ArrayList<Double> array){ |
+ double[] newArray = convertArray(array); |
+ double skewness = SkewnessPopulation(newArray); |
return skewness; |
} |
/** |
* Sub-function to calculate the sample skewness (Fisher-Pearson skewness) of a dataset. |
* Skewness = (n /((n-1)(n-2))) * sum{ ((x - average)/standard Deviation) ^ 3 } |
- * @param dataList input list data for skewness |
+ * @param array input list data for skewness |
* @return skewness of the dataset. |
*/ |
- public double SkewnessSample(double[] dataList){ |
+ public double SkewnessSample(double[] array){ |
//Get the average and standard deviation for use in the skewness formula |
- double average = Average(dataList); |
- double variance = VarianceSample(dataList); |
- double count = dataList.length; |
+ double average = meanArithmetic(array); |
+ double variance = VarianceSample(array); |
+ double count = array.length; |
if(count == 0){//fix divide by zero errors |
count = 1; |
} |
|
//Create list of values to compute the expected value (average) of in order to get the skewness |
- double[] list = new double[dataList.length]; |
- for(int i=0; i<dataList.length; i++){ |
- list[i] = Math.pow((dataList[i] - average)/variance, 3); |
+ double[] list = new double[array.length]; |
+ for(int i=0; i<array.length; i++){ |
+ list[i] = Math.pow((array[i] - average)/variance, 3); |
} |
double coefficient = count /( (count - 1)*(count - 2) ); |
double skewness = coefficient * sum(list); |
@@ -789,28 +646,13 @@ |
return skewness; |
} |
/** |
- * Sub-function to calculate the sample skewness (Fisher-Pearson skewness) of a dataset. |
- * Skewness = (n /((n-1)(n-2))) * sum{ ((x - average)/standard Deviation) ^ 3 } |
- * @param dataList input list data for skewness |
+ * Sub-function to calculate the sample skewness of a dataset. |
+ * @param array input list data for skewness |
* @return skewness of the dataset. |
*/ |
- public double SkewnessSample(ArrayList<Double> dataList){ |
- //Get the average and standard deviation for use in the skewness formula |
- double average = Average(dataList); |
- double variance = VarianceSample(dataList); |
- double count = dataList.size(); |
- if(count == 0){//fix divide by zero errors |
- count = 1; |
- } |
- |
- //Create list of values to compute the expected value (average) of in order to get the skewness |
- double[] list = new double[dataList.size()]; |
- for(int i=0; i<dataList.size(); i++){ |
- list[i] = Math.pow((dataList.get(i) - average)/variance, 3); |
- } |
- double coefficient = count /( (count - 1)*(count - 2) ); |
- double skewness = coefficient * sum(list); |
- |
+ public double SkewnessSample(ArrayList<Double> array){ |
+ double[] newArray = convertArray(array); |
+ double skewness = SkewnessSample(newArray); |
return skewness; |
} |
/** |
@@ -819,7 +661,7 @@ |
* @return Sxx of the dataset. |
*/ |
public double Sxx(double[] xData){ |
- double xBar = Average(xData); |
+ double xBar = meanArithmetic(xData); |
double sxx = 0; |
|
for(int i=0; i<xData.length; i++){ |
@@ -830,81 +672,65 @@ |
} |
/** |
* Sub-function to calculate the sum of squares of x (Sxx) of a dataset. |
- * @param xData input list data for Sxx |
+ * @param xArray input list data for Sxx |
* @return Sxx of the dataset. |
*/ |
- public double Sxx(ArrayList<Double> xData){ |
- double xBar = Average(xData); |
- double sxx = 0; |
- |
- for(int i=0; i<xData.size(); i++){ |
- sxx = sxx + Math.pow(xData.get(i) - xBar, 2); |
- } |
- |
+ public double Sxx(ArrayList<Double> xArray){ |
+ double[] newXarray = convertArray(xArray); |
+ double sxx = Sxx(newXarray); |
return sxx; |
} |
/** |
* Sub-function to calculate the sum of cross products (Sxy) of two datasets. |
- * @param xData input list of x data for Sxy |
- * @param yData input list of y data for Sxy |
+ * @param xArray input list of x data for Sxy |
+ * @param yArray input list of y data for Sxy |
* @return Sxx of the datasets. |
*/ |
- public double Sxy(double[] xData, double[] yData) throws IOException{ |
+ public double Sxy(double[] xArray, double[] yArray) throws IOException{ |
//Check if arrays are the same size |
- if(xData.length != yData.length){ |
+ if(xArray.length != yArray.length){ |
throw(new IOException("Data arrays must be the same size to perform this statistic. X data size:\t" + |
- xData.length + "\tY data size:\t" + yData.length)); |
+ xArray.length + "\tY data size:\t" + yArray.length)); |
} |
|
- double xBar = Average(xData); |
- double yBar = Average(yData); |
+ double xBar = meanArithmetic(xArray); |
+ double yBar = meanArithmetic(yArray); |
double sxy = 0; |
|
- for(int i=0; i<xData.length; i++){ |
- sxy = sxy + (xData[i] - xBar)*(yData[i] - yBar); |
+ for(int i=0; i<xArray.length; i++){ |
+ sxy = sxy + (xArray[i] - xBar)*(yArray[i] - yBar); |
} |
|
return sxy; |
} |
/** |
* Sub-function to calculate the sum of cross products (Sxy) of two datasets. |
- * @param xData input list of x data for Sxy |
- * @param yData input list of y data for Sxy |
+ * @param xArray input list of x data for Sxy |
+ * @param yArray input list of y data for Sxy |
* @return Sxx of the datasets. |
*/ |
- public double Sxy(ArrayList<Double> xData, ArrayList<Double> yData) throws IOException{ |
- //Check if arrays are the same size |
- if(xData.size() != yData.size()){ |
- throw(new IOException("Data arrays must be the same size to perform this statistic. X data size:\t" + |
- xData.size() + "\tY data size:\t" + yData.size())); |
- } |
- |
- double xBar = Average(xData); |
- double yBar = Average(yData); |
- double sxy = 0; |
- |
- for(int i=0; i<xData.size(); i++){ |
- sxy = sxy + (xData.get(i) - xBar)*(yData.get(i) - yBar); |
- } |
- |
+ public double Sxy(ArrayList<Double> xArray, ArrayList<Double> yArray) throws IOException{ |
+ double[] newXarray = convertArray(xArray); |
+ double[] newYarray = convertArray(yArray); |
+ double sxy = Sxy(newXarray, newYarray); |
return sxy; |
} |
/** |
* Sub-function to calculate the correlation coefficient (r) of two datasets. |
- * @param xData input list of x data |
- * @param yData input list of y data |
+ * @param xArray input list of x data |
+ * @param yArray input list of y data |
* @return the correlation coefficient of the two data sets [r = Sxy / squareRoot(Sxx * Syy)] |
*/ |
- public double CorrelationCoefficient(double[] xData, double[] yData) throws IOException{ |
+ public double CorrelationCoefficient(double[] xArray, double[] yArray) throws IOException{ |
//Check if arrays are the same size |
- if(xData.length != yData.length){ |
+ if(xArray.length != yArray.length){ |
throw(new IOException("Data arrays must be the same size to perform this statistic. X data size:\t" + |
- xData.length + "\tY data size:\t" + yData.length)); |
+ xArray.length + "\tY data size:\t" + yArray.length)); |
} |
|
- double sxx = Sxx(xData); |
- double syy = Sxx(yData); |
- double sxy = Sxy(xData, yData); |
+ double sxx = Sxx(xArray); |
+ double syy = Sxx(yArray); |
+ double sxy = Sxy(xArray, yArray); |
|
double r = sxy / Math.pow(sxx*syy, 0.5); |
|
@@ -912,45 +738,36 @@ |
} |
/** |
* Sub-function to calculate the correlation coefficient (r) of two datasets. |
- * @param xData input list of x data |
- * @param yData input list of y data |
+ * @param xArray input list of x data |
+ * @param yArray input list of y data |
* @return the correlation coefficient of the two data sets [r = Sxy / squareRoot(Sxx * Syy)] |
*/ |
- public double CorrelationCoefficient(ArrayList<Double> xData, ArrayList<Double> yData) throws IOException{ |
- //Check if arrays are the same size |
- if(xData.size() != yData.size()){ |
- throw(new IOException("Data arrays must be the same size to perform this statistic. X data size:\t" + |
- xData.size() + "\tY data size:\t" + yData.size())); |
- } |
- |
- double sxx = Sxx(xData); |
- double syy = Sxx(yData); |
- double sxy = Sxy(xData, yData); |
- |
- double r = sxy / Math.pow(sxx*syy, 0.5); |
- |
+ public double CorrelationCoefficient(ArrayList<Double> xArray, ArrayList<Double> yArray) throws IOException{ |
+ double[] newXarray = convertArray(xArray); |
+ double[] newYarray = convertArray(yArray); |
+ double r = CorrelationCoefficient(newXarray, newYarray); |
return r; |
} |
/** |
* Sub-function to calculate the R^2 (Rsquare) value of two datasets. Rsquare is the coefficient of determination fraction of the variance explained by regression |
- * @param xData input list of x data |
- * @param yData input list of y data |
+ * @param xArray input list of x data |
+ * @param yArray input list of y data |
* @return the Rsquare of the two data sets [r = 1 - See/Syy] |
*/ |
- public double Rsquare(double[] xData, double[] yData) throws IOException{ |
+ public double Rsquare(double[] xArray, double[] yArray) throws IOException{ |
//Check if arrays are the same size |
- if(xData.length != yData.length){ |
+ if(xArray.length != yArray.length){ |
throw(new IOException("Data arrays must be the same size to perform this statistic. X data size:\t" + |
- xData.length + "\tY data size:\t" + yData.length)); |
+ xArray.length + "\tY data size:\t" + yArray.length)); |
} |
|
- double sxx = Sxx(xData); |
- double syy = Sxx(yData); |
- double n = xData.length; |
+ double sxx = Sxx(xArray); |
+ double syy = Sxx(yArray); |
+ double n = xArray.length; |
if(n == 0){//fix divide by zero errors |
n = 1; |
} |
- double sxy = Sxy(xData, yData); |
+ double sxy = Sxy(xArray, yArray); |
double b1 = sxy / sxx; |
double Ssquare = (syy - b1*sxx)/(n-2); |
|
@@ -960,70 +777,58 @@ |
} |
/** |
* Sub-function to calculate the R^2 (Rsquare) value of two datasets. Rsquare is the coefficient of determination fraction of the variance explained by regression |
- * @param xData input list of x data for Sxy |
- * @param yData input list of y data for Sxy |
+ * @param xArray input list of x data for Sxy |
+ * @param yArray input list of y data for Sxy |
* @return the Rsquare of the two data sets [r = 1 - See/Syy] |
*/ |
- public double Rsquare(ArrayList<Double> xData, ArrayList<Double> yData) throws IOException{ |
- //Check if arrays are the same size |
- if(xData.size() != yData.size()){ |
- throw(new IOException("Data arrays must be the same size to perform this statistic. X data size:\t" + |
- xData.size() + "\tY data size:\t" + yData.size())); |
- } |
- |
- double sxx = Sxx(xData); |
- double syy = Sxx(yData); |
- double n = xData.size(); |
- double sxy = Sxy(xData, yData); |
- double b1 = sxy / sxx; |
- double Ssquare = (syy - b1*sxx)/(n-2); |
- |
- double rSquare = (syy - Ssquare*(n - 2))/syy; |
- |
+ public double Rsquare(ArrayList<Double> xArray, ArrayList<Double> yArray) throws IOException{ |
+ double[] newXarray = convertArray(xArray); |
+ double[] newYarray = convertArray(yArray); |
+ double rSquare = Rsquare(newXarray, newYarray); |
return rSquare; |
} |
/** |
* Computes the sum of squared errors for a set of give error values |
- * @param errors array of diverence values (y - y_hat) |
+ * @param array array of difference values (y - y_hat) |
* @return the sum of squared errors, sum(error_i ^ 2) |
*/ |
- public double SSE(double[] errors){ |
+ public double SSE(double[] array){ |
double sum = 0; |
- for(int i=0; i<errors.length;i++){ |
- sum = sum + Math.pow(errors[i], 2); |
+ for(int i=0; i<array.length;i++){ |
+ sum = sum + Math.pow(array[i], 2); |
} |
|
return sum; |
} |
/** |
* Computes the Likelihood function for a set of give error values |
- * @param errors array of difference values (y - y_hat) |
+ * @param array array of difference values (y - y_hat) |
* @return the value of the likelihood function LH = product( [1/sqrt(2*pi*sigma_errors^2)] * exp [-(error^2) / (2*sigma_errors^2)] |
*/ |
- public double LF(double[] errors){ |
+ public double LF(double[] array){ |
//Compute the variance of the errors |
- double sigma_e2 = VarianceSample(errors); |
+ double sigma_e2 = VarianceSample(array); |
|
//Calculate the likelihood function for the array and the varience |
double LH = 1; |
- for(int i=0; i<errors.length; i++){ |
- LH = LH * ( (Math.pow(2*Math.PI*sigma_e2, -0.5)) * Math.exp(-Math.pow(errors[i], 2) / (2*sigma_e2)) ); |
+ for(int i=0; i<array.length; i++){ |
+ LH = LH * ( (Math.pow(2*Math.PI*sigma_e2, -0.5)) * Math.exp(-Math.pow(array[i], 2) / (2*sigma_e2)) ); |
} |
|
return LH; |
} |
/** |
* Computes the Log-Likelihood function for a set of give error values |
- * @param errors array of difference values (y - y_hat) |
+ * @param array array of difference values (y - y_hat) |
* @return the value of the likelihood function, LLH = (-n/2)*ln(2*pi) - 1/2*ln(sigma_errors ^ 2n) - 1/2*(sigma_errors ^ -2)*sum(errors[i] ^ 2) |
*/ |
- public double LLF(double[] errors){ |
+ public double LLF(double[] array){ |
//Compute the standard deviation of the errors |
- double N = errors.length; |
- double sigma_e = StandardDeviationSample(errors); |
+ double N = array.length; |
+ double sigma_e = StandardDeviationSample(array); |
|
//Calculate the log-likelihood function for the array and the standard deviation |
- double sse = SSE(errors); |
+ double sse = SSE(array); |
Double part1 = (N * Math.log(2*Math.PI) / (-2)); |
Double part2 = (Math.log(Math.pow(sigma_e, 2*N)) / (2)); |
Double part3 = (Math.pow(sigma_e, -2) / (2)); |
@@ -1044,13 +849,13 @@ |
/** |
* Computes the Akaike Information Criterion (AIC) value for a given dataset using the Log-Likelihood function |
* and number of parameters of the model as a goodness of fit test |
- * @param errors array of difference values (y - y_hat) or (actual - predicted) |
+ * @param array array of difference values (y - y_hat) or (actual - predicted) |
* @param numberOfParams the number of parameters used in the model (AR(p) or ARMA(p,q)) to predict future data |
* @return the value of AIC for the given errors and parameters |
*/ |
- public double AIC(double[] errors, int numberOfParams){ |
+ public double AIC(double[] array, int numberOfParams){ |
//Compute the log-likelihood function |
- double llf = LLF(errors); |
+ double llf = LLF(array); |
|
//Compute the value of aic |
double aic = 2*numberOfParams - 2*llf; |
@@ -1060,14 +865,14 @@ |
/** |
* Computes the Bayesian Information Criterion (BIC) value for a given dataset using the Log-Likelihood function |
* and number of parameters of the model as a goodness of fit test |
- * @param errors array of difference values (y - y_hat) or (actual - predicted) |
+ * @param array array of difference values (y - y_hat) or (actual - predicted) |
* @param numberOfParams the number of parameters used in the model (AR(p) or ARMA(p,q)) to predict future data |
* @return the value of BIC for the given errors and parameters |
*/ |
- public double BIC(double[] errors, int numberOfParams){ |
+ public double BIC(double[] array, int numberOfParams){ |
//Compute the log-likelihood function |
- double llf = LLF(errors); |
- double N = errors.length; |
+ double llf = LLF(array); |
+ double N = array.length; |
|
//Compute the value of bic |
double bic = 2*numberOfParams*Math.log(N) - 2*llf; |
@@ -1088,7 +893,7 @@ |
observed.length + "\tModeled data size:\t" + modeled.length)); |
} |
|
- double observed_ave = Average(observed); |
+ double observed_ave = meanArithmetic(observed); |
double numerator = 0; |
double denominator = 0; |
for(int i=0; i<observed.length; i++){ |
@@ -1107,21 +912,9 @@ |
* @return |
*/ |
public double NashSutcliffe(ArrayList<Double> observed, ArrayList<Double> modeled) throws IOException{ |
- //Check if arrays are the same size |
- if(observed.size() != modeled.size()){ |
- throw(new IOException("Data arrays must be the same size to perform this statistic. Observed data size:\t" + |
- observed.size() + "\tModeled data size:\t" + modeled.size())); |
- } |
- |
- double observed_ave = Average(observed); |
- double numerator = 0; |
- double denominator = 0; |
- for(int i=0; i<observed.size(); i++){ |
- numerator = numerator + (observed.get(i) - modeled.get(i))*(observed.get(i) - modeled.get(i)); |
- denominator = denominator + (observed.get(i) - observed_ave)*(observed.get(i) - observed_ave); |
- } |
- double E = 1 - (numerator/denominator); |
- |
+ double[] newObsArray = convertArray(observed); |
+ double[] newModArray = convertArray(modeled); |
+ double E = NashSutcliffe(newObsArray, newModArray); |
return E; |
} |
/** |
@@ -1148,7 +941,6 @@ |
double S = P - M; |
double n = pairedData.size(); |
double tau = S / (n * (n-1) / 2); |
- |
return tau; |
} |
} |
\ No newline at end of file |