package org.jzy3d.maths;

import java.util.Arrays;
import java.util.List;

public class Statistics {
	/**
	 * Computes the mean value of an array of doubles. NaN values are ignored during the computation.
	 * 
	 * @param values
	 * @return the mean value
	 */
    public static double mean(double[] values){
    	if(values.length==0)
			throw new IllegalArgumentException("Input array must have a length greater than 0");
		
		double sum = 0;
		int    count = 0;
		for( int i=0; i values) {
        if(values.size()==0)
            throw new IllegalArgumentException("Input list must have a length greater than 0");
        
        float minv = Float.POSITIVE_INFINITY;
        
        for( int i=0; i values){
        if(values.size()==0)
            throw new IllegalArgumentException("Input array must have a length greater than 0");

        float maxv = Float.NEGATIVE_INFINITY;
        
        for( int i=0; i maxv )
                    maxv = f;  
        }
        return maxv;
    }

    
	/**
	 * Computes the maximum value of an array of doubles. NaN values are ignored during the computation.
	 * @param values
	 * @return the maximum value
	 */
    public static double max(double[] values){
    	if(values.length==0)
			throw new IllegalArgumentException("Input array must have a length greater than 0");

		double maxv = Double.NEGATIVE_INFINITY;
		
		for( int i=0; i maxv )
					maxv = values[i];				
		return maxv;
	}
    
    public static float max(float[] values){
    	if(values.length==0)
			throw new IllegalArgumentException("Input array must have a length greater than 0");

		float maxv = Float.NEGATIVE_INFINITY;
		
		for( int i=0; i maxv )
					maxv = values[i];				
		return maxv;
	}
    
    public static float max(float[][] values){
    	if(values.length==0)
			throw new IllegalArgumentException("Input array must have a length greater than 0");

		float maxv = Float.NEGATIVE_INFINITY;
		
		for( int i=0; i maxv )
						maxv = values[i][j];				
		return maxv;
	}
    
    public static int max(int[][] values){
    	if(values.length==0)
			throw new IllegalArgumentException("Input array must have a length greater than 0");

		int maxv = Integer.MIN_VALUE;
		
		for( int i=0; i maxv )
					maxv = values[i][j];				
		return maxv;
	}
    
    public static int maxId(int [] values){
		int maxv = Integer.MIN_VALUE;
		int index = -1;
		
		for( int i=0; i maxv ){
				maxv = values[i];
				index = i;
			}
		return index;
	}
    
    
    /**
     * Computes the mad statistic, that is the median of all distances to the median of input
     * values. If the input array is empty, the output value is Double.NaN
	 * 
     * @param values
     * @return
     */
    public static double mad(double[] values){
    	if(values.length==0)
			return Double.NaN;
    	
    	double[] dists = new double[values.length];
		double median  = median(values, true);
		
		for(int i=0; i1, where N is the sample size.  
	 * This is an unbiased estimator of the variance of the population
	 * For N=1, the output is 0.
	 *  
	 * @param values
	 * @return 
	 */
	public static double variance(double[] values){
		double mean  = mean(values);
		double sum   = 0;
		int    count = 0;
		
		for( int i=0; i100 || levels[i]<0)
				throw new IllegalArgumentException("input level " + levels[i] + " is out of bounds [0;100].");
			
			quantileIdx = ( sorted.length - 1 ) * levels[i] / 100;
			
			if(quantileIdx == (int)quantileIdx) // exactly find the quantile
				quantiles[i] = sorted[(int)quantileIdx];
			else{
				quantileIdxCeil  = Math.ceil(quantileIdx);
				quantileIdxFloor = Math.floor(quantileIdx);	

				if(interpolated){ // generate an interpolated quantile
					quantiles[i] = sorted[(int)quantileIdxFloor] * (quantileIdxCeil-quantileIdx)
					             + sorted[(int)quantileIdxCeil]  * (quantileIdx-quantileIdxFloor);
				}
				else{ // return the quantile corresponding to the closest value
					if(quantileIdx-quantileIdxFloor < quantileIdxCeil-quantileIdx)
						quantiles[i] = sorted[(int)quantileIdxFloor];
					else
						quantiles[i] = sorted[(int)quantileIdxCeil];
				}
			}
		}			
		return quantiles;
	}
	
	/**
	 * A convenient shortcut for:
	 * 
	 *   quantile(values, levels, true);
	 * 
	 * @param values
	 * @param levels
	 * @return
	 */
	public static double[] quantile(double[] values, double[] levels){
		return quantile(values, levels, true);
	}
	
	/**
	 * Computes the median value of an array of doubles. 
	 * 
	 * @param values
	 * @param interpolated
	 * @return the mean value
	 */
    public static double median(double[] values, boolean interpolated){
    	if(values.length==0)
			throw new IllegalArgumentException("Input array must have a length greater than 0");
    	
    	double[] med = {50};
    	double[] out = quantile(values, med, interpolated);
    	
    	return out[0];
	}

}

 

Ads help maintain this website.