0 Replies - 1684 Views - Last Post: 02 April 2012 - 07:07 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 Sample Skewness

Posted 02 April 2012 - 07:07 AM

Description: See Example Usage in snippetIn probability theory and statistics, skewness is a measure of the asymmetry of the probability distribution of a real-valued random variable. More information - http://en.wikipedia....Sample_skewness
public class Skewness
{
    public static double sampleSkewness(double[] data)
    {
	double mean = average(data); // get the mean of the data
	double numerator = 0, denominator = 0;

	for (int i = 0; i < data.length; i++)
	{
	    numerator += Math.pow(data[i] - mean, 3); // deviations to the third power
	    denominator += Math.pow(data[i] - mean, 2); // deviations to the second power
	}

	numerator = (1.0 / data.length) * numerator; // multiply by 1/ data size

	denominator = Math.pow(denominator * (1.0 / data.length), 3.0 / 2.0); // multiply by 1/data size and raise to the power of 1.5 

	return numerator / denominator;
    }

    // Returns the mean of the data set
    private static double average(double[] data)
    {
	double sum = 0;

	for (int i = 0; i < data.length; i++)
	{
	    sum += data[i];
	}

	return sum / data.length;
    }

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

	double skewness = Skewness.sampleSkewness(x);
	System.out.println("Sample Skewness = " + skewness);
    }
}

// OUTPUT

// Sample Skewness = 0.37424139836476816





Is This A Good Question/Topic? 0
  • +

Page 1 of 1