First, let’s take a look at what is PPMCC

PPMCC is a measure of the linear correlation between two variables X and Y, giving a value between +1 and −1 inclusive, where 1 is total positive correlation, 0 is no correlation, and −1 is total negative correlation. It is widely used in the sciences as a measure of the degree of linear dependence between two variables

An image to describe post

So this formula in java will look like, and all parameters in follow java impl can be find in the unit test code I attached below.

double rp = (sumOfProduct - (personOneSum * personTwoSum / sameMovies.size())) / Math.*sqrt*((personOneSumSq - Math.*pow*(personOneSum, 2) / sameMovies.size()) * (personTwoSumSq-Math.*pow*(personTwoSum,2)/sameMovies.size()));

This is my first implementation of PPMCC in Java, the data is from the book

Programming Collective Intelligence — chapter 2
The original code in Python can be found in Here

HOW TO RUN THE UNITTEST CODE

Create PearsonTest class in your Java IDE(I use intellij idea 2016), then copy & paste the code below into your new class, make sure you have all libs included, like junit and fastjson

fastjson is from alibaba, if you using maven, then please put following configuration in pom.xml. if not, then please find the lib Here

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.1.39</version>
</dependency>

CODE

Please free feel to give comment on this code, it is not good, and I do think there is better implementation for it.

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;

import java.util.List;
import java.util.Set;

/**
 * Created by zhangchen on 16/6/15.
 */
public class PearsonTest {
    private JSONArray critics = new JSONArray();

    @Before
    public void setup(){
        critics.add(createJSON("Lisa Rose",
                new JSONObject[]{
                        createJSON("Lady in the Water",2.5),
                        createJSON("Snakes on a Plane",3.5),
                        createJSON("Just My Luck",3.0),
                        createJSON("Superman Returns",3.5),
                        createJSON("You, Me and Dupree",2.5),
                        createJSON("The Night Listener",3.0)
                }));

        critics.add(createJSON("Gene Seymour",
                new JSONObject[]{
                        createJSON("Lady in the Water",3.0),
                        createJSON("Snakes on a Plane",3.5),
                        createJSON("Just My Luck",1.5),
                        createJSON("Superman Returns",5.0),
                        createJSON("You, Me and Dupree",3.5),
                        createJSON("The Night Listener",3.0)
                }));

        critics.add(createJSON("Michael Phillips",
                new JSONObject[]{
                        createJSON("Lady in the Water",2.5),
                        createJSON("Snakes on a Plane",3.0),
                        createJSON("Superman Returns",3.5),
                        createJSON("The Night Listener",4.0)
                }));

        critics.add(createJSON("Claudia Puig",
                new JSONObject[]{
                        createJSON("Snakes on a Plane",3.5),
                        createJSON("Just My Luck",3.0),
                        createJSON("Superman Returns",4.0),
                        createJSON("You, Me and Dupree",2.5),
                        createJSON("The Night Listener",4.5)
                }));

        critics.add(createJSON("Mick LaSalle",
                new JSONObject[]{
                        createJSON("Lady in the Water",3.0),
                        createJSON("Snakes on a Plane",4.0),
                        createJSON("Just My Luck",2.0),
                        createJSON("Superman Returns",3.0),
                        createJSON("You, Me and Dupree",2.0),
                        createJSON("The Night Listener",3.0)
                }));

        critics.add(createJSON("Jack Matthews",
                new JSONObject[]{
                        createJSON("Lady in the Water",3.0),
                        createJSON("Snakes on a Plane",4.0),
                        createJSON("Superman Returns",5.0),
                        createJSON("You, Me and Dupree",3.5),
                        createJSON("The Night Listener",3.0)
                }));

        critics.add(createJSON("Toby",
                new JSONObject[]{
                        createJSON("Snakes on a Plane",4.5),
                        createJSON("Superman Returns",4.0),
                        createJSON("You, Me and Dupree",1.0)
                }));

    }

