/** * Calculates the degree of freedom according to Welch-Satterthwaite * * @param mv1 Mean and variance of first sample * @param mv2 Mean and variance of second sample * @return Estimated degrees of freedom. */ public static int calculateDOF(MeanVariance mv1, MeanVariance mv2) { final double relvar1 = mv1.getSampleVariance() / mv1.getCount(); final double relvar2 = mv2.getSampleVariance() / mv2.getCount(); final double wvariance = relvar1 + relvar2; final double div = relvar1 * relvar1 / (mv1.getCount() - 1) + relvar2 * relvar2 / (mv2.getCount() - 1); return (int) (wvariance * wvariance / div); }
/** * Calculates the degree of freedom according to Welch-Satterthwaite * * @param mv1 Mean and variance of first sample * @param mv2 Mean and variance of second sample * @return Estimated degrees of freedom. */ public static int calculateDOF(MeanVariance mv1, MeanVariance mv2) { final double relvar1 = mv1.getSampleVariance() / mv1.getCount(); final double relvar2 = mv2.getSampleVariance() / mv2.getCount(); final double wvariance = relvar1 + relvar2; final double div = relvar1 * relvar1 / (mv1.getCount() - 1) + relvar2 * relvar2 / (mv2.getCount() - 1); return (int) (wvariance * wvariance / div); }
/** * Calculates the degree of freedom according to Welch-Satterthwaite * * @param mv1 Mean and variance of first sample * @param mv2 Mean and variance of second sample * @return Estimated degrees of freedom. */ public static int calculateDOF(MeanVariance mv1, MeanVariance mv2) { final double relvar1 = mv1.getSampleVariance() / mv1.getCount(); final double relvar2 = mv2.getSampleVariance() / mv2.getCount(); final double wvariance = relvar1 + relvar2; final double div = relvar1 * relvar1 / (mv1.getCount() - 1) + relvar2 * relvar2 / (mv2.getCount() - 1); return (int) (wvariance * wvariance / div); }
/** * Calculate the statistic of Welch's t test using statistical moments of the * provided data samples * * @param mv1 Mean and variance of first sample * @param mv2 Mean and variance of second sample * @return Welch's t statistic */ public static double calculateTestStatistic(MeanVariance mv1, MeanVariance mv2) { final double delta = mv1.getMean() - mv2.getMean(); final double relvar1 = mv1.getSampleVariance() / mv1.getCount(); final double relvar2 = mv2.getSampleVariance() / mv2.getCount(); return delta / Math.sqrt(relvar1 + relvar2); }
/** * Calculate the statistic of Welch's t test using statistical moments of the * provided data samples * * @param mv1 Mean and variance of first sample * @param mv2 Mean and variance of second sample * @return Welch's t statistic */ public static double calculateTestStatistic(MeanVariance mv1, MeanVariance mv2) { final double delta = mv1.getMean() - mv2.getMean(); final double relvar1 = mv1.getSampleVariance() / mv1.getCount(); final double relvar2 = mv2.getSampleVariance() / mv2.getCount(); return delta / Math.sqrt(relvar1 + relvar2); }
/** * Calculate the statistic of Welch's t test using statistical moments of the * provided data samples * * @param mv1 Mean and variance of first sample * @param mv2 Mean and variance of second sample * @return Welch's t statistic */ public static double calculateTestStatistic(MeanVariance mv1, MeanVariance mv2) { final double delta = mv1.getMean() - mv2.getMean(); final double relvar1 = mv1.getSampleVariance() / mv1.getCount(); final double relvar2 = mv2.getSampleVariance() / mv2.getCount(); return delta / Math.sqrt(relvar1 + relvar2); }
@Override public <A> D estimate(A data, NumberArrayAdapter<?, A> adapter) { final int len = adapter.size(data); double min = AbstractLogMOMEstimator.min(data, adapter, 0., 1e-10); MeanVariance mv = new MeanVariance(); for (int i = 0; i < len; i++) { final double val = adapter.getDouble(data, i) - min; if (Double.isInfinite(val) || Double.isNaN(val) || val <= 0.) { continue; } mv.put(Math.log(val)); } if (!(mv.getCount() > 1.)) { throw new ArithmeticException("Too small sample size to estimate variance."); } return estimateFromLogMeanVariance(mv, min); } }
@Override default <A> D estimate(A data, NumberArrayAdapter<?, A> adapter) { final int len = adapter.size(data); double min = LogMOMDistributionEstimator.min(data, adapter, 0., 1e-10); MeanVariance mv = new MeanVariance(); for(int i = 0; i < len; i++) { final double val = adapter.getDouble(data, i) - min; if(Double.isInfinite(val) || Double.isNaN(val) || val <= 0.) { continue; } mv.put(FastMath.log(val)); } if(!(mv.getCount() > 1.)) { throw new ArithmeticException("Too small sample size to estimate variance."); } return estimateFromLogMeanVariance(mv, min); } }
@Override default <A> D estimate(A data, NumberArrayAdapter<?, A> adapter) { final int len = adapter.size(data); double min = LogMOMDistributionEstimator.min(data, adapter, 0., 1e-10); MeanVariance mv = new MeanVariance(); for(int i = 0; i < len; i++) { final double val = adapter.getDouble(data, i) - min; if(Double.isInfinite(val) || Double.isNaN(val) || val <= 0.) { continue; } mv.put(FastMath.log(val)); } if(!(mv.getCount() > 1.)) { throw new ArithmeticException("Too small sample size to estimate variance."); } return estimateFromLogMeanVariance(mv, min); } }
if(mv.getCount() < 1. || !(mv.getNaiveVariance() > 0.)) { return -1;
if(mv.getCount() < 1. || !(mv.getNaiveVariance() > 0.)) { return -1;
if(mv.getCount() < 1. || !(mv.getNaiveVariance() > 0.)) { return -1;
DoubleVector row = new DoubleVector(new double[] { iter.getCenter(), iter.getValue().getCount(), iter.getValue().getMean(), iter.getValue().getSampleVariance() }); res.add(row);
res.add(new double[] { iter.getCenter(), iter.getValue().getCount(), iter.getValue().getMean(), iter.getValue().getSampleVariance() });
/** * Note: this test tests an earlier bug with tiny arrays. Keep. */ @Test public void basic() { MeanVariance mv = new MeanVariance(); mv.put(0); mv.put(new double[] {}); mv.put(new double[] { 0 }); mv.put(new double[] { 0, 0 }); mv.put(new double[] { 0, 0, 0 }); assertEquals("Count wrong.", 7, mv.getCount(), 0.); assertEquals("Mean wrong.", 0, mv.getMean(), 0.); assertEquals("Variance wrong.", 0, mv.getNaiveVariance(), 0.); assertEquals("No toString", -1, mv.toString().indexOf('@')); }
@Test public void combine() { MeanVariance m1 = new MeanVariance(), m2 = new MeanVariance(); m1.put(new double[] { 1, 2, 3 }); m2.put(new double[] { 4, 5, 6, 7 }); MeanVariance m3 = new MeanVariance(m1); m3.put(m2); assertEquals("First mean", 2, m1.getMean(), 0.); assertEquals("First std", 1, m1.getSampleStddev(), 0.); assertEquals("Second mean", 5.5, m2.getMean(), 0.); assertEquals("Second std", Math.sqrt(1.25), m2.getNaiveStddev(), 0.); assertEquals("Third mean", 4, m3.getMean(), 0.); assertEquals("Third std", 4., m3.getNaiveVariance(), 0.); m2.put(new double[] { 1, 2, 3 }, new double[] { 4, 2, 1 }); assertEquals("Fourth mean", 3.0, m2.getMean(), 0); assertEquals("Fourth weight", 11, m2.getCount(), 0); assertEquals("Fourth stddev", 4.8, m2.getSampleVariance(), 0); }
g.addMeasure("Mean +-" + FormatUtil.NF4.format(gini.getCount() > 1. ? gini.getSampleStddev() : 0.), gini.getMean(), 0, 1, false);
LOG.statistics("Result hashcode: " + hash); LOG.statistics("Mean number of results: " + mv.getMean() + " +- " + mv.getNaiveStddev()); if(mvdist.getCount() > 0) { LOG.statistics("Mean k-distance: " + mvdist.getMean() + " +- " + mvdist.getNaiveStddev()); LOG.statistics("Result hashcode: " + hash); LOG.statistics("Mean number of results: " + mv.getMean() + " +- " + mv.getNaiveStddev()); if(mvdist.getCount() > 0) { LOG.statistics("Mean k-distance: " + mvdist.getMean() + " +- " + mvdist.getNaiveStddev());
g.addMeasure("Mean +-" + FormatUtil.NF4.format(gini.getCount() > 1. ? gini.getSampleStddev() : 0.), gini.getMean(), 0, 1, false);
g.addMeasure("Mean +-" + FormatUtil.NF4.format(gini.getCount() > 1. ? gini.getSampleStddev() : 0.), gini.getMean(), 0, 1, false);