<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.3.4">Jekyll</generator><link href="https://djrich.dev/feed.xml" rel="self" type="application/atom+xml" /><link href="https://djrich.dev/" rel="alternate" type="text/html" /><updated>2026-11-24T19:33:02+00:00</updated><id>https://djrich.dev/feed.xml</id><title type="html">DJ Rich</title><subtitle>Personal website of DJ Rich. Articles on machine learning, statistics, and financial engineering.</subtitle><author><name>Duane Rich</name></author><entry><title type="html">TrueSkill Part 2: Who is the GOAT?</title><link href="https://djrich.dev/concepts/machine-learning-and-other-topics/true-skill-part-2/" rel="alternate" type="text/html" title="TrueSkill Part 2: Who is the GOAT?" /><published>2025-02-26T00:00:00+00:00</published><updated>2025-02-26T00:00:00+00:00</updated><id>https://djrich.dev/concepts/machine-learning-and-other-topics/true-skill-part-2</id><content type="html" xml:base="https://djrich.dev/concepts/machine-learning-and-other-topics/true-skill-part-2/"><![CDATA[<p>In the <a href="/concepts/machine-learning-and-other-topics/true-skill-part-1/">previous post</a>, we argued that the question of ‘Who is the Greatest Of All Time?’ for any competitive game is answerable with an algorithm. They can account for the facts that skills vary over time and many players never played each other in their prime.</p>

<p>To demonstrate, we reviewed the mechanics of the TrueSkill algorithm, invented by a team from Microsoft Research and in response to the inability of the Elo rating system to be applied to multiplayer, multiteam games.</p>

<p>In this post, we’ll use a variant of this algorithm, ‘TrueSkill Through Time,’ and match data to answer the ‘Who is the GOAT?’ question for Tennis, Boxing, and Warcraft 3.</p>

<h2 id="trueskill-through-time">TrueSkill Through Time</h2>

<p>As mentioned at the end of Part 1, the ‘vanilla’ TrueSkill requires some adjustment to properly answer the GOAT question. First, a player’s skill estimate only depends on <em>past</em> data. This is a fair condition if the skill is to be used for forecasting, but it’s suboptimal for the GOAT question. If a player beats another who later becomes a top-tier player, that player’s skill should be revised upward in retrospect. In other words, defeating Mike Tyson in his first professional bout should create a substantial skill boost to the victor.</p>

<p>Second, vanilla TrueSkill indexes time with matches. It treats two matches separated by three days the same as two matches separated by three years. In reality, skills evolve over time, even when no matches occur.</p>

<p>These issues are addressed by the <a href="https://www.microsoft.com/en-us/research/publication/trueskill-through-time-revisiting-the-history-of-chess/">TrueSkill Through Time</a> algorithm. In this analysis, we use <a href="https://trueskillthroughtime.readthedocs.io/en/latest/">the implementation</a> developed by Gustavo Landfried.</p>

<h2 id="tennis">Tennis</h2>

<p>Our first application is to tennis (as <a href="https://trueskillthroughtime.readthedocs.io/en/latest/man/examples.html">Landfried did as well</a>). The code, available <a href="https://github.com/Duane321/true_theta/blob/main/posts/trueskill/tennis.ipynb">here</a>, produces skill estimates over time:</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/trueskill/tennis_skills_over_time.png" alt="Tennis Skills" style="width: 100%" />
<span class="figure-caption">Skill estimates, along with one standard deviation uncertainty bands, for some top tennis players over the past four decades.</span></p>

<p>For readability, the skill estimates are shown for select top players over the past forty or so years. Specifically, they come from a dataset of 284,664 matches involving 12,307 players over the past 110 years.</p>

<p>A remaining question is, ‘how do we know these estimates are any good?’ To answer that, we’ve held out the past four years of data and produced the following calibration plot.</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/trueskill/tennis_calibration.png" alt="Tennis skill estimate" style="width: 60%" />
<span class="figure-caption">This test-set calibration plot answers the question ‘If the model predicts a win probability of x percent, what is the actual win percentage?’ For a well calibrated model, these numbers should be close; the points plotted should fall along the diagonal line. </span></p>

<p>The model is well calibrated, giving us confidence in its estimates<sup id="fnref:1"><a href="#fn:1" class="footnote" rel="footnote" role="doc-noteref">1</a></sup>. Therefore, we can answer the GOAT question confidently:</p>

<blockquote>
  <p>Established with his 2016 season, <strong>Novak Djokovic</strong> is the greatest of all time. If every player played every other player in their primes, Djokovic is estimated to have the highest win probability.</p>
</blockquote>

<p>As further validation, his 2016 season is <a href="https://en.wikipedia.org/wiki/2016_Novak_Djokovic_tennis_season">noted independently as remarkable</a>.</p>

<h2 id="warcraft-3">Warcraft 3</h2>

<p>Warcraft 3, a classic real time strategy game with a small-but-loyal player base, may seem like an unusual choice relative to tennis and boxing. It was included primarily because it has substantial data. Some players play multiple games a day for many years. Secondarily, I personally have some nostalgia for it and enjoyed the excuse to explore the game’s data.</p>

<p>With <a href="https://github.com/Duane321/true_theta/blob/main/posts/trueskill/warcraft3.ipynb">this notebook</a>, we produce the following skill estimates.</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/trueskill/warcraft3_skills_over_time.png" alt="Warcraft 3 Skills" style="width: 100%" loading="lazy" />
<span class="figure-caption">Skill estimates of five top players over the past 6 years. </span></p>

<p>Again, we view the calibration plot to evaluate the model.</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/trueskill/warcraft3_calibration.png" alt="Warcraft 3 Calibration Plot" style="width: 60%" loading="lazy" />
<span class="figure-caption">The skill estimates for Warcraft 3 are mostly well calibrated.</span></p>

<p>The model is slightly overconfident with predicted win probabilities between 10-30% or 70-90%. This suggests that the single hyperparameter \(\beta\) may not be flexible enough to capture the game’s noisy dynamics; the level of ‘luck’ may depend on the difference in skills.</p>

<p>Nonetheless, this issue would not change the answer to the GOAT question. We may say confidently:</p>

<blockquote>
  <p>Established in late 2021, <a href="https://liquipedia.net/warcraft/Happy"><strong>Dmitry “Happy” Kostin</strong></a> is the greatest of all time.</p>
</blockquote>

<p>For those experienced with the game, this is not surprising, since Happy has been dominant for nearly a decade.</p>

<h2 id="boxing">Boxing</h2>

<p>Boxing was the original motivation for the analysis, since ‘Who is the greatest boxer of all time?’ is a fiercely debated question. Rather disappointingly, the data is not as friendly. Using <a href="https://github.com/Duane321/true_theta/blob/main/posts/trueskill/boxing.ipynb">this notebook</a>, we get the following skill estimates.</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/trueskill/boxing_skills_over_time.png" alt="Boxing Skills" style="width: 100%" loading="lazy" />
<span class="figure-caption">Skill estimates of some top boxers.</span></p>

<p>The uncertainty bands are significantly wider than in previous cases, and the model linearly interpolates skills over long periods without matches. This follows from the sparsity of the data; boxing matches are much rarer than tennis or Warcraft 3 matches. Over their career, a boxer will have a few dozen fights, whereas some tennis players play thousands of matches.</p>

<p>As a result of this sparsity, the model is not especially well calibrated.</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/trueskill/boxing_calibration.png" alt="Boxing Calibration Plot" style="width: 60%" />
<span class="figure-caption">The TrueSkill model for boxing is not well calibrated, but there is a strong correlation between predicted and actual win probabilities</span></p>

<p>Despite this, the model seems to rank skill well. According to the calibration plot, an increase in skill corresponds to an increase in test-set win probability. For this, we can have some confidence in the GOAT answer:</p>

<blockquote>
  <p>Due to his undefeated record, including fights with top boxers, <strong>Floyd Mayweather Jr.</strong> is the greatest of all time.</p>
</blockquote>

<p>However, the application to boxing has some notable differences from tennis and Warcraft 3. First, boxing has weight classes, meaning every match is controlled for this variable. Because of this, Floyd Mayweather Jr. in his prime is unlikely to beat heavier boxers in their prime. So ‘greatest of all time’ is really a pound-for-pound greatest, since the data is controlled for weight.</p>

<p>Further, weight classes strongly separate the data. They represent groups within which matches happen frequently, and between which matches are rare. This severely weakens the comparability across weight classes. This is another source of incomparability of boxers from different weight classes.</p>

<h2 id="in-closing">In Closing</h2>

<p>As we’ve seen, the TrueSkill Through Time algorithm—and its variants—really does allow us to tackle one of the most contentious debates in sports: <em>Who is the GOAT?</em> The calibration plots bolster our confidence by showing that the model does predict matches well, and its conclusions align with what seasoned observers of these sports often say. Whether it’s identifying Djokovic’s remarkable peak in tennis, Happy’s dominance in Warcraft 3, or Mayweather’s supremacy in (pound-for-pound) boxing, the results check out. Moreover, other domains, like chess, can be <a href="https://papers.nips.cc/paper_files/paper/2007/file/9f53d83ec0691550f7d2507d57f4f5a2-Paper.pdf">similarly analyzed</a>. After all, wherever structured match data exists, so does the opportunity to learn about the greatest of all time.</p>

<p>Lastly, I’d like to acknowledge the depth of skill estimation. In state-of-the-art applications, much more contextual data can be incorporated than presented here and algorithms can be more finely tuned and evaluated. For those interested in finding the very best algorithm for esports skill estimation in particular, I recommend exploring the <a href="https://github.com/cthorrez/riix">riix</a> package, designed to evaluate and compare algorithms.</p>

<h3 id="references">References</h3>

<ol>
  <li>P. Dangauthier, R. Herbrich, T. Minka, T. Graepel. <a href="https://www.microsoft.com/en-us/research/publication/trueskill-through-time-revisiting-the-history-of-chess/">TrueSkill Through Time: Revisiting the History of Chess</a> In Advances in Neural Information Processing Systems. MIT Press, January 2008.</li>
</ol>

<hr />

<h3 id="footnotes">Footnotes</h3>

<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:1">
      <p>It should be noted that a good-looking calibration plot is not a complete evaluation of the model. What is missing is the proportions in which the model is giving certain predictions. A perfect model would predict only 0% and 100% and never be wrong. A poor but well calibrated model would predict 50% almost all of the time. In all games, it is unsurprising that the win rate for a random player is 50%. <a href="#fnref:1" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
  </ol>
