如何找到在Java中两个整型数组之间的相关性 [英] How to find correlation between two integer arrays in java

查看:204
本文介绍了如何找到在Java中两个整型数组之间的相关性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻找了很多,但无法找到正是我需要到现在。
我有两个整数arrayas INT []×和int []年。我想找到这两个整数数组之间简单的线性关系,它应该返回结果为双。在java中你知道任何库函数提供这个或任何code段?

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 correlation between these two integer arrays and it should return the result as double. In java do you know any library function providing this or any code snippet?

推荐答案

相关性是很容易计算:

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;
  }

这篇关于如何找到在Java中两个整型数组之间的相关性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