# 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.

```/*
* We've collected a week's worth of data about ice cream consumption
* and general happiness for a particular individual
*/

double[] iceCreamPerDay = new double[] {
1, // Day one, had 1 ice cream
0, // Day two, no ice cream 😦
0, // Etc
1,
2,
1,
0 };

// Happiness is rated on a 1 to 10 scale
double[] happinessPerDay = new double[] { 7, 5, 4, 6, 9, 9, 5 };

PearsonsCorrelation math = new PearsonsCorrelation();
double correlation = math.correlation(iceCreamPerDays, happinessPerDays);
// outputs: 0.8713708444711573
```

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!