@Test public void testCovariance() { logger.info("covariance"); TransposeDataList transposeDataList = new TransposeDataList(); transposeDataList.put(0, new FlatDataList(Arrays.asList(new Object[]{56,75,45,71,61,64,58,80,76,61}))); transposeDataList.put(1, new FlatDataList(Arrays.asList(new Object[]{66,70,40,60,65,56,59,77,67,63}))); boolean isSample = false; double expResult = 76.39; double result = Descriptives.covariance(transposeDataList, isSample); assertEquals(expResult, result, Constants.DOUBLE_ACCURACY_HIGH); }
public static double covariance(TransposeDataList transposeDataList, boolean isSample) { if(transposeDataList.size()!=2) { throw new IllegalArgumentException("The collection must contain observations from 2 groups."); } double covariance = 0.0; Object[] keys = transposeDataList.keySet().toArray(); Object keyX = keys[0]; Object keyY = keys[1]; FlatDataCollection flatDataCollectionX = transposeDataList.get(keyX).toFlatDataCollection(); FlatDataCollection flatDataCollectionY = transposeDataList.get(keyY).toFlatDataCollection(); int n = flatDataCollectionX.size(); if(n<=1 || n!=flatDataCollectionY.size()) { throw new IllegalArgumentException("The number of observations in each group must be equal and larger than 1."); } double meanX = mean(flatDataCollectionX); double meanY = mean(flatDataCollectionY); Iterator<Double> iteratorX = flatDataCollectionX.iteratorDouble(); Iterator<Double> iteratorY = flatDataCollectionY.iteratorDouble(); while (iteratorX.hasNext() && iteratorY.hasNext()) { covariance+=(iteratorX.next()-meanX)* (iteratorY.next()-meanY); } if(isSample) { covariance/=(n-1.0); } else { covariance/=n; } return covariance; }
Descriptives { public static double covariance(TransposeDataList transposeDataList, boolean isSample) { if(transposeDataList.size()!=2) { throw new IllegalArgumentException("The collection must contain observations from 2 groups."); } double covariance = 0.0; Object[] keys = transposeDataList.keySet().toArray(); Object keyX = keys[0]; Object keyY = keys[1]; FlatDataCollection flatDataCollectionX = transposeDataList.get(keyX).toFlatDataCollection(); FlatDataCollection flatDataCollectionY = transposeDataList.get(keyY).toFlatDataCollection(); int n = flatDataCollectionX.size(); if(n<=1 || n!=flatDataCollectionY.size()) { throw new IllegalArgumentException("The number of observations in each group must be equal and larger than 1."); } double meanX = mean(flatDataCollectionX); double meanY = mean(flatDataCollectionY); Iterator<Double> iteratorX = flatDataCollectionX.iteratorDouble(); Iterator<Double> iteratorY = flatDataCollectionY.iteratorDouble(); while (iteratorX.hasNext() && iteratorY.hasNext()) { covariance+=(iteratorX.next()-meanX)* (iteratorY.next()-meanY); } if(isSample) { covariance/=(n-1.0); } else { covariance/=n; } return covariance; } }
Descriptives { public static double covariance(TransposeDataList transposeDataList, boolean isSample) { if(transposeDataList.size()!=2) { throw new IllegalArgumentException("The collection must contain observations from 2 groups."); } double covariance = 0.0; Object[] keys = transposeDataList.keySet().toArray(); Object keyX = keys[0]; Object keyY = keys[1]; FlatDataCollection flatDataCollectionX = transposeDataList.get(keyX).toFlatDataCollection(); FlatDataCollection flatDataCollectionY = transposeDataList.get(keyY).toFlatDataCollection(); int n = flatDataCollectionX.size(); if(n<=1 || n!=flatDataCollectionY.size()) { throw new IllegalArgumentException("The number of observations in each group must be equal and larger than 1."); } double meanX = mean(flatDataCollectionX); double meanY = mean(flatDataCollectionY); Iterator<Double> iteratorX = flatDataCollectionX.iteratorDouble(); Iterator<Double> iteratorY = flatDataCollectionY.iteratorDouble(); while (iteratorX.hasNext() && iteratorY.hasNext()) { covariance+=(iteratorX.next()-meanX)* (iteratorY.next()-meanY); } if(isSample) { covariance/=(n-1.0); } else { covariance/=n; } return covariance; }  }
Descriptives { public static double covariance(TransposeDataList transposeDataList, boolean isSample) { if(transposeDataList.size()!=2) { throw new IllegalArgumentException("The collection must contain observations from 2 groups."); } double covariance = 0.0; Object[] keys = transposeDataList.keySet().toArray(); Object keyX = keys[0]; Object keyY = keys[1]; FlatDataCollection flatDataCollectionX = transposeDataList.get(keyX).toFlatDataCollection(); FlatDataCollection flatDataCollectionY = transposeDataList.get(keyY).toFlatDataCollection(); int n = flatDataCollectionX.size(); if(n<=1 || n!=flatDataCollectionY.size()) { throw new IllegalArgumentException("The number of observations in each group must be equal and larger than 1."); } double meanX = mean(flatDataCollectionX); double meanY = mean(flatDataCollectionY); Iterator<Double> iteratorX = flatDataCollectionX.iteratorDouble(); Iterator<Double> iteratorY = flatDataCollectionY.iteratorDouble(); while (iteratorX.hasNext() && iteratorY.hasNext()) { covariance+=(iteratorX.next()-meanX)* (iteratorY.next()-meanY); } if(isSample) { covariance/=(n-1.0); } else { covariance/=n; } return covariance; }  static int count(Iterable it); static double sum(FlatDataCollection flatDataCollection); static double mean(FlatDataCollection flatDataCollection); static double meanSE(FlatDataCollection flatDataCollection); static double median(FlatDataCollection flatDataCollection); static double min(FlatDataCollection flatDataCollection); static double max(FlatDataCollection flatDataCollection); static double minAbsolute(FlatDataCollection flatDataCollection); static double maxAbsolute(FlatDataCollection flatDataCollection); static double range(FlatDataCollection flatDataCollection); static double geometricMean(FlatDataCollection flatDataCollection); static double harmonicMean(FlatDataCollection flatDataCollection); static double variance(FlatDataCollection flatDataCollection, boolean isSample); static double std(FlatDataCollection flatDataCollection, boolean isSample); static double cv(double std, double mean); static double moment(FlatDataCollection flatDataCollection, int r); static double moment(FlatDataCollection flatDataCollection, int r, double mean); static double kurtosis(FlatDataCollection flatDataCollection); static double kurtosisSE(FlatDataCollection flatDataCollection); static double skewness(FlatDataCollection flatDataCollection); static double skewnessSE(FlatDataCollection flatDataCollection); static AssociativeArray percentiles(FlatDataCollection flatDataCollection, int cutPoints); static AssociativeArray quartiles(FlatDataCollection flatDataCollection); static double covariance(TransposeDataList transposeDataList, boolean isSample); static double autocorrelation(FlatDataList flatDataList, int lags); static AssociativeArray frequencies(FlatDataCollection flatDataCollection); static FlatDataCollection mode(FlatDataCollection flatDataCollection); static void normalize(AssociativeArray associativeArray); static void normalizeExp(AssociativeArray associativeArray); }
Descriptives { public static double covariance(TransposeDataList transposeDataList, boolean isSample) { if(transposeDataList.size()!=2) { throw new IllegalArgumentException("The collection must contain observations from 2 groups."); } double covariance = 0.0; Object[] keys = transposeDataList.keySet().toArray(); Object keyX = keys[0]; Object keyY = keys[1]; FlatDataCollection flatDataCollectionX = transposeDataList.get(keyX).toFlatDataCollection(); FlatDataCollection flatDataCollectionY = transposeDataList.get(keyY).toFlatDataCollection(); int n = flatDataCollectionX.size(); if(n<=1 || n!=flatDataCollectionY.size()) { throw new IllegalArgumentException("The number of observations in each group must be equal and larger than 1."); } double meanX = mean(flatDataCollectionX); double meanY = mean(flatDataCollectionY); Iterator<Double> iteratorX = flatDataCollectionX.iteratorDouble(); Iterator<Double> iteratorY = flatDataCollectionY.iteratorDouble(); while (iteratorX.hasNext() && iteratorY.hasNext()) { covariance+=(iteratorX.next()-meanX)* (iteratorY.next()-meanY); } if(isSample) { covariance/=(n-1.0); } else { covariance/=n; } return covariance; }  static int count(Iterable it); static double sum(FlatDataCollection flatDataCollection); static double mean(FlatDataCollection flatDataCollection); static double meanSE(FlatDataCollection flatDataCollection); static double median(FlatDataCollection flatDataCollection); static double min(FlatDataCollection flatDataCollection); static double max(FlatDataCollection flatDataCollection); static double minAbsolute(FlatDataCollection flatDataCollection); static double maxAbsolute(FlatDataCollection flatDataCollection); static double range(FlatDataCollection flatDataCollection); static double geometricMean(FlatDataCollection flatDataCollection); static double harmonicMean(FlatDataCollection flatDataCollection); static double variance(FlatDataCollection flatDataCollection, boolean isSample); static double std(FlatDataCollection flatDataCollection, boolean isSample); static double cv(double std, double mean); static double moment(FlatDataCollection flatDataCollection, int r); static double moment(FlatDataCollection flatDataCollection, int r, double mean); static double kurtosis(FlatDataCollection flatDataCollection); static double kurtosisSE(FlatDataCollection flatDataCollection); static double skewness(FlatDataCollection flatDataCollection); static double skewnessSE(FlatDataCollection flatDataCollection); static AssociativeArray percentiles(FlatDataCollection flatDataCollection, int cutPoints); static AssociativeArray quartiles(FlatDataCollection flatDataCollection); static double covariance(TransposeDataList transposeDataList, boolean isSample); static double autocorrelation(FlatDataList flatDataList, int lags); static AssociativeArray frequencies(FlatDataCollection flatDataCollection); static FlatDataCollection mode(FlatDataCollection flatDataCollection); static void normalize(AssociativeArray associativeArray); static void normalizeExp(AssociativeArray associativeArray);  }