{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Probabilistic Matrix Factorization for Making Personalized Recommendations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The model discussed in this analysis was developed by Ruslan Salakhutdinov and Andriy Mnih. All of the code and supporting text, when not referenced, is the original work of [Mack Sweeney](https://www.linkedin.com/in/macksweeney).\n", "\n", "## Motivation\n", "\n", "Say I download a handbook of a hundred jokes, and I'd like to know very quickly which ones will be my favorite. So maybe I read a few, I laugh, I read a few more, I stop laughing, and I indicate on a scale of -10 to 10 how funny I thought each joke was. Maybe I do this for 5 jokes out of the 100. Now I go to the back of the book, and there's a little program included for calculating my preferences for all the other jokes. I enter in my preference numbers and shazam! The program spits out a list of all 100 jokes, sorted in the order I'll like them. That certainly would be nice. Today we'll write a program that does exactly this.\n", "\n", "We'll start out by getting some intuition for how our model will work. Then we'll formalize our intuition. Afterwards, we'll examine the dataset we are going to use. Once we have some notion of what our data looks like, we'll define some baseline methods for predicting preferences for jokes. Following that, we'll look at Probabilistic Matrix Factorization (PMF), which is a more sophisticated Bayesian method for predicting preferences. Having detailed the PMF model, we'll use PyMC3 for MAP estimation and MCMC inference. Finally, we'll compare the results obtained with PMF to those obtained from our baseline methods and discuss the outcome.\n", "\n", "## Intuition\n", "\n", "Normally if we want recommendations for something, we try to find people who are similar to us and ask their opinions. If Bob, Alice, and Monty are all similar to me, and they all like knock-knock jokes, I'll probably like knock-knock jokes. Now this isn't always true. It depends on what we consider to be \"similar\". In order to get the best bang for our buck, we really want to look for people who have the most similar sense of humor. Humor being a complex beast, we'd probably like to break it down into something more understandable. We might try to characterize each joke in terms of various factors. Perhaps jokes can be dry, sarcastic, crude, sexual, political, etc. Now imagine we go through our handbook of jokes and assign each joke a rating in each of the categories. How dry is it? How sarcastic is it? How much does it use sexual innuendos? Perhaps we use numbers between 0 and 1 for each category. Intuitively, we might call this the joke's humor profile.\n", "\n", "Now let's suppose we go back to those 5 jokes we rated. At this point, we can get a richer picture of our own preferences by looking at the humor profiles of each of the jokes we liked and didn't like. Perhaps we take the averages across the 5 humor profiles and call this our ideal type of joke. In other words, we have computed some notion of our inherent _preferences_ for various types of jokes. Suppose Bob, Alice, and Monty all do the same. Now we can compare our preferences and determine how similar each of us really are. I might find that Bob is the most similar and the other two are still more similar than other people, but not as much as Bob. So I want recommendations from all three people, but when I make my final decision, I'm going to put more weight on Bob's recommendation than those I get from Alice and Monty.\n", "\n", "While the above procedure sounds fairly effective as is, it also reveals an unexpected additional source of information. If we rated a particular joke highly, and we know its humor profile, we can compare with the profiles of other jokes. If we find one with very close numbers, it is probable we'll also enjoy this joke. Both this approach and the one above are commonly known as _neighborhood approaches_. Techniques that leverage both of these approaches simultaneously are often called _collaborative filtering_ [[1]](http://www2.research.att.com/~volinsky/papers/ieeecomputer.pdf). The first approach we talked about uses user-user similarity, while the second uses item-item similarity. Ideally, we'd like to use both sources of information. The idea is we have a lot of items available to us, and we'd like to work together with others to filter the list of items down to those we'll each like best. My list should have the items I'll like best at the top and those I'll like least at the bottom. Everyone else wants the same. If I get together with a bunch of other people, we all read 5 jokes, and we have some efficient computational process to determine similarity, we can very quickly order the jokes to our liking.\n", "\n", "## Formalization\n", "\n", "Let's take some time to make the intuitive notions we've been discussing more concrete. We have a set of $M$ jokes, or _items_ ($M = 100$ in our example above). We also have $N$ people, whom we'll call _users_ of our recommender system. For each item, we'd like to find a $D$ dimensional factor composition (humor profile above) to describe the item. Ideally, we'd like to do this without actually going through and manually labeling all of the jokes. Manual labeling would be both slow and error-prone, as different people will likely label jokes differently. So we model each joke as a $D$ dimensional vector, which is its latent factor composition. Furthermore, we expect each user to have some preferences, but without our manual labeling and averaging procedure, we have to rely on the latent factor compositions to learn $D$ dimensional latent preference vectors for each user. The only thing we get to observe is the $N \\times M$ ratings matrix $R$ provided by the users. Entry $R_{ij}$ is the rating user $i$ gave to item $j$. Many of these entries may be missing, since most users will not have rated all 100 jokes. Our goal is to fill in the missing values with predicted ratings based on the latent variables $U$ and $V$. We denote the predicted ratings by $R_{ij}^*$. We also define an indicator matrix $I$, with entry $I_{ij} = 0$ if $R_{ij}$ is missing and $I_{ij} = 1$ otherwise.\n", "\n", "So we have an $N \\times D$ matrix of user preferences which we'll call $U$ and an $M \\times D$ factor composition matrix we'll call $V$. We also have a $N \\times M$ rating matrix we'll call $R$. We can think of each row $U_i$ as indications of how much each user prefers each of the $D$ latent factors. Each row $V_j$ can be thought of as how much each item can be described by each of the latent factors. In order to make a recommendation, we need a suitable prediction function which maps a user preference vector $U_i$ and an item latent factor vector $V_j$ to a predicted ranking. The choice of this prediction function is an important modeling decision, and a variety of prediction functions have been used. Perhaps the most common is the dot product of the two vectors, $U_i \\cdot V_j$ [[1]](http://www2.research.att.com/~volinsky/papers/ieeecomputer.pdf).\n", "\n", "To better understand CF techniques, let us explore a particular example. Imagine we are seeking to recommend jokes using a model which infers five latent factors, $V_j$, for $j = 1,2,3,4,5$. In reality, the latent factors are often unexplainable in a straightforward manner, and most models make no attempt to understand what information is being captured by each factor. However, for the purposes of explanation, let us assume the five latent factors might end up capturing the humor profile we were discussing above. So our five latent factors are: dry, sarcastic, crude, sexual, and political. Then for a particular user $i$, imagine we infer a preference vector $U_i = <0.2, 0.1, 0.3, 0.1, 0.3>$. Also, for a particular item $j$, we infer these values for the latent factors: $V_j = <0.5, 0.5, 0.25, 0.8, 0.9>$. Using the dot product as the prediction function, we would calculate 0.575 as the ranking for that item, which is more or less a neutral preference given our -10 to 10 rating scale.\n", "\n", "$$ 0.2 \\times 0.5 + 0.1 \\times 0.5 + 0.3 \\times 0.25 + 0.1 \\times 0.8 + 0.3 \\times 0.9 = 0.575 $$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data\n", "\n", "The [v1 Jester dataset](http://eigentaste.berkeley.edu/dataset/) provides something very much like the handbook of jokes we have been discussing. The original version of this dataset was constructed in conjunction with the development of the [Eigentaste recommender system](http://eigentaste.berkeley.edu/about.html) [[2]](http://goldberg.berkeley.edu/pubs/eigentaste.pdf). At this point in time, v1 contains over 4.1 million continuous ratings in the range [-10, 10] of 100 jokes from 73,421 users. These ratings were collected between Apr. 1999 and May 2003. In order to reduce the training time of the model for illustrative purposes, 1,000 users who have rated all 100 jokes will be selected randomly. We will implement a model that is suitable for collaborative filtering on this data and evaluate it in terms of root mean squared error (RMSE) to validate the results.\n", "\n", "Let's begin by exploring our data. We want to get a general feel for what it looks like and a sense for what sort of patterns it might contain." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | 1 | \n", "2 | \n", "3 | \n", "4 | \n", "5 | \n", "6 | \n", "7 | \n", "8 | \n", "9 | \n", "10 | \n", "... | \n", "91 | \n", "92 | \n", "93 | \n", "94 | \n", "95 | \n", "96 | \n", "97 | \n", "98 | \n", "99 | \n", "100 | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "4.08 | \n", "-0.29 | \n", "6.36 | \n", "4.37 | \n", "-2.38 | \n", "-9.66 | \n", "-0.73 | \n", "-5.34 | \n", "8.88 | \n", "9.22 | \n", "... | \n", "2.82 | \n", "-4.95 | \n", "-0.29 | \n", "7.86 | \n", "-0.19 | \n", "-2.14 | \n", "3.06 | \n", "0.34 | \n", "-4.32 | \n", "1.07 | \n", "
1 | \n", "-6.17 | \n", "-3.54 | \n", "0.44 | \n", "-8.50 | \n", "-7.09 | \n", "-4.32 | \n", "-8.69 | \n", "-0.87 | \n", "-6.65 | \n", "-1.80 | \n", "... | \n", "-3.54 | \n", "-6.89 | \n", "-0.68 | \n", "-2.96 | \n", "-2.18 | \n", "-3.35 | \n", "0.05 | \n", "-9.08 | \n", "-5.05 | \n", "-3.45 | \n", "
2 | \n", "6.84 | \n", "3.16 | \n", "9.17 | \n", "-6.21 | \n", "-8.16 | \n", "-1.70 | \n", "9.27 | \n", "1.41 | \n", "-5.19 | \n", "-4.42 | \n", "... | \n", "7.23 | \n", "-1.12 | \n", "-0.10 | \n", "-5.68 | \n", "-3.16 | \n", "-3.35 | \n", "2.14 | \n", "-0.05 | \n", "1.31 | \n", "0.00 | \n", "
3 | \n", "-3.79 | \n", "-3.54 | \n", "-9.42 | \n", "-6.89 | \n", "-8.74 | \n", "-0.29 | \n", "-5.29 | \n", "-8.93 | \n", "-7.86 | \n", "-1.60 | \n", "... | \n", "4.37 | \n", "-0.29 | \n", "4.17 | \n", "-0.29 | \n", "-0.29 | \n", "-0.29 | \n", "-0.29 | \n", "-0.29 | \n", "-3.40 | \n", "-4.95 | \n", "
4 | \n", "1.31 | \n", "1.80 | \n", "2.57 | \n", "-2.38 | \n", "0.73 | \n", "0.73 | \n", "-0.97 | \n", "5.00 | \n", "-7.23 | \n", "-1.36 | \n", "... | \n", "1.46 | \n", "1.70 | \n", "0.29 | \n", "-3.30 | \n", "3.45 | \n", "5.44 | \n", "4.08 | \n", "2.48 | \n", "4.51 | \n", "4.66 | \n", "
5 rows × 100 columns
\n", "