-1

I have 2D array :

21 6 160 110 3.90 2.62 16.46 0 1 4 4
21 6 160 110 3.9 2.875 17.02 0 1 4 4
22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
14.3 8 360 245 3.21 3.570 15.84 0 0 3 4
24.4 4 146.7 62 3.69 3.190 20 1 0 4 2
22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
19.2 6 167.6 123 3,92 3.440 18.3 1 0 4 4

And I want to calculate the covariance-variance matrix in C language using GSL library. Can anyone guide me on how to use this library?

Sercan
  • 4,739
  • 3
  • 17
  • 36
MOVIES FX
  • 25
  • 5

1 Answers1

0

I am assuming that you have a 2D array with 55 rows and 2 columns (as you have mentioned 110 values value of data above). First, you have to put this data in a file, say test.dat, (download from here) then you can read it in a gsl_matrix and calculate the covariance matrix using the gsl_stats_covariance function as follows:

#include <stdio.h>
#include <gsl/gsl_statistics.h>
#include <gsl/gsl_matrix.h>

int main (void)
{
  int i, j;
  int rows = 55, col = 2;
  
  gsl_matrix * m = gsl_matrix_alloc (rows, col);
  gsl_matrix * C = gsl_matrix_alloc (col, col);
  
  {
     FILE * f = fopen ("test.dat", "rb");
     gsl_matrix_fscanf (f, m);
     fclose (f);
  }
  
  for (i = 0; i < m->size2; i++) 
  {
        for (j = 0; j < m->size2; j++) 
        {
          gsl_vector_view col1, col2;  
          col1 = gsl_matrix_column (m, i);
          col2 = gsl_matrix_column (m, j);
          double cov = gsl_stats_covariance(col1.vector.data, col1.vector.stride, 
                                            col2.vector.data, col2.vector.stride, 
                                            col1.vector.size);
          gsl_matrix_set (C, i, j, cov);
        }
    }
    
  for (i = 0; i < C->size1; i++) 
  {
      for (j = 0; j < C->size2; j++) 
        {
            printf("%f ", gsl_matrix_get(C, i, j));
        }
  }

  gsl_matrix_free (m);
  gsl_matrix_free (C);
  
  return 0;
}

You can see that the C matrix has been initialized as a 2x2 matrix because the covariance matrix is a square matrix. Each column of the matrix m is sliced as gsl_vector_view and utilized in the gsl_stats_covariance function. In the end, the covariance matrix is printed.

Muhammad Mohsin Khan
  • 1,444
  • 7
  • 16
  • 23