Java Code Examples for org.apache.commons.math3.stat.StatUtils#mean()

The following examples show how to use org.apache.commons.math3.stat.StatUtils#mean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MeanEvaluator.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Object doWork(Object value) throws IOException{
  if(null == value){
    throw new IOException(String.format(Locale.ROOT, "Unable to find %s(...) because the value is null", constructingFactory.getFunctionName(getClass())));
  }
  else if(value instanceof List){
    @SuppressWarnings({"unchecked"})
    List<Number> c = (List<Number>) value;
    double[] data = new double[c.size()];
    for(int i=0; i< c.size(); i++) {
      data[i] = c.get(i).doubleValue();
    }

    return StatUtils.mean(data);
  }
  else{
    throw new IOException(String.format(Locale.ROOT, "Unable to find %s(...) because the value is not a collection, instead a %s was found", constructingFactory.getFunctionName(getClass()), value.getClass().getSimpleName()));
  }
}
 
Example 2
Source File: SemiVarianceTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSample() {
    final double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    final int length = values.length;
    final double mean = StatUtils.mean(values); // 6.333...
    final SemiVariance sv = new SemiVariance();  // Default bias correction is true
    final double downsideSemiVariance = sv.evaluate(values); // Downside is the default
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {-2d, 2d, 4d, -2d, 3d, 5d}, mean) / (length - 1),
            downsideSemiVariance, 1E-14);

    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upsideSemiVariance = sv.evaluate(values);
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {22d, 11d, 14d}, mean) / (length - 1),
            upsideSemiVariance, 1E-14);

    // Verify that upper + lower semivariance against the mean sum to variance
    Assert.assertEquals(StatUtils.variance(values), downsideSemiVariance + upsideSemiVariance, 10e-12);
}
 
Example 3
Source File: SemiVarianceTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSample() {
    final double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    final int length = values.length;
    final double mean = StatUtils.mean(values); // 6.333...
    final SemiVariance sv = new SemiVariance();  // Default bias correction is true
    final double downsideSemiVariance = sv.evaluate(values); // Downside is the default
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {-2d, 2d, 4d, -2d, 3d, 5d}, mean) / (length - 1),
            downsideSemiVariance, 1E-14);

    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upsideSemiVariance = sv.evaluate(values);
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {22d, 11d, 14d}, mean) / (length - 1),
            upsideSemiVariance, 1E-14);

    // Verify that upper + lower semivariance against the mean sum to variance
    Assert.assertEquals(StatUtils.variance(values), downsideSemiVariance + upsideSemiVariance, 10e-12);
}
 
Example 4
Source File: SemiVarianceTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSample() {
    final double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    final int length = values.length;
    final double mean = StatUtils.mean(values); // 6.333...
    final SemiVariance sv = new SemiVariance();  // Default bias correction is true
    final double downsideSemiVariance = sv.evaluate(values); // Downside is the default
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {-2d, 2d, 4d, -2d, 3d, 5d}, mean) / (length - 1),
            downsideSemiVariance, 1E-14);

    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upsideSemiVariance = sv.evaluate(values);
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {22d, 11d, 14d}, mean) / (length - 1),
            upsideSemiVariance, 1E-14);

    // Verify that upper + lower semivariance against the mean sum to variance
    Assert.assertEquals(StatUtils.variance(values), downsideSemiVariance + upsideSemiVariance, 10e-12);
}
 
Example 5
Source File: SemiVarianceTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSample() {
    final double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    final int length = values.length;
    final double mean = StatUtils.mean(values); // 6.333...
    final SemiVariance sv = new SemiVariance();  // Default bias correction is true
    final double downsideSemiVariance = sv.evaluate(values); // Downside is the default
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {-2d, 2d, 4d, -2d, 3d, 5d}, mean) / (length - 1),
            downsideSemiVariance, 1E-14);

    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upsideSemiVariance = sv.evaluate(values);
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {22d, 11d, 14d}, mean) / (length - 1),
            upsideSemiVariance, 1E-14);

    // Verify that upper + lower semivariance against the mean sum to variance
    Assert.assertEquals(StatUtils.variance(values), downsideSemiVariance + upsideSemiVariance, 10e-12);
}
 
Example 6
Source File: SemiVarianceTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSample() {
    final double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    final int length = values.length;
    final double mean = StatUtils.mean(values); // 6.333...
    final SemiVariance sv = new SemiVariance();  // Default bias correction is true
    final double downsideSemiVariance = sv.evaluate(values); // Downside is the default
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {-2d, 2d, 4d, -2d, 3d, 5d}, mean) / (length - 1),
            downsideSemiVariance, 1E-14);

    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upsideSemiVariance = sv.evaluate(values);
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {22d, 11d, 14d}, mean) / (length - 1),
            upsideSemiVariance, 1E-14);

    // Verify that upper + lower semivariance against the mean sum to variance
    Assert.assertEquals(StatUtils.variance(values), downsideSemiVariance + upsideSemiVariance, 10e-12);
}
 
Example 7
Source File: SemiVarianceTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSample() {
    final double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    final int length = values.length;
    final double mean = StatUtils.mean(values); // 6.333...
    final SemiVariance sv = new SemiVariance();  // Default bias correction is true
    final double downsideSemiVariance = sv.evaluate(values); // Downside is the default
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {-2d, 2d, 4d, -2d, 3d, 5d}, mean) / (length - 1),
            downsideSemiVariance, 1E-14);

    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upsideSemiVariance = sv.evaluate(values);
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {22d, 11d, 14d}, mean) / (length - 1),
            upsideSemiVariance, 1E-14);

    // Verify that upper + lower semivariance against the mean sum to variance
    Assert.assertEquals(StatUtils.variance(values), downsideSemiVariance + upsideSemiVariance, 10e-12);
}
 
