Latent factors on Analysis Paralysis
https://blog.recommend.games/tags/latent-factors/
Recent content in Latent factors on Analysis ParalysisHugo -- gohugo.ioencontact@recommend.games (Recommend.Games)contact@recommend.games (Recommend.Games)Thu, 09 May 2024 21:47:57 +0300A brief introduction to Collaborative Filtering
https://blog.recommend.games/posts/collaborative-filtering/
Thu, 09 May 2024 21:47:57 +0300contact@recommend.games (Recommend.Games)https://blog.recommend.games/posts/collaborative-filtering/<h2 id="what-is-a-good-recommendation">What is a good recommendation?</h2>
<p>Collaborative filtering is the workhorse powering the recommendations by Recommend.Games. Over the years, I’ve been asked every now and then how it works. So, I thought it’s high time I outlined the basic ideas behind our recommendation engine.</p>
<p>Let’s first take a step back and talk about recommendations in general. What is it we’re trying to achieve? The answer to this question is far from trivial, and it gets harder when you want to formalise its goals. Maybe a somewhat naïve approach would be to say that we want to recommend items that the user will like. But recommendations are as much about predicting what the user wants as what they didn’t even know they wanted. Sometimes the most “correct” answer is also the least useful: maybe our #1 recommendation is <a href="https://recommend.games/#/game/266192" style="font-variant: small-caps;">Wingspan</a> and the user indeed would love to play it - but if they already knew about it, why recommend it in the first place?</p>
<p>To be honest, the solution that powers Recommend.Games pretty much ignores all those questions and asks a much simpler question: given the games a user has rated, can we predict how they would rate all the other games? We can then take the highest predicted ratings and use those to recommend games to the user.</p>
<h2 id="the-intuition-behind-collaborative-filtering">The intuition behind collaborative filtering</h2>
<p>Let’s make this a little more concrete and assume we only have six users we’ll call A through F. They’ve left the following ratings:</p>
<table>
<thead>
<tr>
<th>Game</th>
<th style="text-align:center">A</th>
<th style="text-align:center">B</th>
<th style="text-align:center">C</th>
<th style="text-align:center">D</th>
<th style="text-align:center">E</th>
<th style="text-align:center">F</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong><a href="https://recommend.games/#/game/266192" style="font-variant: small-caps;">Wingspan</a></strong></td>
<td style="text-align:center"><strong>10</strong></td>
<td style="text-align:center"><em>?</em></td>
<td style="text-align:center"><strong>3</strong></td>
<td style="text-align:center"><strong>7</strong></td>
<td style="text-align:center"><em>?</em></td>
<td style="text-align:center"><em>?</em></td>
</tr>
<tr>
<td><strong><a href="https://recommend.games/#/game/13" style="font-variant: small-caps;">CATAN</a></strong></td>
<td style="text-align:center"><em>?</em></td>
<td style="text-align:center"><strong>8</strong></td>
<td style="text-align:center"><strong>6</strong></td>
<td style="text-align:center"><em>?</em></td>
<td style="text-align:center"><strong>5</strong></td>
<td style="text-align:center"><strong>2</strong></td>
</tr>
<tr>
<td><strong><a href="https://recommend.games/#/game/174430" style="font-variant: small-caps;">Gloomhaven</a></strong></td>
<td style="text-align:center"><strong>9</strong></td>
<td style="text-align:center"><em>?</em></td>
<td style="text-align:center"><em>?</em></td>
<td style="text-align:center"><strong>10</strong></td>
<td style="text-align:center"><em>?</em></td>
<td style="text-align:center"><strong>8</strong></td>
</tr>
<tr>
<td><strong><a href="https://recommend.games/#/game/12333" style="font-variant: small-caps;">Twilight Struggle</a></strong></td>
<td style="text-align:center"><em>?</em></td>
<td style="text-align:center"><strong>7</strong></td>
<td style="text-align:center"><strong>5</strong></td>
<td style="text-align:center"><em>?</em></td>
<td style="text-align:center"><strong>6</strong></td>
<td style="text-align:center"><em>?</em></td>
</tr>
</tbody>
</table>
<p>Our task is to find a model to fill in the blanks. How would we go about this? The basic idea behind collaborative filtering is to find users with similar tastes and use their ratings to predict the missing ones. Let’s take a closer look at users <strong>C</strong> and <strong>E</strong>. They both seem to agree fairly well on their ratings for <a href="https://recommend.games/#/game/13" style="font-variant: small-caps;">CATAN</a> and <a href="https://recommend.games/#/game/12333" style="font-variant: small-caps;">Twilight Struggle</a>, so it’s a fair guess that user <strong>E</strong> would rate <a href="https://recommend.games/#/game/266192" style="font-variant: small-caps;">Wingspan</a> similar to user <strong>C</strong>. But we can also make this argument “in the other direction”: users seem to rate <a href="https://recommend.games/#/game/13" style="font-variant: small-caps;">CATAN</a> and <a href="https://recommend.games/#/game/12333" style="font-variant: small-caps;">Twilight Struggle</a> in general similarly, so one game’s ratings should be a good predictor for the other’s. So since users <strong>F</strong> seems to dislike <a href="https://recommend.games/#/game/13" style="font-variant: small-caps;">CATAN</a>, it’s a fair assumption that they would also dislike <a href="https://recommend.games/#/game/12333" style="font-variant: small-caps;">Twilight Struggle</a>.</p>
<h2 id="matrix-factorisation-via-alternating-least-squares">Matrix factorisation via alternating least squares</h2>
<p>That’s the high level idea behind collaborative filtering – but how do we actually implement it? If you know your Machine Learning 101, you might be familiar with linear regression, which tries to find a “line of best fit” for a set of given data points. We can think of collaborative filtering as linear regression in two directions: if we fix the games and try to predict the users’ ratings, we’re doing linear regression in the user space. If we fix the users and try to predict the games’ ratings, we’re doing linear regression in the game space. Collaborative filtering tries to do both at the same time – by alternating between the two, which is why this method is known as <em>alternating least squares</em> (ALS, “least squares” referring to minimising the squared error).</p>
<p>After running the algorithm, we end up with one vector for each user and one for each game. Taking the inner product (sometimes referred to as dot product) of those vectors yields a single number, the predicted rating. If we stack all the vectors for the users into a matrix \(U\) and the vectors for the games into a matrix \(G\), we can multiply those two matrices to get a matrix \(R\) of predicted ratings:</p>
<p>\[
R = U^\top \cdot G.
\]</p>
<p>In other words: we’ve taken the matrix of ratings from the table above and factored it into two matrices. So another way of thinking about collaborative filtering is as a matrix factorisation problem: we’re trying to find two matrices that, when multiplied together, approximate the original matrix as closely as possible.</p>
<h2 id="latent-factors-and-embeddings">Latent factors and embeddings</h2>
<p>Those user and game vectors are also known as <em>latent factors</em>. That’s because they take the high-dimensional space of users and games and project it into a lower-dimensional space. While those latent dimensions don’t carry any particular human interpretable meaning, they capture the essence of what makes a user like a game. For example, one latent factor might capture how much a user likes games with a lot of player interaction, while another might capture how much they like games with a lot of strategic depth. Note that we can freely choose the number of latent factors, which is an important hyperparameter of the model. The more latent factors we use, the more expressive the model can be – but it also becomes more prone to overfitting. Recommend.Games uses 32 latent dimensions, which is a decent default choice for many recommendation problems.<sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup></p>
<p>The latent factors also have some interesting properties. For example, there’s a meaningful distance between them: if two users are close together in the latent space, they have similar tastes. The distance in question is the cosine similarity, which measures the angle between two vectors. Think of it this way: if the vectors for two users point in the same direction (cosine similarity close to 1), they have similar tastes. If they point in opposite directions (cosine similarity close to -1), they have opposite tastes. If they’re orthogonal (cosine similarity close to 0), they have no correlation.</p>
<p>The same goes for the game vectors: we can calculate the cosine similarity between two games to see if they appeal to the same users. Every game page on Recommend.Games has a “You might also like” section at the bottom. Those are the games with the highest cosine similarity to the game in question.</p>
<p>Vector representations with some measure of distance are called <em>embeddings</em>. So this is yet another way of thinking about collaborative filtering: we’re embedding users and games into a latent space where we can measure their similarity.</p>
<p>So, this was a pretty lengthy and technical article, but I hope it provides some intuition of how the recommendation engine behind Recommend.Games works. Obviously, there are a lot more details to it, but those are the most important ideas. One of the reasons why I wanted to talk about this topic is that we can have more fun with those latent factors, so stay tuned for more articles on this topic!</p>
<div class="footnotes" role="doc-endnotes">
<hr>
<ol>
<li id="fn:1">
<p>The number of latent dimensions also affects the size of the model. As mentioned, we have one latent vector for every user and every game, each with 32 entries. Currently, we have over 500 thousand users and over 90 thousand games, so the matrix \(U\) has over \(500\,000 \cdot 32 = 16\,000\,000\) entries and the matrix \(G\) has over \(90\,000 \cdot 32 = 2\,880\,000\) entries. Each entry is a 64-bit (8 bytes) floating point number, so the total size of the model is around \((16\,000\,000 + 2\,880\,000) \cdot 8\,\text{B} \approx 150\,\text{MB}\). It’s not a negligible size to load into the memory of a cheap server (which is what we’re using), but in the modern age of deep neural networks with gazillions of parameters, it feels almost tiny. <a href="#fnref:1" class="footnote-backref" role="doc-backlink">↩︎</a></p>
</li>
</ol>
</div>