PageSourceSearch

https://terrancelaw.github.io/iQuery/vender/pearsonCorrelation.js

js terrancelaw.github.io collected 2026-10-03 10:05:13 UTC 2,169 bytes, 69 lines download raw bytes

1//--------------------------------------------------------
2// FUNCTION pearsonCorrelation
3//--------------------------------------------------------
4function pearsonCorrelation(independent, dependent)
5{
6    // covariance
7    let independent_mean = arithmeticMean(independent);
8    let dependent_mean = arithmeticMean(dependent);
9    let products_mean = meanOfProducts(independent, dependent);
10    let covariance = products_mean - (independent_mean * dependent_mean);
11
12    // standard deviations of independent values
13    let independent_standard_deviation = standardDeviation(independent);
14
15    // standard deviations of dependent values
16    let dependent_standard_deviation = standardDeviation(dependent);
17
18    // Pearson Correlation Coefficient
19    let rho = covariance / (independent_standard_deviation * dependent_standard_deviation);
20
21    return rho;
22}
23
24//--------------------------------------------------------
25// FUNCTION arithmeticMean
26//--------------------------------------------------------
27function arithmeticMean(data)
28{
29    let total = 0;
30
31    // note that incrementing total is done within the for loop
32    for(let i = 0, l = data.length; i < l; total += data[i], i++);
33
34    return total / data.length;
35}
36
37//--------------------------------------------------------
38// FUNCTION mean_of_products
39//--------------------------------------------------------
40function meanOfProducts(data1, data2)
41{
42    let total = 0;
43
44    // note that incrementing total is done within the for loop
45    for(let i = 0, l = data1.length; i < l; total += (data1[i] * data2[i]), i++);
46
47    return total / data1.length;
48}
49
50//--------------------------------------------------------
51// FUNCTION standardDeviation
52//--------------------------------------------------------
53function standardDeviation(data)
54{
55    let squares = [];
56
57    for(let i = 0, l = data.length; i < l; i++)
58    {
59        squares[i] = Math.pow(data[i], 2);
60    }
61
62    let mean_of_squares = arithmeticMean(squares);
63    let mean = arithmeticMean(data);
64    let square_of_mean = Math.pow(mean, 2);
65    let variance = mean_of_squares - square_of_mean;
66    let std_dev = Math.sqrt(variance);
67
68    return std_dev;
69}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.