</div>]]></content><author><name>Duane Rich</name></author><category term="machine-learning-and-other-topics" /><category term="Post Formats" /><summary type="html"><![CDATA[We use the TrueSkill algorithm to answer 'Who is the GOAT?' for Boxing, Tennis and Warcraft 3.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://djrich.dev/assets/images/posts/trueskill/tennis_skills_over_time_zoomed.png" /><media:content medium="image" url="https://djrich.dev/assets/images/posts/trueskill/tennis_skills_over_time_zoomed.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">TrueSkill Part 1: The Algorithm</title><link href="https://djrich.dev/concepts/machine-learning-and-other-topics/true-skill-part-1/" rel="alternate" type="text/html" title="TrueSkill Part 1: The Algorithm" /><published>2025-02-18T00:00:00+00:00</published><updated>2025-02-19T00:00:00+00:00</updated><id>https://djrich.dev/concepts/machine-learning-and-other-topics/true-skill-part-1</id><content type="html" xml:base="https://djrich.dev/concepts/machine-learning-and-other-topics/true-skill-part-1/"><![CDATA[<p>Who is the greatest boxer of all time? Who is the ‘GOAT?’ Is it Muhammad Ali? Mike Tyson? Someone else?</p>

<p>Because they never fought each other, you might say it can’t be known. Indeed, it can’t be <em>known</em>, but it can be estimated.</p>

<p>The absence of a prime Tyson vs prime Ali match does not mean we have no evidence. Something can be inferred from Tyson’s win over Larry Holmes and Larry Holmes’ win over Ali. Additional evidence comes from a chain of two fighters, one who fought Tyson, one who fought Ali and who fought each other. Still more evidence comes from a chain of three fighters, and so on. Collectively, across all chains, there’s substantial evidence to predict the outcome of the Tyson vs Ali fight.</p>

<p>An immediate critique is that Ali fought Holmes at the end of his career and Tyson fought him near his prime. This effect can be handled as well, with an assumption that skill evolves incrementally over time and with experience.</p>

<blockquote>
  <p>Introduced in <a href="https://www.microsoft.com/en-us/research/publication/trueskilltm-a-bayesian-skill-rating-system/" target="_blank" rel="noopener noreferrer">Herbrich et al. (2007)</a>, the <span style="color:#205081"><strong>TrueSkill</strong></span> algorithm captures these effects beautifully. With some extensions, Microsoft deploys it at scale to matchmake many millions of gamers annually. Beyond its applied utility, it is an exceptional victory for Bayesian methods, where simple assumptions and probabilistic reasoning produce accurate estimates of counterfactual matchups.</p>
</blockquote>

<p>Further, the algorithm is game agnostic. All that is needed is when-and-who data on matches. As such, it can be applied to boxing, tennis, chess, MMA, and others. In fact, it can be applied to arbitrarily team multiplayer games, like Halo.</p>

<h2 id="the-elo-rating-system">The Elo Rating System</h2>

<p>To motivate, let’s consider the issue with counting metrics to evaluate skill. If you were to evaluate a chess player’s skill, which metric would you use? Win Rate?</p>

<p>That creates a problem. Win rate doesn’t account for the strength of the opponent. A win rate of 20% against exclusively Magnus Carlsen, arguably the GOAT, is much more impressive than a 90% win rate against an amateur.</p>

<p>This is why the <em>Elo rating system</em> is used for chess and many other two player games.</p>

<p>The idea is to assign skill values to players and to model the probability of a win as the difference of their skills. If player 1 has skill \(s_1\) and player 2 has skill \(s_2\), the probability player 1 wins is model as<sup id="fnref:1"><a href="#fn:1" class="footnote" rel="footnote" role="doc-noteref">1</a></sup>:</p>

\[\Phi\Big(\frac{s_1 - s_2}{\sqrt{2}\beta}\Big)\]

<p>where \(\Phi(\cdot)\) is the cumulative density of a standard normal distribution. It looks like:</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/trueskill/normal_CDF.png" alt="Normal CDF" style="width: 65%" />
<span class="figure-caption">The cumulative density function of the standard normal distribution. It is chosen for its S-shape, nothing more.</span></p>

<p>The effect is that if player 1 is much more skilled than player 2, \(s_1 - s_2\) is large and positive and the probability of plyer 1 winning is close to one. Similarly, it’s close to zero when player 2 is much more skilled than player 1. \(\beta\) is a tunable parameter to set what counts as ‘large’.</p>

<p>This becomes useful when we have \(N\) players who have played each other many times. Per maximum likelihood estimation, we can estimate \(s_1, s_2, \cdots, s_N\) by applying the win probability to all observed games and finding those that make the observations most likely.</p>

<blockquote>
  <p>The key is that skills are estimated <em>jointly</em>. They are inferred simultaneously, from observed match outcomes to skill differences to skill values. This means all skills are determined in relation to each other, such that skill differences explain the data maximally well.</p>
</blockquote>

<p>Admittedly, I’ve oversimplified things. In practice, skills evolve over time and the optimization can be converted into convenient update rules to be applied after each match. Regardless, this online procedure does not contradict the skills’ relative definition.</p>

<h2 id="problems-with-elo">Problems with Elo</h2>

<p>In 2005, Microsoft Research considered Elo when looking to improve their matchmaking for XBox Live and Halo specifically.</p>

<blockquote>
  <p>Elo wasn’t the answer for a few reasons:</p>

  <ul>
    <li><strong>There is no measure of uncertainty.</strong> Surely, there should be something separating the skill of a player who has played once and another who has played 100+ times.</li>
    <li><strong>It only applies to two player games.</strong> Complicating things, Halo normally involves more than two players.</li>
    <li><strong>There are no teams.</strong> Halo could involve free-for-all matches or team-based play.</li>
  </ul>
</blockquote>

<h2 id="trueskill-what-it-does">TrueSkill: What It Does</h2>

<p>In 2006, TrueSkill was invented in response, and it has since stood the test of time.</p>

<p>To understand it, we’ll begin with TrueSkill’s assumptions. It assumes the data is produced by a certain data generating process or simulation. Rather than describing it precisely and in its full generality, I’ll illustrate the essentials.</p>

<p>Suppose there are \(N\) players playing over \(M\) matches. In fact, <em>with</em> loss of generality, we’re treating the match index like a time index, as though there were one match per timestep, involving all players. Further, each match could be a free-for-all, or with two-player teams, or three-player teams. This is also a loss of generality, since TrueSkill works with arbitrary teams.</p>

<p>Next, we assume players skill evolve as random gaussian walks over the matches. With three players, that looks like:</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/trueskill/skill_over_time.png" alt="Skills Over Time" style="width: 90%" loading="lazy" />
<span class="figure-caption">The match index \(m\) acts like a time index, where we assume there is one match per day involving all players. In this case, skills of three players evolve like a gaussian random walk.</span></p>

<p>This can be expressed as \(s_{i, m+1} \sim \mathcal{N}(s_{i, m}, \gamma^2)\), where \(s_{i, m}\) is player \(i\)’s skill at match \(m\) and \(\gamma\) is a parameter of the simulation, giving the standard deviation of skill change with each match.</p>

<p>The match outcome is simulated as follows. \(p_{i, m}\) is player \(i\)’s <em>performance</em> on match \(m\) and is normally distributed around their skill. That is, \(p_{i, m} \sim \mathcal{N}(s_{i, m}, \beta^2)\). The standard deviation of performance, \(\beta\), indicates the game’s noisiness. Poker, with its element of chance, has a higher \(\beta\) than chess. For two players and a game without draws, their performances are normally distributed about their skills and the winner, denoted with a green dot, has the higher performance.</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/trueskill/performances_of_2.png" alt="Skills and performances" style="width: 90%" loading="lazy" />
<span class="figure-caption">Performances, indicated as circles, are normally distributed, centered on player skill. In this two player game without draws, the player with the higher performance wins, indicated with a green dot.</span></p>

<p>In the style of Elo, the probability of a win for player 1 on match \(m\) is:</p>

\[\Phi\Big(\frac{s_{1, m} - s_{1, m}}{\sqrt{2}\beta}\Big)\]

<p>However, unlike Elo, TrueSkill can handle teams. To do this, it assumes the performance of the team is the sum of its players’ performances. If there were \(N=6\) players with three teams of two, then:</p>

\[\begin{align}
t_{1, m} &amp;= p_{1, m} + p_{2, m}\\
t_{2, m} &amp;= p_{3, m} + p_{4, m}\\
t_{3, m} &amp;= p_{5, m} + p_{6, m}
\end{align}\]

<p>In this case, the team with the highest performance wins. Similar to the two player case, the probability that team 1 ranks above team 2 in match \(m\) is:</p>

\[\Phi\Big(\frac{(s_{1, m} + s_{2, m}) - (s_{3, m} + s_{4, m})}{\sqrt{2}\beta}\Big)\]

<p>It should be noted that in multi-team matches, the outcome is not binary; it is a <em>ranking</em> of teams. In a three team match, a match outcome might be \(2, 3, 1\), showing team \(2\) came in first and team \(1\) came in last. These rankings and which players are on which teams constitution the observations; we know their values when applying TrueSkill. Picture that like this:</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/trueskill/observations.png" alt="Observations" style="width: 100%" />
<span class="figure-caption">The top plot shows which team each player is on for every match. The bottom plot shows the team rankings for every match.</span></p>

<p>Using only the data shown here, TrueSkill infers players skills and provides uncertainties, quantifying the inference quality.</p>

<p>Seeing this inference is what is enabled by our simulation. We can feed TrueSkill the generated observations, pull out skill estimates and their uncertainties, and compare them to the simulated skills. For one player, we get:</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/trueskill/skill_inferred.png" alt="Observations" style="width: 100%" loading="lazy" />
<span class="figure-caption">For a player, TrueSkill infers their skill from the match outcomes. </span></p>

<p>This gives the spirit of TrueSkill, but to show this, a cheat was necessary. Methods like this run into the problem of <em>identifiability</em>; skills cannot be uniquely inferred from observations. One reason is that the probability of a match outcome depends only on skill <em>differences</em>, and there is a whole range of skill pairs with a given skill difference<sup id="fnref:2"><a href="#fn:2" class="footnote" rel="footnote" role="doc-noteref">2</a></sup>. So to create this satisfying recovering-of-the-truth, I allowed TrueSkill to know the actual skill of one player. This anchored the other estimates.</p>

<p>In applied cases, identifiability is not an issue for TrueSkill. It is anchored to match outcome probabilities, and those are the essential ingredients to matchmaking and the primary counterfactual question. In other words, we only care about skill differences and those can be uniquely determined.</p>

<h2 id="trueskill-how-it-works">TrueSkill: How It Works</h2>

<p>The math is sophisticated and detailed, so I will go just deep enough to capture the basics. We will follow the presentation of <a href="https://www.microsoft.com/en-us/research/publication/trueskilltm-a-bayesian-skill-rating-system/" target="_blank" rel="noopener noreferrer">Herbrich et al. (2007)</a>, and explain the computation of a single match, ignoring the random walk of skills over matches. If \(\mathbf{s}\) is a vector of player skills for a match, the goal is to compute its posterior distribution given the observation. That is, we want:</p>

\[p(\mathbf{s} \vert \mathbf{r}, A)\]

<p>where \(A\) tells us which players are on which teams and \(\mathbf{r}\) is the team rankings. Ultimately, we won’t be concerned with the full joint distribution over \(\mathbf{s}\). Rather, we will model each \(s_i\) with a normal distribution, where its mean and variance are the skill estimate and uncertainty we seek. This skill distribution for player \(i\) will be conditional on \(\mathbf{r}\) and \(A\), <em>and</em> on the estimates of the other players.</p>

<p>The example considered has player 1 on team 1, players 2 and 3 on team 2 and player 4 on team 3. The rankings are \((1, 2, 2)\); team 1 came in first and teams 2 and 3 tied for second. We have thus far avoided ties, but these are supported in TrueSkill. Teams tie when their performance is within some small difference \(\epsilon\). In this case, \(\vert t_2  - t_3 \vert &lt; \epsilon\).</p>

<p>To compute the posterior over skills, we must integrate out all possible performances, for both teams and players. Written out explicitly, \(p(\mathbf{s} \vert \mathbf{r}, A)\) is equal to the follow integral:</p>

\[\small
\int_\infty^\infty \int_\infty^\infty \int_\infty^\infty \int_\infty^\infty \int_\infty^\infty \int_\infty^\infty \int_\infty^\infty p(\mathbf{s}, p_1, p_2, p_3, p_4, t_1, t_2, t_3 \vert \mathbf{r}, A)dp_1dp_2dp_3dp_4dt_1dt_2dt_3\]

<p>In this generic form, this is not an easy calculation, even for a single match. However, we can exploit structure in the TrueSkill assumptions for efficiency.</p>

<p>To see this, we start with computing the integrand, more compactly expressed as \(p(\mathbf{s}, \mathbf{p}, \mathbf{t} \vert \mathbf{r}, A)\). This computation is represented with Figure 1 from <a href="https://www.microsoft.com/en-us/research/publication/trueskilltm-a-bayesian-skill-rating-system/" target="_blank" rel="noopener noreferrer">Herbrich et al. (2007)</a>, copied here:</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/trueskill/figure_1_from_original_paper.png" alt="illustration" style="width: 100%" />
<span class="figure-caption">This is a factor graph representing the computation of \(p(\mathbf{s}, \mathbf{p}, \mathbf{t} \vert \mathbf{r}, A)\) under the TrueSkill data generation assumptions.</span></p>

<p>This is a <em>factor graph</em> representation of \(p(\mathbf{s}, \mathbf{p}, \mathbf{t} \vert \mathbf{r}, A)\), where \(\mathbf{r}=(1,2,2)\) and \(A\) represents the team participations, as described. To be clear, \(\mathbf{r}\) and \(A\) dictate the shape of the graph. Just like \(p(\mathbf{s}, \mathbf{p}, \mathbf{t} \vert \mathbf{r}, A)\), think of this graph as something that receives values of \(\mathbf{s}, \mathbf{p}, \mathbf{t}\) and gives back a number. Though it is more than that, as we’ll see.</p>

<p>Computing \(p(\mathbf{s}, \mathbf{p}, \mathbf{t} \vert \mathbf{r}, A)\) involves a product of <em>factors</em>, which are functions of variables. To understand this, we explain the factor graph.</p>

<p>A factor graph has two types of nodes. Large circles (so not including the small numbered circles) represent random variables. The first row of circle nodes labeled \(s_1, \cdots, s_4\) are the player skills as of this match. The second row, labeled \(p_1, \cdots, p_4\) are player performances. The third row, labeled, \(t_1, t_2, t_3\) are team performances. The last row gives rank-consecutive team performance differences. That is, \(d_1 = t_1 - t_2\) and \(d_2 = t_2 - t_3\). As will be discussed next, these variables are introduced to assert the given \(\mathbf{p}\) and \(\mathbf{t}\) are consistent with \(\mathbf{r} = (1,2,2)\).</p>

<p>Black square nodes represent factors, functions of those random variables that connect with them. So the top row of black squares are four functions of \(s_1, s_2, s_3, s_4\) respectively. In fact, they are normal densities under \(\mu_i\) and \(\sigma_i\), representing their prior distributions (or distributions conditional on previous matches). The next row of black squares represent the densities of performances given skills and \(\beta\). The third row of black squares are indicator functions checking whether the team performances in fact sum to the player performances. This is to zero-out combinations of \(\mathbf{p}\) and \(\mathbf{t}\) that don’t match that definition. The second to last row and last row define \(d_1\) and \(d_2\) and indicate whether they are such to produce the \(\mathbf{r}=(1,2,2)\) ranking.</p>

<p>The factor graph instructs a faster computation than the generic six-nested integral. This is because each factor only concerns a small subset of variables, those connected to it. Put simply, given \(\mathbf{s}, \mathbf{p}, \mathbf{t}\), we could compute products of factors, starting from the top black squares and cascading downwards, producing a number equal to \(p(\mathbf{s}, \mathbf{p}, \mathbf{t} \vert \mathbf{r}, A)\). If the given \(\mathbf{s}\), \(\mathbf{p}\) and \(\mathbf{t}\) were inconsistent with \(\mathbf{r}\) and \(A\), the result is zero. That is the purpose of the indicator functions seen in the graph.</p>

<p>What remains is to integrate out \(\mathbf{p}\) and \(\mathbf{t}\) from \(p(\mathbf{s}, \mathbf{p}, \mathbf{t} \vert \mathbf{r}, A)\). This is done with <em>Expectation Propagation</em> (EP), a message passing algorithm from the category of <em>variational inference</em> methods. This is an extensive topic, so I will be brief. Essentially, the integration can be <em>approximated</em> with an iterative but fast computation that proceeds down and up the graph, as indicated with light-to-dark curved arrows. The messages flowing through the graph all take the form of univariate normal distributions, even for the non-gaussian portions of the graph near the indicator functions. Gaussians are chosen because they make products and integrals easy.</p>

<p>For some intuition on variational methods, see <a href="/concepts/probabilistic-graphical-models/pgms-part-3/" target="_blank" rel="noopener noreferrer">Part 3 of my series on Probabilistic Graphical Models</a>. For a more complete explanation, see <a href="https://www.microsoft.com/en-us/research/publication/trueskilltm-a-bayesian-skill-rating-system/" target="_blank" rel="noopener noreferrer">Herbrich et al. (2007)</a> or section 22.5 of <a href="https://probml.github.io/pml-book/book0.html" target="_blank" rel="noopener noreferrer">Murphy (2012)</a>.</p>

<p>The point is we can approximate the integral with an efficient computation over this graph, made possible by the TrueSkill assumptions and the EP algorithm. Further, it is simply combined with the random walk of skills over matches; the same up-down routine can be used to update skill estimates with each new match.</p>

<p>Doing so provides a univariate distribution over each player \(i\)’s skill \(s_i\), with mean \(\hat{\mu}_i\) and standard deviation \(\hat{\sigma}_i\). These values represent not only who a player has beaten, but who the beaten player has beaten, and so on, for all chains of matches. Naturally, these skills are used to rank players. For XBox Live, players were ranked according to a conservative skill estimate of \(\hat{\mu}_i - 3\hat{\sigma}_i\).</p>

<p>Most importantly, given any two players, including those who have never played each other, we can accurately estimate the probability of a win or loss.</p>

<h2 id="trueskill-performance">TrueSkill Performance</h2>

<p>In <a href="https://www.microsoft.com/en-us/research/publication/trueskilltm-a-bayesian-skill-rating-system/" target="_blank" rel="noopener noreferrer">Herbrich et al. (2007)</a>, there are several quantifications and plots demonstrating TrueSkill’s efficacy. One that I found especially convincing is copied here:</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/trueskill/delta_from_50.png" alt="Performance Graph" style="width: 60%" />
<span class="figure-caption">An optimal matchmaking system will ensure one-on-one games have close to a 50% probability of player 1 winning. However, in real applications, it takes some games for the system to determine skill levels, to make those 50-50 matches. For each skill measure, this skill plot shows the difference from 50% of the empirical win-rate. A value of 0 is optimal. This plot shows that TrueSkill is closer to 0 and approaches it more quickly then Elo or the Halo 2 ranking system.</span></p>

<h2 id="trueskill-variants">TrueSkill Variants</h2>

<p>Since TrueSkill’s publishing, there have been several notable extensions:</p>

<ul>
  <li>
    <p><strong>TrueSkill Through Time</strong> (<a href="https://www.microsoft.com/en-us/research/publication/trueskill-through-time-revisiting-the-history-of-chess/" target="_blank" rel="noopener noreferrer">Dangauthier (2008)</a>): An interesting limitation of vanilla TrueSkill is that skill estimates depend only on past matches. However, suppose a player defeated someone who later became the champion. In retrospect, the estimate of the player who beat the champion should be revised upward. This is the difference between vanilla TrueSkill, performing <em>filtering,</em> and TrueSkill Through Time, performing <em>smoothing</em>. Smoothing is the better option for retroactively determining the GOAT.</p>

    <p>Another element is that vanilla TrueSkill assumptions skills only change with matches. TrueSkill Through Time assumes skills evolve over time. This is more realistic, since it is likely that matches occur outside of the data or skills change due other time related factors, like age.</p>
  </li>
  <li>
    <p><strong>TrueSkill 2</strong> (<a href="https://www.microsoft.com/en-us/research/publication/trueskill-2-improved-bayesian-skill-rating-system/?msockid=0749eb24e7c9612828c2f976e67c6045" target="_blank" rel="noopener noreferrer">Minka (2018)</a>): Vanilla TrueSkill only operates on wins, losses and draws, but in most games, there’s other useful information. TrueSkill 2 is an extension to factor in, among other things, players quitting matches, death/kill ratios, and correlations across match types.</p>
  </li>
  <li>
    <p><strong>Multidimensional Skill</strong> (<a href="https://doi.org/10.1145/2835776.2835787" target="_blank" rel="noopener noreferrer">Chen (2016)</a>): While not a TrueSkill variant, it is worth mentioning. An assumption of TrueSkill is that a skill is a single number. However, players and their styles might not be so simply modeled. We could have an arrangement like rock-paper-scissors, where player 1 has an advantage of player 2, who has an advantage of player 3, who has an advantage of player 1. This presents <em>intransitivity</em>; observing player 1 beating player 2 many times and player 2 beating player 3 many times may not be enough to confidently say player 1 will beat player 3. It should be noted that this is an exotic effect to capture, and most systems consider univariate skills.</p>
  </li>
</ul>

<h2 id="the-details">The Details</h2>

<p>For those interested in the details of the above analysis, see <a href="https://github.com/Duane321/true_theta/blob/main/posts/trueskill/true_skill_simulated.ipynb" target="_blank" rel="noopener noreferrer">this notebook</a>. It relies on the <a href="https://trueskill.org/" target="_blank" rel="noopener noreferrer">TrueSkill implementation</a> by <a href="https://subl.ee/" target="_blank" rel="noopener noreferrer">Heungsub Lee</a>.</p>

<h2 id="part-two">Part Two</h2>

<p>We have briefly toured the ingenious TrueSkill algorithm, but we’ve yet to see it applied to real data. In an upcoming post, we will apply it to boxing, tennis and warcraft 3, a classic real time strategy game.</p>

<hr />
<h3 id="references">References</h3>

<ol>
  <li>
    <p>R. Herbrich, T. Minka, T. Graepel. <a href="https://www.microsoft.com/en-us/research/publication/trueskilltm-a-bayesian-skill-rating-system/" target="_blank" rel="noopener noreferrer">TrueSkill(TM): A Bayesian Skill Rating System</a> Advances in Neural Information Processing Systems. January 2007</p>
  </li>
  <li>
    <p>P. Dangauthier, R. Herbrich, T. Minka, T. Graepel. <a href="https://www.microsoft.com/en-us/research/publication/trueskill-through-time-revisiting-the-history-of-chess/" target="_blank" rel="noopener noreferrer">TrueSkill Through Time: Revisiting the History of Chess</a> In Advances in Neural Information Processing Systems. MIT Press, January 2008</p>
  </li>
  <li>
    <p>T. Minka, R. Cleven, Y. Zaykov. <a href="https://www.microsoft.com/en-us/research/publication/trueskill-2-improved-bayesian-skill-rating-system/?msockid=0749eb24e7c9612828c2f976e67c6045">TrueSkill 2: An improved Bayesian skill rating system</a>. Microsoft. March 2018</p>
  </li>
  <li>
    <p>S. Chen, T. Joachims. <a href="https://doi.org/10.1145/2835776.2835787" target="_blank" rel="noopener noreferrer">Modeling Intransitivity in Matchup and Comparison Data</a> Association for Computing Machinery. 2016</p>
  </li>
  <li>
    <p>K. Murphy. <a href="https://probml.github.io/pml-book/book0.html" target="_blank" rel="noopener noreferrer">Machine Learning: A Probabilistic Perspective</a>. <em>MIT Press</em>. 2012.</p>
  </li>
</ol>

<hr />
<h3 id="footnotes">Footnotes</h3>

<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:1">
      <p>In practice, the ELO system is slightly different. Beta is used to scale skills to be in the range of a few thousand. Also, the logistic function, rather than the normal CDF, is used for compute purposes. <a href="#fnref:1" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:2">
      <p>Some of this is relieved with a prior distribution over skills, but that only mitigates the issue. For instance, if one player <em>always</em> beats another player, then there is a huge range of plausible skill pairs. They need only have at least some large skill difference. A prior will bias this difference to be smaller, but the truth of the matter may be a much larger difference. <a href="#fnref:2" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
  </ol>
</div>]]></content><author><name>Duane Rich</name></author><category term="machine-learning-and-other-topics" /><category term="Post Formats" /><summary type="html"><![CDATA[A walkthrough of the classic, effective and beautiful TrueSkill algorithm.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://djrich.dev/assets/images/posts/trueskill/figure_1_from_original_paper.png" /><media:content medium="image" url="https://djrich.dev/assets/images/posts/trueskill/figure_1_from_original_paper.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Algorithmic Operations: Lessons from Bandit Algorithms</title><link href="https://djrich.dev/concepts/machine-learning-and-other-topics/algorithmic-operations/" rel="alternate" type="text/html" title="Algorithmic Operations: Lessons from Bandit Algorithms" /><published>2024-11-18T00:00:00+00:00</published><updated>2024-12-10T00:00:00+00:00</updated><id>https://djrich.dev/concepts/machine-learning-and-other-topics/algorithmic-operations</id><content type="html" xml:base="https://djrich.dev/concepts/machine-learning-and-other-topics/algorithmic-operations/"><![CDATA[<blockquote>
  <p><span style="color:#205081"><strong>Algorithmic Operations</strong></span>: the maximal use of algorithms to make essential business decisions.</p>
</blockquote>

<p>The practice can be traced to the hedge fund industry. The operations of ingesting data to predict prices and execute trades is a friendly environment to algorithms, which are constrained to act only via programmatic interfaces. By the mid 1990’s, the early-digitized financial markets produced decisive proof of algorithms’ efficacy in the form of the unprecedented success of quantitative hedge funds like Renaissance Technologies and D. E. Shaw. Its influence spread, with one path traceable from D. E. Shaw, when Jeff Bezos, a freshly indoctrinated algorithm evangelist, left the company to start Amazon in 1994.</p>

<p>However, the retail business lacked programmatic access. In the late 1990’s, ordering from wholesalers, tracking shipments, directing inventory, packaging items, and shipping orders were frequently done manually. Ordering from wholesalers often involved phone calls or faxed purchase orders, tracking shipments relied on manually updated spreadsheets or physical ledgers, inventory management required store managers to walk through aisles with clipboards, and packaging and shipping orders depended on workers assembling boxes with printed labels based on handwritten instructions. To direct these operations digitally would require new technology throughout the entire supply chain. Over the following three decades, Amazon performed this transformation with enormous effort and contributed<sup id="fnref:1"><a href="#fn:1" class="footnote" rel="footnote" role="doc-noteref">1</a></sup> to a growing industry philosophy of algorithmic essentialism.</p>

<p>Today, entire industries, like ridesharing, depend sensitively on effective algorithmic operations and any declaration of its value reads as a cliché. Yet, they remain the exception rather than the norm. Recognizing colloquialisms like ‘Excel Hell’, ‘Email Ping-Pong’ and ‘Death by Meeting’ is all one needs to appreciate this fact. The answer to ‘why?’ is ‘because it is hard’. This article seeks to explore this more thoroughly.</p>

<p>More constructively, this article is to provide clarity on the causes and make informed recommendations for progress. In total, this is an article about automated decisioning.</p>

<p>Our approach is to compare a company’s algorithmic operations to those of fully programmatic learning algorithms. The analogy contextualizes problems and clarifies requirements. Further, in our recommendations, we cite time-tested, nonspeculative solutions.</p>

<p>This article is informed by the authors’ combined 8 years of experience at Lyft, a review of 14 Amazon employee interviews, comments from other industry practitioners, and a survey of literature, including 12 MLOps papers and nearly 100 blogs from Lyft, Uber, Instacart, DoorDash and Airbnb. We focus on these companies because they have been successful in algorithmic operations and are local to our experience.</p>

<h2 id="algo-ops-what-it-is">Algo Ops: What It Is</h2>

<p>Repeating the definition, Algorithmic Operations (‘Algo Ops’) refer to the maximal use of algorithms to make essential business decisions. The term ‘maximal’ is used as an alternative to ‘exclusive’, since no organization exclusively uses algorithms for decisions. This is to suggest ‘getting algorithms to do everything they can and should do.’</p>

<p>Algo Ops are different from algorithmic products. Algo Ops are about automating the internal machinery of a company, while algorithmic products are about delivering value to users through algorithm-driven features. Google Search’s decision of which websites to return most directly benefits the user. Uber’s dynamic pricing system is designed for revenue and profitability. Both rely on  algorithms, but their context, intent and beneficiaries set them apart. These differences create distinct desiderata and challenges.</p>

<p>Algo Ops are different from Machine Learning Operations (‘MLOps’), though they heavily overlap. Indeed, much of our literature review was from MLOps. However, while MLOps focuses on the development lifecycle of ML models<sup id="fnref:2"><a href="#fn:2" class="footnote" rel="footnote" role="doc-noteref">2</a></sup>, Algo Ops encompasses the broader application of algorithms to automate and optimize decision making across the entire business. MLOps is a critical component of Algo Ops, but not all algorithms involved in business operations are ML models. For example, data-less rule-based systems, optimization algorithms, and other non-ML methods are integral parts of Algo Ops. Revealing the term’s breadth, Algo Ops subsumes much of operations research in addition to MLOps.</p>

<p>We’ll also define <em>partial</em> Algo Ops, or Algo Ops designed with ‘humans-in-the-loop.’ Partial Algo Ops, for many categories of decisions, are best practices. An example is the the entire scientific discipline of commercial experimentation, whereby control and treatments groups are deployed, logged and analyzed to inform a person’s go/no-go decision. Partial Algo Ops are accepted as the final destination, since many questions cannot be raised and answered by algorithms alone, like some system-level engineering decisions or UI design choices. Other motivations are for transparency and well defined accountability. Further, they may benefit when difficult-to-measure economic trade-offs are involved, and can only be properly balanced with domain experts. In other cases, partial Algo Ops are considered an intermediate step on the way to Algo Ops; humans are there to ‘fill in the gaps’.</p>

<h2 id="deploy-explore-log-learn">Deploy, Explore, Log, Learn</h2>

<p>In pursuit of Algo Ops, we will consistently reference a framework described in the 2017 Microsoft Research paper “Making Contextual Decisions with Low Technical Debt.” <a href="https://arxiv.org/abs/1606.03966" target="_blank" rel="noopener noreferrer">Agarwal et al. (2017)</a> was partially a response to the well known 2015 Google paper “Hidden Technical Debt in Machine Learning Systems,” which explained the pernicious causes underneath the expensive and deleterious process of large scale machine learning model maintenance. <a href="https://arxiv.org/abs/1606.03966" target="_blank" rel="noopener noreferrer">Agarwal et al. (2017)</a> explains the ‘Deploy, Explore, Log, Learn’ framework (“DELL” as only we will call it) in their presentation of their ‘Decision Service’, a product enabling applications of contextual bandit (CB) algorithms and addressing challenges described in <a href="https://papers.nips.cc/paper_files/paper/2015/hash/86df7dcfd896fcaf2674f757a2463eba-Abstract.html" target="_blank" rel="noopener noreferrer">Sculley et al. (2015)</a>.</p>

<p>Bandit algorithms are of particular interest because they perform the complete loop of automated decisioning, including observing, deciding and learning. They are dedicated to the expansive class of causal modeling, which encapsulates predictive modeling<sup id="fnref:3"><a href="#fn:3" class="footnote" rel="footnote" role="doc-noteref">3</a></sup>. Indeed, bandit algorithms are attractive for this completeness, but such completeness brings the full set of technical issues. The paper’s approach to this broad class of challenges is what makes it relevant for our purposes.</p>

<p><a href="https://arxiv.org/abs/1606.03966" target="_blank" rel="noopener noreferrer">Agarwal et al. (2017)</a> present DELL as a software abstraction designed to capture a wide range of automated decisioning applications. More technically, they recognize that any CB system’s data is be made up of, implicitly or explicitly, many tuples described as follows:</p>

<p class="center"><img src="/assets/images/posts/algorithmic_operations/tuple.webp" alt="Contextual Bandit tuple" style="width: 65%" />
<span class="figure-caption">Contextual bandits are trained on tuples of four pieces of data. </span></p>

<p>The challenge is maintaining the consistency and integrity of these tuples. This is challenging since they are often generated across widely separate business functions. The DELL abstraction is to provide decisioning flexibility, and their software is to guarantee the quality of these tuples and the models developed on top of them.</p>

<p>Our observation is that this framework must operate for <em>any</em> Algo Ops system, even those involving humans. Any such systems should know the context of its decisions, the decisions themselves, the associated probabilities and the rewards. Doing so is necessary for it to improve via data. In what follows, we will apply this perspective as a lens to contextualize problems and recommend solutions. Before that, we describe the component operations:</p>

<blockquote>
  <p>The components are operations, to be executed repeatedly and in circular fashion by an Algo Ops system:</p>

  <ul>
    <li><strong>Explore</strong>: In addition to effective actions, <em>exploratory</em> actions must be made to generate informative data.</li>
    <li><strong>Log</strong>: Correctly store observations of all relevant variables, from exogenous inputs, to model internals, to target variables.</li>
    <li><strong>Learn</strong>: Update the model according to observations and feedback.</li>
    <li><strong>Deploy</strong>: Deploy the updated model into the environment.</li>
  </ul>
</blockquote>

<p class="center"><img src="/assets/images/posts/algorithmic_operations/DELL_circle.webp" alt="DELL" style="width: 65%" loading="lazy" />
<span class="figure-caption">The four required, synchronized operations of an adaptive decision system. Image source: <a href="https://arxiv.org/abs/1606.03966" target="_blank" rel="noopener noreferrer">Agarwal et al. (2017)</a> </span></p>

<p>We find this perspective informative for several reasons. First, naming the components and declaring that each must be done well is a useful exercise for all contributors; each are frequently neglected by one group or another, yet they are all requirements. For example, experimentation is frequently deprioritized due to its expense and abstract value, despite its typical necessity.</p>

<p>Second, the components must be developed in code-level and organization-level coordination with each other, and this, in our review, is a cavernous source of problems. Components are frequently managed by independent processes spread across teams, preventing coordination with anything more reliable than conversation. The model destroying misalignment of production and development environments is a notorious symptom.</p>

<p>Third, the system improves with each complete cycle through the components, so any frictions or disruptions can be identified as interfering with that overall goal. For instance, delayed decisioning feedback expands the time between Explore and Learn, slowing iteration cycles. Manual deployment procedures, slow experimental design processes, unreliable ETL jobs and ad-hoc model tuning are other procedural pathologies that slow DELL cycles.</p>

<h2 id="dell-system-development">DELL System Development</h2>

<p>To organize technical challenges, we will sequence them similarly to how they might be encountered while building from scratch. To do that, we provide a simple building procedure. We assume the following is done with the necessary infrastructure in place. To clarify, these are not the DELL actions. DELL actions are what the system must do once it is functioning properly. Rather, these are steps to develop a minimal procedure that performs DELL. Since human-in-the-loop Algo Ops is the norm, so is this procedure.</p>

<ol>
  <li>
    <p><strong>Gather initial data</strong>: This typically involves writing code to generate and save a dataset. It is expected this data can be regenerated on an ongoing basis, is structured in a way a model can ingest and contains the necessary information for the model to perform well. Assuming it is a causal model, some experimental data should be included.</p>
  </li>
  <li>
    <p><strong>Develop and evaluate the model</strong>: The model is built and iterated on until some holdout evaluation is satisfactory. As much as possible, the evaluation metrics should be representative of the broadest intent of the model.</p>
  </li>
  <li>
    <p><strong>Develop data serving</strong>: For the model to operate in a live environment, data identically defined to that used for offline model development must be made available in the online live environment. This step is to build the required pipelines. The ‘identically defined’ requirement necessitates a variety of data quality checks.</p>
  </li>
  <li>
    <p><strong>Develop model deployment procedure</strong>: Code is written that trains the model on regularly refreshed data. Often, a serialization procedure is written to export the model in a standardized format and serve it to a live environment, though there are alternative approaches.</p>
  </li>
  <li>
    <p><strong>Develop model logging and observability</strong>: Pipelines are developed to record the served model’s context, inputs, intermediate values and outputs. These are logged for evaluation purposes and to accrue future training data. Also, analytics are built to create visibility and assist future decisions about fixing or improve the system.</p>
  </li>
  <li>
    <p><strong>Deploy model</strong>: Execute the deployment procedure, which trains and moves the model into production. Beyond what is required to do this, it may also involve inspecting data quality checks, running unit and integration tests, manually reviewing the serialized model, data analysis of recently served data and crossing fingers.</p>
  </li>
  <li>
    <p><strong>Monitor live performance, gather feedback, update processes developed in steps 2-5 and return to step 6</strong>: For many reasons, some avoidable and some not, live data is likely to reveal problems. It may suggest the model is not fulfilling its goal or is deviating from expectations made in step 2. Observations and hypotheses should be written down, turned into recommendations, prioritized and executed. With those updates, a new model is ready for deployment.</p>
  </li>
</ol>

<p>This relates to the DELL framework as follows. ‘Deploy’ is performed with steps 4 and 6. ‘Log’ is performed with steps 5 and 7.</p>

<p>‘Explore’ is done on two levels. The model itself is responsible for taking exploratory actions. In this sense, Explore is a requirement of the model and should be captured in step 2. On a higher level, exploration is done in step 7 and by those developing the system. Broadly, exploration is about generating observations which inform future improvements, and in this sense, both the algorithms and contributors participate.</p>

<p>‘Learning’ is similarly done on two level. Learning is the translation of new information into system improvements. That may be done by retraining the model on fresh data or updating the system based on live monitoring feedback.</p>

<h1 id="challenges">Challenges</h1>

<p>A small team applying these steps to a modest problem may not encounter any technical difficulties outside of modeling questions. They arise when the system ingests large and diverse data, involves multiple teams, utilizes distributed computing and storage, is written in multiple programming languages, requires long configuration files, creates sprawling dependencies, contains many pipelines, glues infrastructure, demands vigilance, and faces dynamic real-world conditions. In our analysis, these effects dislocate DELL operations and complicate them individually, slowing or stopping feedback.</p>

<p>In this section, we focus on challenges which are informed by the DELL perspective. This mostly omits engineering questions that are common to any large scale system and not necessarily a decisioning one.</p>

<h2 id="challenge-modeling-and-biased-data">Challenge: Modeling and Biased Data</h2>

<p>Modeling challenges are those addressed in textbooks on statistics, optimization, machine learning or simulation. Generally, they concern the process of turning observations into algorithmic updates. In this sense, they are of the ‘Learn’ components. However, modeling challenges also arise in the design of exploratory actions, and in this sense, they are a component of Explore.</p>

<p>Modeling challenges can normally be well expressed as mathematical problems and have mature software solutions. For this reason, outside of this section, we will abstract away the modeling problem as the task of using observational and exploratory data to make predictions and decisions<sup id="fnref:4"><a href="#fn:4" class="footnote" rel="footnote" role="doc-noteref">4</a></sup>. The post <a href="https://ai.stanford.edu/~zayd/" target="_blank" rel="noopener noreferrer">Why is machine learning ‘hard’?</a> explains some of what we are skipping here.</p>

<p>We make one important note on a ubiquitous issue concerning the interaction of learning and exploratory. Decisioning system often generate their future training data, creating a strong possibility of biased data. Recommender systems, which decide which items to show customers, are informed by historical transactions. However, once the recommender is deployed, it will influence those transactions, partially invalidating them for evaluating a future recommender. This is a well appreciated problem for recommenders (see <a href="https://dl.acm.org/doi/10.1145/3556536" target="_blank" rel="noopener noreferrer">Zangerle (2022)</a>), but not specific to them, as discussed in <a href="https://papers.nips.cc/paper_files/paper/2015/hash/86df7dcfd896fcaf2674f757a2463eba-Abstract.html" target="_blank" rel="noopener noreferrer">Sculley et al. (2015)</a>. The only proper solution is sufficient randomized exploration, a la bandit algorithms.</p>

<h2 id="challenge-code-data-and-metadata-misalignment">Challenge: Code, Data and Metadata Misalignment</h2>

<p>The challenge of steps 1-7 and updating previous work with feedback is strong path dependency. Step 2 depends on the details of step 1. Step 3 depends on steps 1 and 2, and so on. In the worst case, every step could depend on every previous step, including the ongoing feedback iterations. However, engineering best practices intends to create abstractions which simplify dependencies, so operations may be considered independently. Unfortunately, as pointed out by <a href="https://www.slideshare.net/slideshow/the-challenges-of-bringing-machine-learning-to-the-masses/63629932" target="_blank" rel="noopener noreferrer">Zheng (2014)</a> and labeled ‘Abstraction Debt’ by <a href="https://papers.nips.cc/paper_files/paper/2015/hash/86df7dcfd896fcaf2674f757a2463eba-Abstract.html" target="_blank" rel="noopener noreferrer">Sculley et al. (2015)</a>, there is a lack of solid, time-tested interfaces for data streams, pipelines, models or outputs.</p>

<p>The result is misalignment. Contributors, by the limits of time and attention, either treat dependent components independently or must use weak alignment mechanisms, like meetings.</p>

<p>Next, we describe common symptoms and causes.</p>

<h3 id="misalignment-of-model-development-and-live-environments">Misalignment of model development and live environments</h3>

<p>A typical disconnect is between the data used for model development and the data it encounters in production. Data scientists often build models using static, personally-curated datasets (as in steps 1 and 2) that are readily available in development environments. However, once deployed, these models receive data from pipelines built by data engineers and often with different definitions, transformations, and latency.</p>

<p>A common culprit, but technically an orthogonal issue, is the difference between batch and streaming data pipelines, which process data at different cadences and with varying levels of granularity. In a batch pipeline, data is processed at regular intervals, allowing for heavier transformations and joins. In contrast, streaming pipelines handle real-time data with lower latency and may lack the same compute budget.</p>

<p>Contributors will normally check for these differences, but those checks are not guarantees. Data scientists and engineers may verbally agree they are equivalently transforming the same upstream data, but this is an unreliable process that must be repeated on every model deployment, slowing DELL cycles and blocking feedback.</p>

<p>The separate pipelines and the additional proactive effort to align them is made clear with a figure from <a href="https://dl.acm.org/doi/10.1145/3035918.3054782" target="_blank" rel="noopener noreferrer">Polyzotis (2017)</a>.</p>

<p class="center"><img src="/assets/images/posts/algorithmic_operations/serving_vs_training.webp" alt="Extra Pipeline" style="width: 100%" loading="lazy" />
<span class="figure-caption">The pipelines for providing data in production and the data for model development are necessarily different, necessitating alignment. Image source: <a href="https://dl.acm.org/doi/10.1145/3035918.3054782">Polyzotis (2017)</a> </span></p>

<p>The Training Data \(\rightarrow\) Training \(\rightarrow\) model pipeline is what is built in step 4. The Serving Data \(\rightarrow\) Serving \(\leftarrow\) Model pipeline is what is built in step 3. The dotted line is the separate pipeline that needs to be aligned. These pipelines are necessarily under different constraints and environment, yet must produce equivalent data. In general, there is no perfect test to assert their equivalence.</p>

<p>Another source of misalignment is the model’s serialization and deserialization. Serialization is the process of saving a trained model in a format such that it can be loaded across multiple computers, programming languages or frameworks. The alignment across programming language is especially difficult, since it involve identically recreating the model’s logic using different representations of numbers, data structure and optimization routines, among other things. Today, there are well tested solutions, like <a href="https://pytorch.org/serve/" target="_blank" rel="noopener noreferrer">TorchServing</a> for PyTorch models and <a href="https://github.com/onnx/onnx" target="_blank" rel="noopener noreferrer">ONNX</a>, which provides an open standard for representing deep learning and traditional machine learning models.</p>

<p>However, these are not airtight fixes to the serialization problem. The reason is there is no complete consensus abstraction of what defines a model. It cannot be everything that impacts the model’s predictions, since that would include the entire upstream pipeline, which is too large and distributed to represent with one tool. Inevitably, these serialization methods create gaps; differences in the models’ environments, not captured in serialization, are potential causes for model differences.</p>

<p>In reaction, contributors can apply the ‘Multi-Languages Smell’, as mentioned in <a href="https://www.amazon.science/publications/on-challenges-in-machine-learning-model-management" target="_blank" rel="noopener noreferrer">Schelter (2018)</a> and <a href="https://papers.nips.cc/paper_files/paper/2015/hash/86df7dcfd896fcaf2674f757a2463eba-Abstract.html" target="_blank" rel="noopener noreferrer">Sculley et al. (2015)</a>. This is to distrust any system that straddles multiple languages, and to insist on avoiding them without strong guarantees.</p>

<h3 id="misalignment-of-joined-features-and-features-over-time">Misalignment of joined features and features over time</h3>

<p>Enrichment is the process of improving a model with the addition of input features, often joined from a new data source. The new features are added in production if shown to be incremental in development. However, this offline process brings no guarantees of future feature homogeneity that the model may assume. The new feature set may have different latency, timestamp conventions, backfill triggers or null handling, all creating opportunities for future breakages.</p>

<p>Misalignment across time is another challenge. A feature with the same name can evolve in subtle ways as data engineering processes are updated or business requirements shift. For example, what starts as a simple user count might later be filtered to the reflect the company’s higher standard of an ‘activated’ user. A change like this may be done without relabeling, with only a partial backfill, and communicated with a company wide email. With different consequences, a data scientist might do nothing, shorten their backtest window, invert the feature change or drop the feature.</p>

<p>One solution is the use of pipeline metadata, which can be compared and asserted across features and time. However, as discussed in <a href="https://www.amazon.science/publications/on-challenges-in-machine-learning-model-management">Schelter (2018)</a>, this metadata abstraction is often not sufficient for representing the model’s requirements and introduces new metadata management complexities.</p>

<p>Another solution is data quality checks, which verify that the data meets standards of completeness, consistency and accuracy. This is a useful line of defense, but as described in <a href="https://dl.acm.org/doi/10.1145/3035918.3054782" target="_blank" rel="noopener noreferrer">Polyzotis (2017)</a>, a model’s requirement for a feature cannot be discrete checked, since it is statistical and approximate. There is no perfect rule to detect feature drift, like there is to check whether a value is NULL or of a required data type. Further, as described in <a href="https://www.amazon.science/publications/towards-automated-data-quality-management-for-machine-learning" target="_blank" rel="noopener noreferrer">Rukat (2020)</a>, data quality checks normally must be specified by the modeler, since they depend on the downstream model. The necessary cross team communication makes their specification slow and unreliable.</p>

<h3 id="poor-representations-from-new-categories">Poor representations from new categories</h3>

<p>A demand for a new product category paired with a short timeline creates pressure to reuse existing pipelines as much as possible. However, those pipelines were developed in little anticipation of the new category and can not provide the appropriate representation. For example, as mentioned in <a href="https://www.amazon.com/Everything-Store-Jeff-Bezos-Amazon/dp/0316219266" target="_blank" rel="noopener noreferrer">Stone 2013</a>, this was a constant challenge for Amazon engineers, as they expanded their retail business into new product categories, from books to virtually everything. A symptom of this is the employees being forced to enter into the system whether a toy was hardcover or paperback.</p>

<p>In our experience, Lyft experienced this with different ride modes. What’s the singular price of a shared ride involving two passengers? What about three? In the shared category, many routines involving estimating elasticities, determining profitability and attributing financial line-items were not appropriate to apply, but applied nonetheless. When the shared rides were properly represented, there was additional complexity in the new pipelines to management.</p>

<p>The incomparability across products necessitates either two partially or entirely separated DELL routines. Considering humans are in the loop, this splits attention and slows cycles.</p>

<h2 id="challenge-arduous-vague-delayed-incomplete-incorrect-or-unavailable-feedback">Challenge: Arduous, Vague, Delayed, Incomplete, Incorrect or Unavailable Feedback</h2>

<p>Delayed rewards are an unsolved inhibitor to bandit algorithms. If action feedback is not observed for a long time, the algorithm can only slowly improve itself, degrading performance in the short run. In our analogy of bandit algorithms to multi-team Algo Ops systems, the sources of feedback degradation grows. Delays are only one source. More generally, anything that obfuscates the attribution of outcomes to actions degrades feedback.</p>

<p>To see this in the general context, we provide the following steps for applying feedback:</p>

<ol>
  <li><strong>An error or performance gap is identified</strong>: Feedback cannot begin without recognizing some deviation from expectations.</li>
  <li><strong>The impact is estimated</strong>: This is to inform prioritization and the extent of the correction, since trade-offs are inevitable.</li>
  <li><strong>The cause is located</strong>: The error or performance gap must be traced back to its cause.</li>
  <li><strong>The solution is determined and executed</strong>: Knowing the cause is not necessarily knowing the fix. Using the estimate of impact, a fix is traded-off against alternatives, determined and executed.</li>
</ol>

<p>For bandit algorithms, many of these are trivial since its theoretical problem statement declares that actions are the only thing to adjust and we are assured of their attribution to rewards. In an Algo Ops system, each is a source of failure in the feedback loop. Next, we explain common cases.</p>

<h3 id="observability-does-not-represent-model-context-and-behavior">Observability does not represent model context and behavior</h3>

<p>Observability involves building dashboards to reflect the models’ inputs, internals and outputs. The challenge is the dashboard must be interpretable to people while representing the high dimensional condition of the model. If one looks at the individual vectors fed to the model and its output, they can only inspect a tiny portion of the model’s traffic. Therefore, inputs are often averaged, as are the outputs. However, in some cases, the average input may be meaningless. The average location may be the middle of nowhere, and there is no average product category. Creating categorical dashboard filters is a common solution but divides attention. Ultimately, this mismatch hinders feedback steps 1-4.</p>

<p>Further, important behavior may be in the extremes and hard to detect in the aggregate. At Lyft, we discovered rare but impactful ‘bad experiences’, where prices or ETAs were extremely high, causing a rider to abandon the app permanently. The solution was to monitor these tail events directly and tune hardcoded rules to prevent them. This handled it, but it addressed a singular problem, required additional dashboarding and divided attention.</p>

<p>In short, passing system feedback through contributors’ minds creates friction and fog, complicating steps 1-4 and slows the Learn operation.</p>

<p>Regardless, observability is always necessary. No existing algorithm is sufficiently general to provide the types of corrections human attention produces.</p>

<h3 id="delayed-feedback">Delayed Feedback</h3>

<p>Much of what a business cares about is only realized over long time horizons. Quarterly profits are an obvious example. However, quarterly profits are a terrible objective for a bandit algorithm, since there’s no reliable method attributing the rarely observed fluctuations in quarterly profits to the myriad decisions it is intended to make. In our analogy to Algo Ops, this problem remains.</p>

<p>To illustrate, consider the goal of a long term forecasting systems. At Lyft, this was a primary focus of ours (<a href="https://eng.lyft.com/causal-forecasting-at-lyft-part-1-14cca6ff3d6d" target="_blank" rel="noopener noreferrer">Rich (2022)</a>). Such a causally-valid system was used for planning; take the actions today which produce the most attractive future. However, delayed feedback is ambiguous feedback. If profitability forecasts are off, we could inspect errors in all observed intermediate variables (e.g. were we mistaken on rides?), but this does not necessarily inform the fix. The intermediate variable with the largest error may not in fact drive the profitability error. It may be due to a small upstream error to which profitability is very sensitive, and the model was mistaken on the level of sensitivity. Ultimately, domain intuition was relied upon, but this pervasive ambiguity makes this routine precarious for an aspiring Algo Ops system.</p>

<h3 id="uninformative-error-messaging">Uninformative Error Messaging</h3>

<p>Continuing with Lyft references, suppose a contributor recognizes that conversion rates, the probability a ride is accepted after the price is seen, are falling below expectations after a price increase. We assume they can trace the cause to an exogenous variable, like a prediction of competitor pricing, failing a data quality check. The check alerts that the price is far above what can be reasonable expected, and so the model’s price sensitivity was incorrect.</p>

<p>The challenge is that an alert of ‘competitor price is too high’ does not inform on how to correct it. Inevitably, feedback is slowed, as teams communicate, fixes are proposed and iterations follow.</p>

<h3 id="lack-of-model-reproducibility">Lack of Model Reproducibility</h3>

<p>The standard debugging procedure involves recreating the observe problem in an environment in which changes can be tested as solutions. However, in real time systems, the exact conditions that caused a model to behave problematically may not be recoverable. Among other things, this may be due to data collection timing, missing logging or a misaligned development environment. Ultimately, it prevents issues from being understood and corrected, interfering with the Learn operation.</p>

<h2 id="challenge-gaps-in-skills-and-education">Challenge: Gaps in Skills and Education</h2>

<p>We argue that Algo Ops requires DELL components to be built well and executed in tight coordination. The knowledge to do so is not easily compartmentalized, but contributors must specialize to some extent, creating knowledge gaps. Ideally, everyone is aligned and knowledgeable of the motivations behind the full system’s design, but this is rarely so. We discuss a variety of consequences and causes.</p>

<p>First, it inhibits the four feedback steps described previously. A data scientist unfamiliar with the business’s ETL pipelines may be unable to trace the cause of a model’s performance degradation back to its source. A data engineer may be unequipped to set the right statistical data quality checks required by a downstream model. A contributor without causal inference training may misattribute outcomes to actions, resulting in ineffectual model updates. These are just a few example.</p>

<p>Second, the DELL operations cannot be well developed individually. In an effort of exploration, a data scientist underskilled in statistics may design an experiment insufficient for resolving the intended relationship. A data engineer unfamiliar with the model may writing logging that misses essential model behavior. A machine learning engineer underskilled in machine learning may create a deployment process which changes the model’s logic. Again, these are just examples.</p>

<p><a href="https://ieeexplore.ieee.org/document/10172624" target="_blank" rel="noopener noreferrer">Mailach (2023)</a> describes a common cause as ‘Headless-Chicken-Hiring,’ where challenging projects are addressed with hiring, often of those with mismatched skills. This often delays the timeline and complicates the project, as more training, communication and coordination are required. They report common causes are unclear titles and uneducated hiring. Those making the hiring decisions do not understand what is required specifically by the task. As a consequence, they defer to unspecified terms like ‘data’ to match hires to projects. In reality, such terms are poor matching criteria. <a href="https://ieeexplore.ieee.org/document/10172624" target="_blank" rel="noopener noreferrer">Mailach (2023)</a> also cites skill shortages in the labor market as another commonly reported cause.</p>

<h2 id="challenge-missing-culture">Challenge: Missing Culture</h2>

<p>Especially among leadership, a culture of skepticism, scientific discipline, objectivity through data and algorithm supremacy is required for effective Algo Ops. Individuals should distrust intuitions, especially their own. People should look to experiments, whenever possible, to make decisions. They should read data to falsify rather than to confirm. They should notice and discourage speculation. They should insist on measurement. They should believe that, with sufficient data and at scale, mathematics and technology provide more effective decisioning than any group of people.</p>

<p>This culture may not exist because some do not have a scientific background, have been successful with their intuitions, are comfortable with manual operations, disapprove of the cost of scientific approaches, are impatient with the pace of knowledge discovery or find the mathematics opaque and irrelevant.</p>

<p>The consequence is that iterative improvement of an Algo Ops system cannot happen. For one to function at scale, it demands relentless effort in the form of applying a high experimentation standard to many well informed proposed changes. This can only be justified by those who believe it is the only approach.</p>

<h1 id="recommendations">Recommendations</h1>

<p>In this section, we’ll provide recommendations to address the problems mentioned above. We break them down into recommendations on technology and development strategy.</p>

<h2 id="recommendations-technology">Recommendations: Technology</h2>

<p>To address these issues, companies have built a variety of technology solutions. Below, we discuss those that are common and effective.</p>

<h3 id="feature-stores">Feature Stores</h3>

<p>A feature store is a service designed to align features, in addition to efficiently managing their storage and compute. Lyft has the <a href="https://eng.lyft.com/ml-feature-serving-infrastructure-at-lyft-d30bf2d3c32a" target="_blank" rel="noopener noreferrer">ML Feature Service</a> for aligning features between development and production environments and between batch and event stream processing. Instacart developed their <a href="https://tech.instacart.com/lessons-learned-the-journey-to-real-time-machine-learning-at-instacart-942f3a656af3" target="_blank" rel="noopener noreferrer">feature store</a> for aligning development and production features, after experiencing the challenges described above. Uber has their <a href="https://www.uber.com/blog/from-predictive-to-generative-ai/" target="_blank" rel="noopener noreferrer">Michaelangelo</a> platform, which began with a feature store, first presented in <a href="https://proceedings.mlr.press/v67/li17a.html" target="_blank" rel="noopener noreferrer">Li (2017)</a> on the merits of its alignment of batch and real time processing. Airbnb has <a href="https://medium.com/airbnb-engineering/chronon-a-declarative-feature-engineering-framework-b7b8ce796e04" target="_blank" rel="noopener noreferrer">Chronon</a> to address the same problem and <a href="https://careersatdoordash.com/blog/building-a-gigascale-ml-feature-store-with-redis/" target="_blank" rel="noopener noreferrer">so does DoorDash</a>. Lastly, <a href="https://ieeexplore.ieee.org/document/10172624" target="_blank" rel="noopener noreferrer">Mailach (2023)</a> identified the feature store as a common and useful solution when surveying MLOps engineers. These are well utilized technologies and necessary to avoid the often debilitating difficulties of misalignment.</p>

<p>Another benefit of a feature store is it centralizes data creation to a single team and interface. Data engineers can focus on testing, data quality checks, maintenance, and documentation, and data scientist can source from the feature store knowing it is consistently available across environments. This also mitigates an unmanageable proliferation of unsynchronized data pipeline.</p>

<p>A feature store is a targeted investment into feature misalignment, but not a root-cause fix. Many of the same alignments issues persistent, but are confined to a dedicated team and product. Additionally, there are new issues to consider. First, the centralized feature store prevents data scientists from self serving unsupported data. Second, individual features may feed into multiple models each with different data quality criteria, making any single criteria insufficient for some models.</p>

<h3 id="visualization-tools">Visualization Tools</h3>

<p>Rich visualizations tools for viewing high dimensional objects are effective for feedback. Uber created <a href="https://kepler.gl/">Kepler.gl</a> (picture below) for visualizing complex geospatial activity and <a href="https://www.uber.com/blog/manifold/" target="_blank" rel="noopener noreferrer">Manifold</a> for visualizing model behavior and performance. Instacart uses <a href="https://datashader.org/" target="_blank" rel="noopener noreferrer">datashader</a> to visualize their <a href="https://tech.instacart.com/space-time-and-groceries-a315925acf3a" target="_blank" rel="noopener noreferrer">logistical operations</a>. <a href="https://careersatdoordash.com/blog/monitor-machine-learning-model-drift/" target="_blank" rel="noopener noreferrer">Doordash uses</a> <a href="https://careersatdoordash.com/blog/monitor-machine-learning-model-drift/" target="_blank" rel="noopener noreferrer">Grafana</a> for real time model monitoring. Lastly, there are popular tools for visualizing the internals of deep learning models, like <a href="https://github.com/lutzroeder/netron" target="_blank" rel="noopener noreferrer">Netron</a>, <a href="https://github.com/wandb/wandb" target="_blank" rel="noopener noreferrer">Weights and Biases</a> and <a href="https://www.tensorflow.org/tensorboard" target="_blank" rel="noopener noreferrer">TensorBoard</a>.</p>

<p class="center"><img src="/assets/images/posts/algorithmic_operations/kepler.webp" alt="Extra Pipeline" style="width: 80%" loading="lazy" />
<span class="figure-caption"> Kepler.gl, created by Uber and now opensource, provides rich visualizations of region activity. Image source: <a href="https://kepler.gl/" target="_blank" rel="noopener noreferrer">Kepler.gl</a> </span></p>

<p>These visualization tools help contributors understand models’ conditions and provide accurate feedback. Further, they often provide UIs, avoiding the need to arduously trace through code and data. This eases DELL cycles, producing more of them.</p>

<h3 id="machine-learning-platforms">Machine Learning Platforms</h3>

<p>A machine learning platform (‘ML platform’) is a broad term to describe a product designed to support the lifecycle of machine learning models. An ML platform may offer a model development environment, a feature store, distributed training, a model registry, model serving, cloud storage and user friendly interfaces. They centralize much of the redundant work for moving an ML model from development to production.</p>

<p>Due to the proliferation of ML model at some companies, ML platforms are normally well resourced and utilized. Uber’s ML Platform is <a href="https://www.uber.com/blog/from-predictive-to-generative-ai/" target="_blank" rel="noopener noreferrer">Michelangelo</a>. Lyft’s platform is <a href="https://eng.lyft.com/lyftlearn-ml-model-training-infrastructure-built-on-kubernetes-aef8218842bb">LyftLearn</a>. Airbnb has <a href="https://medium.com/acing-ai/airbnbs-end-to-end-ml-platform-8f9cb8ba71d8">Bighead</a>. Instacart has <a href="https://tech.instacart.com/introducing-griffin-2-0-instacarts-next-gen-ml-platform-b7331e73b8d7" target="_blank" rel="noopener noreferrer">Griffin</a>. DoorDash has <a href="https://careersatdoordash.com/blog/transforming-mlops-at-doordash-with-machine-learning-workbench/" target="_blank" rel="noopener noreferrer">Machine Learning Workbench</a>. Each has developed their own to focus resources on the precarious process of moving a data scientists’ development model into a real time, large scale, product-integrated environment, and maintain it from there on.</p>

<h3 id="experimentation-platforms">Experimentation Platforms</h3>

<p>An experimentation platform is designed to simplify, standardize, make reliable, and speedup experimental setup, deployment and analysis. It is a centralized product to allow contributors to focus only on the change to the website or app, the design of the experiment and interpreting results. Everything else necessary for launching the experiment is the responsibility of the platform.</p>

<p>An experimentation platform is a clear example of a partial Algo Ops system, since its functions neatly maps to the components:</p>

<ol>
  <li>Run an A/B test \(\rightarrow\) <strong>Explore</strong></li>
  <li>Record context and A/B outcomes \(\rightarrow\) <strong>Log</strong></li>
  <li>Analyze results and decide A or B \(\rightarrow\) <strong>Learn</strong></li>
  <li>Launch A or B \(\rightarrow\) <strong>Deploy</strong></li>
</ol>

<p>The value of such a system was expressed by Jeff Bezos when describing one of the earliest experimentation platforms, Weblabs:</p>

<p class="center"><img src="/assets/images/posts/algorithmic_operations/jeff_on_experiments.webp" alt="Extra Pipeline" style="width: 80%" loading="lazy" />
<span class="figure-caption"> Jeff Bezos on the value of experimentation. </span></p>

<p>Many other companies have developed their own experimentation platforms. Uber developed Morpheus nearly a decade ago, and has since replace it with <a href="https://www.uber.com/blog/supercharging-a-b-testing-at-uber/" target="_blank" rel="noopener noreferrer">a more expansive system</a>. Doordash has <a href="https://careersatdoordash.com/blog/experimentation-analysis-platform-mvp/" target="_blank" rel="noopener noreferrer">Curie</a>. Airbnb an <a href="https://medium.com/airbnb-engineering/how-airbnb-safeguards-changes-in-production-9fc9024f3446" target="_blank" rel="noopener noreferrer">experimentation framework</a>. Instacart has an adaptive experiment platform, <a href="https://tech.instacart.com/supercharging-ml-ai-foundations-at-instacart-d48214a2b511" target="_blank" rel="noopener noreferrer">Axon</a>, which does much of the same control-treatment management, but is ‘adaptive’ in that it <em>decides</em> the relative value of the variants and allocates traffic accordingly. Lyft has one as well, described <a href="https://eng.lyft.com/challenges-in-experimentation-be9ab98a7ef4" target="_blank" rel="noopener noreferrer">here</a>.</p>

<h2 id="recommendations-development-strategy">Recommendations: Development Strategy</h2>

<p>In this section, we use the above framework to make recommendations on what to build and how to build it.</p>

<h3 id="what-to-build">What to Build</h3>

<p>This section is to provide guidelines for deciding when its worth developing or improving a given Algo Ops system.</p>

<p><strong>In order to create substantial decision feedback, look for decisions that produce a large amount of relevant and nearly independent data.</strong> That is, the decision should happen many times. Variables that discriminate a good from a bad decision should be observable, as should the decision variable itself and an immediately attributable reward. Decisions should not heavily influence each other.</p>

<p>By this description, setting prices on highly transacted goods is a decision worth targeting. In the case of rideshare, most factors that influence a rider’s decision to pay are observable: the starting and ending locations, the time, the weather, the supply of drivers, traffic conditions and the estimated time of arrival. The price itself and whether the ride was accepted are also available. Finally, rider acceptances are nearly independent; we can assume one riders acceptance is not influenced by another’s<sup id="fnref:5"><a href="#fn:5" class="footnote" rel="footnote" role="doc-noteref">5</a></sup>. Decision problems of a similar character, especially those so immediately related to revenue, are good targets.</p>

<p><strong>If it has not already been modeled thoroughly, start with the company’s transaction data.</strong> Transaction data, by requirements of financial reporting, is often of high quality. Second, transaction data informs purchase behavior, which is often an attractive target to influence. As an example, one of Amazon’s early technology successes was an unusual use of their transaction data, eventually labeled “Similarities.” It learned purchase correlations via customer baskets, which then informed item recommendations. In an <a href="https://www.youtube.com/watch?v=MvaRrKAVo6M&amp;t=2017s" target="_blank" rel="noopener noreferrer">interview</a>, Eric Benson, an early Amazon engineer, attributes its success over competing technologies to the transaction data’s size and quality. More details can be found in <a href="https://assets.amazon.science/76/9e/7eac89c14a838746e91dde0a5e9f/two-decades-of-recommender-systems-at-amazon.pdf" target="_blank" rel="noopener noreferrer">Smith (2017)</a>.</p>

<p><strong>Look for independence.</strong> Dependency and complexity are a scourge to Algo Ops. There are several forms of independence. Look for independence across system components; the deployment procedure should be independent of the specific model deployed. Look for independence across time; a short term signal, like a user reaction, is much better than slow moving time series, like user retention curves. Look for independence concurrently across variables; variables that can safely be assumed to be causally ‘upstream’ of other variables are good targets, especially when starting. Look for independence across systems; ridesharing companies simplify their operations greatly when they can treat their supply and demand populations separately.</p>

<p><strong>Target breaks or frictions in DELL cycles.</strong> In a review of <a href="https://www.youtube.com/@inventlikeanowner5834/videos" target="_blank" rel="noopener noreferrer">14 Amazon employee interviews</a>, we found five recommendations of fast, continuous feedback cycles. Feedback is slowed when its meditated through conversation, involves tracing complex and multi-language code and produces ambiguity. Several of our technology recommendations involve focusing resources to improve observability and lessen complexity, reducing the work to generate informative feedback.</p>

<p><strong>Unless it is from a group dedicated to data quality and integrity, be skeptical of data sourced across organizations.</strong> To be incremental to a model, data pipelines must be align on difficult-to-check characteristics. Also, data is surprisingly problematic to communication across teams and organizations. As an example, <a href="https://arxiv.org/abs/2103.08937" target="_blank" rel="noopener noreferrer">Granlund (2021)</a> studied the development of Oravizio, a software product that provides patient-level risk assessments for hip and knee surgery. This involved moving data between the hospital and those maintaining the model. They concluded that operations related to data communication were the greatest challenges.</p>

<p><strong>Design for Control</strong>: The mechanism people use to control the Algo Ops system present an important design question. As discussed, quickly and reliable deploying well informed code changes is one ingredient, but it neglects the full scope of the design question. For instance, which variables should a person specify to guide an algorithm? In algorithmic trading, these are often risk tolerance parameters, like ‘value-at-risk’ or ‘risk tolerance’, establishing how much risk should be traded for expected return. In ridesharing, we considered parameters that traded short term profit for long term growth. More generally, it’s worth investing time to design control variables that are intuitive to humans to specify and fully corral the algorithm’s behavior.</p>

<p>Another question of control design is how to evaluate whether to shut the system down or fallback to a previous version. This may be specified via guardrail metrics, whereby crossing of a threshold value provided reason to investigate. Since a single metric is rarely enough to make the decision, a variety of metrics are often available. However, this creates an opportunity for confusion; what if some thresholds are crossed and others are not? A prioritization of metrics is a partial solution, but also introduces some complexity (e.g. how do you decide the prioritization?). In general, there is no perfect solution to this problem. We can only recommend careful and deliberate design, specific to the problem.</p>

<h3 id="build-strategy">Build Strategy</h3>

<blockquote>
  <p><strong>Gall’s Law</strong>: A complex system that is working started as a simple system that was working.</p>
</blockquote>

<p>From our experience and survey, we believe this is true and relevant. In anticipation of the aforementioned difficulties, it is wise to limits sources of complexity. Specifically, for developing Algo Ops, it may help to:</p>

<ul>
  <li><strong>Before anything else, gather an initial dataset</strong>: Simply getting the data may resolve questions. It may reveal that the data is not of the necessary scale, granularity, timeliness or structure.</li>
  <li><strong>Define a small set of readily available success metrics</strong>: For feedback cycles to begin early, they must be with respect to objective measures.</li>
  <li><strong>Start with a small geographic region</strong>: In the early days of ridesharing, models were built per region because regions were so incomparable. When starting, it is worthwhile place to start.</li>
  <li><strong>Start with a small set of variables</strong>: A small set of variables is easier to model. Prefer to model those that are better understood.</li>
  <li><strong>Start with a single, reliable data source</strong>: As mentioned, melding data sources is problematic and likely unnecessary when starting.</li>
  <li><strong>Start with a small, skilled team</strong>: An Algo Ops system must be built in tight alignment with itself, so the contributors themselves must agree on the principles and communicate the details of their actions. This cannot be done across a large team and is challenging with unskilled contributors.</li>
  <li><strong>Start with building the baseline model</strong>: A baseline model establishes something objective to measure improvements from. Without it, there is no frame of reference to declare where the model is good or not.</li>
  <li><strong>Use only one programming language</strong>: Multiple languages is often unnecessary, and sometimes motivated by the preferences of team member. It can present serious technical difficulties and should be avoided where possible.</li>
  <li><strong>Establish explicit DELL components</strong>: This was the strategy in <a href="https://arxiv.org/abs/1606.03966" target="_blank" rel="noopener noreferrer">Microsoft’s bandit technology</a>. To reiterate, these component operations are necessary for any Algo Ops decision system, so labeling them may give them the proper priority and attention.</li>
  <li><strong>Execute the first feedback system early</strong>: Improvements come from DELL cycles. Getting to them quickly is to improve the system more quickly.</li>
</ul>

<p>It should be cautioned that most simple systems are not on the path to an effective complex one. In other words, the initial system must be carefully designed. Below are some things that must be anticipated up front.</p>

<ul>
  <li><strong>Interfaces</strong>: As best they can, abstractions should anticipate future functionality. If it does not, updating abstractions can be an error prone and laborious process. To do this, contributors should speculate on the future system and its requirements.</li>
  <li><strong>Scaling</strong>: If the system is expected to scale, scaling should be anticipated from the start. Prototyping without anticipating scaling may just produce an irrelevant prototype.</li>
</ul>

<p>Lastly, restarting may be a reasonable option. For reasons of incomplete abstractions, Uber <a href="https://www.uber.com/blog/supercharging-a-b-testing-at-uber/" target="_blank" rel="noopener noreferrer">rebuilt</a> their experimentation platform. Amazon in 2016 had to <a href="https://www.amazon.science/latest-news/the-evolution-of-amazons-inventory-planning-system" target="_blank" rel="noopener noreferrer">redesign</a> much of their supply chain systems. The reality is even thoughtful and well educated engineering design is not enough to protect against rewrite-worthy complexities in the future.</p>

<h1 id="conclusion">Conclusion</h1>

<p>This article has emphasized that algorithmic operations cannot operate effectively without the components Deploy, Explore, Log and Learn. Each must be done well and in tight coordinating. This is made challenging by separated software, data, teams and the required coordination efforts to compensate. We have provided tried and true technologies and recommendations to ease the path to Algo Ops.</p>

<p>We would like to thank a few of our peers for their helpful comments on this article: Alex Chin, Sumeet Kumar, Wilhelm Leinemann, Sameer Manek and Varun Pattabhiraman.</p>

<hr />
<h3 id="references">References</h3>

<ol>
  <li>
    <p>A. Agarwal, S. Bird, M. Cozowicz, L. Hoang, J. Langford, S. Lee, J. Li, D. Melamed, G. Oshri, O. Ribas, S. Sen, and A. Slivkins. <a href="https://arxiv.org/abs/1606.03966" target="_blank" rel="noopener noreferrer">Making Contextual Decisions with Low Technical Debt</a>. ArXiv. 2017</p>
  </li>
  <li>
    <p>D. Sculley, G. Holt, D. Golovin, E. Davydov, T. Phillips, D. Ebner, V. Chaudhary, M.l Young, J. Crespo,  and D. Dennison. <a href="https://papers.nips.cc/paper_files/paper/2015/hash/86df7dcfd896fcaf2674f757a2463eba-Abstract.html" target="_blank" rel="noopener noreferrer">Hidden Technical Debt in Machine Learning Systems</a>. NIPS. 2015</p>
  </li>
  <li>
    <p>E. Zangerle and C. Bauer. <a href="https://dl.acm.org/doi/10.1145/3556536" target="_blank" rel="noopener noreferrer">Evaluating Recommender Systems: Survey and Framework</a>. ACM Computing Surveys, Volume 55, Issue 8.  2022.</p>
  </li>
  <li>
    <p>N. Polyzotis, S. Roy, S. Whang and M. Zinkevich. <a href="https://dl.acm.org/doi/10.1145/3035918.3054782" target="_blank" rel="noopener noreferrer">Data Management Challenges in Production Machine Learning</a>. SIGMOD. 2017</p>
  </li>
  <li>
    <p>A. Mailach and N. Siegmund. <a href="https://ieeexplore.ieee.org/document/10172624" target="_blank" rel="noopener noreferrer">Socio-Technical Anti-Patterns in Building ML-Enabled Software: Insights from Leaders on the Forefront</a>. IEEE Xplore. 2023</p>
  </li>
  <li>
    <p>E. Li, E. Chen, J. Hermann, P. Zhang and L. Wang <a href="https://proceedings.mlr.press/v67/li17a.html" target="_blank" rel="noopener noreferrer">Scaling Machine Learning as a Service. International Conference on Predictive Applications and APIs</a>. 2017.</p>
  </li>
  <li>
    <p>T. Granlund, A.Kopponen, V. Stirbu, L.Myllyaho and T. Mikkonen. <a href="https://arxiv.org/abs/2103.08937" target="_blank" rel="noopener noreferrer">MLOps Challenges in Multi-Organization Setup: Experiences from Two Real-World Cases</a>.  ArXiv. 2021</p>
  </li>
</ol>

<hr />
<h3 id="footnotes">Footnotes</h3>

<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:1">
      <p>I should be careful to not given Amazon too much credit. Many companies pre-dating Amazon contributed to development of algorithmic operations. For example, <a href="https://www.forbes.com/sites/forbestechcouncil/2018/07/09/ai-enabled-personalization-the-new-frontier-in-dynamic-pricing/" target="_blank" rel="noopener noreferrer">American Airlines introduced dynamic pricing</a> in the 1980’s and <a href="https://www2.newpaltz.edu/~liush/MOIT/Barilla%20Case%20Study.pdf" target="_blank" rel="noopener noreferrer">Barilla introduced the Just-in-Time Distribution</a> style of inventory management. <a href="#fnref:1" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:2">
      <p>A model, in this context, is essentially a function that is learned from data. That is, it produces an output from an input, where the input-output mapping is informed by historical data. <a href="#fnref:2" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:3">
      <p>We should differentiate between two types of modeling: predictive modeling and causal modeling. Predictive modeling seeks only to predict outcomes from observations. A predictive model may be thought of as a noninteracting observer, inferring unseen variables from seen ones. Causal modeling is a strictly larger task, subsuming predictive modeling. A causal model is there to predict and optimize the actions of something operating within an environment. It must disentangle actions, their effects and the environment in order to estimate counterfactuals and pursue the most attractive one.</p>

      <p>The distinction relevant for our discussion is that causal modeling requires exploration. Exploratory or experimental actions are strict requirements for any system with a meaningful impact on its environment.</p>

      <p>Since it is typical for actions to impact the environment, we will assume causal modeling is the goal from here on. For more information on the distinction between predictive (or ‘associative’) and causal modeling, see <a href="https://web.cs.ucla.edu/~kaoru/3-layer-causal-hierarchy.pdf" target="_blank" rel="noopener noreferrer">this note on Judea Pearl’s Causal Hierarchy</a>. <a href="#fnref:3" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:4">
      <p>However, it is not a well contained black box; the specific modeling task has strong implications throughout the system. The granularity, size, latency and frequency of the data all follow considerations of the modeling problem. The design and extent of experimentation, observability and evaluation similarly follow. As an example, much of Lyft’s early scientific investment was to <a href="https://eng.lyft.com/experimentation-in-a-ridesharing-marketplace-b39db027a66e" target="_blank" rel="noopener noreferrer">A/B test market level changes</a>. Further, the modeling problem will have implications for the model’s complexity, which will have implications for model development and deployment. <a href="#fnref:4" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:5">
      <p>The reality is more complicated, since prices influence future driver supply, which correlates future acceptance, but our assumptions works at a first approximation. <a href="#fnref:5" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
  </ol>
</div>]]></content><author><name>DJ Rich</name></author><category term="machine-learning-and-other-topics" /><category term="Post Formats" /><summary type="html"><![CDATA[Taking inspiration from bandit algorithms, we propose a framework for effective algorithmic operations.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://djrich.dev/assets/images/posts/algorithmic_operations/DELL_circle.webp" /><media:content medium="image" url="https://djrich.dev/assets/images/posts/algorithmic_operations/DELL_circle.webp" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">A Reliable Contextual Bandit Algorithm: LinUCB</title><link href="https://djrich.dev/concepts/reinforcement-learning/lin-ucb/" rel="alternate" type="text/html" title="A Reliable Contextual Bandit Algorithm: LinUCB" /><published>2024-08-06T00:00:00+00:00</published><updated>2024-08-06T00:00:00+00:00</updated><id>https://djrich.dev/concepts/reinforcement-learning/lin-ucb</id><content type="html" xml:base="https://djrich.dev/concepts/reinforcement-learning/lin-ucb/"><![CDATA[<blockquote>
  <p>A user visits a news website. Which articles should they be shown?</p>
</blockquote>

<p>This question was the target of the paper <a href="https://arxiv.org/abs/1003.0146" target="_blank" rel="noopener noreferrer">A Contextual-Bandit Approach to Personalized News Article Recommendation</a>, which introduced the now famous <strong>LinUCB</strong> contextual bandit algorithm. In fact, personalizing news is just one application. Others include:</p>

<ul>
  <li><strong>Dynamic Pricing</strong>: Which discounts should be offered to maximize profit?</li>
  <li><strong>Personalized Advertising</strong>: Which advertisements should be shown to maximize clicks?</li>
  <li><strong>Medical Trials</strong>: Which treatments should be prescribed to maximize survival?</li>
</ul>

<p>To see how it applies generally, it helps to understand the personalized news application in more detail. When a user arrives at a website, they are represented as a feature vector. This may measure their gender, geography, age, device, or anything else considered relevant. Also, each recommendable article is represented with a feature vector. This may measure the article’s topic, category, geography and date. Together, the user’s and articles’ features are considered the ‘context’ of the recommendation. LinUCB is an algorithm that, when given a context, will select an article the user is likely to click.</p>

<p>However, the articles need not be actual articles. It is any set of actions where each is represented as a vector. A user need not be an actual user. It is any single vector describing the circumstance of the decision. The goal need not be click-through-rate, but rather any value dependent on the context and action selected. Within this framework, linUCB can be applied to dynamic pricing, advertising, medical trials and others.</p>

<h2 id="this-post">This Post</h2>

<p>In this post, I’ll explain the LinUCB algorithm from the ground up. I selected it for its utility. It is broadly applicable and strikes an attractive balance of simplicity and flexibility, making it a reliable choice for real world problems.</p>

<p>Since I’d like to communicate intuition, I will only focus on the disjoint variant. This is ‘Algorithm 1’ from the paper. The difference with ‘Algorithm 2’ is the disjoint variant omits <em>shared features</em>, features that are the same across actions. The user features described earlier are shared features. The article features are non-shared features. In practice, shared features are essential and in fact, more common than non-shared features. However, they are not essential for understanding the algorithm. Algorithm 2 is the same idea, slightly expanded.</p>

<p>To begin, I’ll state the problem generally and a bit redundantly.</p>

<h2 id="stochastic-contextual-bandits-with-linear-payoffs">Stochastic Contextual Bandits with Linear Payoffs</h2>

<p>The contextual bandit problem is as follows. In each of \(T\) rounds, we select one of several actions, each represented with a vector. For the action selected, we observe and obtain a reward whose expectation is a linear function of the selected vector. The reward then becomes part of data to inform future selections. The goal is to maximize cumulative reward over all rounds.</p>

<blockquote>
  <p>To introduce notation and restate this precisely, round \(t = 1, 2, \cdots, T\) proceeds in steps:</p>

  <ul>
    <li>
      <p>For each of \(K_t\) actions, a vector is observed: \(\{\mathbf{x}_{t, k} \mid k = 1, 2, \cdots, K_t\}\)<sup id="fnref:1"><a href="#fn:1" class="footnote" rel="footnote" role="doc-noteref">1</a></sup>. This is the <em>context</em> of the decision. Note there are no shared features.</p>
    </li>
    <li>
      <p>Based on the vectors and their relationships with rewards learned from previous observations, an algorithm \(\mathsf{A}\) selects an action \(a_t \in \{1, \cdots, K_t\}\).</p>
    </li>
    <li>
      <p>A reward \(r_{t, a_t}\) is observed, sampled independently from an unknown distribution dependent on the context and action selected. This makes the problem <em>stochastic</em>. To be explicit, the unchosen rewards \(r_{t, k}\) where \(k \neq a_t\) are unobserved.</p>
    </li>
  </ul>

  <p>The reward expectation is linearly related to the context, making the problem <em>with linear payoffs</em>:</p>

\[\mathbb{E}\big[r_{t, k} \vert \mathbf{x}_{t, k} \big] = \mathbf{x}_{t, k}^{\top}\boldsymbol{\theta}_{k}\]

  <p>where \(\{\boldsymbol{\theta}_k \mid k = 1, 2, \cdots, K_t\}\) are unknown ‘true’ parameters. Also, the unknown reward distribution is bounded. A common bound is \([0, 1]\).</p>

  <p>The goal is to design an \(\mathsf{A}\) that maximizes the sum of rewards over rounds. As data rolls in, \(\mathsf{A}\) learns a policy \(\pi\), which maps features to a selected action. A performant algorithm will, in minimal rounds, learn enough about the \(\boldsymbol{\theta}_k\)’s to develop a high reward \(\pi\).</p>
</blockquote>

<p>To ground this, we’ll start with a simple example where the true parameters, the \(\boldsymbol{\theta}_k\)’s, are known.</p>

<h2 id="known-parameters">Known Parameters</h2>

<p>We suppose in all rounds, there are only two actions, represented with a single number (not counting an intercept term) and the parameters are known. Prior to selecting an action, this can be represented as:</p>

<p class="center"><img src="/assets/images/posts/linUCB/pic1.webp" alt="illustration" style="width: 100%" loading="lazy" />
<span class="figure-caption"><strong>Figure 1</strong>: This shows the contextual bandit decision to make when action functions are known. The blue lines tells us \(\mathbb{E}[r_{0, k}|x]\), the expected reward as a function of the context for the actions \(k=1\) and \(k=2\). The vertical lines give us the contexts for the first round: \(x_{0, 1}\) and \(x_{0, 2}\). The green bands give us standard deviations of \(r_{0, k}\) around its expected value.</span></p>

<p>The contexts are shown with vertical lines. Since \(\boldsymbol{\theta}_1\) and \(\boldsymbol{\theta}_2\) are known, we can see the linear relationship between contexts and expected rewards. We refer to these as <em>action functions</em>. Further, one and two standard deviation bands are shown, highlighting the reward distributions.</p>

<p>Recalling the goal to maximize reward, which action would you select? That is, in figure 1, would you select action 1 on the left or action 2 on the right?</p>

<p>Clearly, we should choose action 2, because it yields a higher expected reward, indicated with the horizontal lines.</p>

<p class="center"><img src="/assets/images/posts/linUCB/pic2.webp" alt="illustration" style="width: 100%" loading="lazy" />
<span class="figure-caption"><strong>Figure 2</strong>: This tells us the decision criteria when action functions are known. Simply pick the action which yields the higher expected reward given the context. In this case, it is action 2.</span></p>

<p>Following the selection, we observe a reward of only the selected action. Due to noise, it is not equal to the expected reward. Selecting \(a_1 = 2\) gives us our first observed data point:</p>

<p class="center"><img src="/assets/images/posts/linUCB/pic3.webp" alt="illustration" style="width: 100%" loading="lazy" />
<span class="figure-caption"><strong>Figure 3</strong>: After making a selection, we observe a sampled reward, indicated as a dot at the chosen context.</span></p>

<p>All of this is to illustrate the information available prior to a decision, the decision itself and the information gained with a decision. It is crucial to crystalize this repeating process when working with contextual bandits.</p>

<p>Lastly, because the action functions are known, we can determine the optimal policy \(\pi^*\) and its expected reward by taking the max across action functions:</p>

<p class="center"><img src="/assets/images/posts/linUCB/pic3b.webp" alt="illustration" style="width: 80%" loading="lazy" />
<span class="figure-caption"><strong>Figure 4</strong>: This is the optimal policy. It chooses the max expected reward action for any \(x\). The blue line is the policy’s expected reward.</span></p>

<h2 id="unknown-parameters">Unknown Parameters</h2>

<blockquote>
  <p>Knowing the true parameters makes the above a trivial case. The complexity arises when the parameters<sup id="fnref:2"><a href="#fn:2" class="footnote" rel="footnote" role="doc-noteref">2</a></sup> are unknown. In this case, selecting an action has two purposes:</p>
  <ul>
    <li>Obtain a high reward.</li>
    <li>Obtain information needed to make future high reward selections.</li>
  </ul>

  <p>This is the fundamental challenge of bandits, known as the <em>exploration-exploitation</em> trade-off.</p>
</blockquote>

<p>To see it, let’s play the same game, but with unknown action functions. The first round begins with the context:</p>

<p class="center"><img src="/assets/images/posts/linUCB/pic4.webp" alt="illustration" style="width: 100%" loading="lazy" />
<span class="figure-caption"><strong>Figure 5</strong>: With unknown action functions, we face the following decision in round 1. Clearly, there is no information to favor one action over the other.</span></p>

<h2 id="a-naive-approach">A Naive Approach</h2>

<p>In round \(t=1\), we have no data, so we may as well guess randomly. In fact, the second round will be subject to the same, albeit slightly ameliorated, problem and so, the next round might as well be random as well. This suggests a naive algorithm:</p>

<ul>
  <li>
    <p>Randomly select an action for the first 100 rounds.</p>
  </li>
  <li>
    <p>Fit a linear regression model for each action.</p>
  </li>
  <li>
    <p>For all future rounds, use the fitted linear models as though they were the true action functions.</p>
  </li>
</ul>

<p>Let’s call this the ‘explore-then-exploit’ approach. After 100 exploratory rounds, we have data and a fit like this:</p>

<p class="center"><img src="/assets/images/posts/linUCB/pic5.webp" alt="illustration" style="width: 100%" loading="lazy" />
<span class="figure-caption"><strong>Figure 6</strong>: The dotted lines are the estimated action functions after 100 exploratory rounds of randomly selected actions.</span></p>

<p>Going forward, we select the action which, given the context, yields a higher value.</p>

<p>The question is: how will it perform?</p>

<p>To answer that, we need to discuss the contextual bandit community’s favor metric, <em>regret</em>. To get there, we start with something simpler.</p>

<h2 id="expected-payoff">Expected Payoff</h2>

<p>A natural metric of the learning algorithm \(\mathsf{A}\) is the total \(T\)-round payoff:</p>

\[\sum_{t=1}^T r_{t, a_t}\]

<p>This, however, is a random variable. Running \(\mathsf{A}\) through a contextual bandits simulation twice will give two different values. So some of it speaks to the randomness of the simulation. We prefer the <em>expected</em> payoff:</p>

\[\mathbb{E}\Big[\sum_{t=1}^T r_{t, a_t}\Big]\]

<p>If we run \(\mathsf{A}\) for \(S\) simulations of \(T\)-rounds, this value is what the average payoff approaches as \(S \rightarrow \infty\). It is a more pure evaluation of \(\mathsf{A}\) than the payoff. Note that in addition to \(\mathsf{A}\), it depends on \(T\) and the environment.</p>

<p>However, the expected payoff lacks something important. To see this, we plot it as a function of \(T\) for the explore-then-exploit algorithm:</p>

<p class="center"><img src="/assets/images/posts/linUCB/pic6.webp" alt="illustration" style="width: 75%" loading="lazy" />
<span class="figure-caption"><strong>Figure 7</strong>: Expected payoff of the 100-round explore-then-exploit algorithm.</span></p>

<p>Is this good? It’s increasing, but how much is good enough? Notice it’s increasing for the first 100 rounds where we select actions randomly. This is because rewards for both actions are, on average, slightly positive. So increasing payoff is no indication of a performant algorithm.</p>

<p>The problem is there’s no benchmark.</p>

<h2 id="regret">Regret</h2>
<blockquote>
  <p><strong>Regret</strong> measures performance against the <em>optimal policy</em>, the policy derived knowing the true action functions. It’s the policy we derived in the trivial game played in the ‘Known Parameters’ section. Following the definition<sup id="fnref:3"><a href="#fn:3" class="footnote" rel="footnote" role="doc-noteref">3</a></sup> in <a href="https://arxiv.org/abs/1003.0146" target="_blank" rel="noopener noreferrer">Li (2010)</a>, it’s written as:</p>

\[R_\mathsf{A}(T) = \underbrace{\mathbb{E}\Big[\sum_{t=1}^T r_{t, a_t^*}\Big]}_{\substack{\textrm{Expected payoff} \\ \textrm{of optimal policy}}} - \underbrace{\mathbb{E}\Big[\sum_{t=1}^T r_{t, a_t}\Big]}_{\substack{\textrm{Expected payoff} \\ \textrm{of }\mathsf{A}}}\]

  <p>This is better. If and when \(R_\mathsf{A}(T)\) flattens out, the additional rounds create near-zero terms, meaning \(\mathsf{A}\) has learned approximately the optimal policy.</p>
</blockquote>

<p>Let’s view it for the explore-then-exploit algorithm:</p>

<p class="center"><img src="/assets/images/posts/linUCB/pic7.webp" alt="illustration" style="width: 75%" loading="lazy" />
<span class="figure-caption"><strong>Figure 7</strong>: Regret of the 100-round explore-then-exploit algorithm</span></p>

<p>This is more informative. We see each exploratory round accrues a constant increment of regret, creating an upward sloping line. On the 101-th round, we approximately learn the optimal policy, so the increments of regret fall to near zero, creating a near flat line.</p>

<p>The kink at \(t=100\) suggests regret strongly depends on the number of exploratory rounds, so we vary that value next:</p>

<p class="center"><img src="/assets/images/posts/linUCB/pic8.webp" alt="illustration" style="width: 100%" loading="lazy" />
<span class="figure-caption"><strong>Figure 9</strong>: The regret of the exploit-then-exploit algorithm with varying exploratory rounds</span></p>

<p>This clearly shows the explore-exploit trade-off. With a few exploratory rounds, we reduce the length of the initial steep incline but pay for it with a positive slope in the exploit rounds. With more exploration, this long term slope is reduced.</p>

<p>One might tune the number of exploratory rounds, but doing so requires knowing the total number of rounds \(T\) upfront, which is only sometimes true in practice. Fortunately, LinUCB offers a better solution.</p>

<p>Before we get into it, I’ll make a few general comments on regret.</p>

<ul>
  <li>
    <p>Computing regret requires the optimal policy. In applications, the optimal policy is unknown, so regret is unknown. It is only known in theoretical or simulated environments. It is assumed, without controversy, an \(\mathsf{A}\) with performant regret in such environments will perform well in the real world.</p>
  </li>
  <li>
    <p>Regret, as defined here as a difference of expectations, is always non-decreasing in \(T\). This is because in every round, no learning algorithm, after observing finite data, can produce a policy better than the optimal policy in expectation. Therefore, the goal is for \(R_\mathsf{A}(T)\) to grow as slowly as possible.</p>
  </li>
  <li>
    <p>Outside of plots, an algorithm’s regret is expressed as a category of increasing function of \(T\) and often of other variables, like the dimensionality of the context \(d\) or the number of actions \(K\). For instance, an algorithm might achieve ‘\(O(d\sqrt{TK})\) regret,’ meaning it eventually always remains less than \(cd\sqrt{TK}\) for some constant \(c\). See Table 2 of <a href="https://arxiv.org/abs/1508.03326" target="_blank" rel="noopener noreferrer">Zhou (2016)</a> for a list of algorithms and their regret, albeit sometimes defined differently than here.</p>
  </li>
  <li>
    <p>Revealing my applied-consultant-non-academic side, I believe regret receives extra attention in the literature because bounds on it can be <em>proven</em>, unlike most reinforcement learning metrics. Indeed, often an algorithm is sold on a proof of an attractively slow growing regret bound.</p>

    <p>However in my experience, regret is not sufficient for selecting the best algorithm for an application. Regret is conditional on an idealized environment, which may only approximate the real world. In <a href="https://arxiv.org/abs/1802.04064" target="_blank" rel="noopener noreferrer">Bietti (2021)</a>, after evaluating several algorithms against a large number of supervised learning datasets, the authors discovered performance often ran counter to what the theory suggests.</p>

    <p>Also, compute efficiency matters. An algorithm that is slow to run may be impossible to improve iteratively.</p>

    <p>Lastly, simple, easy-to-debug algorithms are preferred, since evaluation and getting targeted feedback in live environments is challenging.</p>
  </li>
</ul>

<h2 id="linucb-the-idea">LinUCB: the Idea</h2>
<blockquote>
  <p>With the problem defined, we move onto the <strong>LinUCB</strong> algorithm, the ‘disjoint’ variate described in <a href="https://arxiv.org/abs/1003.0146" target="_blank" rel="noopener noreferrer">Li (2010)</a>. “Lin” refers to the linearity assumption relating contexts to expected rewards. “UCB” refers to a broad class of <em>upper confidence bound</em> algorithms, which establish a simple approach to the exploration-exploitation trade-off. For every action, they estimate a confidence interval around its expected reward and select the action with the highest upper confidence bound. This is referred to as an ‘optimism under uncertainty’ heuristic, and empirically, it performs well.</p>

  <p>Further, the linearity assumption and optimism heuristic work quite nicely together. The upper bound can be computed analytically and <em>updated</em> in each round. As we’ll see, the algorithm essentially runs \(K\) online ridge regressions, which bring convenient Bayesian computations of confidence<sup id="fnref:4"><a href="#fn:4" class="footnote" rel="footnote" role="doc-noteref">4</a></sup> intervals.</p>
</blockquote>

<p>To motivate this, suppose the explore-then-exploit approach applied ridge regression following the initial 100 exploratory rounds. On the next round, we face the contexts of the vertical lines:</p>

<p class="center"><img src="/assets/images/posts/linUCB/pic9.webp" alt="illustration" style="width: 100%" loading="lazy" />
<span class="figure-caption"><strong>Figure 10</strong>: After 100 exploratory rounds, we fit ridge regression models. Ridge regression permits efficient calculations of confidence intervals over the expected reward. The optimism-under-uncertainty heuristic means, for the given contexts, we select the action with the higher UCB, indicated with the yellow arrows. In this case, that is to select action 2.</span></p>

<p>The UCB’s are indicated with the height of the yellow arrows. By the optimism heuristic, we select action 2 (that is, \(a_{101} = 2\)), since it has the higher UCB. This is to be ‘optimistic’ because its equivalent to believing each action’s true expected reward is <em>at its upper confidence bound</em> and selecting the highest. This optimism is what causes the algorithm to explore.</p>

<p>To clarify a point of confusion, these confidence bands are referring to the likely positions of the true <em>expected</em> rewards. They do not illustrate the distribution of the reward around its expectation, as was done in the first section. Notably, the action-selection rule only references the distribution of the expected reward and no other piece of the reward distribution. So if two actions had equal expected reward and equal expected reward variance but action 1 had a higher reward variance, LinUCB would be indifferent between the two. The assumption is that other differences in reward distribution are no indication of something worth learning for accruing future reward.</p>

<p>Further, the width of the confidence bands are scaled by a tunable hyperparameter \(\alpha &gt; 0\). It answers the question ‘<em>how</em> optimistic are we?’ If \(\alpha \approx 0\), we are quick to exploit. The confidence bands are very thin and we select the highest expected reward action virtually every time. If \(\alpha\) is very large, the confidence intervals dominate the selection; we select the action we are most uncertain about.</p>

<h2 id="linucb-the-algorithm">LinUCB: the Algorithm</h2>

<p>The previous section is illustrative, but is univariate and not quite right because LinUCB does not begin with a batch of random exploratory rounds. In LinUCB, each round balances exploration and exploitation, much like the 101-th round plotted above. In this section, we’ll discuss the algorithm generally and exactly, following the presentation in the original paper.</p>

<p>Let \(\mathbf{r}_{k}\) refer to the observed rewards of action \(k\) collected into a vector. Note that after 100 rounds, this vector will very likely have fewer than 100 observations, since we observe only one action’s reward per round. Next, let \(\mathbf{X}_{k}\) be a matrix with the context vectors of action \(k\) as rows. Again, this matrix is only for context vectors that were chosen<sup id="fnref:5"><a href="#fn:5" class="footnote" rel="footnote" role="doc-noteref">5</a></sup>. With regularization hyperparameter \(\lambda\), the ridge regression estimate is:</p>

\[\hat{\boldsymbol{\theta}}_k = \big(\mathbf{X}_{k}^{\top}\mathbf{X}_{k} + \lambda \mathbf{I}\big)^{-1}\mathbf{X}_{k}^{\top}\mathbf{r}_{k}\]

<p>And the estimated expected reward for action \(k\) is \(\mathbf{x}_{t, k}^{\top}\hat{\boldsymbol{\theta}}_k\). Further, when the assumptions of ridge regression hold (e.g. IID noise), we can form a confidence interval as follows. With probability at least \(1 - \delta\), we have:</p>

\[\Big| \mathbf{x}_{t, k}^{\top}\hat{\boldsymbol{\theta}}_k - \mathbf{x}_{t, k}^{\top}\boldsymbol{\theta}_k \Big| &lt; \alpha \sqrt{\mathbf{x}_{t, k}^{\top}\mathbf{A}_k^{-1}\mathbf{x}_{t, k}^{\phantom{\top}}}\]

<p>where \(\mathbf{A}_k = \mathbf{X}_{k}^{\top}\mathbf{X}_{k} + \lambda \mathbf{I}\) and \(\alpha = 1 + \sqrt{\ln(2/\delta)/2}\) is the width-controlling hyperparameter describe previously. Note that \(\mathbf{x}_{t, k}^{\top}\boldsymbol{\theta}_k\) is the true expected reward of action \(k\) under our assumptions.</p>

<p>This enables us. For every action, we can construct ranges which will contain the true expected reward with a pre-specified probability. By the optimism-under-uncertainty heuristic, we choose the action with the highest expected reward within these ranges. That is, we set:</p>

\[a_t = \underset{k=1, \cdots, K_t}{\arg\max} \Big(\mathbf{x}_{t, k}^{\top}\hat{\boldsymbol{\theta}}_k + \alpha \sqrt{\mathbf{x}_{t, k}^{\top}\mathbf{A}_k^{-1}\mathbf{x}_{t, k}^{\phantom{\top}}}\Big)\]

<p>This expression is the generalized action-selection rule illustrated in the previous section and tells us everything we need to compute to select an action. Outside of the given context vectors and \(\alpha\), we need \(\mathbf{A}_k\) and \(\hat{\boldsymbol{\theta}}_k\). Both of these could be computed in an online fashion, updating (instead of recomputing) their values on each round.</p>

<p>Expanding notation slightly, if \(\mathbf{A}_k^{(t)}\) is defined for data up until round \(t\), then on each round, we perform:</p>

\[\mathbf{A}_{a^t}^{(t)} = \mathbf{A}_{a^t}^{(t-1)} + \mathbf{x}_{t, a^t}^{\phantom{\top}}\mathbf{x}_{t, a^t}^{\top}\]

<p>Notice we are using the subscript \(a^t\) where one might expect \(k\). This says the update is only done for the selected action.</p>

<p>To update \(\hat{\boldsymbol{\theta}}_k\), we let \(\mathbf{b}_k = \mathbf{X}_{k}^{\top}\mathbf{r}_{k}\). So \(\hat{\boldsymbol{\theta}}_k = \mathbf{A}_k^{-1}\mathbf{b}_k\). On each round, we update:</p>

\[\mathbf{b}_{a_t}^{(t)} = \mathbf{b}_{a_t}^{(t-1)} + r_{t, a_t}\mathbf{x}_{t, a_t}\]

<p>One could also update \(\mathbf{A}_k^{-1}\) with the <a href="/concepts/linear-algebra/matrix-inversion-lemma/" target="_blank" rel="noopener noreferrer">matrix inversion lemma</a>, but this may create issues of numerical stability. If the inversion creates a compute constraint, one could use the <a href="https://en.wikipedia.org/wiki/Delta_rule" target="_blank" rel="noopener noreferrer">Delta rule</a> to approximate the changes to \(\hat{\boldsymbol{\theta}}_{a_t}\).</p>

<p>With this, the algorithm is copied here:</p>

<p class="center"><img src="/assets/images/posts/linUCB/algo.webp" alt="illustration" style="width: 75%" loading="lazy" />
<span class="figure-caption">Algorithm 1 from <a href="https://arxiv.org/abs/1003.0146" target="_blank" rel="noopener noreferrer">Li (2010)</a></span></p>

<p>I’ll clarify some minor differences. I included the regularizing hyperparameter \(\lambda \geq 0\) that comes with ridge regression<sup id="fnref:6"><a href="#fn:6" class="footnote" rel="footnote" role="doc-noteref">6</a></sup>. The adjustment to this algorithm is to replace \(\mathbf{I}_d\) with \(\lambda\mathbf{I}_d\), where \(d\) is the dimension of the associated context vector. \(\mathcal{A}_t = \{1, \cdots, K_t\}\) is the set of actions available at round \(t\). Further, this algorithm uses \(a\) where I would use \(k\), to index all actions and distinguish it from the selected action \(a_t\).</p>

<p>Essentially, this algorithm does what we illustrated on the 101-th round, except for all rounds and in an efficient online fashion. Here, with one simulation, we show its behavior in selecting actions, updating the distribution of expected rewards and accruing a flattening regret:</p>

<p class="center"><img src="/assets/images/posts/linUCB/regret_gif.gif" alt="gif" style="width: 100%" loading="lazy" />
<span class="figure-caption"><strong>Figure 11</strong>: Running the LinUCB algorithm using \(\lambda = 1\) and \(\alpha = .5\). Here, we show <em>realized regret</em> (also called <em>pseudo regret</em>), since it is available for a single simulation. It is defined similarly to how we defined regret, except the action selections are not averaged out. In other words, it is random since action selections depend on the random history of observations. </span></p>

<p>Running many simulations, we estimate regret, which is substantially lower than all the explore-then-exploit runs we did above.</p>

<p class="center"><img src="/assets/images/posts/linUCB/pic10.webp" alt="illustration" style="width: 100%" loading="lazy" />
<span class="figure-caption"><strong>Figure 12</strong>: LinUCB’s regret estimated over 5000 simulations using \(\lambda = 1\) and \(\alpha = .5\).</span></p>

<p>If you’d like to play with the algorithm yourself, you can find the demo code <a href="https://github.com/Duane321/true_theta/blob/main/posts/linUCB/run_lin_ucb.ipynb" target="_blank" rel="noopener noreferrer">here</a>.</p>

<h2 id="in-closing">In Closing</h2>

<p>This algorithm and its cousin algorithm, Algorithm 2 from <a href="https://arxiv.org/abs/1003.0146" target="_blank" rel="noopener noreferrer">Li (2010)</a> handling the shared-features case, are strong options for applied contextual bandit problems, in my experience. They are empirically performant, interpretable, simple to code, able to handle large data, stable when training and flexible via context feature engineering. If you are considering an online<sup id="fnref:7"><a href="#fn:7" class="footnote" rel="footnote" role="doc-noteref">7</a></sup> contextual bandits algorithm, it is a fine place to start. Another good option is to reach out to us.</p>

<p>Also, I will mention a technical <em>mistake</em> of LinUCB. It was pointed out in <a href="https://proceedings.neurips.cc/paper_files/paper/2011/file/e1d5be1c7f2f456670de3d53c7b54f4a-Paper.pdf" target="_blank" rel="noopener noreferrer">Abbasi-Yadkori (2011)</a> that LinUCB, among others, assumes the observations are independent across rounds. Indeed, ridge regression assumes rows are independent samples. This is not actually true. Observations depend on actions, which depend on historical observations. This technicality has implications for LinUCB performance, but it remains a safe option in practice. Because it’s an interesting complexity, I expect to explore it in a future post.</p>

<p>Finally, I’d like to thank <a href="https://www.linkedin.com/in/oege-dijk/" target="_blank" rel="noopener noreferrer">Oege Dijk</a> for his helpful feedback and commentary on this article.</p>

<hr />

<h3 id="references">References</h3>

<ol>
  <li>
    <p>L. Li, W. Chu, J. Langford and R. E. Schapire. <a href="https://arxiv.org/abs/1003.0146" target="_blank" rel="noopener noreferrer">A Contextual-Bandit Approach to Personalized News Article Recommendation</a>. Proceedings of the 19th international conference on World wide web. April, 2010.</p>
  </li>
  <li>
    <p>L. Zhou. <a href="https://arxiv.org/abs/1508.03326" target="_blank" rel="noopener noreferrer">A Survey on Contextual Multi-armed Bandits</a>. arXiv:1508.03326v2. February 1, 2016.</p>
  </li>
  <li>
    <p>Y. Abbasi-Yadkori, D. Pal and C. Szepesvari. <a href="https://proceedings.neurips.cc/paper_files/paper/2011/file/e1d5be1c7f2f456670de3d53c7b54f4a-Paper.pdf" target="_blank" rel="noopener noreferrer">Improved Algorithms for Linear Stochastic Bandits</a>. NIPS. 2011.</p>
  </li>
  <li>
    <p>Aleksandrs Slivkins. <a href="https://arxiv.org/abs/1904.07272" target="_blank" rel="noopener noreferrer">Introduction to Multi-Armed Bandits</a>. <em>arXiv</em>. 2019.</p>
  </li>
  <li>
    <p>A. Bietti, A. Agarwal and J. Langford. <a href="https://arxiv.org/abs/1802.04064" target="_blank" rel="noopener noreferrer">A Contextual Bandit Bake-off</a>. arXiv:1802.04064v5. 2021</p>
  </li>
</ol>

<hr />
<h3 id="something-to-add">Something to Add?</h3>

<p>If you see an error, egregious omission, something confusing or something worth adding, please email <a href="mailto:dj@truetheta.io">dj@truetheta.io</a> with your suggestion. If it’s substantive, you’ll be credited. Thank you in advance!</p>

<hr />
<h3 id="footnotes">Footnotes</h3>

<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:1">
      <p>If you read the post <a href="/concepts/reinforcement-learning/contextual-bandits/" target="_blank" rel="noopener noreferrer">Contextual Bandits as Supervised Learning</a>, you may recall there was one feature vector per round. That is, contextual bandits as presented there had <em>only</em> shared features. So, does this present two version of the contextual bandit problem? No, a single vector in each round <em>could</em> be a concatenation of vectors describing each action. So, separating shared features and non-shared features does not change the permissible information of the contextual bandit problem. In this post and the last, the problem is the same. However, the LinUCB algorithm has two variants which are different in this regard. This is because the calculation of uncertainty depends on where a feature is shared or not. <a href="#fnref:1" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:2">
      <p>In applications, we have the added complexity of selecting the functional form of the action functions. Is it linear? A neural net? Something tree-like? In my experience, real world applications are quite noisy, data constrained and nonstationary, making linear models a safe and reasonable choice.</p>

      <p>On a technical note, the assumption that the true expected reward function belongs to a class of function from which we estimate is called <em>realizability</em>. For example, we assume realizability if we assume the action functions are linear and apply linear regression. In practice, realizability rarely holds exactly but is accepted as an approximation. <a href="#fnref:2" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:3">
      <p>The literature has several definitions of regret, so I highlight the specific one I’m using. <a href="#fnref:3" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:4">
      <p>To distinguish Bayesian and frequentist concepts, we should technically call these ‘credible’ intervals. However, the contextual bandit literature makes no such distinction, so I’ll continue calling them confidence intervals. <a href="#fnref:4" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:5">
      <p>It’s worth noting that LinUCB is throwing out all contextual vectors which weren’t selected. This is quite a bit of information, and it could be used, a la semi-supervised learning, to learn covariance information on the contextual vectors. In practice, however, it is reasonable. LinUCB quickly learns which actions are worth not selecting. To perform updates for all contextual vectors is to waste compute on actions known to yield little reward. <a href="#fnref:5" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:6">
      <p>There is something lost in the import of ridge regression’s \(\lambda\). In supervised learning, \(\lambda\) may be chosen via cross validation. That selection process is specific to the size of the data. Smaller data is likely to select for a larger \(\lambda\). In contextual bandits, the size of the data is not fixed, and so there is no analogous selection criteria. I suspect this may be a reason \(\lambda\) was omitted from the original algorithm. Nonetheless, it has a meaningful impact on performance, so I include it. <a href="#fnref:6" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:7">
      <p>Since it would be a digression and this post is long enough, I have left out the discussion of <em>on-policy</em> versus <em>off-policy</em> methods. In practice, off-policy methods are much more common. On-policy methods require the algorithm learns in live environments. Off-policy methods allow learning from historical data collected under a different policy, but it suffers degraded estimates of the policy’s performance. LinUCB, as presented, is an on-policy method but can be adapted to be off-policy. You should be aware that managing the bias and variance issues of off-policy methods is a delicate business. <a href="#fnref:7" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
  </ol>
</div>]]></content><author><name>Duane Rich</name></author><category term="reinforcement-learning" /><category term="Post Formats" /><summary type="html"><![CDATA[In this post, we learn about contextual bandits and the reliable linUCB algorithm.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://djrich.dev/assets/images/posts/linUCB/thumbnail.webp" /><media:content medium="image" url="https://djrich.dev/assets/images/posts/linUCB/thumbnail.webp" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Contextual Bandits as Supervised Learning</title><link href="https://djrich.dev/concepts/reinforcement-learning/contextual-bandits/" rel="alternate" type="text/html" title="Contextual Bandits as Supervised Learning" /><published>2024-06-28T00:00:00+00:00</published><updated>2024-06-28T00:00:00+00:00</updated><id>https://djrich.dev/concepts/reinforcement-learning/contextual-bandits</id><content type="html" xml:base="https://djrich.dev/concepts/reinforcement-learning/contextual-bandits/"><![CDATA[<p><strong>Contextual bandit algorithms are arguably<sup id="fnref:1"><a href="#fn:1" class="footnote" rel="footnote" role="doc-noteref">1</a></sup> the most profitable form of reinforcement learning deployed in the real world.</strong> Concretely, in <a href="https://arxiv.org/abs/1606.03966" target="_blank" rel="noopener noreferrer">Agarwal et al (2017)</a>, the Microsoft team demonstrated how a well engineered contextual bandit system delivered a 25% increase in click-through rate on the MSN homepage. As we’ll see, contextual bandits are ideal for targeted advertising, so their value is certainly measured in billions. There have also been substantial applications in <a href="https://arxiv.org/abs/2204.11910" target="_blank" rel="noopener noreferrer">tax auditing</a>,  <a href="https://eng.lyft.com/a-review-of-multi-armed-bandits-applications-at-lyft-132550dd4df3" target="_blank" rel="noopener noreferrer">ride sharing</a>, <a href="https://github.com/eHarmony/vw-webservice" target="_blank" rel="noopener noreferrer">dating services</a><sup id="fnref:2"><a href="#fn:2" class="footnote" rel="footnote" role="doc-noteref">2</a></sup>, <a href="https://doordash.engineering/2022/03/15/using-a-multi-armed-bandit-with-thompson-sampling-to-identify-responsive-dashers/" target="_blank" rel="noopener noreferrer">food delivery</a>, <a href="https://www.aboutwayfair.com/careers/tech-blog/griffin-how-wayfair-leverages-reinforcement-learning-to-send-customers-relevant-communications" target="_blank" rel="noopener noreferrer">e-commerce</a>, <a href="https://multithreaded.stitchfix.com/blog/2020/08/05/bandits" target="_blank" rel="noopener noreferrer">online retail</a> and many others (see <a href="https://arxiv.org/abs/1904.10040" target="_blank" rel="noopener noreferrer">Bouneffouf et al. (2019)</a> for more cases).</p>

<p>Despite their utility, contextual bandits are misunderstood. This is partly due to how data scientists are typically trained. They are trained primarily in <em>supervised learning</em>, the task of learning a function from data.</p>

<blockquote>
  <p>As hammers see nails, data scientists see supervised learning problems everywhere. But as the causal inference people will scream, most <em>decisioning</em> problems are undeniably not of this type.</p>
</blockquote>

<p>So in this post, I’ll present contextual bandits, but from the perspective of supervised learning. My thinking is that we can leverage something familiar, supervised learning, to learn something less familiar, contextual bandits. This should simplify contextual bandits while highlighting its very consequential difference with supervised learning.</p>

<h2 id="the-contextual-bandit-problem">The Contextual Bandit Problem</h2>

<p>The contextual bandit (‘CB’) problem is defined with repeated runs of the following process:</p>

<blockquote>
  <p>1) A <strong>context</strong> is presented.</p>

  <p>2) An <strong>action</strong> is taken in this context.</p>

  <p>3) A <strong>reward</strong>, depending on the context and action, is realized.</p>
