算法(Pearson相关系数)

/*
* @param {Array} a, b
*/
function pearson(a, b) {
    var ra = average(a),
        rb = average(b);
    var i, n = 0, d = [0, 0];
    for (i = 0; i < a.length; i++) {
        n += (a[i] - ra) * (b[i] - rb);
        d[0] += Math.pow(a[i] - ra, 2);
        d[1] += Math.pow(b[i] - rb, 2);
    }
    return n / Math.sqrt(d[0] * d[1]);
    function average(arr) {
        var i = 0;
        for (var x of arr) {
            i += x;
        }
        return i / arr.length;
    }
}

推荐阅读更多精彩内容