How to find correlation between two integer arrays in java

后端 未结 2 1878
长发绾君心
长发绾君心 2021-02-20 06:48

I am searching a lot but could not find exactly what i need till now. I have two integer arrayas int[] x and int[] y. I want to find simple linear

2条回答
  •  孤城傲影
    2021-02-20 07:28

    Correlation is quite easy to compute manually:

    http://en.wikipedia.org/wiki/Correlation_and_dependence

      public static double Correlation(int[] xs, int[] ys) {
        //TODO: check here that arrays are not null, of the same length etc
    
        double sx = 0.0;
        double sy = 0.0;
        double sxx = 0.0;
        double syy = 0.0;
        double sxy = 0.0;
    
        int n = xs.length;
    
        for(int i = 0; i < n; ++i) {
          double x = xs[i];
          double y = ys[i];
    
          sx += x;
          sy += y;
          sxx += x * x;
          syy += y * y;
          sxy += x * y;
        }
    
        // covariation
        double cov = sxy / n - sx * sy / n / n;
        // standard error of x
        double sigmax = Math.sqrt(sxx / n -  sx * sx / n / n);
        // standard error of y
        double sigmay = Math.sqrt(syy / n -  sy * sy / n / n);
    
        // correlation is just a normalized covariation
        return cov / sigmax / sigmay;
      }
    

提交回复
热议问题