</blockquote>

<p>A classic example is <a href="https://arxiv.org/abs/1003.0146" target="_blank" rel="noopener noreferrer">personalized news article recommendations</a>. That is, which articles should be shown to website users such that they are likely to click them? This can be framed as a CB process:</p>

<ul>
  <li>
    <p><strong>The context describes the circumstance of a user visiting a webpage</strong>, known before an article is recommended. Practically, the context is a vector describing things like the user’s demographics, their previous activities and the time of the visit<sup id="fnref:3"><a href="#fn:3" class="footnote" rel="footnote" role="doc-noteref">3</a></sup>.</p>
  </li>
  <li>
    <p><strong>The action is to choose one of many articles to recommend</strong>. Notably, the available actions depend on the context; only certain news articles are recent and relevant at a given time.</p>
  </li>
  <li>
    <p><strong>The reward is click-through rate.</strong> That is, it’s a binary indicator of whether the user clicked the recommended article.</p>
  </li>
</ul>

<p>In general, the goal is to develop algorithms that learn high-reward actions.</p>

<h2 id="contextual-bandits-as-a-generalization-of-supervised-learning">Contextual Bandits as a Generalization of Supervised Learning</h2>

<p>One way to understand CB is to relate it to supervised learning (‘SL’). As mentioned, SL is the task of learning a function from data. Think of it like this:</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/contextual_bandits/supervised_learning.png" alt="illustration" style="width: 100%" />
<span class="figure-caption">On the left, we have data and the true function \(f(x)\) that generated it. On the right, the data is smoothed to approximate \(f(x)\).</span></p>

