Menu

GSL - Statistics - Covariance Error

2008-06-28
2012-09-26
  • Bruno Calado

    Bruno Calado - 2008-06-28

    Hi,

    I want to know why the code below gives the wrong result.

    code:

    double t[3]={1,2,3}, g[3]={1,2,3};
    cout << endl << "Covariance:  " << gsl_stats_covariance(t, 1, g, 1, 3);
    

    Out:

    Covariance:  1
    

    The covariance is 0.66666 and not 1, anyone can help me?

    anyone know what is stride1 and stride2?
    double gsl_stats_covariance (const double data1[], const size_t stride1, const double data2[], const size_t stride2, const size_t n)

    tks

     
    • cpns

      cpns - 2008-06-28

      The documentation omits to mention the stride parameter, but looking at the meaning of 'stride' in MatLab, I am guessing that it allows you to use say every third element of data by setting a stride of 3.

       
    • Bruno Calado

      Bruno Calado - 2008-06-28

      It doesn't work:

      double t[3]={1,2,3}, g[3]={1,2,3};
      cout << endl << "Covariance:  "<< gsl_stats_covariance(t, 3,g,3,3);
      

      Out:

      Covariance: -0.16667

      tks...

       
      • cpns

        cpns - 2008-06-29

        I was not offering a solution, just an explanation of what stride might possibly mean.

        I am no mathematician, I only have the vaguest notion of what a covariance is, much less how it is calculated. The PDF version of the manual makes more sense that web versions since the mathematical notation is rendered conventionally rather than the weird ASCII notation ( http://people.scs.fsu.edu/~burkardt/pdf/gsl.pdf ). Anyway, now I have seen how it is calculated, I can see how the answer is 1.0, so I can only assume that your understanding of what the function does is incorrect.

        mean_t = 2
        mean_g = 2
        n = 3

        cov = (1/(n-1)) * sum[i=1..n]{ (t[i] - mean_t) * (g[i] - mean_g) }

        i = 1: (1-2)(1-2) = 1
        i = 2: (2-2)
        (2-2) = 0
        i = 3: (3-2)*(3-2) = 1


        sum 2

        cov = 1/2 * 2 = 1

        Clifford

         
        • cpns

          cpns - 2008-06-29

          ... I am guessing perhaps you has 1/n rather than 1/(n-1)

           

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.