Solved

# Commons Math - Variance

Posted on 2011-09-21
1,022 Views
The attached spreadsheet has a dataset of: 2,4,6,8,10,12
The following excel functions give results:

VAR:      14
VARP:      11.66666667

Then In Java, if I code like so:
``````org.apache.commons.math.stat.descriptive.moment.Variance variance = new Variance();
double sigma = variance.evaluate(myDoubleArray);
``````
where myDoubleArray contains 2.0, 4.0, 6.0, 8.0, 10.0, 12.0
then sigma is 14.0.
So I would conclude that the variance calculated by Commons Math is a sample variance not a population variance.

How would I calculate a sample variance using Commons Math?

standardDeviation.xlsx
0
Question by:allelopath
[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
• 2

LVL 74

Expert Comment

ID: 36576153
var =     (sum(  x - M)^2) / (n-1)
varp =  (sum(  x - M)^2) / n

where M  = Mean of the x population
0

LVL 1

Author Comment

ID: 36576246
The question is:
How would I calculate a sample variance using Commons Math?
0

LVL 74

Accepted Solution

sdstuber earned 500 total points
ID: 36576310
just apply the formulas above...

if VAR is sum of squares divided by n-1, then multiply by n-1 and then divide by n to get VARP

variance.evaluate(myDoubleArray) * ( myDoubleArray.length - 1) / myDoubleArray.length
0

LVL 27

Expert Comment

ID: 36576322
M is the mean of the sample (the mean of the populataion being unknown
0

## Featured Post

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Have you ever thought of installing a power system that generates solar electricity to power your house? Some may say yes, while others may tell me no. But have you noticed that people around you are now considering installing such systems in their …
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
###### Suggested Courses
Course of the Month5 days, 18 hours left to enroll