<p>On the left, the \((x, y)\) points are the observed data. The smooth curve is the ‘true’ function we can’t observe, but we assume it generates the data, something like \(y = f(x) + \epsilon\), where \(f(\cdot)\) is the function and \(\epsilon\) is random noise. More explicitly, the data generating process is:</p>

<blockquote>
  <p>1) An \(x\) is sampled from some unknown distribution.</p>

  <p>2) \(x\) is plugged into \(f(\cdot)\) and corrupted with noise: \(y = f(x) + \epsilon\).</p>
</blockquote>

<p>This is what we assume gives us the data on the left.</p>

<p>SL moves us to the plot on the right, where the data has been smoothed into \(\hat{f}(\cdot)\), something approximating the true \(f(\cdot)\). With \(\hat{f}(\cdot)\), we can predict \(y\) for any \(x\)<sup id="fnref:4"><a href="#fn:4" class="footnote" rel="footnote" role="doc-noteref">4</a></sup>. That’s the goal of SL.</p>

<p>To generalize from SL to CB, we now imagine there are two true unknown functions, one for each of two <em>actions</em>:</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/contextual_bandits/two_funcs.png" alt="illustration" style="width: 55%" />
<span class="figure-caption">We assume there are two data generating functions, one for each action.</span></p>

<p>What remains is to specify how actions are selected according to \(x\), which we will now call the context. This is done with a <em>policy</em>, a function \(\pi(\cdot)\) which selects the action \(a\) in a given context.</p>

