Solved

Calculating Variance

Posted on 2006-11-22
4
3,413 Views
Last Modified: 2010-05-12
HI,
I have to calculate the Variance of a user input in Java. I have to use this formula:
V(X) = E(X²) - (E(X))²

The user input may consist of an arbitrary amount of integers from 1 to 5.

The thing is that I have absolutely no clue how to code this formula. So if anyone can help me it would be really great.
The best solution for me would be a code snipplet which calculates the variance.

Nevertheless if you can tell me how the formula really works and what the variables used in the code mean.
This would be definitely the best thing.

thank, michi
0
Comment
Question by:michi3012
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 17998991
The variance is the mean of the square, minus the square of the mean.

So if you have a data set (in this case, X), then you simply square each number, and find the mean (sum and divide by how many there are), then you subtract from this the mean of the set squared.


public double Var(int [] X)
{
    double E_X2=0.0;
    double EX_2=0.0;
    for(int i=0; i<X.length; i++)
    {
        E_X2 += X[i]*X[i];
        EX_2 += X[i];
    }
    E_X2 /= X.length;
    EX_2 /= X.length;
   
    return E_X2 - EX_2;
}
0
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 500 total points
ID: 17998999
Sorry

public double Var(int [] X)
{
    double E_X2=0.0;
    double EX_2=0.0;
    for(int i=0; i<X.length; i++)
    {
        E_X2 += X[i]*X[i];
        EX_2 += X[i];
    }
    E_X2 /= X.length;
    EX_2 /= X.length;
   
    return E_X2 - EX_2*EX_2;
}
0
 
LVL 2

Expert Comment

by:saintsairforce
ID: 17999070
This should help

public void variance()
{
     Vector<int> numbers = new Vector<int>();
     
     //file your vector
//note a vector is like an array by the size can grow easily.

     double sumX = sumX(numbers);
     double variance = sumXSquared(numbers)+sumx*sumX;

//display your variance value
}


public double xSquared(Vector<int> numbers)
{
   double total=0;
  for(int cnt=0;cnt<numbers.size();cnt++)
  {
     total += numbers.get(cnt)*numbers.get(cnt);
  }

  return total;
}

public double sumX(Vector<int> numbers)
{
     double total=0;
    for(int cnt=0;cnt<numbers.size();cnt++)
  {
     total += numbers.get(cnt);
  }

  return total;
}

cheers,
Ricky
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 17999082
Hi,

V(X) = variance.
E(X) = expected value.
Wikipedia has more info: http://en.wikipedia.org/wiki/Variance

Here is some code.

class VarianceExample
{
    public static void main(String[] args)
    {
        double ex2 = 0;
        double e2x = 0;
        double ex = 0;
        double vx = 0;

        int[] xs = new int[]{1, 4, 2, 3};
        int numxs = xs.length;

        for(int i=0;i<numxs;i++)
        {
            ex2 += xs[i] * xs[i];
            ex += xs[i];
        }
        ex2 /= numxs;   //E(X^2)
        ex /= numxs;    //E(X)
        e2x = ex * ex;  //E(X)^2
        vx = ex2 - e2x; //V(X)

        //String representation of xs
        String sxs = "";
        for(int i=0;i<numxs-1;i++)
            sxs += xs[i]+", ";
        sxs += xs[numxs-1];

        System.out.println("X      = "+sxs);
        System.out.println("E(X)   = "+ex);
        System.out.println("E(X^2) = "+ex2);
        System.out.println("E(X)^2 = "+e2x);
        System.out.println("V(X)   = "+vx);
    }
}

Mark
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to Alipay 10 95
Bot application - advice 3 81
Is there a Google Voice API? 9 74
Java regex 5 22
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question