public static double [ ] [ ] cov ( double [ ] [ ] data , double [ ] mu ) { double [ ] [ ] sigma = new double [ data [ 0 ] . length ] [ data [ 0 ] . length ] ; for ( int i = 0 ; i < data . length ; i ++ ) { for ( int j = 0 ; j < mu . length ; j ++ ) { for ( int k = 0 ; k <= j ; k ++ ) { sigma [ j ] [ k ] += ( data [ i ] [ j ] - mu [ j ] ) * ( data [ i ] [ k ] - mu [ k ] ) ; } } } int n = data . length - 1 ; for ( int j = 0 ; j < mu . length ; j ++ ) { for ( int k = 0 ; k <= j ; k ++ ) { sigma [ j ] [ k ] /= n ; sigma [ k ] [ j ] = sigma [ j ] [ k ] ; } } return sigma ; }
Returns the sample covariance matrix .