2011-05-20 87 views
4

我有兩個雙列如下協方差使用EJML

double[] x = { 2.5, 0.5, 2.2, 1.9, 3.1, 2.3, 2.0, 1.0, 1.5, 1.1 }; 
double[] y = { 2.4, 0.7, 2.9, 2.2, 3.0, 2.7, 1.6, 1.1, 1.6, 0.9 }; 

你怎麼找到使用efficient-java-matrix-library (EJML)庫它們之間的協方差? 任何代碼片段最受歡迎。謝謝!

+0

EJML不是一個統計庫... – soandos 2011-05-20 06:10:42

回答

2

這裏是SimpleMatrix做到這一點的一種方式,但我沒有測試代碼:

double[] x = { 2.5, 0.5, 2.2, 1.9, 3.1, 2.3, 2.0, 1.0, 1.5, 1.1 }; 
double[] y = { 2.4, 0.7, 2.9, 2.2, 3.0, 2.7, 1.6, 1.1, 1.6, 0.9 }; 

SimpleMatrix a = new SimpleMatrix(x.length,1,true,x); 
SimpleMatrix b = new SimpleMatrix(x.length,1,true,x); 

double meanA = a.elementSum()/x.length; 
double meanB = a.elementSum()/y.length; 

// X = X - mean(A) 
CommonOps.add(a.getMatrix(),-meanA); 
CommonOps.add(b.getMatrix(),-meanB); 

// compute the covariance 
double c11 = a.transpose().mult(a).get(0,0)/x.length; 
double c12 = a.transpose().mult(b).get(0,0)/x.length; 
double c22 = b.transpose().mult(b).get(0,0)/x.length; 

SimpleMatrix covariance = new SimpleMatrix(2,2,true,c11,c12,c12,c22);