0 Replies - 1979 Views - Last Post: 27 April 2012 - 08:22 AM

#1 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Calculate the Truncated Mean

Posted 27 April 2012 - 08:22 AM

Description: See Example Usage in snippetA truncated mean or trimmed mean is a statistical measure of central tendency, much like the mean and median. It involves the calculation of the mean after discarding given parts of a probability distribution or sample at the high and low end, and typically discarding an equal amount of both.
import java.util.Arrays;

public class Means
{    

     /**
     * Returns the truncated mean of a dataset
     * 
     * @param data
     *            The dataset to average
     * @param truncPercentage
     *            The percentage of values to ignore from the beginning and end of the dataset
     * @return The truncated mean
     */
    public static double truncatedMean(double[] data, double truncPercentage)
    {
	// get number of values to remove each side
	int numRemove = (int) Math.floor(((truncPercentage / 100) * data.length) / 2);

	// sort the data
	double[] sortedData = data.clone();
	Arrays.sort(sortedData);

	double sum = 0;
	int count = 0;

	// sum the values and get a count of the data, not taking into account numRemove values from the start and end
	for (int i = numRemove; i < (sortedData.length - numRemove); i++)
	{
	    sum += sortedData[i];
	    count++;
	}

	return sum / count;
    }

    public static void main(String[] args)
    {
	double[] x = { 2, 3, 4, 5, 6, 8, 10, 11.53542 };

        System.out.println("95% truncated mean = " + Means.truncatedMean(x, 95));
    }
}

// OUTPUT

// 95% truncated mean = 5.5


Is This A Good Question/Topic? 0
  • +

Page 1 of 1