    private JSONObject createJSON(String name, JSONObject...jsonObjects) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(name, jsonObjects);
        return jsonObject;
    }

    private JSONObject createJSON(String name, double points) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(name, points);
        return jsonObject;
    }

    @Test
    public void pearson(){
        System.out.println(similarity("Toby", "Lisa Rose"));
        System.out.println(similarity("Toby", "Jack Matthews"));
    }

    private double similarity(String personOne, String personTwo) {
        List<String> sameMovies = sameRatedMovie(personOne, personTwo);
        double personOneSum = sum(sameMovies, personOne);
        double personTwoSum = sum(sameMovies, personTwo);

        double personOneSumSq = sumSq(sameMovies, personOne);
        double personTwoSumSq = sumSq(sameMovies, personTwo);

        double sumOfProduct = sumOfProduct(sameMovies, personOne, personTwo);

        return (sumOfProduct - (personOneSum * personTwoSum / sameMovies.size())) / Math.sqrt((personOneSumSq - Math.pow(personOneSum, 2) / sameMovies.size()) * (personTwoSumSq-Math.pow(personTwoSum,2)/sameMovies.size()));
    }

    private double sumOfProduct(List<String> sameMovies, String personOne, String personTwo) {
        double sum = 0;
        for (String movieString : sameMovies) {
            double scoreOne = getScore(personOne, movieString);
            double scoreTwo = getScore(personTwo, movieString);
            sum += scoreOne * scoreTwo;
        }
        return sum;
    }

    private double getScore(String personOne, String movieString) {
        JSONArray movies = getMovies(personOne);
        for (int i = 0; i < movies.size(); i++) {
            JSONObject movie = movies.getJSONObject(i);
            if (movie.containsKey(movieString)) {
                return movie.getDouble(movieString).doubleValue();
            }
        }
        return 0;
    }

    private double sumSq(List<String> sameMovies, String person) {
        double sum = 0;
        for (String movieString : sameMovies) {
            JSONArray personMovies = getMovies(person);
            for (int i = 0; i < personMovies.size(); i++) {
                JSONObject movie = personMovies.getJSONObject(i);
                if(movie.containsKey(movieString)){
                    Double score = movie.getDouble(movieString);
                    sum+=Math.pow(score.doubleValue(), 2);
                }
            }
        }
        return sum;
    }

    private double sum(List<String> movies, String person) {
        double sum = 0;
        for (String movieString : movies) {
            JSONArray personMovies = getMovies(person);
            for (int i = 0; i < personMovies.size(); i++) {
                JSONObject movie = personMovies.getJSONObject(i);
                if(movie.containsKey(movieString)){
                    Double score = movie.getDouble(movieString);
                    sum += score.doubleValue();
                }
            }
        }
        return sum;
    }

    private List<String> sameRatedMovie(String personOne, String personTwo) {
        List<String> sameMovies = new ArrayList<>();
        JSONArray moviesForPersonOne = getMovies(personOne);
        JSONArray moviesForPersonTwo = getMovies(personTwo);

        for(int i=0;i<moviesForPersonOne.size();i++) {
            JSONObject movieOne = moviesForPersonOne.getJSONObject(i);
            Set<String> movieOneKey = movieOne.keySet();
            for (String key : movieOneKey) {
                for(int n=0;n<moviesForPersonTwo.size();n++) {
                    JSONObject movieTwo = moviesForPersonTwo.getJSONObject(n);
                    if(movieTwo.containsKey(key)){
                        sameMovies.add(key);
                    }
                }
            }

        }

        return sameMovies;
    }

    private JSONArray getMovies(String personOne) {
        for(int i=0;i<critics.size();i++) {
            JSONObject jsonObject = critics.getJSONObject(i);
            if(jsonObject.containsKey(personOne)){
                return jsonObject.getJSONArray(personOne);
            }
        }
        return null;
    }
}