\[\pi(x) = a\]

<p>Now we can write the assumed CB data generating process as a translation of SL:</p>

<blockquote>
  <p>1) A context \(x\) is sampled.</p>

  <p>2) An action is chosen in the context: \(\pi(x) = a\)</p>

  <p>3) A reward<sup id="fnref:5"><a href="#fn:5" class="footnote" rel="footnote" role="doc-noteref">5</a></sup> \(y_a\) is generated by the chosen function: \(y_a = f_a(x) + \epsilon\).</p>
</blockquote>

<p>I write it this way to make the close connection of SL and CB obvious. They heavily overlap. For instance, if the policy always selected one action, CB would reduce to SL, where we would learn the reward of one action.</p>

<p>From this perspective, the goal is to maximize the reward. That is, averaged over many sampled \(x\)’s, we want a \(\pi(x)\) that produces large positive \(y\)’s.</p>

<h2 id="the-policy-problem">The Policy Problem</h2>

<p>Unfortunately, introducing the policy seriously complicates our job.</p>

<p>To see this, remember our goal is to learn high-reward policies. That means for any \(x\), we should select the max of \(f_1(x)\) and \(f_2(x)\), which means we need approximations \(\hat{f}_1(x)\) and \(\hat{f}_2(x)\), like what SL would provide. To do that, we need data. By the CB process, there must be some policy operating that produced that data.</p>

