//package com.nowjava;/*from n o w j a v a . c o m*/
public class Main {
public static void main(String[] argv) throws Exception {
double[] v = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,
37.1234, 67.2344, 68.34534, 69.87700 };
System.out.println(variance(v));
}
/**
* Computes the (bias-corrected sample) variance.
*/
public static double variance(double[] v) {
if (v.length > 1) {
final double m = mean(v);//N o w J a v a . c o m 提 供
double ans = 0.0;
for (int i = 0; i < v.length; i++)
ans += (v[i] - m) * (v[i] - m);
return ans / (v.length - 1);
} else
throw new IllegalArgumentException(
"Array length must be of 2 or greater.");
}
/**
* Computes the (bias-corrected sample) variance.
*/
public static double variance(int[] v) {
if (v.length > 1) {
final double m = mean(v);
double ans = 0.0;
for (int i = 0; i < v.length; i++)
ans += (v[i] - m) * (v[i] - m);
return ans / (v.length - 1);
} else
throw new IllegalArgumentException(
"Array length must be of 2 or greater.");
}
/**
* Computes the mean.
*/
public static double mean(double[] v) {
if (v.length == 0)
throw new IllegalArgumentException(
"Nothing to compute! The array must have at least one element.");
return (mass(v) / (double) v.length);
}
/**
* Computes the mean.
*/
public static double mean(int[] v) {
if (v.length == 0)
throw new IllegalArgumentException(
"Nothing to compute! The array must have at least one element.");
return (mass(v) / (double) v.length);
}
/**
* Returns the sum of the elements of the array.
*/
public static double mass(double[] v) {
double somme = 0.0;
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。