shaun
shaun

Reputation: 570

covariance matrix gsl

I am trying to calculate the Mahalanobis distance between two vectors a and b. Eventually, I will be using this as a distance measure in statistical algorithms. I am using gsl to implement them. The formula for the mahalanobis distance is sqrt((a-b)'c^-1(a-b)), where c is the covariance matrix. According to this gsl documentation, it takes in two data sets and returns one covariance value. I am not sure how to calculate the covariance matrix using that. Any help is appreciated.

Thanks.

Upvotes: 2

Views: 2408

Answers (1)

pyCthon
pyCthon

Reputation: 12361

I think you need to understand the calcuation of a covariance matrix first, second heres a sample code to get you started

for (i = 0; i < A->size1; i++) {
        for (j = i; j < A->size2; j++) {
          a = gsl_matrix_column (A, i);
          b = gsl_matrix_column (A, j);
          double cov = gsl_stats_covariance(a.vector.data, a.vector.stride,b.vector.data, b.vector.stride, a.vector.size);
          gsl_matrix_set (C, i, j, cov);
        }
      }

Upvotes: 3

Related Questions