← Nathanael Silverman

A collection of thoughts from one human being

Calculating a Correlation on Android

Calculating correlations is easy—especially when using a math library. If you're like me and you like playing around with data it's a tool you'll want as part of your arsenal. Here we'll be using "Pearson's product-moment correlation coefficient". It's a common way to measure the linear correlation between two variables. But first a quick note about why this article targets Android.

Smartphones are the ideal data collection tool. They're always on us so they're easy to whip out to save any kind of information. And they're equipped with data collecting hardware like GPS, WiFi, and sensors. AND they're now powerful enough to process all that data themselves!... Excited yet? Let's jump right to it!

First we'll import Apache Commons' math library. This will give us access to the PearsonsCorrelation class. Let's add the following dependency to our build.gradle file:

compile 'org.apache.commons:commons-math3:3.5'

Well done! This library let's us use a couple of data formats: RealMatrix or double arrays. Let's use the latter.

BOOM! We're done!

The correlation coefficient will vary from -1 to 1... 1 is a perfect correlation, meaning the values rise and fall together. -1 is a perfect negative correlation, meaning when one value rises the other falls. Here's an easy way to interpret the values between:

  • -0.1 to 0.1 indicates no linear correlation
  • -0.5 to -0.1 or 0.1 to 0.5 indicates a "weak" correlation
  • -1 to -0.5 or 0.5 to 1 indicates a "strong" correlation

So we've found a strong correlation between eating ice cream and happiness. At least for the person observed. I sense a Nobel Prize in our future!

Thank you for learning how to correlate today! And if you use correlations in your code, tell me about it!


Subscribe by email