Pearson’s Correlations with JavaScript

Have you ever thought of incorporating correlation/regression analysis to your application? Recently I was working on a project which involved generating correlations with JavaScript for stock market data. Initially, we set out to find an API that could handle this type of information. But within the principle of trying to cut back in processing time, we decided to make the correlation analysis internally for the application. We knew two things. One: we were handling massive amounts of information as it was. Two: the last thing we wanted was to build the perfect traffic jam inside our app. While looking for different ways of structuring the correlation in javaScript, I was not able to find something that gave us a boilerplate/structure. This version of the function returns both Pearson’s Correlation Coefficient and R-Squared ( Coefficient of Determination).

I believe the spirit of information sharing and collaboration. Because of this, I just wanted to publish the Pearson’s Correlation function. My intent is for people to copy, use it, and hopefully improve upon it.

Pearson’s Correlation Function in JavaScript

//Pearson's Function
pearsonsCorrelation = (x, y) => {
    let arrayLength = 0;

    if (x.length === y.length) {
      arrayLength = x.length;
    } else if (x.length > y.length) {
      //Handles errors for conflicts of difference in length of arrays inputed [x]&[y]
      arrayLength = y.length;
      console.error(
        "Array X has more items in it, the last " +
          (x.length - arrayLength) +
          " will not run in the correlation..."
      );
    } else {
      arrayLength = x.length;
      console.error(
        "Array Y has more values in it, the last " +
          (y.length - arrayLength) +
          " will not run in the correlation..."
      );
    }

    let xy = [];
    let x2 = [];
    let y2 = [];

    for (let i = 0; i < arrayLength; i++) {
      xy.push(x[i] * y[i]);
      x2.push(x[i] * x[i]);
      y2.push(y[i] * y[i]);
    }

    let sum_x = 0;
    let sum_y = 0;
    let sum_xy = 0;
    let sum_x2 = 0;
    let sum_y2 = 0;

    for (let i = 0; i < arrayLength; i++) {
      sum_x += x[i];
      sum_y += y[i];
      sum_xy += xy[i];
      sum_x2 += x2[i];
      sum_y2 += y2[i];
    }

    let stepOne = arrayLength * sum_xy - sum_x * sum_y;
    let stepTwo = arrayLength * sum_x2 - sum_x * sum_x;
    let stepThree = arrayLength * sum_y2 - sum_y * sum_y;
    let stepFour = Math.sqrt(stepTwo * stepThree);
    let r = stepOne / stepFour;
    let rSquared = r*r

    console.log("Pearson's Coefficient (r) = ", r.toFixed(2))
    console.log("Coefficient of Determination (r^2) = ", rSquared.toFixed(2))
  };

Sidenote – Correlations with JavaScript

TickerCorrelate

Interested in correlation analysis? Check out one of my passion projects at TickerCorrelate.com! We created an application able to correlate stock market and commodity data. Want to take that a step further and collaborate with the project? Then be sure to let me know or send the team an email at tickercorrelate@gmail.com.

Correlations in javaScript

Now go take correlations with JavaScript to the next level!

(Visited 323 times, 1 visits today)

Leave a comment

Your email address will not be published. Required fields are marked *