Example 8
Source File: SemiVarianceTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSample() {
    final double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    final int length = values.length;
    final double mean = StatUtils.mean(values); // 6.333...
    final SemiVariance sv = new SemiVariance();  // Default bias correction is true
    final double downsideSemiVariance = sv.evaluate(values); // Downside is the default
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {-2d, 2d, 4d, -2d, 3d, 5d}, mean) / (length - 1),
            downsideSemiVariance, 1E-14);

    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upsideSemiVariance = sv.evaluate(values);
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {22d, 11d, 14d}, mean) / (length - 1),
            upsideSemiVariance, 1E-14);

    // Verify that upper + lower semivariance against the mean sum to variance
    Assert.assertEquals(StatUtils.variance(values), downsideSemiVariance + upsideSemiVariance, 10e-12);
}
 
Example 9
Source File: SemiVarianceTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSample() {
    final double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    final int length = values.length;
    final double mean = StatUtils.mean(values); // 6.333...
    final SemiVariance sv = new SemiVariance();  // Default bias correction is true
    final double downsideSemiVariance = sv.evaluate(values); // Downside is the default
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {-2d, 2d, 4d, -2d, 3d, 5d}, mean) / (length - 1),
            downsideSemiVariance, 1E-14);

    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upsideSemiVariance = sv.evaluate(values);
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {22d, 11d, 14d}, mean) / (length - 1),
            upsideSemiVariance, 1E-14);

    // Verify that upper + lower semivariance against the mean sum to variance
    Assert.assertEquals(StatUtils.variance(values), downsideSemiVariance + upsideSemiVariance, 10e-12);
}
 
Example 10
Source File: MeanForecasterJava.java    From kieker with Apache License 2.0 5 votes vote down vote up
/**
 * @param numForecastSteps
 *            number of values the forecaster is going to forecast
 *
 * @return Forecast Result
 */
@Override
public IForecastResult forecast(final int numForecastSteps) {
	final ITimeSeries<Double> history = this.getTsOriginal();
	final ITimeSeries<Double> tsFC = this.prepareForecastTS();

	final List<Double> allHistory = new ArrayList<Double>(history.getValues());
	final Double[] histValuesNotNull = MeanForecasterJava.removeNullValues(allHistory);
	final double mean = StatUtils.mean(ArrayUtils.toPrimitive(histValuesNotNull));
	final Double[] forecastValues = new Double[numForecastSteps];
	Arrays.fill(forecastValues, mean);

	tsFC.appendAll(forecastValues);
	return new ForecastResult(tsFC, this.getTsOriginal(), ForecastMethod.MEAN);
}
 
Example 11
Source File: NonParametricStats.java    From ET_Redux with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param dataActiveMap
 * @param sample
 */
public void calculateStats ( boolean[] dataActiveMap, double[] sample ) {
    ArrayList<Double> liveSample = new ArrayList<>();
    sampleMean = 0.0;

    if ( sample.length > 0 ) {

        for (int i = 0; i < sample.length; i ++) {
            if ( dataActiveMap[i] ) {
                sampleMean += sample[i];
                liveSample.add( sample[i] );
            }
        }

        sampleMean /= liveSample.size();
    }
    
    double[] liveSampleArray = new double[liveSample.size()];
    for (int i = 0; i < liveSampleArray.length; i ++){
        liveSampleArray[i] = (double)liveSample.get( i );
    }
    
           
    sampleMean = StatUtils.mean( liveSampleArray );
    variance = StatUtils.variance( liveSampleArray );

    stdErrSampleMean = Math.sqrt(variance) / Math.sqrt( liveSampleArray.length );

}
 
Example 12
Source File: DigitalAlleleCounts.java    From Drop-seq with MIT License 5 votes vote down vote up
public double getMeanUMIPurity () {
	List<String> umis = new ArrayList<String>(this.umis());

	double [] purity = new double [umis.size()];
	for (int i=0; i<purity.length; i++) {
		double d = getUMIPurity(umis.get(i));
		purity[i]=d;
	}

	double result = StatUtils.mean(purity);
	return result;
}
 
Example 13
Source File: AggregateFunctions.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
@Override
public Double summarize(NumericColumn<?> column) {
  double[] col = removeMissing(column);
  return Math.sqrt(StatUtils.variance(col)) / StatUtils.mean(col);
}
 
Example 14
Source File: AggregateFunctions.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
@Override
public Double summarize(NumericColumn<?> column) {
  return StatUtils.mean(removeMissing(column));
}
 
Example 15
Source File: AggregateFunctions.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
@Override
public Double summarize(NumericColumn<?> column) {
  double[] col = removeMissing(column);
  return Math.sqrt(StatUtils.variance(col)) / StatUtils.mean(col);
}
 
Example 16
Source File: AggregateFunctions.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
@Override
public Double summarize(NumericColumn<?> column) {
  return StatUtils.mean(removeMissing(column));
}
 
Example 17
Source File: Statistics.java    From trading-backtest with MIT License 4 votes vote down vote up
public static double sharpe(double[] dailyReturns) {
    return StatUtils.mean(dailyReturns) / Math.sqrt(StatUtils.variance(dailyReturns)) * Math.sqrt(250);
}
 
Example 18
Source File: KnoxShellTable.java    From knox with Apache License 2.0 2 votes vote down vote up
/**
 * Calculates the mean of specified column
 * @param colName the column for which the mean will be calculated
 * @return mean
 */
public double mean(String colName) {
  return StatUtils.mean(toDoubleArray(colName));
}