Java實現求方差演算法
import java.util.List;
/**
* @title: FangCha.java
* @project: ssc
* @package: ssc.com.yuce
* @author: Edy
* @date: Aug 21, 2012
* @description: 方差
* @version: V1.0
*/
public class FangCha {
/**
* 求陣列方差
* @param num
* @return
*/
public static double getFangCha(List
double avg = 0.0;
double count = num.size();
for (int i = 0; i < num.size(); i++) {
avg = avg + num.get(i);
}
avg = avg / count;
return getFangCha(num, avg);
}
/**
* 求陣列方差
* @param num
* @param avg
* @return
*/
public static double getFangCha(List
double count = num.size();
double ff = 0.0;
for (int i = 0; i < num.size(); i ++) {
ff = ff + (num.get(i) - avg) * (num.get(i) - avg);
}
ff = ff / count;
return ff;
}
/**
* 求數列平均值
* @param list
* @return
*/
public static double getAverage(List
double sum = 0;
int len = list.size();
for (int i = 0; i < len; i++) {
sum += list.get(i);
}
return sum / len;
}
/**
* 求陣列標準差
* @param list
* @return
*/
public static double getBZCha(List
return Math.sqrt(getFangCha(list));
}
}