// 计算皮尔逊相关系数
function pearsonCorrelation(s, t) {
let sum1 = 0;
let sum2 = 0;
let sum1Sq = 0;
let sum2Sq = 0;
let pSum = 0;
let n = s.length;
for (let i = 0; i < n; i++) {
sum1 += s[i];
sum2 += t[i];
sum1Sq += Math.pow(s[i], 2);
sum2Sq += Math.pow(t[i], 2);
pSum += s[i] * t[i];
}
let num = pSum - (sum1 * sum2) / n;
let den = Math.sqrt(
(sum1Sq - Math.pow(sum1, 2) / n) * (sum2Sq - Math.pow(sum2, 2) / n)
);
if (den === 0) return 0;
return num / den;
}
// 计算相似度 (0-1,值越大越相似)
function pccSimilarity(s, t) {
return (pearsonCorrelation(s, t) + 1) / 2;
}
上一篇
K线形态-光K线
2023-06-22
下一篇
JavaScript-计算余弦相似度
2023-06-21