<p><strong>The problem is policies can hide the true functions</strong>. Below is a policy that does this. Each data point is colored according to the action selected.</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/contextual_bandits/policy_data.png" alt="illustration" style="width: 62%" />
<span class="figure-caption">Observations of the two action functions according to a policy that is very likely to select the red action if \(x &lt; 5\). </span></p>

<p>Choosing an action is to sample an observation from one of the functions.</p>

<blockquote>
  <p>If the action is fully determined by \(x\), then we never observe one of the functions at every location. This is a blindness.</p>
</blockquote>

<p>In the plot, we have no observations of the blue function on the left and no observations of the red function on the right. If we apply SL to these datasets, we see high errors in the unobserved regions:</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/contextual_bandits/policy_data_SLed.png" alt="illustration" style="width: 55%" />
<span class="figure-caption">Supervised learning applied to a non-exploratory policy. The dotted lines are the learned functions. On the left, the blue learned function is a poor approximation. Similarly on right, the red is a poor approximation.</span></p>

<p>To address this, we want policies which do <em>random exploration</em>. That is, for any \(x\), there’s a nonzero probability of selecting any action.</p>

<blockquote>
  <p>Practically, we need a policy that gathers enough cross-action data to determine which is likely the max action.</p>
</blockquote>

<p>Here is what happens if the policy ignores \(x\) and picks the action by flipping a coin.</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/contextual_bandits/exploratory_policy.png" alt="illustration" style="width:100%" />
<span class="figure-caption">With an exploratory policy, one that selects actions at random, supervised learning can accurately learn the functions of both actions.</span></p>

<p>With the functions learned, we can set the policy to select the max action:</p>

<p class="center"><img src="https://djrich.dev/assets/images/posts/contextual_bandits/optimal_policy.png" alt="illustration" style="width: 55%" />
<span class="figure-caption">Data generated by the optimal policy, the policy that maximizes \(y\)’s.</span></p>

<p>This shows that by approximately learning \(f_1(\cdot)\) and \(f_2(\cdot)\) via random exploration, we can determine a policy that maximizes expected reward in all contexts. That is, this is nearly the best policy for generating large, positive \(y\)’s.</p>

<h2 id="on-vs-off-policy">On Vs Off Policy</h2>

<p>By anchoring to SL, where function learning is done with a fixed batch of data, I’ve explained the <em>off-policy</em> version of CB. That is, data is collected under one policy and used to develop another that achieves higher reward. Without getting into the details, off-policy learning, especially when the data isn’t collected under an exploration-oriented policy, is quite hard. It should be avoided if it can be.</p>

<p>So, I’ll switch our presentation to <em>on-policy</em> learning. That is, there’s a <em>single algorithm</em> that both generates the data by choosing actions and is designed to maximize expected reward.</p>

<h2 id="explore-exploit">Explore-Exploit</h2>

<p>But this raises an issue. As we saw, random exploration is needed to estimate the functions used to construct the optimal policy. However, random exploration is costly in that it foregoes reward; flipping a coin is to select the lesser action half the time. Notice how the optimal policy above does no exploration; it always selects one and only one action depending on the context.</p>

<p>Balancing the learnings produced with random exploration with the reward of <em>exploiting</em> what is known so far is the <em>exploration-exploitation</em> trade-off, something notoriously difficult to management.</p>

<h2 id="well-optimized-tools">Well Optimized Tools</h2>

<p>Fortunately, there are strong, well researched opensource algorithms. In a future post, I’ll discuss my favorite tool for apply contextual bandits: <a href="https://vowpalwabbit.org/" target="_blank" rel="noopener noreferrer">Vowpal Wabbit</a> (‘VW’).</p>

<p>If you’re impatient, I’ll recommend two papers for the meantime:</p>

<ul>
  <li>
    <p><a href="https://arxiv.org/abs/1802.04064" target="_blank" rel="noopener noreferrer"><strong>A Contextual Bandit Bake-off</strong></a>: Reviewing this paper is what inspired my CB-as-SL perspective, but its utility is well beyond that. I believe it’s essential for understanding VW. The paper turns SL problems into CB problems and compares CB algorithms on these tasks. In doing so, the authors define the CB problem generally, show how SL can be used as a sub-routine, explain many of the VW-implemented CB algorithms, demonstrate their capabilities and offer useful advice for practitioners.</p>
  </li>
  <li>
    <p><a href="https://arxiv.org/abs/1606.03966" target="_blank" rel="noopener noreferrer"><strong>Making Contextual Decisions with Low Technical Debt</strong></a>: It should be emphasized that, from an engineering perspective, CB is riskier than SL. CB involves deploying an algorithm that learns on its own as it interacts with the real world. This is a harder thing to evaluate and test than something like a logistic regression model with frozen coefficients. Further, CB agents involve information that isn’t typically stored in the same locations. Things like a decision and its probability need to be as primary as the \(y\) variable. So CB presents unique engineering challenges. In the discussion of their solution (the Decision Service), this paper, authored by the VW people, discusses those challenges and how they should be handled.</p>
  </li>
</ul>

<p>You can also check out the <a href="https://github.com/VowpalWabbit/vowpal_wabbit/wiki" target="_blank" rel="noopener noreferrer">VW Wiki</a>, but as you’ll see, you need to read the surrounding research to understand what VW is doing. That’s why I’d start with these two papers.</p>

