计算样本数据的方差, 标准方差与协方差
在图像处理中有时候会涉及计算图像像素数据的方差,标准方差与协方差等统计学属性作为中间数据。因此知道什么是方差、标准方差、协方差很重要。
二:代码实现
Java代码实现计算数据的方差,标准方差、协方差
package com.gloomyfish.image.gmm;
public class CalculateVariance {
public double mean(double[] data) {
double sum = 0;
int len = data.length;
for(int i=0; i<len; i++) {
sum += data[i];
}
return sum / len;
}
public double variance(double[] data) {
double mean = mean(data);
double sum = 0;
int len = data.length;
double delta = 0;
for(int i=0; i<len; i++) {
delta = data[i] - mean;
sum += (delta*delta);
}
return sum / len;
}
public double sd(double[] data) {
return Math.sqrt(variance(data));
}
public double covariance(double[] X, double[] Y) {
double mx = mean(X);
double my = mean(Y);
// 理论上应该通过插值保证长度一致
int len = X.length == Y.length ? X.length : Math.min(X.length, Y.length);
double sum = 0;
for(int i=0; i<len; i++) {
sum += ((X[i]-mx)*(Y[i]-my));
}
return sum / len;
}
public static void main(String[] args) {
int len = 20;
double[] X = new double[len];
double[] Y = new double[len];
for(int i=0; i<len; i++) {
X[i] = Math.random()*100;
Y[i] = Math.random()*100;
}
CalculateVariance cv = new CalculateVariance();
System.out.println("X Mean: " + cv.mean(X));
System.out.println("Y Mean: " + cv.mean(Y));
System.out.println();
System.out.println("X Variance: " + cv.variance(X));
System.out.println("Y Variance: " + cv.variance(Y));
System.out.println();
System.out.println("X Standard Deviation: " + cv.sd(X));
System.out.println("Y Standard Deviation: " + cv.sd(Y));
System.out.println();
System.out.println("XY Covariance: " + cv.covariance(X, Y));
System.out.println();
}
}
三:意义表述
协方差可以分析样本数据之间的线性相关性,协方差为正数时候,一般情况表示相关,协方差为负数的时候则表示不相关,常见的相关性计算就是基于协方差实现。在图像的直方图数据比较中是常规的手段之一。OpenCV与ImageJ中均有代码实现。