<hr />

<h3 id="references">References</h3>

<ol>
  <li>
    <p>A. Agarwal, et al. <a href="https://arxiv.org/abs/1606.03966" target="_blank" rel="noopener noreferrer">Making Contextual Decisions with Low Technical Debt</a>. arXiv:1606.03966v2. May 9, 2017.</p>
  </li>
  <li>
    <p>P. Henderson, et al. <a href="https://arxiv.org/abs/2204.11910" target="_blank" rel="noopener noreferrer">Integrating Reward Maximization and Population Estimation: Sequential Decision-Making for Internal Revenue Service Audit Selection</a>. arXiv:2204.11910v3. Jan 24, 2023.</p>
  </li>
  <li>
    <p>D. Bouneffouf, I. Rish. <a href="https://arxiv.org/abs/1904.10040" target="_blank" rel="noopener noreferrer">A Survey on Practical Applications of Multi-Armed and Contextual Bandits</a>. arXiv:1904.10040v1. April 2, 2019</p>
  </li>
  <li>
    <p>L. Li, W. Chu, J. Langford, R. Schapire. <a href="https://arxiv.org/abs/1003.0146" target="_blank" rel="noopener noreferrer">A Contextual-Bandit Approach to Personalized News Article Recommendation</a>. arXiv:1003.0146v2. March 1. 2012</p>
  </li>
  <li>
    <p>A. Bietti, A. Agarwal and J. Langford. <a href="https://arxiv.org/abs/1802.04064" target="_blank" rel="noopener noreferrer">A Contextual Bandit Bake-off</a>. arXiv:1802.04064v5. 2021</p>
  </li>
</ol>

<hr />
<h3 id="something-to-add">Something to Add?</h3>

<p>If you see an error, egregious omission, something confusing or something worth adding, please email <a href="mailto:dj@truetheta.io">dj@truetheta.io</a> with your suggestion. If it’s substantive, you’ll be credited. Thank you in advance!</p>

<hr />
<h3 id="footnotes">Footnotes</h3>

<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:1">
      <p>I say ‘arguably’ because it depends on how reinforcement learning is defined. If it’s defined as involving an agent that learns, interacts and accrues reward in the real world, contextual bandits are surely a top contender. If it’s defined as any reward optimization over a simulation, the argument becomes weaker. In such a case, some massive logistical network optimizations would also count as applied RL, and those could certainly dethrone contextual bandits. <a href="#fnref:1" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:2">
      <p>The link points to code that eHarmony uses to deploy <a href="https://vowpalwabbit.org/" target="_blank" rel="noopener noreferrer">Vowpal Wabbit</a>, Microsoft’s opensource online-learning and contextual bandit tool. <a href="#fnref:2" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:3">
      <p>The context can also include information relating each action to the user. <a href="#fnref:3" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:4">
      <p>(Excuse me if this is obvious) SL normally deals with vector inputs \(\mathbf{x}\) and real-valued or categorical \(y\)’s. <a href="#fnref:4" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:5">
      <p>It should be noted that \(r\) is the typical reward notation, but I’m using \(y\) to connect CB and SL. <a href="#fnref:5" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
  </ol>
</div>]]></content><author><name>Duane Rich</name></author><category term="reinforcement-learning" /><category term="Post Formats" /><summary type="html"><![CDATA[This posts explains contextual bandits as a generalization of supervised learning.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://djrich.dev/assets/images/posts/contextual_bandits/policy_data_SLed.png" /><media:content medium="image" url="https://djrich.dev/assets/images/posts/contextual_bandits/policy_data_SLed.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">How to Understand Recommender Systems</title><link href="https://djrich.dev/concepts/recommender-systems/how-to-understand-recommender-systems/" rel="alternate" type="text/html" title="How to Understand Recommender Systems" /><published>2024-06-18T00:00:00+00:00</published><updated>2024-06-18T00:00:00+00:00</updated><id>https://djrich.dev/concepts/recommender-systems/how-to-understand-recommender-systems</id><content type="html" xml:base="https://djrich.dev/concepts/recommender-systems/how-to-understand-recommender-systems/"><![CDATA[<p>In this post, I’ll present a perspective for understanding recommender systems. At their core is a model that infers users’ preferences for items, which are then used to recommend those that are most preferred. The literature is a zoo of such models, and it’s hard to see what they have in common. But at bottom, they handle mostly the same set of information. Thinking in terms of this set makes the range of recommender systems easier to understand.</p>

<p>The context is that I’m expanding the recommender system for <a href="https://aampe.com/" target="_blank" rel="noopener noreferrer">Aampe</a>, a client of mine that does engagement management. For example, if an online retailer signs up with Aampe, their algorithms manage the messages sent to the retailer’s users. The algorithms are optimized for producing purchases<sup id="fnref:1"><a href="#fn:1" class="footnote" rel="footnote" role="doc-noteref">1</a></sup> without sending too many messages, which can cause users to shut off notifications.</p>

<p>Some of the strongest messages are those that recommend a specific and relevant purchase.</p>

<blockquote>
  <p>A great message might be “Do you need to buy sunblock?” after a user purchased towels, sunglasses, a bathing suit and no sunblock.</p>
</blockquote>

<p>So, a strong recommender is critical for effective messaging.</p>

<p>The Aampe team and I are encouraged by the performance of the system we’ve developed so far, but there’s more testing to do and it’s not yet deployed. Once that’s done, we’ll write a post on how exactly it works.</p>

<p>For now, I’ll present how I’m thinking about these systems in general.</p>

<h2 id="the-information">The Information</h2>

<p>The following is most of the information we need to understand recommender systems. Say we have:</p>

<ul>
  <li>
    <p>\(N\) users indexed by \(i = 1, \cdots, N\). Think of these as user IDs.</p>
  </li>
  <li>
    <p>\(M\) items indexed by \(j = 1, \cdots, M\). Think of these as item IDs.</p>
  </li>
  <li>
    <p>User \(i\) has feature vector \(\mathbf{v}_i\). This could indicate things like their gender, geography, search queries, estimated age or engagement levels.</p>
  </li>
  <li>
    <p>Item \(j\) has feature vector \(\mathbf{w}_j\). This could indicate things like the item’s price, category, name, manufacturer or brand.</p>
  </li>
  <li>
    <p>\(y_{i, j}\) is an interaction target. If it’s binary, \(y_{i, j} = 1\) might indicate user \(i\) purchased item \(j\). If it’s an integer between \(1\) and \(5\), it could be user \(i\)’s rating of item \(j\). Being slightly sloppy, I call them <em>preferences</em>.</p>

    <p>We can represent all user-item targets with an \(N\)-by-\(M\) matrix \(\mathbf{Y}\). Note that in typical cases, \(\mathbf{Y}\) is mostly unobserved.</p>
  </li>
  <li>
    <p>\(\mathbf{c}_{i, j}\) is a feature vector of length \(d\) characterizing the circumstance (the ‘context’) under which interaction \(y_{i, j}\) took place. For example, it could represent the time of the interaction or the other items on screen during the interaction.</p>

    <p>We can represent all contextual information with an \(N\)-by-\(M\)-by-\(d\) tensor \(\mathbf{C}\). Note that it will have (at least) the same unobserved pattern as \(\mathbf{Y}\).</p>
  </li>
</ul>

<h2 id="collaborative-filtering">Collaborative Filtering</h2>

<p><strong>Collaborative filtering (‘CF’)</strong> methods use similarities to infer unobserved user-item preferences. We’re interested in <em>unobserved</em> preferences because we’d like to recommend highly preferred items not yet encountered by the user.</p>

<blockquote>
  <p>For a given user, the preferences of a peer group of similar users are used to estimate the user’s preferences for unencountered items. Reference to a peer group is what makes it ‘collaborative’.</p>
</blockquote>

<p>Specifically, an estimate of an unobserved preference is given with the following sum over observed elements, where \(s(i, i')\) is the similarity between users \(i\) and \(i'\):</p>

\[\hat{y}_{i, j} = \sum_{i':\,y_{i', j}\textrm{ observed}} s(i, i')y_{i', j}\]

<p>This is a generalized notion of a peer group; \(s(i, i')\) could be defined such that it indicates discretely if \(i\) and \(i'\) are from the same group. Further, \(s(i, i')\) needs to be properly normalized such that the sum amounts to a weighted average.</p>

<p>Alternatively, items may collaborate to infer unobserved user-item preferences. The prediction is formed as follows, where \(s(j, j')\) is the similarity between items \(j\) and \(j'\):</p>

\[\hat{y}_{i, j} = \sum_{j':\,y_{i, j'}\textrm{ observed}} s(j, j')y_{i, j'}\]

<p>See <a href="https://www.amazon.science/publications/two-decades-of-recommender-systems-at-amazon-com" target="_blank" rel="noopener noreferrer">Two Decades of Recommender Systems at Amazon.com</a> for an example of an item-based system<sup id="fnref:2"><a href="#fn:2" class="footnote" rel="footnote" role="doc-noteref">2</a></sup>.</p>

<p>CF may only require item IDs, user IDs and the matrix \(\mathbf{Y}\). User-similarities could be defined with a similarity metric between \(\mathbf{Y}\)’s rows or item-similarities could be defined with a metric across columns. Depending on how \(\mathbf{Y}\) is defined, more creative similarities are available as well. Again, see the Amazon paper for an example.</p>

<p>Further, precomputing similarities and storing the observed elements of \(\mathbf{Y}\) may be all that is required prior to making recommendations. These are so called <strong>memory-based CF</strong> because the training routine amounts to storing (‘memorizing’) the targets and their similarities. This makes it an easy to implement and little-tuning-required nearest-neighbors algorithm. Like any neighbors-based algorithm, this shifts the compute burden to predict time, where a prediction involves summing over many terms or searching for a small set of neighbors (‘collaborators’).</p>

<p>One difficulty with memory-based methods comes from the sometimes-extreme sparsity of \(\mathbf{Y}\). For instance, if \(\mathbf{Y}\) is ratings of movies, an extreme majority of \(\mathbf{Y}\) is unobserved since most people don’t rate movies even if they watch them. Consequently for a given user, the collaborators necessarily need to be quite dissimilar, producing a bad estimate.</p>

<p>To compensate for this sparsity, <em>we make assumptions</em> in so called <strong>model-based CF</strong>. We assume \(\mathbf{Y}\) has some structure. After fitting to observations, this structure can fill in the unobserved values. The most well known example is <em>matrix factorization</em>, where we make the following approximation:</p>

\[\mathbf{Y} \approx \mathbf{G}\mathbf{H}^{\top}\]

<p>where \(\mathbf{G}\) is \(N\)-by-\(k\), \(\mathbf{H}\) is \(M\)-by-\(k\) and \(k\) is a hyperparameter to be tuned. These matrices can be learned by minimizing (via gradient descent) the <a href="https://en.wikipedia.org/wiki/Matrix_norm#Frobenius_norm" target="_blank" rel="noopener noreferrer">Frobenius norm</a> of the approximation errors. It’s a required speedup to only compute predictions on observed values during training. Further, it eases the training routine to add two intercept vectors, \(\mathbf{a} \in \mathbb{R}^N\) and \(\mathbf{b} \in \mathbb{R}^M\), to match the column and row means of \(\mathbf{Y}\) respectively:</p>

\[\mathbf{Y} \approx \textrm{diag}(\mathbf{a}) + \mathbf{G}\mathbf{H}^{\top} + \textrm{diag}(\mathbf{b})\]

<p>With this, any user-item preference can be predicted as:</p>

\[\hat{y}_{i, j} = a_i + \mathbf{g}_i\mathbf{h}_j^{\top} + b_j\]

<p>See chapter 22 of <a href="https://probml.github.io/pml-book/book1.html" target="_blank" rel="noopener noreferrer">Murphy (2022)</a> for more details on this style of model.</p>

<blockquote>
  <p>In my experience, I’ve only seen matrix factorization be useful when sparsity is severe. If finding reasonably similar collaborators is possible, the modeling assumptions add little predictive value.</p>
</blockquote>

<p>Further, model based approaches require careful tuning and regularization. So before pursuing this, it might be worth exploring a different similarity (e.g. item-item instead of user-user) where sparsity is not an issue.</p>

<h2 id="content-based-methods">Content-Based Methods</h2>

<p>As mentioned, everything above essentially only uses the interaction target \(\mathbf{Y}\). Observed feature vectors describing items (\(\mathbf{w}_j\)) are ignored despite their useful information (e.g. ‘this is a movie about nature’). Similarly, user feature vectors (\(\mathbf{v}_i\)) are ignored. To include them is a key aspect of <strong>content-based methods</strong>.</p>

<p>The typical and simple content-based approach is as follows. Both users and items share the same feature space. That is, \(\mathbf{v}_i\) and \(\mathbf{w}_j\) are of the same length and their elements measure similar things. For example, the second element of both vectors might measure exposure to the topic ‘nature’. If items are movies and users are viewers, a nature movie would have its second element equal to one. A user who watches nature movies 50% of the time would have their second element equal to \(0.5\). From this, users are recommended items with similar feature vectors. That is, someone who watches mostly nature videos should be recommended more nature videos.</p>

<p>The upside of this approach is the feature vector for a new item is known upfront, so it can be recommended immediately upon entry into the system. In CF, preferences need to accumulate before an item is recommended, the so-called ‘cold-start’ problem (though it exists for users as well). On the downside, the hand-designed feature space inclusive of both users and items is restrictive, making it unlikely to produce a powerful model. To compensate, deep learning can be used to add model capacity. See section 3.1 of <a href="https://www.mdpi.com/2076-3417/13/20/11378" target="_blank" rel="noopener noreferrer">H. Zhou et al. (2023)</a> for an example.</p>

<p>Because of this, I like my more general definition of content-based methods as those that use \(\mathbf{v}_i\) and \(\mathbf{w}_j\), however they are defined. It is easy to define models that roll these vectors into the CF-style predictions of \(\hat{y}_{i, j}\) from earlier. I might call them ‘content-aware’ methods. Others call them <strong>hybrid systems</strong> (but I find this terminology to not be especially informative).</p>

<h2 id="using-the-context">Using the Context</h2>

<p>The last piece of information to include is the context \(\mathbf{C}\) describing the conditions under which user-item interactions took place. Since this is the largest chunk of data and reasonable performance can be achieved without it, it is often ignored<sup id="fnref:3"><a href="#fn:3" class="footnote" rel="footnote" role="doc-noteref">3</a></sup>.</p>

<p>In a simple case, \(\mathbf{C}\) could be a binary matrix indicating whether an interaction took place on a mobile device. To incorporate this, we could conditionalize our model on this mobile-yes-or-no fact. That means we essentially have two recommenders, one for mobile and one for not-mobile. But this is a poor use of data since the data is divided and not shared across systems.</p>

<p>Because of this, \(\mathbf{C}\) is especially difficult to use. We are trying to model a high dimensional \(\mathbf{Y}\) conditional on an even higher dimensional \(\mathbf{C}\). This means for a given preference to predict, there is likely very little data considered relevant.</p>

<p>Personally, I have made no attempt of using \(\mathbf{C}\), so I can’t provide much advice on this front. See section II.A.3 of <a href="https://arxiv.org/abs/2306.12680" target="_blank" rel="noopener noreferrer">Y. Li et al. (2023)</a> for a discussion.</p>

<h2 id="other-approaches">Other approaches</h2>

<p>To be a bit more comprehensive, I’ll briefly describe some other approaches and how they relate to this framework:</p>

<ul>
  <li>
    <p><strong>AutoRec</strong>: It is one of the earliest successful demonstrations of autoencoders applied to recommender systems. An autoencoder is a function that recreates its input after passing it through a low-dimensional space. The low dimension forbids a perfect recreation and so the autoencoder must learn the essential characteristics of the input. AutoRec uses an autoencoder on the columns (items) of \(\mathbf{Y}\); missing values are ignored during training but predicted with the final output-recreation of the input. See <a href="https://dl.acm.org/doi/10.1145/2740908.2742726" target="_blank" rel="noopener noreferrer">S. Suvas et al. (2015)</a> for more details.</p>
  </li>
  <li>
    <p><strong>Tree-based methods</strong>: Tree-based methods can be strongly predictive with little tuning, making them an attractive tool for recommender systems. The idea is to turn each column (or row) of \(\mathbf{Y}\) into a prediction problem. A given column becomes the target, and all other columns become the input features. One complication is the features are mostly missing, so their values need to be imputed, ideally as predictions of the other models. This is not a trivial issue. See section 3.2.1 of <a href="https://www.amazon.com/Recommender-Systems-Textbook-Charu-Aggarwal/dp/3319296574" target="_blank" rel="noopener noreferrer">Aggarwal (2016)</a> for details.</p>
  </li>
  <li>
    <p><strong>Reinforcement Learning</strong>: A problem with everything discussed so far is the assumption that accurately predicting \(\mathbf{Y}\) is sufficient for what a recommender system should do. In reality, this is a myopic, short-term measure. It is better to measure long-term metrics. Maybe there exists an item that isn’t highly preferred but gets users returning to the product (e.g. educational items might be such a case). This means the recommender system shouldn’t optimize individual recommendations for a short term signal but a long sequence of recommendations for a long term reward. This is exactly the paradigm of reinforcement learning. However, this brings other challenges. See <a href="https://dl.acm.org/doi/10.1145/3292500.3330668" target="_blank" rel="noopener noreferrer">L. Zou et al. (2019)</a> for a discussion.</p>
  </li>
</ul>

<h2 id="complications">Complications</h2>

<p>In closing, here are some complications to be aware of:</p>

<ul>
  <li>
    <p><strong>Evaluation is difficult.</strong> It is not merely a matter of predicting a held out portion of \(\mathbf{Y}\).</p>
  </li>
  <li>
    <p><strong>Missing-patterns may be signals.</strong> An entry in \(\mathbf{Y}\) might be missing because the item was never exposed to the user or because it was and the user declined to interact. In the latter case, the missing pattern should be modeled to properly infer preferences.</p>
  </li>
  <li>
    <p><strong>There may be feedback loops.</strong> If a system recommends an item often, people may buy it primarily due to their high exposure to it. In the next training, this product will look like an even more attractive item to recommend, due to the exposure-induced buying. The system will lift winners and suppress losers only due the system’s previous behavior and not due to people’s actual preferences. This is one reason why ratings, which allow for explicit negativity, are often a favored signal.</p>
  </li>
</ul>

<hr />
<h3 id="references">References</h3>

<ol>
  <li>
    <p>C. Aggarwal. <a href="https://www.amazon.com/Recommender-Systems-Textbook-Charu-Aggarwal/dp/3319296574" target="_blank" rel="noopener noreferrer">Recommender Systems: The Textbook</a>. <em>Springer International Publishing</em>. 2016.</p>
  </li>
  <li>
    <p>B. Smith and G. Linden. <a href="https://www.amazon.science/publications/two-decades-of-recommender-systems-at-amazon-com" target="_blank" rel="noopener noreferrer">Two Decades of Recommender Systems at Amazon.com</a>. IEEE Internet Computing, vol. 21, no. 3, pp. 12-18, May-June 2017</p>
  </li>
  <li>
    <p>K. Murphy. <a href="https://probml.github.io/pml-book/book1.html" target="_blank" rel="noopener noreferrer">Probabilistic Machine Learning: An Introduction</a>. <em>MIT Press</em>. 2022.</p>
  </li>
  <li>
    <p>H. Zhou, F. Xiong, H. Chen. <a href="https://www.mdpi.com/2076-3417/13/20/11378" target="_blank" rel="noopener noreferrer">A Comprehensive Survey of Recommender Systems Based on Deep Learning</a>. Applied Sciences. 2023</p>
  </li>
  <li>
    <p>Y. Li, K. Liu, R. Satapathy, S. Wang, E. Cambria. <a href="https://arxiv.org/abs/2306.12680" target="_blank" rel="noopener noreferrer">Recent Developments in Recommender Systems: A Survey</a>. arXiv. 2023</p>
  </li>
  <li>
    <p>S. Suvash, A. Menon, S. Sanner, and L. Xie.
<a href="https://dl.acm.org/doi/10.1145/2740908.2742726" target="_blank" rel="noopener noreferrer">Autorec: Autoencoders meet collaborative filtering</a>. In Proceedings of the 24th
International Conference on World Wide Web, pp. 111-112. ACM, 2015.</p>
  </li>
  <li>
    <p>L. Zou, L. Xia, Z. Ding, J. Song, W. Liu, D. Yin. <a href="https://dl.acm.org/doi/10.1145/3292500.3330668" target="_blank" rel="noopener noreferrer">Reinforcement Learning to Optimize Long-term User Engagement in Recommender Systems</a>. Proceedings of the 25th ACM SIGKDD International Conference on Knowledge Discovery &amp; Data Mining. pp. 2810-2818. July 2019</p>
  </li>
</ol>

<hr />
<h3 id="something-to-add">Something to Add?</h3>

<p>If you see an error, egregious omission, something confusing or something worth adding, please email <a href="mailto:dj@truetheta.io">dj@truetheta.io</a> with your suggestion. If it’s substantive, you’ll be credited. Thank you in advance!</p>

<hr />
<h3 id="footnotes">Footnotes</h3>

<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:1">
      <p>Or some other goal behavior. <a href="#fnref:1" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:2">
      <p>The prediction or use of \(\mathbf{Y}\) isn’t explicit in the Amazon paper. This follows from the fact that, implicitly, their \(\mathbf{Y}\) is a binary matrix indicating which users purchased which items. The sum over observed elements that I’ve written is equivalent to the averaging over item-similarities they perform. <a href="#fnref:2" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:3">
      <p>A notable exception is the time of the interaction. This can be rather simply included by recency-weighting preferences. <a href="#fnref:3" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
  </ol>
</div>]]></content><author><name>Duane Rich</name></author><category term="machine-learning-and-other-topics" /><category term="Post Formats" /><summary type="html"><![CDATA[The wide literature on recommender systems obscures their commonalities. This post highlights what makes them similar, offering a simplifying perspective.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://djrich.dev/assets/images/posts/understanding_recommender_systems/matrix-factorization.png" /><media:content medium="image" url="https://djrich.dev/assets/images/posts/understanding_recommender_systems/matrix-factorization.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Randomized Numerical Linear Algebra</title><link href="https://djrich.dev/concepts/linear-algebra/randomized-numerical-linear-algebra/" rel="alternate" type="text/html" title="Randomized Numerical Linear Algebra" /><published>2024-04-08T00:00:00+00:00</published><updated>2024-04-08T00:00:00+00:00</updated><id>https://djrich.dev/concepts/linear-algebra/randomized-numerical-linear-algebra</id><content type="html" xml:base="https://djrich.dev/concepts/linear-algebra/randomized-numerical-linear-algebra/"><![CDATA[<p>This is a follow up post on my video <a href="https://youtu.be/6htbyY3rH1w?si=WhHJJ-VkN_tNjJCE" target="_blank" rel="noopener noreferrer">‘Is the Future of Linear Algebra.. Randomized?’</a>, which explains Randomized Numerical Linear Algebra (‘Rand-NLA’), the methods of using randomization to speed up linear algebra computations. It was spurred by <a href="https://arxiv.org/abs/2302.11474" target="_blank" rel="noopener noreferrer">a paper</a> that convinced me its a promising path through the decades-old operations-complexity wall of NLA algorithms. This post is to answer outstanding questions and add updates after the video is released.</p>

<h2 id="is-rand-nla-just-monte-carlo">Is Rand-NLA just Monte Carlo?</h2>

<p>This is a definitional issue that shouldn’t matter (who cares how we label things?), but it’s worth addressing to spare Rand-NLA some of the Monte Carlo stigma. That stigma is slowness; Monte Carlo methods are remarkably general (you can answer many more computational questions when an approximate answer is acceptable and the tactic of simulate-and-count is available), but the cost of that generality is reduced speed. Repeated simulations are often overkill; they can answer our specific question but can also answer many others, and so intuitively, we’re doing more work than necessary.</p>

<p>But Rand-NLA isn’t like this. It isn’t a problem agnostic strategy for answering a huge set of questions. It addresses specific but ubiquitous problems, like matrix decompositions or linear least squares problems, with randomization as a means of focusing compute on the small subset of the given information that determines the answer. It allows us to precisely separate essential from irrelevant information and this quality is unlike anything to be imagined when recalling Monte Carlo methods. Another quality unlike Monte Carlo is that Rand-NLA probabilities of approximations can sometimes be so favorable, they’re comparable to their deterministic alternatives.</p>

<h2 id="who-cares-about-least-squares">Who cares about least squares?</h2>

<p>In the <a href="https://arxiv.org/abs/2302.11474" target="_blank" rel="noopener noreferrer">paper</a><sup id="fnref:1"><a href="#fn:1" class="footnote" rel="footnote" role="doc-noteref">1</a></sup>, solving linear least squares problems (albeit a generalized flavor) faster is a primary focus of Rand-NLA. One might hope, as I did, to find randomized algorithms for matrix-multiplication and be disappointed. Further, in machine learning, there’s a mild stigma against modeling with least squares due to its sensitivity to outliers. A recommended approach to making a model ‘robust’ is to switch out the least squares objective for something like <a href="https://pytorch.org/docs/stable/generated/torch.nn.L1Loss.html" target="_blank" rel="noopener noreferrer">minimizing mean absolute error</a> or the <a href="https://en.wikipedia.org/wiki/Huber_loss" target="_blank" rel="noopener noreferrer">Huber loss</a>. Also, least squares is favored historically because it makes for an easy-to-optimize objective, but as compute grows, shouldn’t we graduate to more intuitively desirable objectives, like mean absolute error?</p>

<p>This thinking, which I’m guilty of, is myopic. First, if least squares offers a compute advantage and suffers outliers, our desperation for compute speed means we should accept the tradeoff and just handle outliers differently (clip them!). Second, least squares is ubiquitous despite the above thinking, evidenced by its setting as the default loss for regression problems in so much software. Surely, quickening those fitting routines, where possible, is useful. Lastly, linear least squares is about finding a point within a linear subspace that is closest to an arbitrary point. It’s about aiming an input at an output through a linear function. As such, it can serve as a widely useful subroutine, pushing and corralling a point through a large and complex optimization space. One example is <a href="https://en.wikipedia.org/wiki/Newton%27s_method#Systems_of_equations" target="_blank" rel="noopener noreferrer">Newton’s Method</a>, where each step involves solving a system of equations, a special case of the linear least squares problem.</p>

<h2 id="randomization-for-nla-has-been-around-for-over-a-decade-why-is-it-promising-now">Randomization for NLA has been around for over a decade. Why is it promising now?</h2>

<p>It takes a long time for good research to change software, especially when the software is as foundational as LAPACK and the idea is as deviating as randomized NLA. I anticipate it’ll be at least another decade before randomized routines are used as frequently as LAPACK. That said, I see a few reasons to be confident in the trend:</p>

<ul>
  <li><strong>The machine learning craze has created an unprecedented demand for compute, and machine learning in particular is friendly to the probabilistic and approximate nature of randomized NLA.</strong> Most avenues for expanding compute will be pursued and randomization is so cheap and effective, it won’t be neglected. It already has considerable attention.</li>
  <li><strong>Dominant and motivating randomized algorithms have been developed recently.</strong> Before any foundational software is displaced, experimental software needs to demonstrate the advantage and that’s happened decisively recently. We saw this with the list of papers discussed in the video.</li>
  <li><strong>The ‘Randomized Numerical Linear Algebra’ paper itself demonstrates the NLA community’s organized motivation to make this change.</strong> Excuse the argument-by-authority, but it’s indicative that the original designers of LAPACK are in favor of this research direction, and they appear in good company.</li>
</ul>

<p>The friction, from what I gather, is that developing foundational hardware today is extremely hard. In the 1990’s when LAPACK was released, it was easier to get widespread adoption because there was little competition. By the 2020’s, the NLA software and hardware ecosystem had expanded many times over, and so there are many more things to touch when pushing alternative algorithms. That frustrates change and limits the influence of Rand-NLA. If these ideas were thought and appreciated in the 1990’s, randomized algorithms might very well be the standard they aspire for today.</p>

<hr />
<h3 id="footnotes">Footnotes</h3>

<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:1">
      <p>See section 3.1, where the optimization problems addressable with Rand-NLA are presented. They are generalized flavors of least squares and quadratic minimization. <a href="#fnref:1" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
  </ol>
</div>]]></content><author><name>Duane Rich</name></author><category term="linear-algebra" /><category term="Post Formats" /><summary type="html"><![CDATA[Some follow up questions from my video on Randomized Numerical Linear Algebra]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://djrich.dev/assets/images/posts/randomized_numerical_linear_algebra/rand_nla_inverted.png" /><media:content medium="image" url="https://djrich.dev/assets/images/posts/randomized_numerical_linear_algebra/rand_nla_inverted.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Reinforcement Learning at Lyft</title><link href="https://djrich.dev/concepts/reinforcement-learning/reinforcement-learning-at-lyft/" rel="alternate" type="text/html" title="Reinforcement Learning at Lyft" /><published>2024-01-04T00:00:00+00:00</published><updated>2024-01-11T00:00:00+00:00</updated><id>https://djrich.dev/concepts/reinforcement-learning/reinforcement-learning-at-lyft</id><content type="html" xml:base="https://djrich.dev/concepts/reinforcement-learning/reinforcement-learning-at-lyft/"><![CDATA[<p>I created my <a href="https://www.youtube.com/playlist?list=PLzvYlJMoZ02Dxtwe-MmH4nOB5jYlMGBjr" target="_blank" rel="noopener noreferrer">YouTube series on Reinforcement Learning</a> because I saw it applied profitably at Lyft. It was a counterexample to the stigma:</p>

<blockquote>
  <p>RL is only good for scenarios where a perfect simulator can be accessed endlessly. It’s general-but-slow trial-and-error.</p>
</blockquote>

<p>There’s truth here, but I believe the stigma will fade due to things like.. <strong>Lyft’s recent paper explaining a successful application of RL</strong>:</p>

<p><a href="https://arxiv.org/abs/2310.13810" target="_blank" rel="noopener noreferrer">A Better Match for Drivers and Riders: Reinforcement Learning at Lyft</a></p>

<h2 id="an-overview">An Overview</h2>

<ul>
  <li>
    <p><strong>They address the problem of matching</strong>. When a ride is requested, which driver should be matched? This comes down to repeatedly solving a bipartite matching problem, a classic optimization.</p>
  </li>
  <li>
    <p><strong>The optimization requires specifying weights</strong>, each of which indicates the value of matching a particular driver with a particular request.</p>
  </li>
  <li>
    <p><strong>These weights can be determined from action-value estimates, quantities produced when training an RL agent, if the RL problem is defined properly.</strong></p>
  </li>
  <li>
    <p>Specifically, their agents represent drivers and their state is a driver’s place in space and time. The agent actions are to accept/ignore a request. The value of an action is the expected earnings.</p>
  </li>
  <li>
    <p>They train their agents “online,” meaning the agents are continuously updating themselves, which creates challenges (e.g. model monitoring must be hard) that dissuade most. For them, this provides a real-time adaptivity to the market.</p>
  </li>
  <li>
    <p><strong>With trained agents, they can generate improved bipartite matching weights.</strong></p>
  </li>
  <li>
    <p>Evaluating this is hard. You can’t A/B test it. You need to at least put an entire market onto/off the RL approach repeatedly.</p>
  </li>
  <li>
    <p>It works! They saw 13% fewer instances where a driver couldn’t be matched with a rider. <strong>This and other effects equate to 30 million in additional annual revenue.</strong></p>
  </li>
</ul>

<h2 id="some-observations">Some Observations</h2>

<ul>
  <li>
    <p><strong>It’s no surprise it’s not a fully RL solution</strong>. You might expect an RL problem where there’s one agent, one who’s actions are all possible matching decisions. This would be totally unlearnable, due to the size of the action space. They keep the state-space and action-space small by settings agents = drivers. Plus, if you can frame something as a bipartite matching problem, you get to apply powerful solvers.</p>
  </li>
  <li>
    <p><strong>Value-based RL comes with interesting intermediate quantities</strong>, like action-values or state-values. For them, <strong>they are useful intuitive-checks</strong>. They have a sense of when a driver is more/less valuable and if their intuition is matched, they gain confidence. This plot makes sense to them:</p>
  </li>
</ul>

<p class="center"><img src="https://djrich.dev/assets/images/posts/reinforcement_learning_at_lyft/driver_value.png" alt="illustration" style="width: 100%" /></p>

<ul>
  <li><strong>The market changes over time and RL agents need time to learn these changes</strong>. How fast it responds depends on how it represents the market. Given this is all done online and evaluation is hard, I suspect managing the bias-variance tradeoff is especially difficult.</li>
</ul>

<h2 id="well-done">Well done!</h2>

<p>This effort took years, and I know the challenge of developing something tested against a nonstationary, partially observed, self referential, adversarial and complex real world system. This is an impressive feat; well done to the team!</p>]]></content><author><name>Duane Rich</name></author><category term="reinforcement-learning" /><category term="Post Formats" /><summary type="html"><![CDATA[A few comments on the Reinforcement Learning work done by my colleagues at Lyft]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://djrich.dev/assets/images/posts/reinforcement_learning_at_lyft/driver_value.png" /><media:content medium="image" url="https://djrich.dev/assets/images/posts/reinforcement_learning_at_lyft/driver_value.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">When does the Delta Method approximation work?</title><link href="https://djrich.dev/concepts/machine-learning-and-other-topics/when-does-the-delta-method-approximation-work/" rel="alternate" type="text/html" title="When does the Delta Method approximation work?" /><published>2023-11-01T00:00:00+00:00</published><updated>2023-11-01T00:00:00+00:00</updated><id>https://djrich.dev/concepts/machine-learning-and-other-topics/when-does-the-delta-method-approximation-work</id><content type="html" xml:base="https://djrich.dev/concepts/machine-learning-and-other-topics/when-does-the-delta-method-approximation-work/"><![CDATA[<p>Approximations are useful, squirrelly things. Useful because they offer shortcuts, freeing up compute for other purposes. Squirrelly because the approximation error can vary from case to case, and that brings an annoying extra question of whether the approximation is good enough for the case considered. This isn’t always an issue; some approximations (e.g. <a href="https://en.wikipedia.org/wiki/Stirling%27s_approximation" target="_blank" rel="noopener noreferrer">Stirling’s approximation</a>) are virtually always good enough. But for the approximation I’ll discuss here, the annoying extra question needs answering.</p>

<p>In fact, my main motivation for writing this was to develop an intuition for when this approximation works and fails. And that means having a mechanical visual, something I’m comfortable (or naive enough) believing generalizes to the cases I’ll encounter. This post is to share this perspective.</p>

<h2 id="the-delta-method-approximation">The Delta Method Approximation</h2>

<p>The approximation concerns two ubiquitous mathematical objects: <em>functions</em> and <em>distributions</em>. It’s an approximation of the distribution produced by passing a given distribution through a function. Specifically, let \(X\) be normally distributed with mean \(\mu\) and variance \(\sigma^2\) and let \(f(x)\) be a function differentiable at \(\mu\), then the following holds <em>approximately</em>:</p>

\[f(X) \sim \mathcal{N}(f(\mu), f'(\mu)^2\sigma^2)\]

<p>where \(f(X)\) is the random variable produced by passing \(X\) into \(f(x)\) and granted \(f'(\mu)\neq 0\). This follows from a two-term Taylor series expansion on \(f(x)\) at \(\mu\)<sup id="fnref:1"><a href="#fn:1" class="footnote" rel="footnote" role="doc-noteref">1</a></sup>.</p>

<p>To be unambiguous, I’ll refer to this as the <span style="color:#205081"><strong>Delta Method approximation</strong></span>, because as <a href="https://www.jstor.org/stable/23339471" target="_blank" rel="noopener noreferrer">J. Hoef (2012)</a> points out, the ‘Delta Method’ can refer to multiple techniques.</p>

<h2 id="the-approximation-visualized">The Approximation Visualized</h2>

<p>To me, the approximation’s statement isn’t obviously true, but should be. To that end, I offer the following visual, also demonstrating when this approximation is expected to work or fail.
<!-- width="420" height="315" --></p>

<iframe width="560" height="315" src="https://www.youtube.com/embed/x2QqIa0yJ3Q?si=Lvo0sNtigKx67XiQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="" loading="lazy"></iframe>

<p>\(\quad\)
\(\quad\)</p>

<p>From this, it seems <strong>the approximation <em>only</em> works when \(f(x)\) is approximately linear in the region over which \(X\) is distributed</strong><sup id="fnref:2"><a href="#fn:2" class="footnote" rel="footnote" role="doc-noteref">2</a></sup>. <em>Now</em> it feels obviously true.</p>

<h2 id="other-comments">Other Comments</h2>

<p>I’ll make a few other comments:</p>

<ul>
  <li>The approximation to \(f(X)\) only references \(f(x)\) at \(x=\mu\). This is a major weakness. For those who might be familiar, it’s the same weakness found in the <em>Extended Kalman Filter</em> and improved upon with the <em>Unscented Kalman Filter</em>. See section 18.5 of <a href="https://probml.github.io/pml-book/book0.html" target="_blank" rel="noopener noreferrer">Murphy (2012)</a> for more on this.</li>
  <li>It makes the connection of slopes and variances clear. When passing \(X\) through \(f(x)\), the slope is a multiplier on \(X\)’s standard deviation.</li>
  <li>If we hope for a normal approximation to \(f(X)\) to work, we’ll need \(X\) to be normally distributed<sup id="fnref:3"><a href="#fn:3" class="footnote" rel="footnote" role="doc-noteref">3</a></sup>.</li>
  <li>This approximation generalizes to higher dimensions, where we deal in gradients and covariance matrices. In this case, I expect all the above issues to be even more precarious, where it only takes one non-linear dimension to spoil the approach.</li>
</ul>

<p>In summary, I expect the occassion for this approximation is when we’re certain \(f(x)\) is smooth, we can differentiate it and the variance of \(X\) is very small.</p>

<hr />
<h3 id="references">References</h3>

<ol>
  <li>
    <p>J. M. Ver Hoef. <a href="https://www.jstor.org/stable/23339471" target="_blank" rel="noopener noreferrer">Who invented the delta method?</a>. <em>The American Statistician</em>. 2012.</p>
  </li>
  <li>
    <p>K. Murphy. <a href="https://probml.github.io/pml-book/book0.html" target="_blank" rel="noopener noreferrer">Machine Learning: A Probabilistic Perspective</a>. <em>MIT Press</em>. 2012.</p>
  </li>
</ol>

<hr />
<h3 id="something-to-add">Something to Add?</h3>

<p>If you see an error, egregious omission, something confusing or something worth adding, please email <a href="mailto:dj@truetheta.io">dj@truetheta.io</a> with your suggestion. If it’s substantive, you’ll be credited. Thank you in advance!</p>

<hr />
<h3 id="footnotes">Footnotes</h3>

<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:1">
      <p>Different approximations can be produced by considering more terms in the Taylor series expansion. <a href="#fnref:1" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:2">
      <p>More precisely, it’s approximately linear in the sense that it’s well approximated by the linear Taylor series approximation. <a href="#fnref:2" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
    <li id="fn:3">
      <p>This excludes contrived circumstances where \(f(\cdot)\) is designed to make \(f(X)\) normally distributed. <a href="#fnref:3" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
  </ol>
</div>]]></content><author><name>Duane Rich</name></author><category term="machine-learning-and-other-topics" /><category term="Post Formats" /><summary type="html"><![CDATA[I explore when the Delta Method approximation works and fails.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://djrich.dev/assets/images/posts/when_does_the_delta_method_approximation_work/delta-method-equation.png" /><media:content medium="image" url="https://djrich.dev/assets/images/posts/when_does_the_delta_method_approximation_work/delta-method-equation.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Notation Guide</title><link href="https://djrich.dev/concepts/probabilistic-graphical-models/pgms-notation/" rel="alternate" type="text/html" title="Notation Guide" /><published>2018-10-09T00:00:00+00:00</published><updated>2023-09-25T00:00:00+00:00</updated><id>https://djrich.dev/concepts/probabilistic-graphical-models/pgms-notation</id><content type="html" xml:base="https://djrich.dev/concepts/probabilistic-graphical-models/pgms-notation/"><![CDATA[<p>A common source of confusion is notation. To avoid clutter, symbols are often omitted, and their meaning depends on context. So we make the following clarifications:</p>

<ul>
  <li>
    <p><strong>The entire series considers the discrete case</strong>. It should be noted the continuous case is not a simple generalization of the discrete case. The exceptions are in parts 4 and 5, where examples are in the continuous case but the demonstrated principles are the same.</p>
  </li>
  <li>
    <p>An <strong>upper case non-bold letter</strong> indicates a single random variable. The same letter <strong>lower case with a super script</strong> indicates a specific value that random variable may take. For example, \(X=x^1\) is the event the random variable \(X\) took on the value \(x^1\). We call this event an <em>assignment</em>. The set of unique values a random variable may take is \(\textrm{Val}(X)\). For instance, we may have \(\textrm{Val}(X)=\{x^0,x^1\}\), indicating \(X\) is a discrete-valued variable taking one of two values. This is a general form of a case like ‘the <em>grade</em> random variable may be either <em>pass</em> or <em>fail</em>’.</p>
  </li>
  <li>
    <p>An <strong>upper case bold letter</strong> indicates a <em>set</em> of random variables (e.g. \(\mathbf{X}\)) and a <strong>bold lower case letter</strong> indicates a <em>set</em> of values they may take. For example, we may have \(\mathbf{X}=\{A,B\}\) and \(\mathbf{x}=\{a^3,b^1\}\). Then the event \(\mathbf{X}=\mathbf{x}\) is the event that \(A=a^3\) happens and \(B=b^1\) happens. When an assignment involves multiple variables, it’s called a <em>joint</em> assignment. \(\textrm{Val}(\mathbf{X})\) is the set of all possible joint assignments to the random variables of \(\mathbf{X}\).</p>
  </li>
  <li>
    <p>\(\vert \textrm{Val}(\mathbf{X}) \vert\) is the count of elements in \(\textrm{Val}(\mathbf{X})\).</p>
  </li>
  <li>
    <p>\(\mathbf{x}\) (or \(\mathbf{y}\) or \(\mathbf{z}\) etc.) within a probability function (e.g. \(P(\mathbf{x} \vert \cdots)\) or \(P(\cdots \vert \mathbf{x})\)) always abbreviates the event ‘\(\mathbf{X}=\mathbf{x}\)’.</p>
  </li>
  <li>
    <p>Perhaps confusingly, we also abbreviate the event ‘\(\mathbf{X}=\mathbf{x}\)’ as ‘\(\mathbf{X}\)’, though this isn’t a clean abbreviation. Omission of \(\mathbf{x}\) means one of two things: either we mean this for any given \(\mathbf{x}\) or for all possible \(\mathbf{x}\)’s. As an example for the latter case, ‘calculate \(P(\mathbf{X})\)’ means calculate the set of probabilities \(P(\mathbf{X}=\mathbf{x})\) for all \(\mathbf{x}\in \textrm{Val}(\mathbf{X})\).</p>
  </li>
  <li>
    <p>\(\sum_\mathbf{X}f(\mathbf{X})\) is shorthand for \(\sum_{\mathbf{x}\in \textrm{Val}(\mathbf{X})}f(\mathbf{X}=\mathbf{x})\). This is similarly true for \(\prod_\mathbf{X}(\cdot)\) and \(\textrm{argmin}_\mathbf{X}(\cdot)\).</p>
  </li>
  <li>
    <p>Sometimes we write equations like \(f(A,B,C)=g(\mathbf{X})h(\mathbf{Y})\), which appears incorrect; the random variables on the left aren’t on the right. In these cases, we’ll have something like \(\mathbf{X} = \{A,B\}\) and \(\mathbf{Y} = \{B,C\}\). The intended interpretation is \(f(A,B,C)=g(A,B)h(B,C)\).</p>
  </li>
  <li>
    <p>Probability distributions are referenced with a \(P\), \(\textrm{Q}\), \(p\), \(q\) or \(\pi\). It’s worth emphasizing that distributions are a special type of <em>function</em>.</p>
  </li>
</ul>

<hr />
<h3 id="something-to-add">Something to Add?</h3>

<p>If you see an error, egregious omission, something confusing or something worth adding, please email <a href="mailto:dj@truetheta.io">dj@truetheta.io</a> with your suggestion. If it’s substantive, you’ll be credited. Thank you in advance!</p>]]></content><author><name>Duane Rich</name></author><category term="probabilistic-graphical-models" /><category term="Post Formats" /><summary type="html"><![CDATA[Notation can be confusing. This post is to address it directly.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://djrich.dev/assets/images/posts/pgms_notation/cover.png" /><media:content medium="image" url="https://djrich.dev/assets/images/posts/pgms_notation/cover.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry></feed>