<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
	<title>Minimize Regret</title>
	<link>https://minimizeregret.com/</link>
	<description>Minimize Regret</description>
	<language>en-us</language>
	<copyright>© Tim Radtke</copyright>
	<lastBuildDate>Sun, 22 Nov 2026 17:24:12 +0000</lastBuildDate>
	<atom:link href="https://minimizeregret.com/index.xml" rel="self" type="application/rss+xml" />
	<item>
	  <title>Descriptions of Algorithmic Trading for BESS on Intraday Electricity Markets</title>
	  <link>https://minimizeregret.com/note/2026/11/22/algorithmic-trading-intraday-markets/</link>
	  <pubDate>Sun, 22 Nov 2026 18:20:12 +0100</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2026/11/22/algorithmic-trading-intraday-markets/</guid>
	  <description>&lt;p&gt;I&amp;rsquo;m always looking for papers that offer detailed descriptions of optimization problems using forecasts as input. In &lt;a href=&#34;https://minimizeregret.com/papers/schaurecker-2026-high-frequency-intraday-trading-for-battery-storages&#34;&gt;High-frequency Intraday Trading for Battery Storages&lt;/a&gt;, Schaurecker et al. (2026) introduce the problem of using battery energy storage systems (BESS) to trade in European continuous intraday electricity markets. They put considerable effort into a realistic description of the problem because, as they argue, &amp;ldquo;most authors adopt a rather stylized view of the intraday market, avoiding most of its complexity and thereby yielding suboptimal strategies and a biased picture of revenue potentials&amp;rdquo;.&lt;/p&gt;&#xA;&lt;p&gt;In &lt;a href=&#34;https://minimizeregret.com/papers/hirsch-2026-improving-algorithmic-trading-for-bess-on-intraday-electricity-markets-through-distributional-price-forecasts&#34;&gt;Improving Algorithmic Trading for BESS on Intraday Electricity Markets through Distributional Price Forecasts&lt;/a&gt;, Hirsch et al. (2026) build on Schaurecker et al.&amp;rsquo;s problem setup and inject price forecasts into the trading algorithm.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;Two co-authors of Schaurecker et al. (2026) are hiding in the &amp;ldquo;et al.&amp;rdquo; of Hirsch et al. (2026).&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>The Latest in Forecast Benchmark Hill Climbing</title>
	  <link>https://minimizeregret.com/note/2026/11/22/fruits-of-a-busy-summer/</link>
	  <pubDate>Sun, 22 Nov 2026 17:26:01 +0100</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2026/11/22/fruits-of-a-busy-summer/</guid>
	  <description>&lt;p&gt;Let&amp;rsquo;s check in on the fev-bench leaderboard. It&amp;rsquo;s been three weeks since I wrote about Timestral, t0-gamma, and TimesFM-3 in &lt;a href=&#34;https://minimizeregret.com/note/2026/11/01/fruits-of-a-busy-summer&#34;&gt;The Latest in Forecast Benchmark Hill Climbing&lt;/a&gt;. Since then there have been two additions to the very top of the leaderboard, &lt;a href=&#34;https://github.com/continuousintelligence/ci&#34;&gt;ci-proto&lt;/a&gt; and &lt;a href=&#34;https://h2o.ai/blog/2026/timeh2o-foundation-models&#34;&gt;TimeH2O-v1&lt;/a&gt;. Additionally, &lt;a href=&#34;https://github.com/Abel-ai-lab/Simons&#34;&gt;Simons-297M&lt;/a&gt; comes in behind Timestral and t0-gamma but ahead of TimesFM-3, and &lt;a href=&#34;https://github.com/anishsaha12/iAmTime&#34;&gt;iAmTime-base-forecast&lt;/a&gt; comes in behind TimesFM-3 but ahead of Chronos-2.&lt;/p&gt;&#xA;&lt;p&gt;Interestingly, the now ranked-10th-overall Toto-2.0-2.5B continues to come in &lt;a href=&#34;https://minimizeregret.com/note/2026/11/22/fruits-of-a-busy-summer/toto-on-boomlet-tasks.png&#34;&gt;first for 12 out of the 15 Boomlet tasks&lt;/a&gt; using scaled quantile loss. Making model choices for and training on domain-specific data continues to be useful (the Boomlet tasks being derived from &lt;a href=&#34;https://minimizeregret.com/papers/cohen-2025-this-time-is-different-an-observability-perspective-on-time-series-foundation-models&#34;&gt;Datadog&amp;rsquo;s observability time series&lt;/a&gt; on which Toto was trained).&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>&#34;PathFM: Fast Generative Forecasting of Multivariate Trajectories&#34;</title>
	  <link>https://minimizeregret.com/link/2026/11/08/pathfm/</link>
	  <pubDate>Sun, 08 Nov 2026 08:23:22 +0100</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2026/11/08/pathfm/</guid>
	  <description>&lt;p&gt;Of course I find PathFM (Yao et al., 2026) &lt;em&gt;after&lt;/em&gt; &lt;a href=&#34;https://minimizeregret.com/post/2026/11/01/sample-paths-from-any-model&#34;&gt;describing a lack of time series foundation models that produce sample paths&lt;/a&gt;, and its accompanying Forecast Path Score Bench (fps-bench) &lt;em&gt;after&lt;/em&gt; starting work on &lt;a href=&#34;https://minimizeregret.com/post/2026/11/07/sample-path-evaluation&#34;&gt;Sample Path Evaluation&lt;/a&gt; (&lt;a href=&#34;https://github.com/timradtke/spev&#34;&gt;spev&lt;/a&gt;).&lt;/p&gt;&#xA;&lt;p&gt;Citing from the paper&amp;rsquo;s abstract:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Time series foundation models are becoming increasingly popular for general-purpose forecasting. However, most leading models predict marginal quantiles, leaving the dependence structure of future trajectories unspecified. This distinction matters for trajectory-dependent quantities such as cumulative demand, peak load, and event timing, yet point and marginal scores do not reveal it. To model this dependence, we introduce PathFM, a trajectory-based forecasting model that uses flow matching to generate complete future paths. [&amp;hellip;] To complement these evaluations, we introduce Forecast Path Score Bench (fps-bench), which augments fev-bench tasks with energy and variogram scores and downstream metrics.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;I can&amp;rsquo;t yet judge the solutions they came up with, but I love the problems they chose to tackle.&lt;/p&gt;&#xA;&lt;p&gt;The PathFM model is available via &lt;a href=&#34;https://github.com/autogluon/PathFM&#34;&gt;Github&lt;/a&gt; and &lt;a href=&#34;https://huggingface.co/autogluon/pathfm-300m&#34;&gt;HuggingFace&lt;/a&gt;.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Sample Path Evaluation</title>
	  <link>https://minimizeregret.com/post/2026/11/07/sample-path-evaluation/</link>
	  <pubDate>Sat, 07 Nov 2026 20:13:36 +0100</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2026/11/07/sample-path-evaluation/</guid>
	  <description>&lt;p&gt;In my &lt;a href=&#34;https://minimizeregret.com/post/2026/11/01/sample-paths-from-any-model&#34;&gt;previous post&lt;/a&gt;, I attributed the lack of research into time series foundation models that forecast a joint distribution in part to the lack of benchmarks evaluating sample paths. You can&amp;rsquo;t claim state-of-the-art on forecast tasks that no one else evaluates themselves on. But claiming state-of-the-art &lt;a href=&#34;https://minimizeregret.com/note/2026/11/01/fruits-of-a-busy-summer/&#34;&gt;matters&lt;/a&gt;. If &lt;a href=&#34;https://minimizeregret.com/papers/aksu-2024-gift-eval-a-benchmark-for-general-time-series-forecasting-model-evaluation&#34;&gt;GIFT-Eval&lt;/a&gt;, &lt;a href=&#34;https://minimizeregret.com/papers/shchur-2025-fev-bench-a-realistic-benchmark-for-time-series-forecasting&#34;&gt;fev-bench&lt;/a&gt;, and &lt;a href=&#34;https://minimizeregret.com/papers/qiao-2026-its-time-towards-the-next-generation-of-time-series-forecasting-benchmarks&#34;&gt;TIME&lt;/a&gt; all evaluate quantiles of marginal distributions, then that&amp;rsquo;s what&amp;rsquo;s being built.&lt;/p&gt;&#xA;&lt;p&gt;So be the change you want to see in the world. Build a sample path evaluation benchmark.&lt;/p&gt;&#xA;&lt;p&gt;That&amp;rsquo;s what I did. Well, started. Prototyped. Considered: How could a benchmark for sample path evaluation look like?&lt;/p&gt;&#xA;&lt;p&gt;I took the approach to sample path evaluation described in &lt;a href=&#34;https://minimizeregret.com/papers/seeger-2016-bayesian-intermittent-demand-forecasting-for-large-inventories&#34;&gt;Bayesian Intermittent Demand Forecasting for Large Inventories&lt;/a&gt; (Seeger et al., 2016) and &lt;a href=&#34;https://minimizeregret.com/papers/salinas-2017-deepar-probabilistic-forecasting-with-autoregressive-recurrent-networks&#34;&gt;DeepAR: Probabilistic Forecasting with Autoregressive Recurrent Networks&lt;/a&gt; (Salinas et al., 2017) as a starting point. With the goal of using demand forecasts for inventory replenishment, the papers&amp;rsquo; authors evaluate not (only) marginal forecasts but specific functionals of the joint forecast distribution relevant to their replenishment decisions. They evaluate, for example, the 90% quantile of the sum of daily forecasts of the twelve weeks following a lead time of three weeks (see Table 1 in Seeger et al.). This is the kind of aggregation &lt;a href=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths&#34;&gt;I visualized previously&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths/calculate_order_size.png&#34; alt=&#34;Illustration of how sample paths can be aggregated to functionals of arbitrary combinations of points in time.&#34;&gt;&lt;figcaption&gt;Ten sample paths are summarized to a quantile of the sums of the three months following a lead time of five months.&lt;/figcaption&gt;&lt;/figure&gt;&lt;p&gt;Aggregating forecasts of arbitrary points in time is crucial for replenishment decisions, and it requires forecasting the joint distribution over the forecast horizon. In lieu of closed form equations, sample paths make working with the joint distribution easy. To define these aggregates, the Seeger et al. and Salinas et al. parameterize them with a lead time and a span. Quoting Seeger et al. (2016):&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Our metrics quantify the forecast accuracy of certain quantiles of predictive distributions. They&#xA;are defined in terms of &lt;em&gt;spans&lt;/em&gt; $[L,L+S)$ in the prediction range, where $L$ are &lt;em&gt;lead times&lt;/em&gt;.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;This approach to parameterizing aggregates of sample paths is what I used as one key ingredient to my proof of concept of an sample path evaluation benchmark. The other key ingredient was the &lt;a href=&#34;https://github.com/autogluon/fev&#34;&gt;fev&lt;/a&gt; package and its approach to defining forecast tasks and loading datasets for fev-bench.&lt;/p&gt;&#xA;&lt;p&gt;Instead of rebuilding all that infrastructure, why not extend it. With &lt;a href=&#34;https://github.com/timradtke/spev&#34;&gt;spev&lt;/a&gt; (as in &lt;strong&gt;S&lt;/strong&gt;ample &lt;strong&gt;P&lt;/strong&gt;ath &lt;strong&gt;EV&lt;/strong&gt;aluation), I extend a forecast task defined via fev by functionals of sample paths (such as the sum of a span following a lead time) and accompanying metrics. The metrics aren&amp;rsquo;t all that different from those already used by fev and other benchmarks. But I focus on quantile loss of individual quantile levels (such as the 90% quantile evaluated by Seeger et al.) and a CRPS calculated from samples &lt;a href=&#34;https://minimizeregret.com/note/2026/10/31/crps-as-evaluation-metric-in-gift-eval&#34;&gt;instead of an approximation from quantiles&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Using spev then looks as follows. Import both fev and spev:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; numpy &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; np&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; fev &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; Task&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; spev &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; SamplePathTask&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then define a fev task that will serve as basis for the evaluation. Here, to both keep it simple and to reproduce the evaluation setup from Seeger et al. and Salinas et al., define the standard car parts task of forecasting the last eight months:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;base_task &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; Task(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;dataset_path&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;autogluon/chronos_datasets&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;dataset_config&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;monash_car_parts&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;horizon&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;8&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;num_windows&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;seasonality&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;target&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;target&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next, define a &lt;code&gt;SamplePathTask&lt;/code&gt; by extending the &lt;code&gt;Task&lt;/code&gt;. As described above, this implies defining aggregates and metrics. Here, I pick the ones from the papers:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;task &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; SamplePathTask(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;base&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;base_task,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;aggregates&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;[&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;{&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;name&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;SUM&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;lead_time&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;span&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;},&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;{&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;name&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;SUM&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;lead_time&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;span&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;},&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;{&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;name&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;SUM&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;lead_time&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;span&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;8&lt;/span&gt;},&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;metrics&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;[&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&lt;span style=&#34;color:#75715e&#34;&gt;# Mean Quantile Loss (Risk Score in the papers)&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;{&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;name&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;MQL&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;quantile_level&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;0.9&lt;/span&gt;},&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;{&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;name&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;MQL&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;quantile_level&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;0.5&lt;/span&gt;},&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With the task defined, draw some sample paths forecasts to evaluate.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; The sample path forecasts need to be a list of one array per forecast window defined in the fev task (in this case, one window). Each array is four-dimensional: The first dimension is the number of time series, the second dimension is the number of samples, the third dimension is the length of the forecast horizon, and the fourth is the number of targets. In this case, the shape will be (2674, 2501, 8, 1).&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;rng &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; np&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;random&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;default_rng(seed&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;512&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;sample_path_forecasts &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;rng&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;poisson(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;lam&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;0.25&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;size&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&lt;span style=&#34;color:#ae81ff&#34;&gt;2674&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# Number of time series&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&lt;span style=&#34;color:#ae81ff&#34;&gt;2501&lt;/span&gt;, &lt;span style=&#34;color:#75715e&#34;&gt;# Number of sample paths&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&lt;span style=&#34;color:#ae81ff&#34;&gt;8&lt;/span&gt;,    &lt;span style=&#34;color:#75715e&#34;&gt;# Forecast horizon length&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;,    &lt;span style=&#34;color:#75715e&#34;&gt;# Number of targets&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Finally, pass the sample path forecasts to the spev task for evaluation. It returns a list of evaluated metrics, one for each combination of aggregates and metrics defined in the sample path task. Here, with 3 aggregates and 2 metrics, there are 6 evaluated metrics.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;task&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;evaluation_summary(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;y_pred_per_window&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;sample_path_forecasts&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;[{&amp;#39;window&amp;#39;: 0,&#xA;  &amp;#39;aggregate&amp;#39;: &amp;#39;Sum_[0,1]&amp;#39;,&#xA;  &amp;#39;metric&amp;#39;: &amp;#39;MeanQuantileLoss@0.9&amp;#39;,&#xA;  &amp;#39;value&amp;#39;: array([1.40470307])},&#xA; {&amp;#39;window&amp;#39;: 0,&#xA;  &amp;#39;aggregate&amp;#39;: &amp;#39;Sum_[0,1]&amp;#39;,&#xA;  &amp;#39;metric&amp;#39;: &amp;#39;MeanQuantileLoss@0.5&amp;#39;,&#xA;  &amp;#39;value&amp;#39;: array([0.47070546])},&#xA; {&amp;#39;window&amp;#39;: 0,&#xA;  &amp;#39;aggregate&amp;#39;: &amp;#39;Sum_[2,1]&amp;#39;,&#xA;  &amp;#39;metric&amp;#39;: &amp;#39;MeanQuantileLoss@0.9&amp;#39;,&#xA;  &amp;#39;value&amp;#39;: array([1.44639299])},&#xA; {&amp;#39;window&amp;#39;: 0,&#xA;  &amp;#39;aggregate&amp;#39;: &amp;#39;Sum_[2,1]&amp;#39;,&#xA;  &amp;#39;metric&amp;#39;: &amp;#39;MeanQuantileLoss@0.5&amp;#39;,&#xA;  &amp;#39;value&amp;#39;: array([0.47588681])},&#xA; {&amp;#39;window&amp;#39;: 0,&#xA;  &amp;#39;aggregate&amp;#39;: &amp;#39;Sum_[0,8]&amp;#39;,&#xA;  &amp;#39;metric&amp;#39;: &amp;#39;MeanQuantileLoss@0.9&amp;#39;,&#xA;  &amp;#39;value&amp;#39;: array([4.12243922])},&#xA; {&amp;#39;window&amp;#39;: 0,&#xA;  &amp;#39;aggregate&amp;#39;: &amp;#39;Sum_[0,8]&amp;#39;,&#xA;  &amp;#39;metric&amp;#39;: &amp;#39;MeanQuantileLoss@0.5&amp;#39;,&#xA;  &amp;#39;value&amp;#39;: array([2.79713033])}]&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This result highlights an aspect that makes evaluating sample paths as part of a benchmark more uncomfortable than the evaluation of quantiles. Each aggregate multiplies the number of model comparisons.&lt;/p&gt;&#xA;&lt;p&gt;But defining joint distribution forecast tasks is clearly possible. How fun would it be to have a benchmark of not only datasets from different domains, but also domain-specific &lt;a href=&#34;https://github.com/timradtke/spev/blob/main/src/spev/aggregates.py&#34;&gt;aggregates&lt;/a&gt; and evaluation metrics. Use the &lt;code&gt;SUM&lt;/code&gt; for inventory-related data, but the &lt;code&gt;MAX&lt;/code&gt; for electricity load, or the &lt;code&gt;ALL_GREATER_THAN&lt;/code&gt; for electricity prices.&lt;/p&gt;&#xA;&lt;p&gt;Less one-size-fits-all, more sample paths.&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;Using random samples from a Poisson distribution isn&amp;rsquo;t an exciting example&amp;mdash;especially due the lack of autocorrelation&amp;mdash;but it keeps this introduction short. &lt;a href=&#34;https://github.com/timradtke/spev/blob/main/docs/evaluate_car_parts_using_spev.ipynb&#34;&gt;Look at this notebook&lt;/a&gt; for an extended tutorial using Chronos-2 and Shuffler to draw sample paths using a time series foundation model.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>Sample Paths From Any Model</title>
	  <link>https://minimizeregret.com/post/2026/11/01/sample-paths-from-any-model/</link>
	  <pubDate>Sun, 01 Nov 2026 10:26:03 +0100</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2026/11/01/sample-paths-from-any-model/</guid>
	  <description>&lt;p&gt;One gripe I have with the current era of time series foundation models and forecast research generally is the focus on quantile forecasts. Even more so the promotion of quantiles as the be-all and end-all of probabilistic forecasting. Even though quantiles of &lt;em&gt;marginal&lt;/em&gt; distributions do not answer questions such as&amp;hellip;&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&amp;ldquo;What is the probability that half of the product’s inventory will be consumed &lt;em&gt;over the next 12 days&lt;/em&gt;?&amp;rdquo;&lt;/li&gt;&#xA;&lt;li&gt;&amp;ldquo;What is the probability that electricity prices will be higher than 200€/MWh &lt;em&gt;for a period of six hours&lt;/em&gt; in the next 24 hours?&amp;rdquo;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;&amp;hellip;which concern aggregations across arbitrary spans in the forecast horizon. &lt;a href=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths&#34;&gt;You need sample paths&lt;/a&gt; from the &lt;em&gt;joint&lt;/em&gt; forecast distribution to answer these questions.&lt;/p&gt;&#xA;&lt;p&gt;The research community started out so well back in &lt;a href=&#34;https://minimizeregret.com/papers/seeger-2016-bayesian-intermittent-demand-forecasting-for-large-inventories&#34;&gt;2016&lt;/a&gt; and &lt;a href=&#34;https://minimizeregret.com/papers/salinas-2017-deepar-probabilistic-forecasting-with-autoregressive-recurrent-networks&#34;&gt;2017&lt;/a&gt;. But then it turned around to follow a different path. It&amp;rsquo;s easier to build models that forecast quantiles only, so this is where early innovation on foundation models happens. Later on, as the low-hanging fruits are picked, perhaps some research will take care of native sample path forecasts.&lt;/p&gt;&#xA;&lt;p&gt;With their ability to incentivize researchers, leaderboards are part of the problem. Current leaderboards (GIFT-eval, fev, TIME) focus their evaluation on marginal forecasts. Then again, I wouldn&amp;rsquo;t want to define a &lt;em&gt;general&lt;/em&gt; leaderboard evaluating sample paths. Too many options. Perhaps we need more specialized leaderboards. Anyway.&lt;/p&gt;&#xA;&lt;p&gt;What makes the absence of sample paths doubly frustrating is that the latest generation of time series foundation models is impressive and &lt;em&gt;should&lt;/em&gt; be used as a baseline if nothing else.&lt;/p&gt;&#xA;&lt;p&gt;A way around this frustration is the following: Take a foundation model&amp;rsquo;s quantile forecasts and transform them into sample paths post hoc. While the autocorrelation of the resulting sample paths won&amp;rsquo;t have been learned by the foundation model, this approach could trump approaches that draw sample paths natively due to the quality of the underlying marginal quantiles.&lt;/p&gt;&#xA;&lt;p&gt;In &lt;a href=&#34;https://minimizeregret.com/papers/baron-2025-efficiently-generating-correlated-sample-paths-from-multi-step-time-series-foundation-models&#34;&gt;Efficiently Generating Correlated Sample Paths from Multi-step Time Series Foundation Models&lt;/a&gt; (Baron et al., 2025), the authors take that approach. Given a time series and its lag one auto-correlation, they fit a simple copula. They then sample quantile level paths from the copula. Next, they evaluate the marginal forecast distribution at those quantile levels.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; This transfers auto-correlation structure from the copula via the sampled quantile levels into the quantiles. In effect, the resulting values resemble sample paths from a joint forecast distribution.&lt;/p&gt;&#xA;&lt;p&gt;In &lt;a href=&#34;https://github.com/timradtke/shuffler&#34;&gt;Shuffler&lt;/a&gt;, I implemented the method described in Baron et al. (2025) to try it for myself. Refer to its &lt;a href=&#34;https://github.com/timradtke/shuffler/blob/main/README.md&#34;&gt;README&lt;/a&gt; or a detailed &lt;a href=&#34;https://github.com/timradtke/shuffler/blob/main/docs/draw_sample_paths_using_chronos.ipynb&#34;&gt;tutorial notebook&lt;/a&gt; for a worked example, using Chronos-2 to forecast electricity prices and Shuffler to draw sample paths building on Chronos&amp;rsquo; quantile forecasts. For brevity I&amp;rsquo;m not going to rehash the code here. But take a look at the following visualizations that contrast naive samples from Chronos&amp;rsquo; marginal forecast distributions with post hoc sample paths via Shuffler on top of Chronos&amp;rsquo; quantiles.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/shuffler/sample_paths_marginal.png&#34; alt=&#34;A line chart of electricity prices and three sample path forecasts drawn from marginal forecast distributions.&#34;&gt;&lt;figcaption&gt;Three sample paths drawn from marginal forecast distributions using Chronos-2. Note how the samples jump up and down from one time step to the next.&lt;/figcaption&gt;&lt;/figure&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/shuffler/sample_paths.png&#34; alt=&#34;A line chart of electricity prices and three sample path forecasts drawn using Shuffler.&#34;&gt;&lt;figcaption&gt;Three sample paths drawn using Chronos-2 and Shuffler. Note the smooth change over time that visually corresponds well to the ground truth data smoothness.&lt;/figcaption&gt;&lt;/figure&gt;&lt;p&gt;The difference between the two sets of sample paths is striking. The upper image highlights the limitations of marginal distributions: If you draw samples from them naively, each time step in the forecast horizon is independent from the next. This leads to the fluctuating structure in the resulting paths, which is unrealistic when (visually) compared with past data.&lt;/p&gt;&#xA;&lt;p&gt;In contrast, the bottom image shows what look like &lt;em&gt;realistic&lt;/em&gt; future paths of the time series. They are smooth to the extent that the time series changes smoothly.&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;Since only a limited set of quantiles of the marginal distributions are forecasted, you&amp;rsquo;ll have to approximate the arbitrary quantiles that the copula requests by inter- and extrapolating the known quantiles. The approach taken by Baron et al. (2025) and implemented in Shuffler is described in &lt;a href=&#34;https://minimizeregret.com/papers/park-2021-learning-quantile-functions-without-quantile-crossing-for-distribution-free-time-series-forecasting&#34;&gt;Learning Quantile Functions without Quantile Crossing for Distribution-free Time Series Forecasting&lt;/a&gt; (Park et al., 2021).&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>The Latest in Forecast Benchmark Hill Climbing</title>
	  <link>https://minimizeregret.com/note/2026/11/01/fruits-of-a-busy-summer/</link>
	  <pubDate>Sun, 01 Nov 2026 10:24:34 +0100</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2026/11/01/fruits-of-a-busy-summer/</guid>
	  <description>&lt;p&gt;Researchers are harvesting the fruits of what must have been a busy summer. &lt;a href=&#34;https://research.google/blog/timesfm-3-a-zero-shot-foundation-model-for-multivariate-forecasting/&#34;&gt;Google released TimesFM-3&lt;/a&gt; back on August 31:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;We evaluated TimesFM-3 on three comprehensive public forecasting benchmarks: Gift-Eval, FEV-Bench, and Time. On all three benchmarks, TimesFM-3 is the top-ranked model in terms of both point and probabilistic forecasting metrics among all pre-trained foundation models.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://www.theforecastingcompany.com/blog/t0-gamma/&#34;&gt;The Forecasting Company released t0-gamma&lt;/a&gt; on October 13:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;t0-gamma is our new forecasting foundation model. It has a similar architecture to t0-beta, which our technical report describes in detail. It strengthens the t0 family’s ability to forecast from target history, related series and inputs known ahead of time. Weeks after the release of t0-beta, t0-gamma achieves state-of-the-art performance on public benchmarks. At the time of release, it ranks first among time-series foundation models on GIFT-Eval, fev-bench and TIME.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Only to be surpassed by Mistral on October 15, getting into the business of time series foundation models with &lt;a href=&#34;https://mistral.ai/news/timestral&#34;&gt;Timestral&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;We evaluated Timestral on the three main public forecasting benchmarks: GIFT-Eval, TIME and fev-bench. Among pre-trained, zero-shot models, it ranks first across all five metrics.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Records be tumbling.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>CRPS as Evaluation Metric in GIFT-eval</title>
	  <link>https://minimizeregret.com/note/2026/10/31/crps-as-evaluation-metric-in-gift-eval/</link>
	  <pubDate>Sat, 31 Oct 2026 07:08:28 +0100</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2026/10/31/crps-as-evaluation-metric-in-gift-eval/</guid>
	  <description>&lt;p&gt;While &lt;a href=&#34;https://minimizeregret.com/post/2026/10/29/ensembles-are-called-agents-now/&#34;&gt;going over the GIFT-eval leaderboard the other day&lt;/a&gt;, I perceived the usage of CRPS as evaluation metric as a laudable choice&amp;mdash;it being a proper scoring function. But as I continued to browse the GIFT-eval repository, reading about their preferred usage of GluonTS for evaluating forecasts, keeping in mind that most of the foundation models return only quantile forecasts, it dawned on me: You can&amp;rsquo;t calculate the CRPS from quantiles!&lt;/p&gt;&#xA;&lt;p&gt;I never before questioned how papers and benchmarks report the CRPS for quantile predictions. But the CRPS can only be calculated from samples or via closed-form equations for the Normal distribution. So how does GluonTS&amp;ndash;and by extension GIFT-eval&amp;ndash;do it?&lt;/p&gt;&#xA;&lt;p&gt;Approximate is how. GluonTS doesn&amp;rsquo;t even call the metric CRPS, but &lt;a href=&#34;https://github.com/awslabs/gluonts/blob/99eb407d7dd8bd5726738a8fb409560d86d19de0/src/gluonts/ev/metrics.py#L538&#34;&gt;&lt;code&gt;MeanWeightedSumQuantileLoss&lt;/code&gt;&lt;/a&gt;, because that&amp;rsquo;s what it is. The &lt;a href=&#34;https://huggingface.co/spaces/Salesforce/GIFT-Eval/blob/main/src/utils.py#L50&#34;&gt;metric is only renamed to CRPS&lt;/a&gt; in the GIFT-eval&amp;rsquo;s Hugging Face app. The &lt;a href=&#34;https://github.com/SalesforceAIResearch/gift-eval/blob/577a65cfa3acb812405c00d989230bf59f7f8a3d/notebooks/chronos-2.ipynb#L63&#34;&gt;notebooks in the GIFT-eval Github repository&lt;/a&gt; call the &lt;code&gt;MeanWeightedSumQuantileLoss&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;The paper &lt;a href=&#34;https://minimizeregret.com/papers/park-2021-learning-quantile-functions-without-quantile-crossing-for-distribution-free-time-series-forecasting/&#34;&gt;Learning Quantile Functions Without Quantile Crossing for Distribution-free Time Series Forecasting&lt;/a&gt; (Park et al., 2021) defines the metric in appendix F.2 and calls out that they use it as an approximation of the CRPS.&lt;/p&gt;&#xA;&lt;p&gt;The approximation of the CRPS is discussed in &lt;a href=&#34;https://minimizeregret.com/papers/adachi-2025-fixing-the-pitfalls-of-probabilistic-time-series-forecasting-evaluation-by-kernel-quadrature&#34;&gt;Fixing the Pitfalls of Probabilistic Time Series Forecasting Evaluation by Kernel Quadrature&lt;/a&gt; (Adachi et al., 2025). They specifically reference GluonTS in their abstract:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;We found that popular CRPS estimators&amp;ndash;specifically, the quantile-based estimator implemented in the widely used GluonTS library and the probability-weighted moment approximation&amp;ndash;both exhibit inherent estimation biases.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;</description>
	</item>
	<item>
	  <title>&#34;LG AI Research and KOSCOM Partner to Launch EXAONE-Powered AI Service for the Korean Stock Market&#34;</title>
	  <link>https://minimizeregret.com/link/2026/10/30/lgai-koscom-exaone/</link>
	  <pubDate>Fri, 30 Oct 2026 18:35:55 +0100</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2026/10/30/lgai-koscom-exaone/</guid>
	  <description>&lt;p&gt;The first lesson our Time Series Analysis 101 lecturer taught us was that we shouldn&amp;rsquo;t even try to predict future stock prices straight up.&lt;/p&gt;&#xA;&lt;p&gt;When discussing exchange rate impacts with colleagues, we keep on concluding that if we were able to predict exchange rates, we&amp;rsquo;d be sipping piña coladas at a beach.&lt;/p&gt;&#xA;&lt;p&gt;In other news, &lt;a href=&#34;https://www.lgresearch.ai/news/view?seq=666&#34;&gt;LG AI Research and KOSCOM&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Built on EXAONE BI, AI-Powered Equity Forecast Score (AEFS) is a financial data product that predicts the expected price movement of individual stocks over the next four weeks while providing AI-generated commentary explaining the rationale behind each prediction.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;I suppose one approach to overcome the efficient market hypothesis is to let everyone trade on the same forecast, implementing a self-fulfilling prophecy.&lt;/p&gt;&#xA;&lt;p&gt;While I was lamenting so-called agentic submissions on GIFT-eval that perform roughly as the underlying foundation models &lt;a href=&#34;https://minimizeregret.com/posts/2026/10/10/ensembles-are-called-agents-now/&#34;&gt;in an earlier post&lt;/a&gt;, one of the two submissions that do rank (significantly?) differently from the rest is EXAONE-Forecast-Agent by LG AI Research (at time of writing with a MASE of 0.610 and a CRPS of 0.419). Perhaps they do have figured it out.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Ensembles Are Called Agents Now</title>
	  <link>https://minimizeregret.com/post/2026/10/29/ensembles-are-called-agents-now/</link>
	  <pubDate>Thu, 29 Oct 2026 19:41:00 +0100</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2026/10/29/ensembles-are-called-agents-now/</guid>
	  <description>&lt;p&gt;The &lt;a href=&#34;https://huggingface.co/spaces/Salesforce/GIFT-Eval&#34;&gt;GIFT-eval forecast benchmark&lt;/a&gt; allows submissions to be listed under model type &amp;ldquo;agentic&amp;rdquo;. From &lt;a href=&#34;https://github.com/SalesforceAIResearch/gift-eval#field-descriptions&#34;&gt;its README&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;&lt;code&gt;agentic&lt;/code&gt;: Multi-step systems that use agents or LLMs to reason, generate or select forecasts.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Its leaderboard is now dominated by models tagged as such (indicated by the yellow circle emoji). Not being able to check all of the submissions (as some did not even provide links to documentation let alone replication code), at least a good chunk of the top submissions are barely using &amp;ldquo;reasoning&amp;rdquo;. Instead, they are ensembles, combining models such as TimesFM, Chronos, Toto, and TiRex.&lt;/p&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/TW3-Partners-OS/TW3-Cast&#34;&gt;TW3Cast&lt;/a&gt; is a complicated ensemble. From &lt;a href=&#34;https://github.com/TW3-Partners-OS/TW3-Cast/blob/main/README.md#tw3cast&#34;&gt;its README&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Listed in the leaderboard&amp;rsquo;s multi-model (agentic) category, but it runs no agent and no LLM, and makes no inference-time decisions: a frozen per-configuration router over specialized models.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;While there is a bit more to it, TW3Cast is a uniform ensemble for some datasets, as listed in its &lt;a href=&#34;https://github.com/TW3-Partners-OS/TW3-Cast/blob/main/router.csv&#34;&gt;router.csv&lt;/a&gt; file. For example, for the Car Parts dataset, it uses a uniform ensemble of Chronos-2, TimesFM, and TiRex-2.&lt;/p&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://huggingface.co/birlaailabs/Prism&#34;&gt;PRISM by Birla AI Labs&lt;/a&gt; is an ensemble:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Prism is a leakage-free probabilistic forecasting ensemble developed by the Frontier Research and DeepTech Team at Birla AI Labs. Rather than training a new model, Prism combines the quantile outputs of several pretrained foundation models, learning how to blend them from a held-out validation window that never touches test data. The key insight is that different models have complementary strengths: learning a separate blend at each quantile level means the model best at the median does not have to be the model that covers the tails.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://huggingface.co/racineai/RacineCast-1&#34;&gt;RacineCast-1&lt;/a&gt; and &lt;a href=&#34;https://huggingface.co/racineai/RacineCast-1-NC&#34;&gt;RacineCast-1-NC&lt;/a&gt; (i.e. non-commercial) are weighted ensembles of models. RacineAI summarizes their approach in this recipe:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;0.55 · Toto-2.0-FnF + 0.16 · Toto-2.0-2.5B + 0.16 · TiRex-2 + 0.045 · Chronos-2 + 0.045 · FlowState-r1.1 + 0.03 · PatchTST-FM-r1&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Back in July of this year, it ranked them first on the leaderboard.&lt;/p&gt;&#xA;&lt;p&gt;55% of the RacineCast approach is &lt;a href=&#34;https://huggingface.co/Datadog/Toto-2.0-Family-and-Friends&#34;&gt;Toto-2.0-FnF&lt;/a&gt; (as in Family-and-Friends). Toto-2.0-FnF is an ensemble of different Toto checkpoints and other models&amp;mdash;meta-learned by a &lt;a href=&#34;https://robjhyndman.com/publications/fforma/&#34;&gt;FFORMA-style XGBoost&lt;/a&gt;. It, too, is submitted as &amp;ldquo;agentic&amp;rdquo; to the GIFT-eval leaderboard.&lt;/p&gt;&#xA;&lt;p&gt;My personal favorite, however, is &lt;a href=&#34;https://github.com/IndarKarhana/ik-quadtsfm&#34;&gt;IK-QuadTSFM&lt;/a&gt;. It is a &lt;a href=&#34;https://github.com/IndarKarhana/ik-quadtsfm/blob/f1476cfa756553ca2c598ec643cf0607eb8be515/README.md#candidate&#34;&gt;uniform ensemble of Chronos-2, TimesFM-3, TiRex-2, and Toto-2.0-FT&lt;/a&gt;, and as such questions the additional complexity of any other &amp;ldquo;agentic&amp;rdquo; submission in its vicinity.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2026/10/29/ensembles-are-called-agents-now/gift-eval-snapshot.png&#34; alt=&#34;Screenshot of GIFT-eval leaderboard.&#34;&gt;&lt;figcaption&gt;Right-click to open the full-size image. Screenshot of the GIFT-eval overall leaderboard ranked by the default MASE_Rank. The yellow emoji stands for GIFT-eval’s “agentic” model type, red stands for “zero-shot”, green for “pretrained”.&lt;/figcaption&gt;&lt;/figure&gt;&lt;p&gt;The above screenshot was taken on October 29, 2026. It shows the default sorting based on the MASE_Rank column. Sorting by MASE would shuffle the results somewhat, though &amp;ldquo;Stride w/ Synapse&amp;rdquo; and &amp;ldquo;EXAONE-Forecast-Agent&amp;rdquo; would continue to rank at the top. The rest would cover the range from 0.650 MASE (for LS-MoE and TIMEHEDGE) over 0.663 for the aforementioned IK-QuadTSFM and 0.667 for TimesFM, to 0.698 for Chronos-2.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Similar-Day Naive Method</title>
	  <link>https://minimizeregret.com/note/2025/12/01/similar-day-naive-method/</link>
	  <pubDate>Mon, 01 Dec 2025 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2025/12/01/similar-day-naive-method/</guid>
	  <description>&lt;p&gt;Like the &lt;a href=&#34;https://arxiv.org/abs/2510.15821&#34;&gt;Chronos-2 technical report&lt;/a&gt;, Uniejewski and Weron compare the approaches implemented in their &lt;a href=&#34;https://minimizeregret.com/note/2025/11/30/chronos-2-scaling-inverse-hyperbolic-sine&#34;&gt;aforementioned&lt;/a&gt; &lt;a href=&#34;https://www.mdpi.com/1996-1073/11/8/2039&#34;&gt;paper&lt;/a&gt; against a naive baseline. Unlike the technical report which uses seasonal naive for all data sets, Uniejewski and Weron, to their credit, employ a baseline that, while naive, is tuned to their use case of electricity price forecasting and employs as much knowledge as possible:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;The first benchmark, the so-called naive method, belongs to the class of similar-day techniques. It is defined by $\hat{P}_{d,h} = \hat{P}_{d-7,h}$ for Monday, Saturday and Sunday, and $\hat{P}_{d,h} = \hat{P}_{d-1,h}$ otherwise.&#xA;As Contreras et al. argued, forecasting procedures that are not calibrated carefully fail to outperform this extremely simple method surprisingly often.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;So given the different human patterns in electricity usage on workdays and on weekends, they use the naive method for workdays and seasonal naive for the weekend and Monday (because Sunday can&amp;rsquo;t be used to predict Monday).&lt;/p&gt;&#xA;&lt;p&gt;Reminds me of &lt;a href=&#34;https://minimizeregret.com/post/2022/08/30/where-is-the-seasonal-naive-benchmark/#fn1&#34;&gt;this footnote&lt;/a&gt;.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Scaling Time Series Using the Inverse Hyperbolic Sine</title>
	  <link>https://minimizeregret.com/note/2025/11/30/chronos-2-scaling-inverse-hyperbolic-sine/</link>
	  <pubDate>Sun, 30 Nov 2025 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2025/11/30/chronos-2-scaling-inverse-hyperbolic-sine/</guid>
	  <description>&lt;p&gt;This one&amp;rsquo;s new to me: The Chronos-2 tokenization pipeline follows up an initial standardization of input time series with a variance stabilizing transformation in the &lt;em&gt;inverse hyperbolic sine function&lt;/em&gt;, $\sinh^{-1}$, as described in the &lt;a href=&#34;https://arxiv.org/abs/2510.15821&#34;&gt;model&amp;rsquo;s technical report&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;The &lt;a href=&#34;https://en.wikipedia.org/wiki/Inverse_hyperbolic_functions&#34;&gt;inverse hyperbolic sine function&lt;/a&gt; is defined as:&lt;/p&gt;&#xA;$$&#xA;\sinh^{-1}(x) = \ln(x + \sqrt{x^2+1}).&#xA;$$&lt;p&gt;As such it is an alternative to the $\ln(x+1)$ transform with the added benefit of being applicable to negative values.&lt;/p&gt;&#xA;&lt;p&gt;While news to me, the technical report points out that the inverse hyperbolic sine is no news to the electricity price forecasting literature, citing &amp;ldquo;&lt;a href=&#34;https://www.mdpi.com/1996-1073/11/8/2039&#34;&gt;Efficient Forecasting of Electricity Spot Prices with&#xA;Expert and LASSO Models&lt;/a&gt;&amp;rdquo; by Bartosz Uniejewski and Rafał Weron, which describes the inverse hyperbolic sine and other variance stabilizing transformations in more detail.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Stacking Simple Forecasts by Classification</title>
	  <link>https://minimizeregret.com/note/2025/11/23/stacking-simple-forecasts-by-classification/</link>
	  <pubDate>Sun, 23 Nov 2025 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2025/11/23/stacking-simple-forecasts-by-classification/</guid>
	  <description>&lt;p&gt;&lt;a href=&#34;https://arxiv.org/abs/2511.15350&#34;&gt;This paper&lt;/a&gt; got me thinking about stacking and ensembling of forecasts again.&lt;/p&gt;&#xA;&lt;p&gt;Different from what the paper proposes but in the stacking models-sense, consider some multi-class classification model used to predict which base model forecasts a given observation from a set of many time series best. Something more akin to the &lt;a href=&#34;https://robjhyndman.com/papers/fforma.pdf&#34;&gt;FFORMA approach&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;But use simple forecast methods to keep the overhead of first creating base forecasts at a minimum. Use the seasonal lag, the seasonal average, the most recent observation, zero, year-to-date average, etc. as base forecasts. Anything, really, that makes for a strong baseline for part of your dataset and can be calculated &amp;ldquo;without training&amp;rdquo;. If nothing else, this avoids the need for expensive cross-validation loads (as those described in the &lt;a href=&#34;https://arxiv.org/abs/2511.15350&#34;&gt;aforementioned paper&lt;/a&gt;).&lt;/p&gt;&#xA;&lt;p&gt;Then, for each observation of your time series data, derive a label based on the base forecast that comes closest to the observation.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#xA;&lt;p&gt;Finally, train the classifer model to predict the labels, using the base models as features, but also any other kind of feature. Now well and truly spitballing: Have some features summarize base predictions relative to one another, and relative to recent observations (&amp;ldquo;The seasonal lag is 190% of the year-to-date average&amp;rdquo;) to help the model distinguish patters.&lt;/p&gt;&#xA;&lt;p&gt;Given that the classifier model would only pick between simple forecast methods to arrive at the final prediction, the overall forecast accuracy will always be limited. I can imagine scenarios, however, where the prediction ends up being more stable and at the right level compared to models trained to predict the time series directly. Constrained by the quality of the base forecasts, this stacking approach could pay off handsomely if the optimal base learner differs across the set of time series, or frequently changes within the same series. That is, when there is no dominant baseline.&lt;/p&gt;&#xA;&lt;p&gt;For example, it could work well in scenarios where time series have multi-modal distributions due to zeroes, or frequently exhibit structural breaks.&lt;/p&gt;&#xA;&lt;p&gt;In a way, the classifier would learn the sort of &amp;ldquo;item routing&amp;rdquo; &lt;a href=&#34;https://vldb.org/pvldb/vol10/p1694-schelter.pdf&#34;&gt;described here&lt;/a&gt;. Instead of assigning time series to different forecast models based on rules, the classifier will split the data into subsets and assign the model.&lt;/p&gt;&#xA;&lt;p&gt;One more nice side effect: There is no need for target scaling when base forecasts are non-trained methods and when the meta learner only needs to classify.&lt;/p&gt;&#xA;&lt;p&gt;The overall approach is also fairly explainable if not interpretable. Especially if one picks the features for additional context wisely.&lt;/p&gt;&#xA;&lt;p&gt;Finally, while the base forecasts will limit the overall forecast quality that can be achieved, we can measure the best &lt;em&gt;possible&lt;/em&gt; forecast quality the stacking approach can reach in the hypothetical case of a perfect classification of every observation. So before putting any effort into training a good classifier, we will know whether the effort could hypothetically be worth it.&lt;/p&gt;&#xA;&lt;p&gt;Though there is a limit to the number of base forecasts you should use to derive the labels for the classifier. Dare to use the range of real numbers and you&amp;rsquo;ll end up back with a (&lt;a href=&#34;https://arxiv.org/abs/2005.10111&#34;&gt;discretized&lt;/a&gt;) regression problem.&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;Why not throw in an additional label category indicating that none of the methods came even close to the actual observation? To predict that a data point is not predictable by the base forecasts.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>&#39;Data Engineers Should Be Held to the Same Standards as Bakers&#39;</title>
	  <link>https://minimizeregret.com/link/2025/05/10/data-engineer-baker-standards/</link>
	  <pubDate>Sat, 10 May 2025 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2025/05/10/data-engineer-baker-standards/</guid>
	  <description>&lt;p&gt;Over at Hermit Tech, Jordan Andersen laments over embarrassingly low project delivery expectations within the software and data industry when compared to other professions such as the one of a baker. His concern, however, is not only with the customers of the project, but with the developers:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;The latter consequence of low expectations is the more serious one. It&amp;rsquo;s something that some people may never recover from. [&amp;hellip;] The trajectory of a data or software engineer seems to be a bit different: they burn out from the mental stress of working in dishonest and fraudulent cultures that produce unsatisfying work, but the engineer stays in the profession. Only they&amp;rsquo;re a shell of a human.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;</description>
	</item>
	<item>
	  <title>Inherently Coherent Forecasts</title>
	  <link>https://minimizeregret.com/note/2025/03/16/inherently-coherent-forecasts/</link>
	  <pubDate>Sun, 16 Mar 2025 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2025/03/16/inherently-coherent-forecasts/</guid>
	  <description>&lt;p&gt;Suppose you offer a single API endpoint that takes historical observations of one time series at a time (not unlike &lt;a href=&#34;https://docs.nixtla.io/reference/forecast_forecast_post&#34;&gt;Nixtla&amp;rsquo;s TimeGPT&lt;/a&gt;). Suppose further that everyone at your company uses this endpoint to generate any business forecast they need. What behaviors should the API&amp;rsquo;s forecast exhibit?&lt;/p&gt;&#xA;&lt;p&gt;An obvious one would be &lt;a href=&#34;https://otexts.com/fpp3/hierarchical.html&#34;&gt;forecast coherence across aggregation levels&lt;/a&gt;. Say your company sells three products. You have the historical revenue for the three products, $Y_1$, $Y_2$, $Y_3$, as well as the historical revenue for the company as a whole, $Y$, where $Y=Y_1+Y_2+Y_3$. When you feed $Y_1$ into the API, the API returns forecast $\text{API}(Y_1)=\hat{Y}_1$. It returns $\hat{Y}_2$ given $Y_2$, $\hat{Y}_3$ given $Y_3$, and given $Y$ it returns $\hat{Y}$. If we want to see a forecast at company-level that &amp;ldquo;makes sense&amp;rdquo; given product-level forecasts, we want the forecasts to be coherent: $\hat{Y}=\hat{Y}_1 + \hat{Y}_2 + \hat{Y}_3$. But given that the API only ever sees one time series at a time, what model could be used to provide coherent forecasts?&lt;/p&gt;&#xA;&lt;p&gt;Again, keep in mind that the API doesn’t have access to more information than its current input. For example, when you feed it $Y_2$, it doesn’t know that $Y_1$ and $Y_3$ exist. It also doesn’t know whether $Y_2$ is at the hierarchy’s bottom- or top-level. The API does not have access to all possible time series that exist at the company. Consequently, the API does not have the information required to apply the methods from the &lt;a href=&#34;https://otexts.com/fpp3/hierarchical.html&#34;&gt;vast&lt;/a&gt; &lt;a href=&#34;https://proceedings.mlr.press/v139/rangapuram21a.html&#34;&gt;literature&lt;/a&gt; &lt;a href=&#34;https://arxiv.org/abs/2502.15983&#34;&gt;on&lt;/a&gt; &lt;a href=&#34;https://www.tandfonline.com/doi/full/10.1080/01621459.2018.1448825&#34;&gt;coherent&lt;/a&gt; &lt;a href=&#34;https://proceedings.mlr.press/v70/taieb17a.html&#34;&gt;forecasts&lt;/a&gt;. What modeling approach will return coherent forecasts nevertheless?&lt;/p&gt;&#xA;&lt;p&gt;The seasonal naive method would. As would the naive method, of course. The mean forecast, too. And any other linear combination of the past observations with a pre-determined context length and coefficients.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; Any pre-trained—the coefficients must not change depending on the input provided to the API—linear autoregressive model of order p will do.&lt;/p&gt;&#xA;&lt;p&gt;That’s a fairly flexible model family. It also includes the &lt;a href=&#34;https://arxiv.org/abs/2312.14657&#34;&gt;Non-Parametric Time Series Forecaster&lt;/a&gt; and &lt;a href=&#34;https://minimizeregret.com/post/2023/10/19/threedx&#34;&gt;Threedx&lt;/a&gt;. Thus, the API could do an OK job for most series passed to it. It would have an obvious weakness in new or recently discontinued time series, though. The API could not, for example, override its usual forecast for a recently discontinued product and forecast zero instead. The same holds for any other kind of structural break as well as anomalies. That would make it tough to rely exclusively on such an API for operational decisions happening at the low hierarchy level ridden with discontinuities.&lt;/p&gt;&#xA;&lt;p&gt;The above combined with &lt;a href=&#34;https://minimizeregret.com/linked/2025/02/23/performance-of-zero-shot-time-series-foundation-models-on-cloud-data/&#34;&gt;recent evidence&lt;/a&gt; make me inclined to offer the seasonal naive method as &lt;em&gt;Zero-Shot Foundation Model with Inherently Coherent Forecasts&lt;/em&gt; available as API.&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;For example, the mean forecast must not be the mean of the provided observations, but the mean of $k$ observations resulting in coefficients $1/k$. A time series with $n \lt k$ observations needs to be padded with zeros. A time series with $n \gt k$ observations is shortened to the $k$ most recent observations.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>&#39;Performance of Zero-Shot Time Series Foundation Models on Cloud Data&#39;</title>
	  <link>https://minimizeregret.com/link/2025/02/23/performance-of-zero-shot-time-series-foundation-models-on-cloud-data/</link>
	  <pubDate>Sun, 23 Feb 2025 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2025/02/23/performance-of-zero-shot-time-series-foundation-models-on-cloud-data/</guid>
	  <description>&lt;p&gt;Toner et al. compare time series foundation models such as Chronos, Mamba4Cast, and TimesFM on data from Huawei data centers (&lt;a href=&#34;https://github.com/sir-lab/data-release&#34;&gt;data is available on Github!&lt;/a&gt;):&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;To examine the behaviour of (zero-shot) FMs on cloud time series, we perform experiments on function demand data drawn from real-world usage. Our results show that FMs perform poorly, being outperformed by the simple baselines of a linear model and a naive seasonal forecaster.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;If you think about it, the seasonal naive method is the original zero-shot foundation model.&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;For example, the naive seasonal forecaster performs better than all the FMs across all datasets and forecast horizons. Moreover, the performance difference is often large; for example, the naive seasonal forecaster incurs a MASE typically half that of TimesFM.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Alas, the authors make little attempt to explain why the foundation models are outperformed by the seasonal naive method beyond the data&amp;rsquo;s spikiness. But perhaps that&amp;rsquo;s just it. Chronos, for example, is known to fail on spiky data due to its mean scaling and quantization (see figure 16 in the &lt;a href=&#34;https://arxiv.org/abs/2403.07815v3&#34;&gt;corresponding paper&lt;/a&gt;).&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;We also present evidence of pathological behaviours of FMs, demonstrating they can produce chaotic (as in Figure 1) or illogical (as in Figure 2) forecasts.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Check out the mentioned plots in figures 1, 2, 4 and 5. They&amp;rsquo;re why I don&amp;rsquo;t trust any paper that doesn&amp;rsquo;t visualize its method&amp;rsquo;s predictions.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>R-squared as Forecast Evaluation Metric</title>
	  <link>https://minimizeregret.com/note/2025/01/12/r-squared-as-forecast-evaluation-metric/</link>
	  <pubDate>Sun, 12 Jan 2025 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2025/01/12/r-squared-as-forecast-evaluation-metric/</guid>
	  <description>&lt;p&gt;When Jane Street&amp;rsquo;s \$120,000 &amp;ldquo;Real-Time Market Data Forecasting&amp;rdquo; &lt;a href=&#34;https://www.kaggle.com/competitions/jane-street-real-time-market-data-forecasting/overview&#34;&gt;Kaggle competition&lt;/a&gt; closes tomorrow, submitted forecasts &lt;a href=&#34;www.kaggle.com/competitions/jane-street-real-time-market-data-forecasting/overview/evaluation&#34;&gt;will be evaluated&lt;/a&gt; using $R^2$:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Submissions are evaluated on a scoring function defined as the sample weighted zero-mean R-squared score ($R^2$) of &lt;code&gt;responder_6&lt;/code&gt;. The formula is give by: $R^2 = 1 - \frac{\sum w_i(y_i - \hat{y}_i)^2}{\sum w_i y_i^2}$ where $y$ and $\hat{y}$ are the ground-truth and predicted value vectors of &lt;code&gt;responder_6&lt;/code&gt;, respectively; $w$ is the sample weight vector.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Stats 101 courses have left $R^2$ with a bad rep as gameable regression metric. But I&amp;rsquo;ve come to appreciate it as a metric in forecasting&amp;mdash;when calculated on the test set with the &lt;a href=&#34;https://en.wikipedia.org/wiki/Total_sum_of_squares&#34;&gt;total-sum-of-squares&lt;/a&gt; denominator based on the test period&amp;rsquo;s empirical mean, $\bar{y}_{T,h} = \sum_{t = T+1}^{T+h}y_t$, where $T$ is the training set&amp;rsquo;s last observation and $h$ the number of observations in the test set. Then:&lt;/p&gt;&#xA;$$&#xA;R^2 = 1 - \frac{\sum_{t = T+1}^{T+h}(y_t - \hat{y}_t)^2}{\sum_{t = T+1}^{T+h}(y_t - \bar{y}_{T,h})^2}&#xA;$$&lt;p&gt;Above definition gives the total-sum-of-squares the advantage of future knowledge included in $\bar{y}_{T,h}$ that the predictions $\hat{y}_t$ used for the &lt;a href=&#34;https://en.wikipedia.org/wiki/Residual_sum_of_squares&#34;&gt;residual-sum-of-squares&lt;/a&gt; do not have. In comparison to its linear regression origin, this test set-focused definition is not going to improve as parameters are added to the forecast model. But it does keep its interpretability:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;A value of 1 indicates a perfect model&lt;/li&gt;&#xA;&lt;li&gt;A value of 0 indicates a model as good as the test mean (the latter being a forecast that is both perfectly unbiased and perfectly predicts the total sum over the forecast horizon)&lt;/li&gt;&#xA;&lt;li&gt;Any negative value indicates a model that isn&amp;rsquo;t even as good as the simple test mean (but then again, the test mean uses future knowledge)&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;For example, when predicting the final twelve months of &lt;a href=&#34;https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/AirPassengers.html&#34;&gt;AirPassengers&lt;/a&gt; data, the obviously poor prediction given by the training set mean results in an $R^2$ of -8.24 whereas a seasonal naive prediction achieves an $R^2$ of 0.54.&lt;/p&gt;&#xA;&lt;p&gt;This shouldn&amp;rsquo;t come as a suprise. Interpreting $R^2$ as the &lt;a href=&#34;https://en.wikipedia.org/wiki/Fraction_of_variance_unexplained&#34;&gt;fraction of (test) variance explained&lt;/a&gt;, the seasonal naive prediction can explain more than half of the variance that the test mean left unexplained. The model captures a large part of the signal hidden in the data&amp;rsquo;s variation.&lt;/p&gt;&#xA;&lt;p&gt;Just like Jane Street replaced the test set empirical mean in their definition of $R^2$ by zero, which in their application of predicting expected financial returns is a hard to beat expected value, one could replace the total sum of squares denominator by the squared errors of a different benchmark such as seasonal naive predictions when it is known that due to the data&amp;rsquo;s seasonality the seasonal naive prediction will consistently outperform the test set mean despite its future knowledge. Thus, $R^2$ is not so different from the more common relative measures such as the Relative Mean Squared Error described in section 6.1.4 of &lt;a href=&#34;https://arxiv.org/pdf/2203.10716v2&#34;&gt;Hewamalage et al. (2022)&lt;/a&gt;, or Skill Scores employed in weather prediction described by &lt;a href=&#34;https://doi.org/10.1175/1520-0493(1988)116%3C2417:SSBOTM%3E2.0.CO;2&#34;&gt;Murphy (1988)&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Then again, when a time series&amp;rsquo; variance is not dominated by its seasonal component, $R^2$ and its interpretation as fraction of variance explained can leave a damning picture of a model&amp;rsquo;s forecasts not actually being all that skillful. It casts doubt by asking how much signal your model has picked up to meaningfully predict future variation. Suddenly any value larger than zero will feel like an enormous success.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Explosion Back to Its Roots</title>
	  <link>https://minimizeregret.com/link/2024/10/06/spacy-back-to-its-roots/</link>
	  <pubDate>Sun, 06 Oct 2024 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2024/10/06/spacy-back-to-its-roots/</guid>
	  <description>&lt;p&gt;In 2016, Matthew Honnibal presented at &lt;a href=&#34;https://www.meetup.com/pydata-berlin/events/228773954/&#34;&gt;the first PyData Berlin meetup that I attended&lt;/a&gt;. He had already started spaCy and was training models on Reddit comments, whereas I wasn&amp;rsquo;t really into NLP and heard about spaCy for the first time that evening. And while I&amp;rsquo;m still not really into NLP, I never stopped keeping tabs on what he and Ines Montani were building at &lt;a href=&#34;https://explosion.ai&#34;&gt;Explosion&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Honnibal recounts &lt;a href=&#34;https://honnibal.dev/blog/back-to-our-roots&#34;&gt;Explosion&amp;rsquo;s history in view of recent changes&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;For most of Explosion’s life we’ve been a very small company, running off revenues. In 2021 that changed, and we became a slightly less small company running off venture capital. We’ve been unable to make that configuration work, so we’re back to running Explosion as an independent-minded self-sufficient company. We’re going to stay small and not look for any more venture capital. spaCy and Prodigy will continue.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;With a focus on &amp;ldquo;&lt;a href=&#34;https://spacy.io&#34;&gt;industrial-strength&lt;/a&gt;&amp;rdquo;, Explosion has built &lt;a href=&#34;https://github.com/explosion/thinc&#34;&gt;opinionated&lt;/a&gt; data science tooling with &lt;a href=&#34;https://prodi.gy&#34;&gt;beautiful&lt;/a&gt; documentation. SpaCy is beloved open-source software (with the community coming together for &lt;a href=&#34;https://irl.spacy.io/2019/&#34;&gt;spaCy IRL&lt;/a&gt;&amp;mdash;a real treat of a conference) that convinces data scientists to spend company budget on Prodigy. This combination of spaCy and Prodigy is the ingredient to Explosion&amp;rsquo;s unique success as small, self-sufficient company in a venture-funded AI environment. Already familiar with spaCy, data scientists are comfortable with purchasing Prodigy licenses to ease their annotation workflows common to NLP. And being technical expert users, they also are capable of hosting the software &lt;em&gt;themselves&lt;/em&gt;. Explosion doesn&amp;rsquo;t have to handle customers&amp;rsquo; data.&lt;/p&gt;&#xA;&lt;p&gt;License revenues, no hosting, no data: Enablers of a profitable business run by a small team. I wish they continue to thrive!&lt;/p&gt;&#xA;&lt;p&gt;In his post, Honnibal shares realities of maintaining software that companies and developers rarely admit to, yet which are determinants of a team&amp;rsquo;s success:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Engineering for spaCy and our other projects was also very challenging to hand over. spaCy is implemented in Cython, and big chunks of the project are essentially C code with funny syntax. We pass around pointers to arrays of structs, and if you access them out of bounds, well, hopefully it crashes. You have to just not do that. And then in addition to this memory-managed code, there’s all the GPU-specific considerations, all the numpy minutiae, and maintaining compatibility with a big matrix of Python versions, operating systems and hardware. It’s a lot.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;The infrastructure required for machine learning doesn&amp;rsquo;t make it any easier:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;I’ve been finding the transition back to the way things were quite difficult. I still know our codebases well, but the associated infrastructure isn’t easy to wrangle. Overall I haven’t been very productive over the last few months, but it’s getting better now.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;On top come unexpected team dynamics as the previous architect shifts his focus:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;As I became less involved in the hands-on work, I struggled to be effective as a decision-maker. A lot of the bigger questions got deferred, and we had an increasing bias towards whichever approach was least committal.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;On a different note, I am fascinated that Hugging Face has the funds to provide a quarter-million grant for open-source developers. How many of these funds do they provide?&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;We considered selling the company, but we weren’t able to find a good fit. Instead, we’re back at the same sort of size we had before the investment. We’re very grateful to Hugging Face for a $250,000 grant to support our open-source work as our funding ran out, and we’ve applied successfully for a German R&amp;amp;D reimbursement grant that will give us up to €1.5m in unconditional funding.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;To me, Explosion is one of the coolest exports that Berlin and Germany have to offer. Great to see them receive such grant.&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;At roughly &lt;a href=&#34;https://techcrunch.com/2023/08/24/hugging-face-raises-235m-from-investors-including-salesforce-and-nvidia/&#34;&gt;0.1% of their Series D funding round&lt;/a&gt;, there might be a few.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>The New Internet</title>
	  <link>https://minimizeregret.com/link/2024/10/06/the-new-internet/</link>
	  <pubDate>Sun, 06 Oct 2024 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2024/10/06/the-new-internet/</guid>
	  <description>&lt;p&gt;Avery Pennarun, CEO of Tailscale, in a company all-hands meeting:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;In modern computing, we tolerate long builds, and then docker builds, and uploading to container stores, and multi-minute deploy times before the program runs, and even longer times before the log output gets uploaded to somewhere you can see it, all because we’ve been tricked into this idea that everything has to scale. People get excited about deploying to the latest upstart container hosting service because it only takes tens of seconds to roll out, instead of minutes. But on my slow computer in the 1990s, I could run a perl or python program that started in milliseconds and served way more than 0.2 requests per second, and printed logs to stderr right away so I could edit-run-debug over and over again, multiple times per minute.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;</description>
	</item>
	<item>
	  <title>Trained Random Forests Completely Reveal Your Dataset</title>
	  <link>https://minimizeregret.com/link/2024/07/27/dataset-reconstruction-from-random-forests/</link>
	  <pubDate>Sat, 27 Jul 2024 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2024/07/27/dataset-reconstruction-from-random-forests/</guid>
	  <description>&lt;p&gt;The paper&amp;rsquo;s title is a small portion of clickbait: As of yet not &lt;em&gt;all&lt;/em&gt; but &lt;em&gt;some&lt;/em&gt; trained random forests completely reveal your dataset. Still, using constraint programming the authors completely reconstruct the data used to train binary classification random forests without bagging on only binary features.&lt;/p&gt;&#xA;&lt;p&gt;I imagine a lot of random forests having been trained on sensitive data in the past and their model files been more loosely handeled than the data. What private information could the model possibly reveal? Yeah.&lt;/p&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://youtu.be/qguwZzLV-eE&#34;&gt;Watch Julien Ferry present his paper&lt;/a&gt; in a video recorded &lt;a href=&#34;https://icml.cc/virtual/2024/poster/33585&#34;&gt;for ICML 2024&lt;/a&gt;.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>How The Economist&#39;s Presidential Forecast Works</title>
	  <link>https://minimizeregret.com/link/2024/06/16/how-the-economists-presidential-forecast-works/</link>
	  <pubDate>Sun, 16 Jun 2024 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2024/06/16/how-the-economists-presidential-forecast-works/</guid>
	  <description>&lt;p&gt;The Economist is back with a &lt;a href=&#34;https://www.economist.com/interactive/us-2024-election/prediction-model/president/&#34;&gt;forecast for the 2024 US presidential election&lt;/a&gt; in collaboration with Andrew Gelman and others. One detail in &lt;a href=&#34;https://www.economist.com/interactive/us-2024-election/prediction-model/president/how-this-works/&#34;&gt;the write-up&lt;/a&gt; of their approach stood out to me:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;The ultimate result is a list of 10,001 hypothetical paths that the election could take.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Not 10,000, but 10,000 &lt;em&gt;and one&lt;/em&gt; MCMC samples. I can&amp;rsquo;t remember seeing any reference for this choice before (&lt;a href=&#34;https://paul-buerkner.github.io/brms/reference/brm.html&#34;&gt;packages&lt;/a&gt; &lt;a href=&#34;https://cran.r-project.org/web/packages/bsts/bsts.pdf&#34;&gt;love an&lt;/a&gt; &lt;a href=&#34;https://blackjax-devs.github.io/blackjax/#hello-world&#34;&gt;even&lt;/a&gt; &lt;a href=&#34;https://num.pyro.ai/en/stable/getting_started.html#a-simple-example-8-schools&#34;&gt;number as&lt;/a&gt; &lt;a href=&#34;https://www.pymc.io/projects/docs/en/stable/api/generated/pymc.sample.html#pymc.sample&#34;&gt;default&lt;/a&gt;), but I have been adding a single additional sample as tie breaker for a long time: If nothing else, it comes in handy to have a dedicated path represent the median to prevent an awkward median estimate of 269.5 electoral votes.&lt;/p&gt;&#xA;&lt;p&gt;The extra sample is especially helpful when the main outcome of interest is a sum of more granular outcomes. In the case of the presidential election, the main outcome is the sum of electoral votes provided by the states. One can first identify the median of the main outcome (currently 237 Democratic electoral votes). Given the extra sample, there will be one MCMC sample that results in the median. From here, one can work backwards and identify this sample index and the corresponding value for every state, for example. The value might not be a state&amp;rsquo;s most &amp;ldquo;representative&amp;rdquo; outcome and it is unlikely to be the state&amp;rsquo;s median number of electoral votes. But the sum across states will be the median of the main outcome. Great for a visualization depicting what scenario would lead to this projected constellation of the electoral college.&lt;/p&gt;&#xA;&lt;p&gt;In contrast, summing up the median outcome of each state, there would be only 226 Democratic electoral votes as of today.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;CA, DC, HI, MA, MD, ME 1, NY, VT, CO, CT, DE, IL, NJ, OR, RI, WA, NM, ME, NE 2, VA, NH, MN.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>(Deep) Non-Parametric Time Series Forecaster</title>
	  <link>https://minimizeregret.com/link/2024/05/06/npts-paper/</link>
	  <pubDate>Mon, 06 May 2024 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2024/05/06/npts-paper/</guid>
	  <description>&lt;p&gt;If you read &lt;a href=&#34;https://www.amazon.science/latest-news/the-history-of-amazons-forecasting-algorithm&#34;&gt;The History of Amazon&amp;rsquo;s Forecasting Algorithm&lt;/a&gt;, you&amp;rsquo;ll hear about fantastic models such as Quantile Random Forests, and the MQTransformer. In &lt;a href=&#34;https://ts.gluon.ai/stable/getting_started/models.html&#34;&gt;GluonTS&lt;/a&gt; you&amp;rsquo;ll find DeepAR and DeepVARHierarchical. But the real hero is the simple model that does the work when all else fails. Tim Januschowski &lt;a href=&#34;https://www.linkedin.com/posts/tim-januschowski_forecasting-amazon-reviewer3-activity-7157725169543700480-JiG7&#34;&gt;on Linkedin&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;One of the baselines that we’ve developed over the years is the non-parametric forecaster or NPTS for short. Jan Gasthaus invented it probably a decade ago and Valentin Flunkert made it seasonality aware and to the best of my knowledge it’s been re-written a number of times and still runs for #amazon retail (when other surrounding systems were switched off long ago).&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Januschowski mentions this to celebrate the &lt;a href=&#34;https://arxiv.org/abs/2312.14657&#34;&gt;Arxiv paper&lt;/a&gt; describing NPTS and its DeepNPTS variant with additional &amp;ldquo;bells and whistles&amp;rdquo;. Which I celebrate as I no longer have to refer people to section 4.3 of &lt;a href=&#34;https://arxiv.org/abs/1906.05264&#34;&gt;the GluonTS paper&lt;/a&gt;.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Debug Forecasts with Animated Plots</title>
	  <link>https://minimizeregret.com/note/2024/04/21/animated-forecasts/</link>
	  <pubDate>Sun, 21 Apr 2024 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2024/04/21/animated-forecasts/</guid>
	  <description>&lt;p&gt;&lt;a href=&#34;https://minimizeregret.com/link/2024/04/15/masson-pydata/&#34;&gt;Speaking of GIFs&lt;/a&gt;, animated visualizations of rolling forecasts are eye-opening to the impact of individual observations, the number of observations, and default settings on a model&amp;rsquo;s forecasts. In the example below, the default &lt;code&gt;forecast::auto.arima()&lt;/code&gt; transitions between poor model specifications until it can finally pick up the seasonality after 24 observations, only to generate a negative point forecast despite purely non-negative observations.&lt;/p&gt;&#xA;&lt;p&gt;Fantastic way to understand forecast methods&amp;rsquo; edge-case behavior.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/note/2024/04/21/animated-forecasts/arima_animation.gif&#34;&gt;&lt;/figure&gt;&lt;p&gt;My favorite frame? After nine observations, when a model specification with trend is picked and returns an exploding forecast based on very little evidence.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/note/2024/04/21/animated-forecasts/arima_animation_frame_9.jpg&#34;&gt;&lt;/figure&gt;&lt;p&gt;The code for this GIF &lt;a href=&#34;https://github.com/timradtke/anime&#34;&gt;in this small Github repo&lt;/a&gt;.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Reliably Forecasting Time-Series in Real Time</title>
	  <link>https://minimizeregret.com/link/2024/04/15/masson-pydata/</link>
	  <pubDate>Mon, 15 Apr 2024 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2024/04/15/masson-pydata/</guid>
	  <description>&lt;p&gt;Straight from my YouTube recommendations, a &lt;a href=&#34;https://www.youtube.com/watch?v=0zpg9ODE6Ww&#34;&gt;PyData London 2018 (!) presentation by Charles Masson&lt;/a&gt; of Datadog. To predict whether server metrics cross a threshold, he builds a method model that focuses on being robust to all the usual issues of anomalies and structural breaks. He keeps it simple, interpretable, and&amp;ndash;for the sake of real-time forecasting&amp;ndash;fast. Good stuff all around. The GIFs are the cherry on top.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Chronos: Learning the Language of Time Series</title>
	  <link>https://minimizeregret.com/link/2024/03/27/chronos-forecasting/</link>
	  <pubDate>Wed, 27 Mar 2024 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2024/03/27/chronos-forecasting/</guid>
	  <description>&lt;p&gt;&lt;a href=&#34;https://arxiv.org/abs/2403.07815&#34;&gt;Ansari et al. (2024)&lt;/a&gt; introduce their Chronos model family &lt;a href=&#34;https://github.com/amazon-science/chronos-forecasting&#34;&gt;on Github&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Chronos is a family of pretrained time series forecasting models based on language model architectures. A time series is transformed into a sequence of tokens via scaling and quantization, and a language model is trained on these tokens using the cross-entropy loss. Once trained, probabilistic forecasts are obtained by sampling multiple future trajectories given the historical context.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;The whole thing is very neat. The repository can be pip-installed as package wrapping the &lt;a href=&#34;https://huggingface.co/collections/amazon/chronos-models-65f1791d630a8d57cb718444&#34;&gt;pre-trained models on HuggingFace&lt;/a&gt; so that &lt;a href=&#34;https://github.com/amazon-science/chronos-forecasting?tab=readme-ov-file#usage&#34;&gt;the installation and &amp;ldquo;Hello, world&amp;rdquo; example&lt;/a&gt; just work, and the paper is extensive at 40 pages overall. I commend the authors for using that space to include section 5.7 &amp;ldquo;Qualitative Analysis and Limitations&amp;rdquo; discussing and visualizing plenty of examples. The limitation arising from the quantization approach (Figure 16) would not have been as clear otherwise.&lt;/p&gt;&#xA;&lt;p&gt;Speaking of quantization, the approach used to tokenize time series onto a fixed vocabulary reminds me of the 2020 paper &amp;ldquo;&lt;a href=&#34;https://arxiv.org/abs/2005.10111&#34;&gt;The Effectiveness of Discretization in Forecasting&lt;/a&gt;&amp;rdquo; by Rabanser et al., a related group of (former) Amazon researchers.&lt;/p&gt;&#xA;&lt;p&gt;The large set of authors of Chronos also points to the NeurIPS 2023 paper &amp;ldquo;&lt;a href=&#34;https://arxiv.org/abs/2310.07820&#34;&gt;Large Language Models Are Zero-Shot Time Series Forecasters&lt;/a&gt;&amp;rdquo;, though the approach of letting GPT-3 or LLaMA-2 predict a sequence of numeric values directly is &lt;em&gt;very&lt;/em&gt; different.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Average Temperatures by Month Instead of Year</title>
	  <link>https://minimizeregret.com/link/2024/03/25/average-temperatures-by-month-not-year/</link>
	  <pubDate>Mon, 25 Mar 2024 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2024/03/25/average-temperatures-by-month-not-year/</guid>
	  <description>&lt;p&gt;&lt;a href=&#34;https://twitter.com/pmddomingos/status/1771992929940508795&#34;&gt;This tweet&lt;/a&gt; is a prime example for why it&amp;rsquo;s hard to analyze one signal in a time series (here, its trend) without simultaneously adjusting for its other signal components (here, its seasonality).&lt;/p&gt;&#xA;&lt;p&gt;If the tweet gets taken down, perhaps this &lt;a href=&#34;https://tech.lgbt/@wronglang/112157391033320552&#34;&gt;screenshot on Mastodon&lt;/a&gt; remains.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Stop Using Dynamic Time Warping for Business Time Series</title>
	  <link>https://minimizeregret.com/note/2023/12/12/stop-using-dtw/</link>
	  <pubDate>Tue, 12 Dec 2023 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2023/12/12/stop-using-dtw/</guid>
	  <description>&lt;p&gt;Dynamic Time Warping (DTW) is designed to reveal inherent similarity between two time series of similar scale that was obscured because the time series were shifted in time or sampled at different speeds. This makes DTW useful for time series of natural phenomena like electrocardiogram measurements or recordings of human movements, but less so for business time series such as product sales.&lt;/p&gt;&#xA;&lt;p&gt;To see why that is, let’s first refresh our intuition of DTW, to then check why DTW is not the right tool for business time series.&lt;/p&gt;&#xA;&lt;h2 id=&#34;what-dynamic-time-warping-does&#34;&gt;What Dynamic Time Warping Does&lt;/h2&gt;&#xA;&lt;p&gt;Given two time series, DTW computes aligned versions of the time series to minimize the cumulative distance between the aligned observations. The alignment procedure repeats some of the observations to reduce the resulting distance. The aligned time series end up with more observations than in the original versions, but the aligned versions still have to have the same start and end, no observation may be dropped, and the order of observations must be unchanged.&lt;/p&gt;&#xA;&lt;p&gt;But the best way to understand the alignment is to visualize it.&lt;/p&gt;&#xA;&lt;h3 id=&#34;aligning-a-shift&#34;&gt;Aligning a Shift&lt;/h3&gt;&#xA;&lt;p&gt;To start, I&amp;rsquo;ll use an example where DTW can compute a near perfect alignment: That of a cosine curve and a shifted cosine curve&amp;mdash;which is just a sine curve.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; For each of the curves, we observe 12 observations per period, and 13 observations in total.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;series_sine &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;sinpi&lt;/span&gt;((&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;series_cosine &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;cospi&lt;/span&gt;((&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/note/2023/12/12/stop-using-dtw/dtw_example_original-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;To compute the aligned versions, I use the &lt;a href=&#34;https://dynamictimewarping.github.io/r/&#34;&gt;DTW implementation of the &lt;code&gt;dtw&lt;/code&gt; package&lt;/a&gt; (also &lt;a href=&#34;https://dynamictimewarping.github.io/python/&#34;&gt;available for Python as &lt;code&gt;dtw-python&lt;/code&gt;&lt;/a&gt;) with default parameters.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;library&lt;/span&gt;(dtw)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;dtw_shift &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; dtw&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;dtw&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; series_sine, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; series_cosine)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Besides returning the distance of the aligned series, DTW produces a mapping of original series to aligned series in the form of alignment vectors &lt;code&gt;dtw_shift$index1&lt;/code&gt; and &lt;code&gt;dtw_shift$index2&lt;/code&gt;. Using those, I can visualize both the original time series and the aligned time series along with the repetitions used for alignment.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# DTW returns a vector of indices of the original observations&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# where some indices are repeated to create aligned time series&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;dtw_shift&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;index2&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;##  [1]  1  1  1  1  2  3  4  5  6  7  8  9 10 11 12 13&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# In this case, the first index is repeated thrice so that the first&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# observation appears four times in the aligned time series&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;series_cosine[dtw_shift&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;index2] &lt;span style=&#34;color:#f92672&#34;&gt;|&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;head&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;8&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;|&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;round&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## [1]  1.00  1.00  1.00  1.00  0.87  0.50  0.00 -0.50&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The plot below shows the original time series in the upper half and the aligned time series in the lower half, with the sine in orange and the cosine in black. Dashed lines indicate where observations were repeated to achieve the alignment.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/note/2023/12/12/stop-using-dtw/dtw_example_shift-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Given that the sine is a perfect shifted copy of the cosine, three-quarters of the observed period can be aligned perfectly. The first quarter of the sine and the last quarter of the cosine’s period, however, can’t be aligned and stand on their own. Their indices are mapped to the repeated observations from the other time series, respectively.&lt;/p&gt;&#xA;&lt;h3 id=&#34;aligning-speed&#34;&gt;Aligning Speed&lt;/h3&gt;&#xA;&lt;p&gt;Instead of shifting the cosine, I can sample it at a different speed (or, equivalently, observe a cosine of different frequency) to construct a different time series that can be aligned well by DTW. In that case, the required alignment is not so much a shift as it is a squeezing and stretching of the observed series.&lt;/p&gt;&#xA;&lt;p&gt;Let&amp;rsquo;s create a version of the original cosine that is twice as &amp;ldquo;fast&amp;rdquo;: In the time that we observe one period of the original cosine, we observe two periods of the fast cosine.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;series_cosine_fast &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;cospi&lt;/span&gt;((&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;dtw_fast &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; dtw&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;dtw&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; series_cosine_fast, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; series_cosine)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The resulting alignment mapping looks quite different than in the first example. Under a shift, most observations still have a one-to-one mapping after alignment. Under varying frequencies, most observations of the faster time series have to be repeated to align. Note how the first half of the fast cosine&amp;rsquo;s first period can be neatly aligned with the first half of the slow cosine&amp;rsquo;s period by repeating observations (in an ideal world exactly twice).&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/note/2023/12/12/stop-using-dtw/dtw_example_frequency-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;The kind of alignment reveals itself better when the time series are observed for more than just one or two periods. Below, for longer versions of the same series, half of the fast time series can be matched neatly with the slow cosine as we observe twice the number of periods for the fast cosine.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/note/2023/12/12/stop-using-dtw/dtw_example_frequency_long-1.png&#34;&gt;&lt;/figure&gt;&lt;h3 id=&#34;aligning-different-scales&#34;&gt;Aligning Different Scales&lt;/h3&gt;&#xA;&lt;p&gt;What’s perhaps unexpected, though, is that the alignment works only on the time dimension. Dynamic time warping will not scale the time series’ amplitude. But at the same time DTW is &lt;em&gt;not&lt;/em&gt; scale independent. This can make the alignment fairly awkward when time series have varying scales as DTW exploits the time dimension to reduce the cumulative distance of observations in the value dimension.&lt;/p&gt;&#xA;&lt;p&gt;To illustrate this, let’s take the sine and cosine from the first example but scale the sine&amp;rsquo;s amplitude by 5 and check the resulting alignment.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;series_sine_scaled &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; series_sine &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;dtw_scaled &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; dtw&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;dtw&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; series_sine_scaled, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; series_cosine)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We might expect DTW to align the two series as it did in the first example above with unscaled series. After all, the series have the same frequencies as before and the same period shift as before.&lt;/p&gt;&#xA;&lt;p&gt;This is what the alignment would look like using the alignment vectors from the first example above based on un-scaled observations. While it&amp;rsquo;s a bit hard to see due to the different scales, the observations at peak amplitude are aligned (e.g., indices 4, 10, 16) as are those at the minimum amplitude (indices 7 and 13).&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/note/2023/12/12/stop-using-dtw/dtw_example_amplitude_using_period_shift-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;But Dynamic Time Warping&amp;rsquo;s optimization procedure doesn&amp;rsquo;t actually try to identify characteristics of time series such as their period length to align them. It warps time series purely to minimize the cumulative distance between aligned observations. This &lt;em&gt;may&lt;/em&gt; lead to a result in which also the periodicity is aligned as in the first and second examples above. But that&amp;rsquo;s more by accident than by design.&lt;/p&gt;&#xA;&lt;p&gt;This is how DTW actually aligns the scaled sine and the unscaled cosine:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/note/2023/12/12/stop-using-dtw/dtw_example_amplitude-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;The change in the series&amp;rsquo; amplitude leads to a more complicated alignment: Observations at the peak amplitude of the cosine (which has the small amplitude) are repeated many times to reduce the Euclidean distance to the already high amplitude observations of the sine. Reversely, the minimum-amplitude of the sine is repeated many times to reduce the Euclidean distance to the already low amplitude observations of the cosine.&lt;/p&gt;&#xA;&lt;h2 id=&#34;dtw-is-good-at-many-things&#34;&gt;DTW Is Good at Many Things…&lt;/h2&gt;&#xA;&lt;p&gt;Dynamic time warping is great when you&amp;rsquo;re observing physical phenomena that are naturally shifted in time or at different speeds.&lt;/p&gt;&#xA;&lt;p&gt;Consider, for example, measurements taken in a medical context such as those of an electrocardiogram (ECG) that measures the electrical signals in a patient&amp;rsquo;s heart. In this context, it is helpful to align time series to identify similar heart rhythms across patients. The rhythms&amp;rsquo; periods could be aligned to check whether one patient has the same issues as another. Even for the same person a DTW can be useful to align measurements taken on different days &lt;a href=&#34;https://en.wikipedia.org/wiki/Electrocardiography#Rate_and_rhythm&#34;&gt;at different heart rates&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;data&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;aami3a&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#75715e&#34;&gt;# ECG data included in `dtw` package&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;dtw_ecg &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; dtw&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;dtw&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;as.numeric&lt;/span&gt;(aami3a)[1&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2880&lt;/span&gt;],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;as.numeric&lt;/span&gt;(aami3a)[40001&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;42880&lt;/span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/note/2023/12/12/stop-using-dtw/dtw_example_ecg-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;An application such as this one is nearly identical to the first example of the shifted cosine. And as the scale of the time series is naturally the same across the two series, the alignment works well, mapping peaks and valleys with minimal repetitions.&lt;/p&gt;&#xA;&lt;p&gt;There is also a natural interpretation to the alignment, namely that we are aligning the heart rhythm across measurements. A large distance after alignment would indicate differences in rhythms.&lt;/p&gt;&#xA;&lt;h2 id=&#34;but-comparing-sales-aint-one-of-them&#34;&gt;&amp;hellip;But Comparing Sales Ain’t One of Them&lt;/h2&gt;&#xA;&lt;p&gt;It is enticing to use Dynamic Time Warping in a business context, too. Not only does it promise a better distance metric to identify time series that are &amp;ldquo;similar&amp;rdquo; and to separate them from those that are &amp;ldquo;different&amp;rdquo;, but it also has a cool name. Who doesn&amp;rsquo;t want to &lt;em&gt;warp&lt;/em&gt; stuff?&lt;/p&gt;&#xA;&lt;p&gt;We can, for example, warp time series that count the number of times products are used for patients at a hospital per month. Not &lt;em&gt;exactly&lt;/em&gt; product sales but sharing the same characteristics. The data comes with the &lt;code&gt;expsmooth&lt;/code&gt; package.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;47772&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;sampled_series &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;sample&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;ncol&lt;/span&gt;(expsmooth&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;hospital), size &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;product_a &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;as.numeric&lt;/span&gt;(expsmooth&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;hospital[,sampled_series[1]])&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;product_b &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;as.numeric&lt;/span&gt;(expsmooth&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;hospital[,sampled_series[2]])&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;While &lt;code&gt;product_b&lt;/code&gt; is used about twice as often as &lt;code&gt;product_a&lt;/code&gt;, both products exhibit an increase in their level at around index 40 which is perhaps one characteristic that makes them similar or at least more similar compared to a series that doesn&amp;rsquo;t share this increase.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/note/2023/12/12/stop-using-dtw/hospital_products-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;However, the warped versions exhibit a lot of unwanted repetitions of observations. Given the different level of the products, this should not come as a surprise.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;dtw_products_ab &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; dtw&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;dtw&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; product_b, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; product_a)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/note/2023/12/12/stop-using-dtw/dtw_products_ab-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;We can mostly fix this by min-max scaling the series, forcing their observations onto the same range of values.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;dtw_products_ab_standardized &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; dtw&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;dtw&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (product_b &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;min&lt;/span&gt;(product_b)) &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; (&lt;span style=&#34;color:#a6e22e&#34;&gt;max&lt;/span&gt;(product_b) &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;min&lt;/span&gt;(product_b)),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (product_a &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;min&lt;/span&gt;(product_a)) &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; (&lt;span style=&#34;color:#a6e22e&#34;&gt;max&lt;/span&gt;(product_a) &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;min&lt;/span&gt;(product_a))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/note/2023/12/12/stop-using-dtw/dtw_products_ab_standardized-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;While we got rid of the unwanted warping artifacts in the form of extremely long repetitions of the same observation, the previous plot should raise some questions:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;How can we interpret the modifications for &amp;ldquo;improved&amp;rdquo; alignment?&lt;/li&gt;&#xA;&lt;li&gt;Are we aligning just noise?&lt;/li&gt;&#xA;&lt;li&gt;What does the warping offer us that we didn&amp;rsquo;t already have before?&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;If you ask me, there is no useful interpretation to the alignment modifications, because the time series already &lt;em&gt;were&lt;/em&gt; aligned. Business time series are naturally aligned by the point in time at which they are observed.&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#xA;&lt;p&gt;We also are not taking measurement of a continuous physical phenomenon that goes up and down like the electric signals in a heart. While you can double the amount of ECG measurements per second, it does not make sense to &amp;ldquo;double the amount of sales measurements per month&amp;rdquo;. And while a product can &amp;ldquo;sell faster&amp;rdquo; than another, this results in an increased amplitude not an increased frequency (or shortened period lengths).&lt;/p&gt;&#xA;&lt;p&gt;So if you apply Dynamic Time Warping to business time series, you will mostly be warping random fluctuations with the goal of reducing the cumulative distance just that tiny bit more.&lt;/p&gt;&#xA;&lt;p&gt;This holds when you use Dynamic Time Warping to derive distances for clustering of business time series, too. You might as well calculate the Euclidean distance&lt;sup id=&#34;fnref:3&#34;&gt;&lt;a href=&#34;#fn:3&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;3&lt;/a&gt;&lt;/sup&gt; on the raw time series without warping first. At least then the distance between time series will tell you that they were similar (or different) at the same point in time.&lt;/p&gt;&#xA;&lt;p&gt;In fact, if you want to cluster business time series (or train any kind of model on them), put your focus on aligning them well in the &lt;em&gt;value&lt;/em&gt; dimension by thinking carefully about how you standardize them. That makes all the difference.&lt;/p&gt;&#xA;&lt;p&gt;Before using Dynamic Time Warping, ask yourself: What kind of similarity are you looking for when comparing time series? Will there be meaning to the alignment that Dynamic Time Warping induces in your time series? And what is it that you can do after warping that you weren&amp;rsquo;t able to do before?&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;A similar example is also used by the &lt;code&gt;dtw&lt;/code&gt; package in its documentation.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:2&#34;&gt;&#xA;&lt;p&gt;Two exceptions come to mind: First, you may want to align time series of products by the first point in time at which the product was sold. But that&amp;rsquo;s straightforward without DTW. Second, you may want to align products that actually exhibit a shift in their seasonality&amp;mdash;such as when a product is heavily affected by the seasons and sold in both the northern and southern hemisphere. DTW might be useful for this, but there might also be easier ways to accomplish it.&amp;#160;&lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:3&#34;&gt;&#xA;&lt;p&gt;Or Manhattan distance, or any other distance.&amp;#160;&lt;a href=&#34;#fnref:3&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>Comes with Anomaly Detection Included</title>
	  <link>https://minimizeregret.com/note/2023/12/03/anomaly-detection-included/</link>
	  <pubDate>Sun, 03 Dec 2023 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2023/12/03/anomaly-detection-included/</guid>
	  <description>&lt;p&gt;A powerful pattern in forecasting is that of model-based anomaly detection &lt;em&gt;during&lt;/em&gt; model training. It exploits the inherently iterative nature of forecasting models and goes something like this:&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;Train your model up to time step &lt;code&gt;t&lt;/code&gt; based on data &lt;code&gt;[1,t-1]&lt;/code&gt;&lt;/li&gt;&#xA;&lt;li&gt;Predict the forecast distibution at time step &lt;code&gt;t&lt;/code&gt;&lt;/li&gt;&#xA;&lt;li&gt;Compare the observed value against the predicted distribution at step &lt;code&gt;t&lt;/code&gt;; flag the observation as anomaly if it is in the very tail of the distribution&lt;/li&gt;&#xA;&lt;li&gt;Don&amp;rsquo;t update the model&amp;rsquo;s state based on the anomalous observation&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;For another description of this idea, see, for example, &lt;a href=&#34;https://arxiv.org/abs/1906.05264&#34;&gt;Alexandrov et al. (2019)&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Importantly, you use your model to detect anomalies &lt;em&gt;during&lt;/em&gt; training and not &lt;em&gt;after&lt;/em&gt; training, thereby ensuring its state and forecast distribution are not corrupted by the anomalies.&lt;/p&gt;&#xA;&lt;p&gt;The beauty of this approach is that&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;the forecast distribution can properly reflect the impact of anomalies, and&lt;/li&gt;&#xA;&lt;li&gt;you don&amp;rsquo;t require a separate anomaly detection method with failure modes different from your model&amp;rsquo;s.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;In contrast, standalone anomaly detection approaches would first have to solve the handling of trends and seasonalities themselves, too, before they could begin to detect any anomalous observation. So why not use the model you already trust with predicting your data to identify observations that don&amp;rsquo;t make sense to it?&lt;/p&gt;&#xA;&lt;p&gt;This approach can be expensive if your model doesn&amp;rsquo;t train iteratively over observations in the first place. Many forecasting models&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; do, however, making this a fairly negligiable addition.&lt;/p&gt;&#xA;&lt;p&gt;But enough introduction, let&amp;rsquo;s see it in action. First, I construct a simple time series &lt;code&gt;y&lt;/code&gt; of monthly observations with yearly seasonality.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;472&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;x &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;80&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;y &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;pmax&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;sinpi&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;25&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;sqrt&lt;/span&gt;(x) &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rnorm&lt;/span&gt;(n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;80&lt;/span&gt;, sd &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Insert anomalous observations that need to be detected&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;y&lt;span style=&#34;color:#a6e22e&#34;&gt;[c&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;55&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;56&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;70&lt;/span&gt;)] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; y&lt;span style=&#34;color:#a6e22e&#34;&gt;[c&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;55&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;56&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;70&lt;/span&gt;)]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To illustrate the method, I&amp;rsquo;m going to use a simple probabilistic variant of the Seasonal Naive method where the forecast distribution is assumed to be Normal with zero mean. Only the $\sigma$ parameter needs to be fitted, which I do using the standard deviation of the forecast residuals.&lt;/p&gt;&#xA;&lt;p&gt;The estimation of the $\sigma$ parameter occurs in lockstep with the detection of anomalies. Let&amp;rsquo;s first define a data frame that holds the observations and will store a cleaned version of the observations, the fitted $\sigma$ and detected anomalies&amp;hellip;&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;df &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;data.frame&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; y,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y_cleaned &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; y,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  forecast &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;NA_real_&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  residual &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;NA_real_&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  sigma &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;NA_real_&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  is_anomaly &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;FALSE&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&amp;hellip; and then let&amp;rsquo;s loop over the observations.&lt;/p&gt;&#xA;&lt;p&gt;At each iteration, I first predict the current observation given the past and update the forecast distribution by calculating the standard deviation over all residuals that are available so far, before calculating the latest residual.&lt;/p&gt;&#xA;&lt;p&gt;If the latest residual is in the tails of the current forecast distribution (i.e., larger than multiples of the standard deviation), the observation is flagged as anomalous.&lt;/p&gt;&#xA;&lt;p&gt;For time steps with anomalous observations, I update the cleaned time series with the forecasted value (which informs a later forecast at step &lt;code&gt;t+12&lt;/code&gt;) and set the residual to missing to keep the anomalous observation from distorting the forecast distribution.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Loop starts when first prediction from Seasonal Naive is possible&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; (t &lt;span style=&#34;color:#66d9ef&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;13&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;nrow&lt;/span&gt;(df)) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;forecast[t] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;y_cleaned[t &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;sigma[t] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;sd&lt;/span&gt;(df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;residual, na.rm &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;TRUE&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;residual[t] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;y[t] &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;forecast[t]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (t &lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;25&lt;/span&gt;) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#75715e&#34;&gt;# Collect 12 residuals before starting anomaly detection&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;is_anomaly[t] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;abs&lt;/span&gt;(df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;residual[t]) &lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;sigma[t]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;is_anomaly[t]) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;y_cleaned[t] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;forecast[t]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;residual[t] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;NA_real_&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Note that I decide to start the anomaly detection not before there are 12 residuals for one full seasonal period, as the $\sigma$ estimate based on less than a handful of observations can be flaky.&lt;/p&gt;&#xA;&lt;p&gt;In a plot of the results, the combination of 1-step-ahead prediction and forecast distribution is used to distinguish between expected and anomalous observations, with the decision boundary indicated by the orange ribbon. At time steps where the observed value falls outside the ribbon, the orange line indicates the model prediction that is used to inform the model&amp;rsquo;s state going forward in place of the anomaly.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/note/2023/12/03/anomaly-detection-included/plot_anomaly_detection-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Note how the prediction at time &lt;code&gt;t&lt;/code&gt; is not affected by the anomaly at time step &lt;code&gt;t-12&lt;/code&gt;. Neither is the forecast distribution estimate.&lt;/p&gt;&#xA;&lt;p&gt;This would look very different when one gets the update behavior slightly wrong. For example, the following implementation of the loop detects the first anomaly in the same way, but uses it to update the model&amp;rsquo;s state, leading to subsequently poor predictions and false positives, and fails to detect later anomalies.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Loop starts when first prediction from Seasonal Naive is possible&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; (t &lt;span style=&#34;color:#66d9ef&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;13&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;nrow&lt;/span&gt;(df)) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;forecast[t] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;y[t &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;sigma[t] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;sd&lt;/span&gt;(df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;residual, na.rm &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;TRUE&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;residual[t] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;y[t] &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;forecast[t]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (t &lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;25&lt;/span&gt;) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#75715e&#34;&gt;# Collect 12 residuals before starting anomaly detection&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;is_anomaly[t] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;abs&lt;/span&gt;(df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;residual[t]) &lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;sigma[t]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;is_anomaly[t]) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;y_cleaned[t] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;forecast[t]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/note/2023/12/03/anomaly-detection-included/plot_anomaly_detection_wrong-1.png&#34;&gt;&lt;/figure&gt;&lt;h2 id=&#34;what-about-structural-breaks&#34;&gt;What about structural breaks?&lt;/h2&gt;&#xA;&lt;p&gt;While anomaly detection during training can work well, it may fail spectacularly if an anomaly is not an anomaly but the start of a structural break. Since structural breaks make the time series look different than it did before, chances are the first observation after the structural break will be flagged as anomaly. Then so will be the second. And then the third. And so on, until all observations after the structural break are treated as anomalies because the model never starts to adapt to the new state.&lt;/p&gt;&#xA;&lt;p&gt;This is particularly frustrating because the Seasonal Naive method is robust against certain structural breaks that occur in the training period. Adding anomaly detection makes it vulnerable.&lt;/p&gt;&#xA;&lt;h2 id=&#34;what-values-to-use-for-the-final-forecast-distribution&#34;&gt;What values to use for the final forecast distribution?&lt;/h2&gt;&#xA;&lt;p&gt;Let&amp;rsquo;s get philosophical for a second. What &lt;em&gt;are&lt;/em&gt; anomalies?&lt;/p&gt;&#xA;&lt;p&gt;Ideally, they reflect a weird measurement that will never occur again. Or if it does, it&amp;rsquo;s another wrong &lt;em&gt;measurement&lt;/em&gt;&amp;mdash;but not the true &lt;em&gt;state&lt;/em&gt; of the measured phenomenon. In that case, let&amp;rsquo;s drop the anomalies and ignore them in the forecast distribution.&lt;/p&gt;&#xA;&lt;p&gt;But what if the anomalies are weird &lt;em&gt;states&lt;/em&gt; that the measured phenomenon can end up in? For example, demand for subway rides after a popular concert. While perhaps an irregular and rare event, a similar event may occur again in the future. Do we want to exclude that possibility from our predictions about the future? What if the mention of a book on TikTok let&amp;rsquo;s sales spike? Drop the observation and assume it will not repeat? Perhaps unrealistic.&lt;/p&gt;&#xA;&lt;p&gt;It depends on your context. In a business context, where measurement errors might be less of an issue, anomalies might need to be &lt;em&gt;modeled&lt;/em&gt;, not &lt;em&gt;excluded&lt;/em&gt;.&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;Notably models from the state-space model family.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>Code Responsibly</title>
	  <link>https://minimizeregret.com/note/2023/11/12/code-responsibly/</link>
	  <pubDate>Sun, 12 Nov 2023 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2023/11/12/code-responsibly/</guid>
	  <description>&lt;p&gt;There exists this comparison of software &lt;em&gt;before&lt;/em&gt; and software &lt;em&gt;after&lt;/em&gt; machine learning.&lt;/p&gt;&#xA;&lt;p&gt;Before machine learning, code was deterministic: Software engineers wrote code, the code included conditions with fixed thresholds, and at least in theory the program was entirely understandable.&lt;/p&gt;&#xA;&lt;p&gt;After machine learning, code is no longer deterministic. Instead of software engineers instantiating it, the program’s logic is determined by a model and its parameters. Those parameters are not artisinally chosen by a software engineer but learned from data. The program becomes a function of data, and in some cases incomprehensible to the engineer due to the sheer number of parameters.&lt;/p&gt;&#xA;&lt;p&gt;Given the current urge to regulate AI and making its use responsible and trustworthy, humans appear to expect machine learning models to introduce an obscene number of bugs into software. Perhaps humans underestimate the ways in which human programmers can mess up.&lt;/p&gt;&#xA;&lt;p&gt;For example, when I hear &lt;em&gt;Regulate AI&lt;/em&gt;, all I can think is &lt;em&gt;Have you seen this stuff&lt;/em&gt;? By &lt;a href=&#34;https://algorithmwatch.org/en/algorithm-school-system-italy/&#34;&gt;Pierluigi Bizzini for Algorithm Watch&lt;/a&gt;&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; (emphasis mine):&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;The algorithm evaluates teachers&amp;rsquo; CVs and cross-references their preferences for location and class with schools&amp;rsquo; vacancies. If there is a match, a provisional assignment is triggered, but the algorithm continues to assess other candidates. If it finds another matching candidate with a higher score, that second candidate moves into the lead. The process continues until the algorithm has assessed all potential matches and selected the best possible candidate for the role.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;[…] [E]rrors have caused much confusion, leaving many teachers unemployed and therefore without a salary. Why did such errors occur?&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;When the algorithm finds an ideal candidate for a position, &lt;strong&gt;it does not reset the list of remaining candidates&lt;/strong&gt; before commencing the search to fill the next vacancy. Thus, those candidates who missed out on the first role that matched their preferences are definitively discarded from the pool of available teachers, with no possibility of employment. The algorithm classes those discarded teachers as “drop-outs”, ignoring the possibility of matching them with new vacancies.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;This is not AI gone rogue. This is just a flawed human-written algorithm. At least Algorithm Watch is aptly named &lt;em&gt;Algorithm&lt;/em&gt; Watch.&lt;/p&gt;&#xA;&lt;p&gt;Algorithms existed before AI.&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt; But there was no outcry, no regulation of algorithms before AI, no “&lt;a href=&#34;https://digital-strategy.ec.europa.eu/en/library/proposal-regulation-laying-down-harmonised-rules-artificial-intelligence&#34;&gt;Proposal for a Regulation laying down harmonised rules on artificial intelligence&lt;/a&gt;”. Except there actually &lt;em&gt;is&lt;/em&gt; regulation in aviation and medical devices and such.&lt;sup id=&#34;fnref:3&#34;&gt;&lt;a href=&#34;#fn:3&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;3&lt;/a&gt;&lt;/sup&gt; Perhaps because of the extent to which these fields are entangled with hardware, posing unmediated danger to human lives.&lt;/p&gt;&#xA;&lt;p&gt;Machine learning and an increased prowess in data processing have not introduced more bugs into software compared to software written by humans previously. What they have done is to enable applications for software that were previously infeasible by way of processing and generating data.&lt;/p&gt;&#xA;&lt;p&gt;Some of these applications are… not great. They should never be done. Regulate those applications. By all means, prohibit them. If a use case poses unacceptable risks, when we can’t tolerate any bugs but bugs are always a possibility, then let’s just not do it.&lt;/p&gt;&#xA;&lt;p&gt;Other applications are high-risk, high-reward. Given large amounts of testing imposed by regulation, we probably want software to enable these applications. The aforementioned aviation and medical devices come to mind.&lt;sup id=&#34;fnref:4&#34;&gt;&lt;a href=&#34;#fn:4&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;4&lt;/a&gt;&lt;/sup&gt; Living with regulated software is nothing new!&lt;/p&gt;&#xA;&lt;p&gt;Then there is the rest that doesn&amp;rsquo;t really harm anyone where people can do whatever.&lt;/p&gt;&#xA;&lt;p&gt;Regulating software in the context of specific use cases is feasible and has precedents.&lt;/p&gt;&#xA;&lt;p&gt;Regulating AI is awkward. Where does the if-else end and AI start? Instead, consider it part of software as a whole and ask in which cases software might have flaws we are unwilling to accept.&lt;/p&gt;&#xA;&lt;p&gt;We need responsible software, not just responsible AI.&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://bayes.club/@timradtke/111087876472984305&#34;&gt;I tried to find sources&lt;/a&gt; for the description of the bug provided in the article, but couldn’t find any. I don’t know where the author takes them from, so take this example with the necessary grain of salt.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:2&#34;&gt;&#xA;&lt;p&gt;Also, much of what were algorithms or statistics a few years ago are now labeled as AI. And large parts of AI systems are just if statements and for loops and databases and networking.&amp;#160;&lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:3&#34;&gt;&#xA;&lt;p&gt;Regulation that the EU regulation of artificial intelligence very much builds upon. See “Demystifying the Draft EU Artificial Intelligence Act” (&lt;a href=&#34;https://arxiv.org/abs/2107.03721&#34;&gt;https://arxiv.org/abs/2107.03721&lt;/a&gt;) for more on the connection to the “&lt;a href=&#34;https://single-market-economy.ec.europa.eu/single-market/goods/new-legislative-framework_en&#34;&gt;New Legislative Framework&lt;/a&gt;”.&amp;#160;&lt;a href=&#34;#fnref:3&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:4&#34;&gt;&#xA;&lt;p&gt;Perhaps this is the category for autonomous driving?&amp;#160;&lt;a href=&#34;#fnref:4&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>A Flexible Model Family of Simple Forecast Methods</title>
	  <link>https://minimizeregret.com/post/2023/10/19/threedx/</link>
	  <pubDate>Thu, 19 Oct 2023 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2023/10/19/threedx/</guid>
	  <description>&lt;p&gt;&lt;em&gt;Introducing a flexible model family that interpolates between simple forecast methods to produce interpretable probabilistic forecasts of seasonal data by weighting past observations.&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;In business forecasting applications for &lt;a href=&#34;https://forecasters.org/wp-content/uploads/gravity_forms/7-c6dd08fee7f0065037affb5b74fec20a/2017/07/Januschowski_Tim_ISF2017.pdf&#34;&gt;operational decisions&lt;/a&gt;, simple approaches are hard-to-beat and provide robust expectations that can be relied upon for short- to medium-term decisions. They&amp;rsquo;re often better at recovering from structural breaks or modeling seasonal peaks than more complicated models, and they don&amp;rsquo;t overfit unrealistic trends.&lt;/p&gt;&#xA;&lt;p&gt;If that wasn&amp;rsquo;t enough, they&amp;rsquo;re interpretable. Not just explainable, but &lt;a href=&#34;https://arxiv.org/abs/1811.10154&#34;&gt;interpretable&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Still, when faced with thousands or millions of time series, not all of them will be modeled best by the naive method, nor will all be modeled best by the seasonal mean. You&amp;rsquo;d still like to pick a method that fits the series. Similarly, the all-or-nothing nature of approaches such as the seasonal naive method can be a bit black-or-white. A bit more smoothness and impact from multiple past observations can be helpful as long as we don&amp;rsquo;t compromise in robustness.&lt;/p&gt;&#xA;&lt;h2 id=&#34;assigning-weights-over-time&#34;&gt;Assigning Weights Over Time&lt;/h2&gt;&#xA;&lt;p&gt;A bit more smoothness is what &lt;a href=&#34;https://otexts.com/fpp3/ses.html&#34;&gt;simple exponential smoothing&lt;/a&gt; offers for the mean and naive forecast methods: As you vary the single $\alpha$ parameter between 0 and 1, simple exponential smoothing will move from the mean forecast to the naive forecast, in between assigning exponentially decreasing weights to past observations. The prediction becomes the weighted average of the past observations. The mean forecast uses the same weight for all observations, whereas the naive method assigns all weight to the most recent observation.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2023/10/19/threedx/threedx_mean_ses_naive.png&#34; alt=&#34;While the mean forecast assigns the same weight to all observations and the naive method assigns all weight to the last observation, Simple Exponential Smoothing interpolates between the two by assigning exponentially decaying weights to past observations.&#34;&gt;&lt;figcaption&gt;While the mean forecast assigns the same weight to all observations and the naive method assigns all weight to the last observation, Simple Exponential Smoothing interpolates between the two by assigning exponentially decaying weights to past observations.&lt;/figcaption&gt;&lt;/figure&gt;&lt;p&gt;In many applications, however, using only simple exponential smoothing could never suffice. A seasonal component in monthly or daily data is often the main signal that needs to be modeled and projected into the future. Not only are more recent observations more representative of the future, but so are observations that are close in the seasonal period: October 2023 is going to be more similar to September 2022 than to May 2023. Wednesday is more similar to Wednesday last week than to Monday.&lt;/p&gt;&#xA;&lt;p&gt;This motivates two more ways of weighting past observations.&lt;/p&gt;&#xA;&lt;h2 id=&#34;assigning-weights-within-and-across-seasonal-periods&#34;&gt;Assigning Weights Within and Across Seasonal Periods&lt;/h2&gt;&#xA;&lt;p&gt;First, observations can be weighted &lt;em&gt;within&lt;/em&gt; a seasonal period. Due to seasonality, the observation exactly a year ago or exactly a week ago should get a larger weight than the observation 11 months ago or two days ago.&lt;/p&gt;&#xA;&lt;p&gt;Second, observations can be weighted &lt;em&gt;across&lt;/em&gt; seasonal periods, assigning more weight to the most recent period than to the period before that and the one before that.&lt;/p&gt;&#xA;&lt;p&gt;As before, exponential weights make for a useful weighting structure. When assigning weights &lt;em&gt;within&lt;/em&gt; a period, each period gets the same weight. When assigning weights &lt;em&gt;across&lt;/em&gt; periods, each observation within a period gets the same weight.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2023/10/19/threedx/threedx_across_within.png&#34; alt=&#34;The within-period and across-period weighting schemes apply exponential weights, similar to Simple Exponential Smoothing, to model a time series’ seasonal component.&#34;&gt;&lt;figcaption&gt;The *within*-period and *across*-period weighting schemes apply exponential weights, similar to Simple Exponential Smoothing, to model a time series&amp;#39; seasonal component.&lt;/figcaption&gt;&lt;/figure&gt;&lt;p&gt;Note how both the &lt;em&gt;within&lt;/em&gt; and the &lt;em&gt;across&lt;/em&gt; period weighting schemes are each controlled by a single parameter if we give them a similar functional form as used in simple exponential smoothing. The picture above illustrates this by using the familiar $\alpha (1 - \alpha)^t$ notation.&lt;/p&gt;&#xA;&lt;p&gt;As we vary the parameter from 0 to 1 for &lt;em&gt;within&lt;/em&gt;-period weights, the same weight will be assigned to all observations in the period, then shifts away from observations that are currently in the middle of the period towards the edges, until all weight is assigned to the observation that&amp;rsquo;s in the same step of the period as the future observation.&lt;/p&gt;&#xA;&lt;p&gt;For &lt;em&gt;across&lt;/em&gt;-period weights, at first the same weight will be assigned to all periods, to then be exponentially decreased for past periods, until all weight will be assigned to just the most recent period.&lt;/p&gt;&#xA;&lt;h2 id=&#34;assigning-weights-over-time-and-seasonal-periods&#34;&gt;Assigning Weights Over Time and Seasonal Periods&lt;/h2&gt;&#xA;&lt;p&gt;At this point we have three ways of assigning weights to past observations to move smoothly between forecast methods, namely using&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;exponential weights that decrease over time controlled by a parameter $\alpha$&lt;/li&gt;&#xA;&lt;li&gt;seasonal within-period exponential weights controlled by a parameter $\alpha_s$, and&lt;/li&gt;&#xA;&lt;li&gt;seasonal across-period exponential weights controlled by a parameter $\alpha_{sd}$.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Combining all three weighting schemes through multiplication&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; provides a powerful weight structure that depends on only three parameters and captures most signal sources in time series of operational business applications.&lt;/p&gt;&#xA;&lt;p&gt;The picture below illustrates the complex weight pattern that this combination can achieve, smoothly assigning weights to observations close in season and time, and then decreasing from there.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2023/10/19/threedx/threedx_product.png&#34; alt=&#34;Achieve general weight patterns that fit various kinds of time series by combining all three exponential weight schemes through multiplication and subsequent re-standardization.&#34;&gt;&lt;figcaption&gt;Achieve general weight patterns that fit various kinds of time series by combining all three exponential weight schemes through multiplication and subsequent re-standardization.&lt;/figcaption&gt;&lt;/figure&gt;&lt;p&gt;This three-dimensional exponential weighting structure captures the most important simple forecast methods as special cases. To make precise which set of parameters leads to which special case, let&amp;rsquo;s define a specific set of weights by the triplet $(\alpha, \alpha_{s}, \alpha_{sd})$.&lt;/p&gt;&#xA;&lt;p&gt;Then, taking the average of weighted observations recovers&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;the Naive method when all weight is assigned to the most recent observation $(1, 0, 0)$&lt;/li&gt;&#xA;&lt;li&gt;the Mean method when equal weight is assigned to all observations $(0, 0, 0)$,&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;but due to the additional seasonal structures it now also recovers&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;the Seasonal Naive method when all weight is assigned to the observation a year ago $(0, 1, 1)$&lt;/li&gt;&#xA;&lt;li&gt;the Seasonal Mean method when equal weight is assigned to only the same months in the past $(0, 1, 0)$&lt;/li&gt;&#xA;&lt;li&gt;the Latest Period Mean method when equal weight is assigned to only observations from the twelve most recent months $(0, 0, 1)$.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;The three latter cases are illustrated below.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2023/10/19/threedx/threedx_seasonal_special_cases.png&#34; alt=&#34;The seasonal naive method, the seasonal mean method, and the latest period mean method are additional special cases of the general model family that are found at the edge of the parameter space.&#34;&gt;&lt;figcaption&gt;The seasonal naive method, the seasonal mean method, and the latest period mean method are additional special cases of the general model family that are found at the edge of the parameter space.&lt;/figcaption&gt;&lt;/figure&gt;&lt;h2 id=&#34;characteristics-of-the-model-family&#34;&gt;Characteristics of the Model Family&lt;/h2&gt;&#xA;&lt;p&gt;What&amp;rsquo;s neat about this generalization of simple forecast methods is how the models built from the three parameter structure maintain the advantages of the simple methods that are found at the edges of the parameter space.&lt;/p&gt;&#xA;&lt;p&gt;The reason for this is that we only apply weights to past observations in order to derive weighted average predictions.&lt;/p&gt;&#xA;&lt;p&gt;For one, this implies that the models are robust in that they won&amp;rsquo;t predict outside the observed range of observations. In fact, you can think of them as regularized, stationary infinite-order AR models. While this is limiting when time series have sustained trends as their dominating signal, it also means that models won&amp;rsquo;t project a trend into the future by mistake. Depending on your data and the level of automation, this can be preferable.&lt;/p&gt;&#xA;&lt;p&gt;For another, any model in this family of models is perfectly interpretable. In fact, all you need to understand how a prediction is formed is to look at the weights allocated to past observations. It doesn&amp;rsquo;t get more interpretable than that. Even ETS models are hard to understand in comparison: Which observations made us predict that trend into the future again?&lt;/p&gt;&#xA;&lt;p&gt;Finally, the three-dimensional exponential weights structure enables a range of possible adaptations.&lt;/p&gt;&#xA;&lt;p&gt;For example, instead of creating probabilistic sample paths from a parametric distribution fitted to residuals (as is usual for the simple forecast methods mentioned before, as well as ARIMA and ETS, but also DeepAR in a sense), sample paths can also be drawn as a non-parametric bootstrap from the weighted observations, which can be helpful for intermittent and low-count data. This has been described in Gasthaus (2016) and &lt;a href=&#34;https://arxiv.org/abs/1906.05264&#34;&gt;Alexandrov et al. (2019)&lt;/a&gt; for the Non-Parametric Time Series Forecaster (NPTS) that is also &lt;a href=&#34;https://docs.aws.amazon.com/forecast/latest/dg/aws-forecast-recipe-npts.html&#34;&gt;available in AWS Forecast&lt;/a&gt; and &lt;a href=&#34;https://ts.gluon.ai/stable/api/gluonts/gluonts.model.npts.html&#34;&gt;GluonTS&lt;/a&gt;. In fact, the NPTS model can be described by the $(\alpha, 0, 0)$ and $(0, 1, \alpha)$ model specifications, depending on whether the non-seasonal or seasonal version is used.&lt;/p&gt;&#xA;&lt;p&gt;Also, due to the parameter space&amp;rsquo;s simplicity, even brute force evaluation of parameter combinations is feasible to fit models. While a bit slower, it allows the use of &lt;em&gt;any&lt;/em&gt; kind of loss function when fitting the model.&lt;/p&gt;&#xA;&lt;h2 id=&#34;implementation-of-the-model-family-in-threedx&#34;&gt;Implementation of the Model Family in Threedx&lt;/h2&gt;&#xA;&lt;p&gt;Have a look at the &lt;a href=&#34;https://github.com/timradtke/threedxr&#34;&gt;&lt;code&gt;threedx&lt;/code&gt; repository&lt;/a&gt; for an implementation and check out the &lt;a href=&#34;https://timradtke.github.io/threedxr&#34;&gt;corresponding website&lt;/a&gt; for more details and examples of how this model family based on weighting past observations can be used to forecast time series.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;library&lt;/span&gt;(threedx)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;y &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rpois&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;55&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  lambda &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;pmax&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;0.1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;sinpi&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;59&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;model &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; threedx&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;learn_weights&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; y,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  period_length &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;12L&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  alphas_grid &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; threedx&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;list_sampled_alphas&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;n_target &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1000L&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;include_edge_cases &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;TRUE&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  loss_function &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; loss_mae&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;forecast &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;predict&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  object &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; model,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  horizon &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;12L&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  n_samples &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2500L&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  observation_driven &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;TRUE&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;autoplot&lt;/span&gt;(forecast)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2023/10/19/threedx/threedx_example_forecast-1.png&#34;&gt;&lt;/figure&gt;&lt;h2 id=&#34;references&#34;&gt;References&lt;/h2&gt;&#xA;&lt;p&gt;Alexander Alexandrov, Konstantinos Benidis, Michael Bohlke-Schneider, Valentin Flunkert, Jan Gasthaus, Tim Januschowski, Danielle C. Maddix, Syama Rangapuram, David Salinas, Jasper Schulz, Lorenzo Stella, Ali Caner Türkmen, Yuyang Wang (2019). &lt;em&gt;GluonTS: Probabilistic Time Series Models in Python&lt;/em&gt;. &lt;a href=&#34;https://arxiv.org/abs/1906.05264&#34;&gt;https://arxiv.org/abs/1906.05264&lt;/a&gt;&lt;/p&gt;&#xA;&lt;p&gt;Jan Gasthaus (2016). &lt;em&gt;Non-parametric time series forecaster&lt;/em&gt;. Technical report, Amazon, 2016.&lt;/p&gt;&#xA;&lt;p&gt;Rob J. Hyndman, Anne B. Koehler, Ralph D. Snyder, and Simone Grose (2002). &lt;em&gt;A State Space Framework for Automatic Forecasting using Exponential Smoothing Methods&lt;/em&gt;. &lt;a href=&#34;https://doi.org/10.1016/S0169-2070(01)00110-8&#34;&gt;https://doi.org/10.1016/S0169-2070(01)00110-8&lt;/a&gt;&lt;/p&gt;&#xA;&lt;p&gt;Vincent Warmerdam (2018). &lt;em&gt;Winning with Simple, even Linear, Models&lt;/em&gt;. PyData London 2018. &lt;a href=&#34;https://youtu.be/68ABAU_V8qI?feature=shared&amp;amp;t=286&#34;&gt;https://youtu.be/68ABAU_V8qI?feature=shared&amp;amp;t=286&lt;/a&gt;&lt;/p&gt;&#xA;&lt;p&gt;P. R. Winters (1960). &lt;em&gt;Forecasting Sales by Exponentially Weighted Moving Averages&lt;/em&gt;. &lt;a href=&#34;https://doi.org/10.1287/mnsc.6.3.324&#34;&gt;https://doi.org/10.1287/mnsc.6.3.324&lt;/a&gt;&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;And, of course, subsequent standardization so they continue to sum to 1.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>Video: Tim Januschowski, ISF 2023 Practitioner Speaker</title>
	  <link>https://minimizeregret.com/link/2023/10/05/isf-2023-practitioner-speaker-video/</link>
	  <pubDate>Thu, 05 Oct 2023 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2023/10/05/isf-2023-practitioner-speaker-video/</guid>
	  <description>&lt;p&gt;We don&amp;rsquo;t have enough presentations of industry practitioners discussing the detailed business problems they&amp;rsquo;re addressing and what solutions and trade-offs they were able to implement. Tim Januschoswki did just that, though, in &lt;a href=&#34;https://www.youtube.com/watch?v=X-f1G3qHb08&#34;&gt;his presentation at the International Symposium on Forecasting 2023&lt;/a&gt;. He discusses demand forecasting for optimal pricing at Zalando.&lt;/p&gt;&#xA;&lt;p&gt;Presentations such as this one are rare opportunites to peak at the design of real world solutions. My &lt;a href=&#34;https://youtu.be/X-f1G3qHb08?feature=shared&amp;amp;t=693&#34;&gt;favorite quote&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;What we&amp;rsquo;re not using that might be also interesting is weather data. My business counterparts, they always, you know, make me aware of that fact. But we&amp;rsquo;re not using it.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;</description>
	</item>
	<item>
	  <title>&#39;ECB Must Accept Forecasting Limitations to Restore Trust&#39;</title>
	  <link>https://minimizeregret.com/link/2023/09/11/ecb-forecasts/</link>
	  <pubDate>Mon, 11 Sep 2023 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2023/09/11/ecb-forecasts/</guid>
	  <description>&lt;p&gt;Christine Lagarde, president of the European Central Bank, declared her intent to communicate the shortcomings of the ECB&amp;rsquo;s forecasts better&amp;mdash;and in doing so, provides applied data science lessons for the rest of us. As &lt;a href=&#34;https://www.ft.com/content/afe704e3-291c-4a06-8106-b235955970e2&#34;&gt;quoted by the Financial Times&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;&amp;ldquo;Even if these [forecast] errors were to deplete trust, we can mitigate this if we talk about forecasts in a way that is both more contingent and more accessible, and if we provide better explanations for those errors,&amp;rdquo; Lagarde said.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;</description>
	</item>
	<item>
	  <title>In Search of Verifiability: Explanations Rarely Enable Complementary Performance in AI-Advised Decision Making</title>
	  <link>https://minimizeregret.com/link/2023/05/31/in-search-of-verifiability/</link>
	  <pubDate>Wed, 31 May 2023 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2023/05/31/in-search-of-verifiability/</guid>
	  <description>&lt;p&gt;Raymond Fok and Daniel S. Weld in a recent &lt;a href=&#34;https://arxiv.org/abs/2305.07722&#34;&gt;Arxiv preprint&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;We argue explanations are only useful to the extent that they allow a human decision maker to verify the correctness of an AI&amp;rsquo;s prediction, in contrast to other desiderata, e.g., interpretability or spelling out the AI&amp;rsquo;s reasoning process.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;This does ring true to me: Put yourself into the position of an employee of Big Company Inc. whose task it is to allocate marketing budgets, to purchase product inventory, or to perform any other monetary decision as part of a business process. Her dashboard, powered by a data pipeline and machine learning model, suggests to increase TV ad spend in channel &lt;em&gt;XYZ&lt;/em&gt;, or to order thousands of units of a seasonal product to cover the summer.&lt;/p&gt;&#xA;&lt;p&gt;In her shoes, if you had to sign the check, what let&amp;rsquo;s you sleep at night: Knowing the model&amp;rsquo;s feature importances, or having verified the prediction&amp;rsquo;s correctness?&lt;/p&gt;&#xA;&lt;p&gt;I&amp;rsquo;d prefer the latter, and the former only so much as it helps in the pursuit of verification. Feature importance alone, it is argued however, can&amp;rsquo;t determine correctness:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Here, we refer to &lt;em&gt;verification&lt;/em&gt; of an answer as the process of determining its correctness. It follows that many AI explanations fundamentally cannot satisfy this desideratum [&amp;hellip;] While feature importance explanations may provide some indication of how much each feature influenced the AI’s decision, they typically do not allow a decision maker to verify the AI’s recommendation.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;We want verifiability, but we &lt;em&gt;cannot&lt;/em&gt; have it for most relevant supervised learning problems. The number of viewers of the TV ad are inherently unknown at prediction time, as is the demand for the seasonal product. These applications are in stark contrast to the maze example the authors provide, in which the explanation method draws the proposed path through the maze.&lt;/p&gt;&#xA;&lt;p&gt;If verifiability is needed to complement human decision making, then this might be why one can get the impression of &lt;a href=&#34;https://hci.social/@upol/110397476968179709&#34;&gt;explanation washing&lt;/a&gt; of machine learning systems: While current explanation methods are the best we can do, they fall short of what is really needed to trust a system&amp;rsquo;s recommendation.&lt;/p&gt;&#xA;&lt;p&gt;What can we do instead? We could start by showing the actual data alongside the recommendation. &lt;a href=&#34;http://worrydream.com/ExplorableExplanations/&#34;&gt;Making the data explorable&lt;/a&gt;. The observation in question can be put into the context of observations from the training data for which labels exist, essentially providing &lt;a href=&#34;https://arxiv.org/abs/2106.02605&#34;&gt;case-based explanations&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Ideally, any context provided to the model&amp;rsquo;s recommendation is &lt;em&gt;not&lt;/em&gt; based on another model that adds another layer to be verified, but on hard actuals.&lt;/p&gt;&#xA;&lt;p&gt;In the case of forecasting, simply visualizing the forecast alongside the historical observations can be extremely effective at establishing trust. When the time series is stable and shows clear patterns, a human actually &lt;em&gt;can&lt;/em&gt; verify the the forecast&amp;rsquo;s correctness up to a point. And a human easily &lt;a href=&#34;https://minimizeregret.com/note/2023/05/20/two-by-two-of-forecasting&#34;&gt;spots likely incorrect forecasts&lt;/a&gt; given historical data.&lt;/p&gt;&#xA;&lt;p&gt;The need for verifiability makes me believe in building data products, not just a model.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>A Framework for Data Product Management for Increasing Adoption &amp; User Love</title>
	  <link>https://minimizeregret.com/link/2023/05/29/saporta-data-product-success/</link>
	  <pubDate>Mon, 29 May 2023 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2023/05/29/saporta-data-product-success/</guid>
	  <description>&lt;p&gt;You might have heard this one before: To build successful data products, focus on the decisions your customers make. But when was the last time you considered &amp;ldquo;how your work get[s] converted into action&amp;rdquo;?&lt;/p&gt;&#xA;&lt;p&gt;At Data Council 2023, Rick Saporta lays out a framework of what data products to build and how to make them successful with customers. He goes beyond the platitudes, his advice sounds hard-earned.&lt;/p&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://www.datacouncil.ai/hubfs/Data%20Council/slides/austin23/Lightning_6_RickSaporta_Data%20Product%20Success%20_%20Ricky%20Saporta%20_%2015%20Min%20Version%20_%202023-03-01.pdf&#34;&gt;Slides&lt;/a&gt; are good, &lt;a href=&#34;https://www.youtube.com/watch?v=ZQ4kz-v-LPk&#34;&gt;talk&lt;/a&gt; is great.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Explainability Washing</title>
	  <link>https://minimizeregret.com/link/2023/05/29/explainability-washing/</link>
	  <pubDate>Mon, 29 May 2023 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2023/05/29/explainability-washing/</guid>
	  <description>&lt;p&gt;Upol Ehsan &lt;a href=&#34;https://hci.social/@upol/110397476968179709&#34;&gt;ponders on Mastodon&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Explainable AI suffers from an epidemic. I call it Explainability Washing. Think of it as window dressing&amp;ndash;techniques, tools, or processes created to provide the illusion of explainability but not delivering it.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Ah yes, slapping feature importance values onto a prediction and asking your users &amp;ldquo;&lt;a href=&#34;https://youtu.be/YbBiXPVKuTA?t=84&#34;&gt;Are you not entertained?&lt;/a&gt;&amp;rdquo;.&lt;/p&gt;&#xA;&lt;p&gt;This thread pairs well with &lt;a href=&#34;https://www.youtube.com/watch?v=ZQ4kz-v-LPk&#34;&gt;Rick Saporta&amp;rsquo;s presentation&lt;/a&gt;. Both urge you to focus solely on your user&amp;rsquo;s decision when deciding what to build.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Bayesian Intermittent Demand Forecasting at NeurIPS 2016</title>
	  <link>https://minimizeregret.com/link/2023/03/25/seeger-neurips-2016/</link>
	  <pubDate>Sat, 25 Mar 2023 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2023/03/25/seeger-neurips-2016/</guid>
	  <description>&lt;p&gt;Oldie but a goodie: A &lt;a href=&#34;https://learn.microsoft.com/en-us/events/neural-information-processing-systems-conference-nips-2016/bayesian-intermittent-demand-forecasting-large-inventories&#34;&gt;recording of Matthias Seeger&amp;rsquo;s presentation&lt;/a&gt; of &amp;ldquo;Bayesian Intermittent Demand Forecasting for Large Inventories&amp;rdquo; at NeurIPS 2016. The &lt;a href=&#34;https://www.amazon.science/publications/bayesian-intermittent-demand-forecasting-for-large-inventories&#34;&gt;corresponding paper&lt;/a&gt; is a favorite of mine, but I only now stumbled over the presentation. It sparked an &lt;a href=&#34;https://arxiv.org/abs/1709.07638&#34;&gt;entire&lt;/a&gt; &lt;a href=&#34;https://arxiv.org/abs/1906.05264&#34;&gt;catalogue&lt;/a&gt; &lt;a href=&#34;https://www.amazon.science/publications/probabilistic-demand-forecasting-at-scale&#34;&gt;of&lt;/a&gt; &lt;a href=&#34;https://arxiv.org/abs/1704.04110&#34;&gt;work&lt;/a&gt; &lt;a href=&#34;https://proceedings.mlr.press/v89/gasthaus19a.html&#34;&gt;on&lt;/a&gt; &lt;a href=&#34;https://papers.nips.cc/paper/2018/hash/5cf68969fb67aa6082363a6d4e6468e2-Abstract.html&#34;&gt;time series forecasting&lt;/a&gt; by Amazon, and like few others called out the usefulness of &lt;a href=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths/&#34;&gt;sample paths&lt;/a&gt;.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>On the Factory Floor</title>
	  <link>https://minimizeregret.com/link/2023/02/26/on-the-factory-floor/</link>
	  <pubDate>Sun, 26 Feb 2023 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2023/02/26/on-the-factory-floor/</guid>
	  <description>&lt;p&gt;What works at Google-scale is not the pattern most data scientists need to employ at their work. But the paper &amp;ldquo;&lt;a href=&#34;https://arxiv.org/abs/2209.05310&#34;&gt;On the Factory Floor: ML Engineering for Industrial-Scale Ads Recommendation Models&lt;/a&gt;&amp;rdquo; is the kind of paper that we need more of: Thrilling reports of what works in practice.&lt;/p&gt;&#xA;&lt;p&gt;Also, the authors do provide abstract lessons anyone can use, such as &lt;em&gt;considering the constraints of your problem&lt;/em&gt; rather than &lt;em&gt;using whatever is state-of-the-art&lt;/em&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;A major design choice is how to represent an ad-query pair &lt;em&gt;x&lt;/em&gt;. The semantic information in the language of the query and the ad headlines is the most critical component. Usage of attention layers on top of raw text tokens may generate the most useful language embeddings in current literature [64], but we find better accuracy and efficiency trade-offs by combining variations of fully-connected DNNs with simple feature generation such as bi-grams and n-grams on sub-word units. The short nature of user queries and ad headlines is a contributing factor. Data is highly sparse for these features, with typically only a tiny fraction of non-zero feature values per example.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;</description>
	</item>
	<item>
	  <title>SAP Design Guidelines for Intelligent Systems</title>
	  <link>https://minimizeregret.com/link/2023/01/08/sap-design-guidelines/</link>
	  <pubDate>Sun, 08 Jan 2023 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2023/01/08/sap-design-guidelines/</guid>
	  <description>&lt;p&gt;From SAP’s &lt;a href=&#34;https://experience.sap.com/fiori-design-web/recommendations/&#34;&gt;Design Guidelines for Intelligent Systems&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;&lt;strong&gt;High–stakes decisions&lt;/strong&gt; are more common in a professional software environment than in everyday consumer apps, where the consequences of an action are usually easy to anticipate and revert.&#xA;While the implications of recommending unsuitable educational content to an employee are likely to be minimal, recommendations around critical business decisions can potentially cause irreversible damage (for example, recommending an unreliable supplier or business partner, leading to the failure or premature termination of a project or contract).&#xA;It’s therefore vital to enable users to take an &lt;strong&gt;informed decision&lt;/strong&gt;.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;While sometimes overlooked, this guide presents software in internal business processes as rich opportunity to augment human capabilities, deserving just as much love and attention as &amp;ldquo;everyday consumer apps&amp;rdquo;.&lt;/p&gt;&#xA;&lt;p&gt;The chapters on intelligent systems are not too tuned to SAP systems, but they do have the specific context of business applications in mind which differentiates them from other (great!) &lt;a href=&#34;https://pair.withgoogle.com/guidebook/&#34;&gt;guides on user interfaces for machine learning systems&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Based on that context, the guide dives deep on &lt;a href=&#34;https://experience.sap.com/fiori-design-web/ranking/&#34;&gt;Ranking&lt;/a&gt;, &lt;a href=&#34;https://experience.sap.com/fiori-design-web/recommendations/&#34;&gt;Recommendations&lt;/a&gt;, and &lt;a href=&#34;https://experience.sap.com/fiori-design-web/matching/&#34;&gt;Matching&lt;/a&gt;, proving that it&amp;rsquo;s based on a much more hands-on view than any text discussing Supervised, Unsupervised, and Reinforcement Learning.&lt;/p&gt;&#xA;&lt;p&gt;When aiming to build systems that &amp;ldquo;augment human capabilities&amp;rdquo;, the importance of &amp;ldquo;gain[ing] the user’s trust and foster[ing] successful adoption&amp;rdquo; can’t be overstated, making it worthwhile to deeply consider how we present the output of our systems.&lt;/p&gt;&#xA;&lt;p&gt;Related: &lt;a href=&#34;https://arxiv.org/abs/2010.07487&#34;&gt;Formalizing Trust in Artificial Intelligence: Prerequisites, Causes and Goals of Human Trust in AI&lt;/a&gt; by Alon Jacovi, Ana Marasović, Tim Miller, and Yoav Goldberg.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Skillful Image Fast-Forwarding</title>
	  <link>https://minimizeregret.com/link/2023/01/02/darksky-weather-prediction/</link>
	  <pubDate>Mon, 02 Jan 2023 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2023/01/02/darksky-weather-prediction/</guid>
	  <description>&lt;p&gt;Russel Jacobs for Slate on the discontinued Dark Sky weather app, via &lt;a href=&#34;https://daringfireball.net/linked/2023/01/02/jacobs-dark-sky&#34;&gt;Daring Fireball&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Indeed, Dark Sky’s big innovation wasn’t simply that its map was gorgeous and user-friendly: The radar map was the forecast. Instead of pulling information about air pressure and humidity and temperature and calculating all of the messy variables that contribute to the weather&amp;ndash;a multi-hundred-billion-dollars-a-year international enterprise of satellites, weather stations, balloons, buoys, and an army of scientists working in tandem around the world (see Blum’s book)&amp;ndash;Dark Sky simply monitored changes to the shape, size, speed, and direction of shapes on a radar map and fast-forwarded those images. “It wasn’t meteorology,” Blum said. “It was just graphics practice.”&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Reminds me of &lt;a href=&#34;https://www.deepmind.com/blog/nowcasting-the-next-hour-of-rain&#34;&gt;DeepMind&amp;rsquo;s&lt;/a&gt; &lt;a href=&#34;https://www.nature.com/articles/s41586-021-03854-z&#34;&gt;&amp;ldquo;Skilful precipitation nowcasting using deep generative models of radar&amp;rdquo;&lt;/a&gt;.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>GluonTS Workshop at Amazon Berlin on September 29</title>
	  <link>https://minimizeregret.com/link/2022/09/28/gluonts-workshop/</link>
	  <pubDate>Wed, 28 Sep 2022 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2022/09/28/gluonts-workshop/</guid>
	  <description>&lt;blockquote&gt;&#xA;&lt;p&gt;The workshop will revolve around tools that automatically transform your data, in particular time series, into high-quality predictions based on AutoML and deep learning models. The event will be hosted by the team at AWS that develops AutoGluon, Syne Tune and GluonTS, and consist of a mix of tutorial-style presentation on the tools, discussion, and contributions from external partners on their applications.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Unique opportunity to hear from industry practitioners and &lt;a href=&#34;https://github.com/awslabs/gluonts&#34;&gt;GluonTS&lt;/a&gt; developers in person or by joining online.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Design a System, not an &#34;AI&#34;</title>
	  <link>https://minimizeregret.com/link/2022/09/14/design-a-system-not-an-ai/</link>
	  <pubDate>Wed, 14 Sep 2022 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2022/09/14/design-a-system-not-an-ai/</guid>
	  <description>&lt;p&gt;Ryxcommar &lt;a href=&#34;https://twitter.com/ryxcommar/status/1569006744059133952&#34;&gt;on Twitter&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;I think one of the bigger mistakes people make when designing AI powered systems is seeing them as an AI first and foremost, and not as a system first and foremost.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Once you have your API contracts in place, the AI parts can be seen as function calls inside the system. Maybe your first version of these functions just return an unconditional expected value. But the system is the bulk of the work, the algorithm is a small piece.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;To me, this is why regulation of AI (in contrast to &lt;a href=&#34;https://ssrn.com/abstract=3230829&#34;&gt;regulation of software&lt;/a&gt; generally) can feel misguided: Any kind of function within a system has the potential to be problematic. It doesn&amp;rsquo;t have to use matrix multiplication for that to be the case.&lt;/p&gt;&#xA;&lt;p&gt;More interestingly though, this is why it&amp;rsquo;s so effective to &lt;a href=&#34;https://developers.google.com/machine-learning/guides/rules-of-ml#rule_4_keep_the_first_model_simple_and_get_the_infrastructure_right&#34;&gt;start with a simple model&lt;/a&gt;. It provides the function call around which you can build the system users care about.&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Some free advice for data scientists&amp;ndash; every time I have seen people treat their systems primarily as AI and not as systems, both the AI and the system suffered for it. Don&amp;rsquo;t make that mistake; design a system, not an &amp;ldquo;AI.&amp;rdquo;&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;</description>
	</item>
	<item>
	  <title>Berlin Bayesians Meetup on September 27</title>
	  <link>https://minimizeregret.com/link/2022/09/06/berlin-bayesians-meetup/</link>
	  <pubDate>Tue, 06 Sep 2022 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2022/09/06/berlin-bayesians-meetup/</guid>
	  <description>&lt;p&gt;The &lt;a href=&#34;https://www.meetup.com/berlinbayesians/events/288023821&#34;&gt;Berlin Bayesians meetup&lt;/a&gt; is happening again &lt;em&gt;in-person&lt;/em&gt;. &lt;a href=&#34;https://juanitorduz.github.io&#34;&gt;Juan Orduz&lt;/a&gt; is going to present &lt;a href=&#34;https://en.wikipedia.org/wiki/Buy_Till_you_Die&#34;&gt;Buy &amp;lsquo;Til You Die&lt;/a&gt; models implemented in PyMC:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;In this talk, we introduce a certain type of customer lifetime models for the non-contractual setting commonly known as BTYD (Buy Till You Die) models. We focus on two sub-model components: the frequency BG/NBD model and the monetary gamma-gamma model. We begin by introducing the model assumptions and parameterizations. Then we walk through the maximum-likelihood parameter estimation and describe how the models are used in practice. Next, we describe some limitations and how the Bayesian framework can help us to overcome some of them, plus allowing more flexibility. Finally, we describe some ongoing efforts in the open source community to bring these new ideas and models to the public.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Buy &amp;lsquo;Til You Die models for the estimation of customer lifetime value were one of the first applications I worked on in my data science career, I&amp;rsquo;m glad to see they&amp;rsquo;re still around and kicking. Now implemented in the shiny new version of PyMC!&lt;/p&gt;&#xA;&lt;p&gt;Edit: The &lt;a href=&#34;https://twitter.com/BBayesians/status/1568131293740777473&#34;&gt;event was rescheduled&lt;/a&gt; to September 27.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Legible Forecasts, and Design for Contestability</title>
	  <link>https://minimizeregret.com/post/2022/08/31/legible-forecasts/</link>
	  <pubDate>Wed, 31 Aug 2022 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2022/08/31/legible-forecasts/</guid>
	  <description>&lt;p&gt;Some models are inherently interpretable because one can read their decision boundary right off them. In fact, you could call them &lt;em&gt;interpreted&lt;/em&gt; as there is nothing left for you to interpret: The entire model is written out for you to read.&lt;/p&gt;&#xA;&lt;p&gt;For example, assume it&amp;rsquo;s July and we need to predict how many scoops of ice cream we&amp;rsquo;ll sell next month. The &lt;a href=&#34;https://otexts.com/fpp3/simple-methods.html#seasonal-na%C3%AFve-method&#34;&gt;Seasonal Naive method&lt;/a&gt; tells us:&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;AS 12 MONTHS AGO IN August, PREDICT sales = 3021.&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Slightly more complicated but with rules that are fitted on past data, this is what a &lt;a href=&#34;https://proceedings.mlr.press/v38/wang15a.html&#34;&gt;Rule List&lt;/a&gt; could look like:&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;&#x9; IF month IS June   THEN PREDICT sales = 5000,&#xA;ELSE IF month IS July   THEN PREDICT sales = 5900,&#xA;ELSE IF month IS August THEN PREDICT sales = 3050,&#xA;ELSE                         PREDICT sales = 1500.&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The models are interpretable because their function $f_{\theta}(x)$ is human-readable. The function is put into words, literally. This makes it possible to understand why a value is predicted, and it makes it possible to disagree with a prediction. A stakeholder could challenge it:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Yes, 3050 is a good estimate, but this August we run a discount code in our podcast advertisements for the first time ever, which is not taken into account by the model. Thus we should be prepared for increased sales of up to 4000 scoops of our delicious ice cream.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Users of interpretable methods can contest predictions &lt;em&gt;because&lt;/em&gt; the models are legible. This is important, because methods that can be contested can also be trusted&amp;mdash;basically every time you inspect the prediction and don&amp;rsquo;t find a reason to contest. I haven&amp;rsquo;t seen the terms &amp;ldquo;legible&amp;rdquo; and &amp;ldquo;contestability&amp;rdquo; mentioned a lot if at all in the usual machine learning literature around ethics, explainability, and trustworthiness. Instead a paper &lt;a href=&#34;https://pubmed.ncbi.nlm.nih.gov/28890949/&#34;&gt;by Hirsch et al. on PubMed&lt;/a&gt; used them to describe a machine learning system built for mental health patients and practicioners. If you want others to trust and act on your model&amp;rsquo;s predictions, I think these are great characteristics to strive for.&lt;/p&gt;&#xA;&lt;p&gt;Unfortunately, compared to the examples above, structural time series models aren&amp;rsquo;t legible. Or what do you call the following?&lt;/p&gt;&#xA;$$&#xA;y_{t} = \mu_t + \tau_t + \epsilon_t, \quad \epsilon_t \sim N(0, \sigma)&#xA;$$&lt;p&gt;&#xA;&lt;/p&gt;&#xA;$$&#xA;\mu_{t+1} = \mu_t + \delta_t + \eta_{\mu,t}, \quad \eta_{\mu,t} \sim N(0, \sigma_{\mu})&#xA;$$&lt;p&gt;&#xA;&lt;/p&gt;&#xA;$$&#xA;\delta_{t+1} = D + \phi(\delta_t - D) + \eta_{\delta,t}, \quad \eta_{\delta,t} \sim N(0, \sigma_{\delta})&#xA;$$&lt;p&gt;&#xA;&lt;/p&gt;&#xA;$$&#xA;\tau_{t+1} = - \sum_{s=1}^{S-1} \tau_t + \eta_{\tau, t}, \quad \eta_{\tau,t} \sim N(0, \sigma_{\tau})&#xA;$$&lt;p&gt;Sure, structural time series models are interpretable. That&amp;rsquo;s because they are made of a set of parameters where every parameter serves a predefined purpose. But it will take considerable effort and background knowledge to interpret not just individual parameters but all of them together to understand why the model predicts 3309 scoops of ice cream for August.&lt;/p&gt;&#xA;&lt;p&gt;What though if we could write an interpreter for the coefficients? Not an explanation model that approximates the fitted model, but a function that puts the fitted parameters into words&amp;mdash;again, the goal is legibility.&lt;/p&gt;&#xA;&lt;p&gt;Let&amp;rsquo;s give it a shot.&lt;/p&gt;&#xA;&lt;h2 id=&#34;fit-the-model&#34;&gt;Fit the Model&lt;/h2&gt;&#xA;&lt;p&gt;I will use the model described by the equations above for this. It&amp;rsquo;s implemented in the &lt;code&gt;bsts&lt;/code&gt; package in R, and corresponds to the specification with semilocal linear trend and seasonal effects.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/08/31/legible-forecasts/berlin_bike_count.png&#34; alt=&#34;Photo of the bike counter installation at Straße des 17. Juni in Berlin. The counts of this station are available at: https://data.eco-counter.com/public2/?id=300019043.&#34;&gt;&lt;/figure&gt;&lt;p&gt;Let&amp;rsquo;s fit this model against &lt;a href=&#34;https://data.eco-counter.com/public2/?id=100032236&#34;&gt;monthly counts of bikes that passed the publicly available counter station at Maybachufer in Berlin&lt;/a&gt;; I aggregated the hourly data from the original dataset made available by the &amp;ldquo;Berlin Senatsverwaltung für Umwelt, Mobilität, Verbraucher- und Klimaschutz&amp;rdquo; &lt;a href=&#34;https://www.berlin.de/sen/uvk/verkehr/verkehrsplanung/radverkehr/weitere-radinfrastruktur/zaehlstellen-und-fahrradbarometer/&#34;&gt;on their website&lt;/a&gt;.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; It&amp;rsquo;s a smooth series of about five years history if we cut it off after June 2021 to keep a holdout year:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/08/31/legible-forecasts/plot_data-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;We construct the model by combining the semilocal linear trend with the monthly seasonality and fit it using the vector of the time series.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;y &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; df[df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;date &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;as.Date&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;2021-08-01&amp;#34;&lt;/span&gt;), ]&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;n_bikes&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;state_spec &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; bsts&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;AddSeasonal&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  state.specification &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;list&lt;/span&gt;(),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; y,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  nseasons &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;state_spec &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; bsts&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;AddSemilocalLinearTrend&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  state.specification &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; state_spec,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; y&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;bsts_fit &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; bsts&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;bsts&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  formula &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; y, &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  state.specification &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; state_spec,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ping &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  niter &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;6000&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  seed &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;7267&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;At this point we haven&amp;rsquo;t called &lt;code&gt;predict()&lt;/code&gt; to produce a forecast. All we have are the fitted parameters of the model hidden in the &lt;code&gt;bsts_fit&lt;/code&gt; object.&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt; Let&amp;rsquo;s plug them into our equation to print the model in the best form available out-of-the box:&lt;/p&gt;&#xA;$$&#xA;y_{t} = \mu_t + \tau_t + \epsilon_t, \quad \epsilon_t \sim N(0, 9290)&#xA;$$&lt;p&gt;&#xA;&lt;/p&gt;&#xA;$$&#xA;\mu_{t+1} = \mu_t + \delta_t + \eta_{\mu,t}, \quad \eta_{\mu,t} \sim N(0, 411)&#xA;$$&lt;p&gt;&#xA;&lt;/p&gt;&#xA;$$&#xA;\delta_{t+1} = -1.86 + -0.59 \cdot (\delta_t + 1.86) + \eta_{\delta,t}, \quad \eta_{\delta,t} \sim N(0, 8866)&#xA;$$&lt;p&gt;&#xA;&lt;/p&gt;&#xA;$$&#xA;\tau_{t+1} = \sum_{s=1}^{11} \tau_t + \eta_{\tau, t}, \quad \eta_{\tau,t} \sim N(0, 141)&#xA;$$&lt;p&gt;&#xA;&lt;/p&gt;&#xA;$$&#xA;\mu_T = 136570, \quad \delta_T = 3388&#xA;$$&lt;p&gt;&#xA;&lt;/p&gt;&#xA;$$&#xA;\tau_T = 39881, \quad \sum_{s=1}^{11} \tau_{T-s+1} = 51118&#xA;$$&lt;p&gt;Well. I mean, one &lt;em&gt;can&lt;/em&gt; interpret this. But would one show this as part of a user interface? Ask business stakeholders to make sense of it? Probably not. Can I type the values into a calculator and get the prediction for next month? Yes. Do I want to? No.&lt;/p&gt;&#xA;&lt;h2 id=&#34;make-the-model-legible&#34;&gt;Make the Model Legible&lt;/h2&gt;&#xA;&lt;p&gt;Let&amp;rsquo;s try something different. In comparison to the numerical parameter values, a semantic representation should be much easier to parse. If there is one thing we can parse well, it&amp;rsquo;s text.&lt;sup id=&#34;fnref:3&#34;&gt;&lt;a href=&#34;#fn:3&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#xA;&lt;p&gt;The &lt;a href=&#34;https://github.com/timradtke/explainer&#34;&gt;following function&lt;/a&gt; generates such legible representation of the fitted model, describes how the model puts the forecast together, and summarizes miscellaneous information such as comparison against benchmarks and detected anomalies.&lt;/p&gt;&#xA;&lt;p&gt;Note that the paragraphs up to the horizontal divider below are automatically generated text returned by the function, based on the specifics of the model it received as input.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# devtools::install_github(&amp;#34;timradtke/explainer&amp;#34;)&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;explanation &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; explainer&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;explain&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  object &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; bsts_fit,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  burn &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1000&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  seasonality &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  start_period &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;print&lt;/span&gt;(explanation)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&#xA;&lt;p&gt;The chosen model takes into account seasonality and allows for a long-run trend with short-term fluctuation around the trend.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;The model explains most of the observed variation in the series at hand. This indicates that the series contains signals such as trend or seasonality, but it doesn’t guarantee that the model’s forecast projects them appropriately into the future.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Historically, the model has predicted the series at hand better than both the current year’s runrate, and the value from a year ago.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;The current level of the series is lower than the average level observed in the history of the series, and lower than a year ago. The current level is neither clearly decreasing nor clearly increasing.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;The model estimates a strong seasonal component in the series which will be projected into the future.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;The model does not predict a clear upward or downward long-run trend.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;The series’ seasonality is expected to push up next month’s observation, similar to a year ago.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Both observation noise and changes of the level of the series contribute to the forecast uncertainty. The level of the time series fluctuated historically both due to noise and due to changes in the trend.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Over the course of the series, there has been a single anomaly 4.5 years ago.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;hr&gt;&#xA;&lt;p&gt;This model representation isn&amp;rsquo;t as succinct as the set of equations. But you can read it from top to bottom. There&amp;rsquo;s no need to jump back and forth to grasp the size of the seasonal effect. It describes the characteristics of the fitted structural time series model without asking the reader to know what a structural time series model is. By doing so, it makes the model legible (literally), and contestable: For example, the model does not project a long-term trend (even though the model family allows for it). Given subject matter expertise, this could be challenged. Maybe 2020 and 2021 are unusual for some reason, and the upward behavior of the years prior will eventually continue.&lt;/p&gt;&#xA;&lt;p&gt;What&amp;rsquo;s hidden in the function definition of &lt;code&gt;explainer()&lt;/code&gt;, is that this interpretation relies on arbitrary thresholds. I purposely avoided printing any numbers to indicate the strength of model components. Consequently, it&amp;rsquo;s not left to the reader to determine whether the model shows a long term trend, whether the level is expected to increase, and which source of uncertainty is the strongest. It&amp;rsquo;s based on predefined cutoffs.&lt;/p&gt;&#xA;&lt;p&gt;However, this allows us to play a bit with the information that is provided by the model object. Instead of purely interpreting the model parameters, one can expose how the model&amp;rsquo;s training (!) error compares to simple benchmarks like the seasonal naive method. If it compared unfavorably, this would presumably indicate that the model should not be relied upon. Similarly, the in-sample errors are used to identify potentially anomalous observations.&lt;sup id=&#34;fnref:4&#34;&gt;&lt;a href=&#34;#fn:4&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;4&lt;/a&gt;&lt;/sup&gt; Given the smooth bike counts, this isn&amp;rsquo;t as relevant here. But it could be valuable information to the user if a recent observation was unexpectedly large.&lt;/p&gt;&#xA;&lt;h2 id=&#34;the-forecast&#34;&gt;The Forecast&lt;/h2&gt;&#xA;&lt;p&gt;Finally, to complete the picture: The corresponding forecast looks as follows.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/08/31/legible-forecasts/plot_forecast-1.png&#34;&gt;&lt;/figure&gt;&lt;h2 id=&#34;references&#34;&gt;References&lt;/h2&gt;&#xA;&lt;p&gt;Tad Hirsch, Kritzia Merced, Shrikanth Narayanan, Zac E. Imel, David C. Atkins (2017). &lt;a href=&#34;https://pubmed.ncbi.nlm.nih.gov/28890949/&#34;&gt;Designing Contestability: Interaction Design, Machine Learning, and Mental Health&lt;/a&gt;. DIS (Des Interact Syst Conf).&lt;/p&gt;&#xA;&lt;p&gt;Fulton Wang, Cynthia Rudin (2015). &lt;a href=&#34;https://proceedings.mlr.press/v38/wang15a.html&#34;&gt;Falling Rule Lists&lt;/a&gt;. Proceedings of the Eighteenth International Conference on Artificial Intelligence and Statistics.&lt;/p&gt;&#xA;&lt;p&gt;Berlin Senatsverwaltung für Umwelt, Mobilität, Verbraucher- und Klimaschutz. &lt;a href=&#34;https://www.berlin.de/sen/uvk/_assets/verkehr/verkehrsplanung/radverkehr/weitere-radinfrastruktur/zaehlstellen-und-fahrradbarometer/gesamtdatei_stundenwerte.xlsx&#34;&gt;Ergebnisse der automatischen Radzählstellen (Jahresdatei)&lt;/a&gt;. 2021-12-31. &lt;a href=&#34;https://www.govdata.de/dl-de/by-2-0&#34;&gt;Data licence Germany – attribution – version 2.0&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;Their Excel file appears to be updated once a year which means the data for 2022 is not yet available. One can see the latest data on their &lt;a href=&#34;https://data.eco-counter.com/ParcPublic/?id=4728#&#34;&gt;interactive map&lt;/a&gt;.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:2&#34;&gt;&#xA;&lt;p&gt;As we used Bayesian inference, we don&amp;rsquo;t have &lt;em&gt;a&lt;/em&gt; fitted parameter, but samples from parameters&amp;rsquo; posterior distributions derived via MCMC. I will not mention it in the rest of the article and only show the posterior median.&amp;#160;&lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:3&#34;&gt;&#xA;&lt;p&gt;Okay, after visual representations.&amp;#160;&lt;a href=&#34;#fnref:3&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:4&#34;&gt;&#xA;&lt;p&gt;See also the documentation of &lt;code&gt;bsts::bsts.prediction.errrors()&lt;/code&gt;.&amp;#160;&lt;a href=&#34;#fnref:4&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>Where Is the Seasonal Naive Benchmark?</title>
	  <link>https://minimizeregret.com/post/2022/08/30/where-is-the-seasonal-naive-benchmark/</link>
	  <pubDate>Tue, 30 Aug 2022 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2022/08/30/where-is-the-seasonal-naive-benchmark/</guid>
	  <description>&lt;p&gt;Yesterday morning, I retweeted &lt;a href=&#34;https://twitter.com/sklearn_inria/status/1564150456938643458&#34;&gt;this tweet&lt;/a&gt; by &lt;a href=&#34;https://twitter.com/sklearn_inria&#34;&gt;sklearn_inria&lt;/a&gt; that promotes &lt;a href=&#34;https://scikit-learn.org/1.1/auto_examples/applications/plot_cyclical_feature_engineering.html&#34;&gt;a scikit-learn tutorial notebook&lt;/a&gt; on time-related feature engineering. It&amp;rsquo;s a neat notebook that shows off some fantastic ways of creating features to predict time series within a scikit-learn pipeline.&lt;/p&gt;&#xA;&lt;p&gt;There are, however, two things that irk me:&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;All features of the dataset &lt;em&gt;including the hourly weather&lt;/em&gt; are passed to the model. I don&amp;rsquo;t know the details of this dataset, but skimming what I believe to be &lt;a href=&#34;https://www.openml.org/search?type=data&amp;amp;status=active&amp;amp;id=42712&#34;&gt;its description on the OpenML repository&lt;/a&gt;, I suspect this might introduce data leakage as in reality we can&amp;rsquo;t know the exact hourly humidity and temperature days in advance.&lt;/li&gt;&#xA;&lt;li&gt;There is no comparison against the simplest benchmark method, the one predicting the value from a week ago (the &lt;a href=&#34;https://otexts.com/fpp3/simple-methods.html#seasonal-na%C3%AFve-method&#34;&gt;seasonal naive&lt;/a&gt; method). Given the daily and weekly seasonality, it should be possible to capture a ton of signal by predicting the same number of bike rentals that were observed a week ago.&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;To be clear, I don&amp;rsquo;t think it&amp;rsquo;s the responsibility of a scikit-learn tutorial to be all encompassing. It&amp;rsquo;s created to show off scikit-learn features. That&amp;rsquo;s what it does, and that&amp;rsquo;s fine. But here we can have some fun, so let&amp;rsquo;s see how far we can get when we drop the weather-related columns, and by using the simplest of benchmarks only.&lt;/p&gt;&#xA;&lt;p&gt;To start off, let&amp;rsquo;s do a bit of a speed run through the pipeline setup used in the original notebook.&lt;/p&gt;&#xA;&lt;h2 id=&#34;first-look-at-the-bike-sharing-data&#34;&gt;First Look at the Bike Sharing Data&lt;/h2&gt;&#xA;&lt;p&gt;The data used in the notebook are bike sharing demand observed as hourly time series for a period of about two years in 2011 and 2012. In contrast to other datasets of its kind, this dataset consists of a single series. The tutorial loads it from the OpenML repository:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; sklearn.datasets &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; fetch_openml&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;bike_sharing &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; fetch_openml(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Bike_Sharing_Demand&amp;#34;&lt;/span&gt;, version&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;, as_frame&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; bike_sharing&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;frame&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To take a sneak peek at what the time series looks like, the tutorial computes the average hourly demand over observed across the entire dataset:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/08/30/where-is-the-seasonal-naive-benchmark/average_hourly_demand-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Unsurprisingly, the bike sharing demand is highly seasonal. There is a within-day pattern that repeats on weekdays corresponding to demand from commuters. Also, weekends are clearly distinct from weekdays but likely similar across weekends (in theory this is not visible from this plot, but&amp;hellip;). Absent any drastic trends, and ignoring the slower yearly seasonality given that we will focus on short-term forecasts, most of the signal from one week to the next will come from the daily and weekly seasonality. Any model we use should be designed to capture it. And that&amp;rsquo;s what the tutorial does, it demonstrates different ways to capture the seasonal patterns via feature engineering.&lt;/p&gt;&#xA;&lt;p&gt;While most of the feature engineering is done to reduce the error of linear regression-based models, the best model of the tutorial turns out to be &lt;a href=&#34;https://scikit-learn.org/1.1/auto_examples/applications/plot_cyclical_feature_engineering.html#gradient-boosting&#34;&gt;the gradient boosting model that is evaluated first&lt;/a&gt; and uses none of the fancy transformations. That&amp;rsquo;s the model we focus on now.&lt;/p&gt;&#xA;&lt;h2 id=&#34;setup-of-the-original-gradient-boosting-pipeline&#34;&gt;Setup of the Original Gradient Boosting Pipeline&lt;/h2&gt;&#xA;&lt;p&gt;First, the target column &lt;code&gt;count&lt;/code&gt; is standardized by dividing by the largest observation. Thus we don&amp;rsquo;t predict the count of bike rentals directly, but the share of the largest possible hourly demand. This means that a mean absolute error of 0.01 corresponds to 1pp difference from the actually observed percent of maximum demand. The transformed column is assigned to &lt;code&gt;y&lt;/code&gt;, then dropped from the dataframe to create the feature matrix &lt;code&gt;X&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; df[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;count&amp;#34;&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; df[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;count&amp;#34;&lt;/span&gt;]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;max()&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;X &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;drop(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;count&amp;#34;&lt;/span&gt;, axis&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;columns&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The initial model matrix consists of the following features (the range shown here is a bit misleading for the categorical columns). Note the weather-related columns such as &lt;code&gt;temp&lt;/code&gt;, &lt;code&gt;humidity&lt;/code&gt;, and &lt;code&gt;windspeed&lt;/code&gt;. These are the problematic features mentioned in the introduction.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; col &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; X&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;columns:&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;print(&lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;col&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39; with values in [&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;min(X[col])&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;, &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;max(X[col])&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;]&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As &lt;code&gt;heavy_rain&lt;/code&gt; was a rare occurence, it&amp;rsquo;s grouped together with &lt;code&gt;rain&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;X[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;weather&amp;#34;&lt;/span&gt;]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;replace(to_replace&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;heavy_rain&amp;#34;&lt;/span&gt;, value&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;rain&amp;#34;&lt;/span&gt;, inplace&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The tutorial uses a time series cross validation with five splits and a gap between train and validation of 48 hours. This gap of 48 hours is what we&amp;rsquo;ll use as the definition of the necessary lag between the moment of creating the prediction, and the moment that we predict. For example, if we need to schedule 48 hours in advance the number of bikes brought into circulation from repairs, then we can&amp;rsquo;t wait to observe the demand at 3pm today to plan for tomorrow&amp;rsquo;s demand. Instead, the latest information we can take into account is the demand observed yesterday, 48 hours in advance.&lt;/p&gt;&#xA;&lt;p&gt;Thus, we won&amp;rsquo;t consider a daily seasonal naive model that projects the observation from 24 hours ago into the future. And because of this lag, the weather columns are somewhat unrealistic. But more on this in a moment.&lt;/p&gt;&#xA;&lt;p&gt;We define the splits, each of which evaluates on a test set of about 42 days.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; sklearn.model_selection &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; TimeSeriesSplit&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ts_cv &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; TimeSeriesSplit(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;n_splits&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;gap&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;48&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;max_train_size&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;10000&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;test_size&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1000&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;all_splits &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; list(ts_cv&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;split(X, y))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# this split is used to plot predictions from the models below&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;train_0, test_0 &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; all_splits[&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The splits will be used to evaluate the model pipelines. For that, an evaluation helper is defined that uses both MAE and RMSE as metrics.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;print_metrics&lt;/span&gt;(mae, rmse):&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  print(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Mean Absolute Error:     &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;mae&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;mean()&lt;span style=&#34;color:#e6db74&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;.3f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; +/- &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;mae&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;std()&lt;span style=&#34;color:#e6db74&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;.3f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;\n&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Root Mean Squared Error: &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;rmse&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;mean()&lt;span style=&#34;color:#e6db74&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;.3f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; +/- &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;rmse&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;std()&lt;span style=&#34;color:#e6db74&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;.3f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;evaluate&lt;/span&gt;(model, X, y, cv):&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;cv_results &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; cross_validate(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;model,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;X,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;y,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;cv&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;cv,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;scoring&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;neg_mean_absolute_error&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;neg_root_mean_squared_error&amp;#34;&lt;/span&gt;],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;mae &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;cv_results[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;test_neg_mean_absolute_error&amp;#34;&lt;/span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;rmse &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;cv_results[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;test_neg_root_mean_squared_error&amp;#34;&lt;/span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;print_metrics(mae, rmse)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next, the tutorial defines the pipeline object (note the inclusion of the &lt;code&gt;weather&lt;/code&gt; column).&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; numpy &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; np&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; matplotlib.pyplot &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; plt&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; sklearn.pipeline &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; make_pipeline&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; sklearn.preprocessing &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; OrdinalEncoder&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; sklearn.compose &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; ColumnTransformer&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; sklearn.ensemble &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; HistGradientBoostingRegressor&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; sklearn.model_selection &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; cross_validate&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;categorical_columns &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;weather&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;season&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;holiday&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;workingday&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;categories &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;clear&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;misty&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;rain&amp;#34;&lt;/span&gt;],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;spring&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;summer&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;fall&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;winter&amp;#34;&lt;/span&gt;],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;False&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;True&amp;#34;&lt;/span&gt;],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;False&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;True&amp;#34;&lt;/span&gt;],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ordinal_encoder &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; OrdinalEncoder(categories&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;categories)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;gbrt_pipeline &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; make_pipeline(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;ColumnTransformer(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;transformers&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;[&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;categorical&amp;#34;&lt;/span&gt;, ordinal_encoder, categorical_columns),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;remainder&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;passthrough&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;HistGradientBoostingRegressor(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;categorical_features&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;range(&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And finally the cross validation performance is being evaluated. Note that we pass the entire model matrix &lt;code&gt;X&lt;/code&gt; here&amp;mdash;including columns describing the hourly temperature, humidity, and wind speed!&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;evaluate(gbrt_pipeline, X, y, cv&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;ts_cv)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This reproduces the performance shown in the tutorial notebook exactly, perfect. Given the standardization that was applied on the series, the mean absolute error corresponds to an error of about 4.4% of the highest hourly demand observed across the time period. Given the maximum of 977 hourly bike rentals, that&amp;rsquo;s an error of about 42 bike rentals.&lt;/p&gt;&#xA;&lt;p&gt;Let&amp;rsquo;s drop the weather-related features and evaluate again.&lt;/p&gt;&#xA;&lt;h2 id=&#34;gradient-boosting-without-weather-features&#34;&gt;Gradient Boosting Without Weather Features&lt;/h2&gt;&#xA;&lt;p&gt;We define the pipeline as before, dropping &lt;code&gt;weather&lt;/code&gt; from the categorical columns and then explicitly selecting columns from &lt;code&gt;X&lt;/code&gt; to not pass through the temperature, humidity, and wind speed. We only keep the columns for which we can know the values in advance.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;categorical_columns_no_weather &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;season&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;holiday&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;workingday&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;categories_no_weather &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;spring&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;summer&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;fall&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;winter&amp;#34;&lt;/span&gt;],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;False&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;True&amp;#34;&lt;/span&gt;],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;False&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;True&amp;#34;&lt;/span&gt;],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ordinal_encoder_no_weather &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; OrdinalEncoder(categories&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;categories_no_weather)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;gbrt_no_weather_pipeline &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; make_pipeline(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;ColumnTransformer(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;transformers&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;[&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;categorical&amp;#34;&lt;/span&gt;, ordinal_encoder_no_weather, categorical_columns_no_weather),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;remainder&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;passthrough&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;HistGradientBoostingRegressor(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;categorical_features&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;range(&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;X_no_weather &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; X[&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  [&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;season&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;year&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;month&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;hour&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;holiday&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;weekday&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;workingday&amp;#39;&lt;/span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Evaluate the new pipeline on the same time series splits:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;evaluate(gbrt_no_weather_pipeline, X_no_weather, y, cv&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;ts_cv)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The MAE increased by 1.8pp. Note also the increase in the standard deviation across splits. The performance clearly worsened, but still isn&amp;rsquo;t too shabby given that we didn&amp;rsquo;t put much effort into building this model.&lt;/p&gt;&#xA;&lt;p&gt;Can we get away with even less work?&lt;/p&gt;&#xA;&lt;h2 id=&#34;comparison-with-seasonal-naive-method&#34;&gt;Comparison with Seasonal Naive Method&lt;/h2&gt;&#xA;&lt;p&gt;Given that the data has several seasonal effects (daily, weekly, yearly), there is no one way to set up a seasonal naive prediction: We could use the value from 24 hours ago, the value from a week ago (168 hours), or even a mix of the two.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; As desribed above, we&amp;rsquo;ll focus on the weekly seasonal naive prediction due to the gap of 48 hours between train and test sets. A lag of one week is anyway more realistic for operational decisions.&lt;/p&gt;&#xA;&lt;p&gt;Given the simplicity of the seasonal naive method, we just need to shift the label series. Since the cross validation splits use a gap of 48 hours that is smaller than our lag, we don&amp;rsquo;t need to do this for each cross validation split. We can do it once across the entire data set and still don&amp;rsquo;t introduce target leakage.&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;snaive &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; y&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;shift(&lt;span style=&#34;color:#ae81ff&#34;&gt;168&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;print(snaive[&lt;span style=&#34;color:#ae81ff&#34;&gt;165&lt;/span&gt;:&lt;span style=&#34;color:#ae81ff&#34;&gt;172&lt;/span&gt;])&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A custom cross validation loop gives us the performance of the seasonal naive method (i.e., of the new column) compared to the target, on the same splits that we used for the gradient boosting models.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mae &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; np&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;empty(&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;rmse &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; np&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;empty(&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; i &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; range(&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;):&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;_, test_idx &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; all_splits[i]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;mae[i] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; np&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;mean(np&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;abs(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;  y&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[test_idx]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;values &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; snaive&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[test_idx]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;values&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;rmse[i] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; np&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;sqrt(np&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;mean(np&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;square(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;  y&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[test_idx]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;values &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; snaive&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[test_idx]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;values&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;)))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;print_metrics(mae, rmse)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;How is that for a one-liner: 7.3% mean absolute error compared to the 6.2% of the gradient boosted trees model without weather features. Given that the seasonal naive method requires neither code nor infrastructure, and is inherently interpretable, one has to check the details of the business case whether any other method is worth the effort. A large chunk of the time series&amp;rsquo; signal is already captured by this method.&lt;/p&gt;&#xA;&lt;p&gt;Then again, the gradient boosted trees pipeline we compare against is the simplest version possible and only served as baseline in the tutorial. It could likely be improved further.&lt;/p&gt;&#xA;&lt;p&gt;But the dataset at hand is also quite simple. Real use cases would likely have multiple series and &lt;a href=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths/&#34;&gt;require uncertainty quantification&lt;/a&gt;, thus increasing the effort going into building a model. This makes it even more important to compare against the simplest of benchmarks.&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;p&gt;We can round this analysis off by plotting the predictions and errors of the models, similar to what was done in the scikit-learn notebook.&lt;/p&gt;&#xA;&lt;h2 id=&#34;graph-of-predictions-over-time&#34;&gt;Graph of Predictions Over Time&lt;/h2&gt;&#xA;&lt;p&gt;We generate predictions for the first test set as defined by the cross validation split.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;gbrt_pipeline&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;fit(X&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[train_0], y&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[train_0])&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;gbrt_predictions &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; gbrt_pipeline&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;predict(X&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[test_0])&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;gbrt_no_weather_pipeline&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;fit(X_no_weather&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[train_0], y&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[train_0])&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;gbrt_no_weather_predictions &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; gbrt_no_weather_pipeline&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;predict(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  X_no_weather&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[test_0]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;snaive_predictions &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; snaive&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[test_0]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;values&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Those we can then use to visualize the last week of the test set:&lt;sup id=&#34;fnref:3&#34;&gt;&lt;a href=&#34;#fn:3&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;last_hours &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; slice(&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;168&lt;/span&gt;, &lt;span style=&#34;color:#66d9ef&#34;&gt;None&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;fig, ax &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;subplots(figsize&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;fig&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;suptitle(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Predictions by models and naive method&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;plot(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;gbrt_predictions[last_hours],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;x-&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;label&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Gradient Boosted Trees&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;plot(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;gbrt_no_weather_predictions[last_hours],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;x-&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;label&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Gradient Boosted Trees without Weather&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;plot(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;snaive_predictions[last_hours],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;x-&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;label&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Weekly Seasonal Naive&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;plot(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;y&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[test_0]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;values[last_hours],&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;x-&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;alpha&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;0.5&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;label&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Actual demand&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;color&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;_ &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;legend()&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/08/30/where-is-the-seasonal-naive-benchmark/predictions_test_0-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;While the graph is a bit busy, the seasonal patterns are clear. While the gradient boosted trees model without the weather information underestimates the demand on most of the weekdays and isn&amp;rsquo;t able to predict the rush-hour peaks, the seasonal naive does a bit better in these respects. But it also makes larger errors on this weekend in particular, presumably due to a very different previous weekend.&lt;/p&gt;&#xA;&lt;p&gt;To compare more than just a week of predictions against actuals, we can use a scatterplot.&lt;/p&gt;&#xA;&lt;h2 id=&#34;graph-of-predictions-against-actuals&#34;&gt;Graph of Predictions Against Actuals&lt;/h2&gt;&#xA;&lt;p&gt;The graph of predictions over time is great to see the actual behavior of the time series. But consistent problems can become apparent in a scatterplot of &lt;em&gt;all&lt;/em&gt; predictions against realized values for the current test set. The predictions from a perfect model would fall on the diagonal line, any deviation from it is the error of the prediction.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;fig, axes &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;subplots(ncols&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;, figsize&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;), sharey&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;predictions &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;gbrt_predictions,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;gbrt_no_weather_predictions,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;snaive_predictions,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;labels &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;GBT&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;GBT without Weather&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Seasonal Naive&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; ax, pred, label &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; zip(axes, predictions, labels):&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;scatter(y&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[test_0]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;values, pred, alpha&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;0.3&lt;/span&gt;, label&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;label)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;plot([&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;], [&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;], &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;--&amp;#34;&lt;/span&gt;, label&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Perfect model&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;xlim&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;ylim&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;xlabel&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;True demand&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;ylabel&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Predicted demand&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;legend()&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;show()&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/08/30/where-is-the-seasonal-naive-benchmark/true_vs_predicted_plot-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;As &lt;a href=&#34;https://scikit-learn.org/1.1/auto_examples/applications/plot_cyclical_feature_engineering.html#modeling-non-linear-feature-interactions-with-kernels&#34;&gt;discussed in the tutorial notebook&lt;/a&gt;, the gradient boosted trees models systematically underestimate the peak demand. For the model without weather information, this also appears to hold for the range of lower demand&amp;mdash;similar to what is visible in the graph of demand over time above.&lt;/p&gt;&#xA;&lt;p&gt;In comparison, the seasonal naive method appears to make some errors that are larger in absolute size, but it doesn&amp;rsquo;t systematically under- or overestimate. This makes sense as its predictions adjust entirely from week to week: A &lt;em&gt;too large&lt;/em&gt; prediction in one week tends to imply a &lt;em&gt;too low&lt;/em&gt; prediction the week thereafter if the data is noisy around some average level. Then the seasonal naive method is low bias, high variance. A bias would easily be introduced by a strong trend that persisted over time in the series, though.&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;For example, using observations from a week ago for weekends, but observations from a day ago for weekdays&amp;mdash;to represent the difference between them. Mondays would have to count as weekends.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:2&#34;&gt;&#xA;&lt;p&gt;The original cross validation setup predicts 42 days into the future. While the gradient boosting models don&amp;rsquo;t see the target test period, we need to assume for the current seasonal naive implementation that we only have to forecast at most 5 days into the future: We use the last week of the training period to predict the first week of the test period, and then use the first week of the test period to predict the next week of the test period. Maybe a future blog post can evaluate how well a seasonal naive model would perform if we used the last week of the train period to forecast the entire test period.&amp;#160;&lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:3&#34;&gt;&#xA;&lt;p&gt;The scikit-learn notebook showed the last four days, but I prefer to show a bit more data from an entire week period. It makes the plot more busy though.&amp;#160;&lt;a href=&#34;#fnref:3&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>When Quantiles Do Not Suffice, Use Sample Paths Instead</title>
	  <link>https://minimizeregret.com/post/2022/07/25/use-sample-paths/</link>
	  <pubDate>Mon, 25 Jul 2022 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2022/07/25/use-sample-paths/</guid>
	  <description>&lt;p&gt;I don’t need to convince you that you should absolutely, to one hundred percent, quantify your forecast uncertainty&amp;mdash;right? We agree about the advantages of using probabilistic measures to answer questions and to automate decision making&amp;mdash;correct? Great. Then let’s dive a bit deeper.&lt;/p&gt;&#xA;&lt;p&gt;So you’re forecasting not just to fill some numbers in a spreadsheet, you are trying to solve a problem, possibly aiming to make optimal decisions in a process concerned with the future. Then please, before you start to forecast, ask yourself the following: &lt;em&gt;&amp;ldquo;Does my problem concern a single time period, or does it span multiple periods?&amp;rdquo;&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;This is a crucial question. Depending on your answer, you’ll either be fine with &lt;em&gt;marginal&lt;/em&gt; forecast distributions, or you’ll have to take it a step further and estimate useful forecast &lt;em&gt;joint&lt;/em&gt; forecast distributions.&lt;/p&gt;&#xA;&lt;h2 id=&#34;marginal-forecast-distributions&#34;&gt;Marginal Forecast Distributions&lt;/h2&gt;&#xA;&lt;p&gt;Marginal forecast distributions, summarized to a handful of quantiles, look like this:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths/marginal_distribution_graph-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;While this kind of visualization is extremely common, it hides the fact that we only know the distributions at the individual points in time, not how they relate to each other. A more honest visualization might be something along these lines:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths/marginal_distribution_graph_alternative-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Marginal forecast distributions describe the probability of outcomes &lt;em&gt;at a single point in time&lt;/em&gt;. If you sample observations at a monthly frequency, a marginal forecast distribution describes possible outcomes for a single month. If you sample observations for your time series every hour, a marginal forecast distribution describes possible outcomes for a given hour. Of course you can have marginal forecast distributions for several months or hours. But they don&amp;rsquo;t tell you how January relates to February.&lt;/p&gt;&#xA;&lt;p&gt;Marginal forecasts can answer questions such as:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;“How likely is it that the wind turbine can cover the electricity load in the next hour without us activating the coal plant?”&lt;/li&gt;&#xA;&lt;li&gt;“Do I need to buy additional beach chairs to be able to offer one to 95% of my hotel guests that ask for one &lt;em&gt;today&lt;/em&gt;?”&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;It&amp;rsquo;s easy to believe that marginal forecast distributions are the only forecast distributions there are. After all, in many cases forecast quantiles are &lt;a href=&#34;https://cran.r-project.org/web/packages/forecast/forecast.pdf&#34;&gt;the default output&lt;/a&gt; &lt;a href=&#34;https://www.statsmodels.org/stable/generated/statsmodels.tsa.base.prediction.PredictionResults.summary_frame.html?highlight=predict%20frame&#34;&gt;of forecast libraries&lt;/a&gt;&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; and &lt;a href=&#34;https://www.kaggle.com/competitions/m5-forecasting-uncertainty/overview/evaluation&#34;&gt;all you need in forecasting competitions&lt;/a&gt; (if &lt;a href=&#34;https://www.kaggle.com/competitions/rossmann-store-sales/overview/evaluation&#34;&gt;at&lt;/a&gt; &lt;a href=&#34;https://www.kaggle.com/competitions/web-traffic-time-series-forecasting/overview/evaluation&#34;&gt;all&lt;/a&gt;). And as in electricity load forecasting, sometimes marginal forecast distributions really are all that&amp;rsquo;s needed to solve a problem.&lt;/p&gt;&#xA;&lt;p&gt;But there are common problems in which one needs to quantify uncertainty across multiple time periods, flexibly. Problems in which the question is inter-temporal. To answer questions as simple as &amp;ldquo;How likely is it that the weather is going to be hot both tomorrow &lt;em&gt;and&lt;/em&gt; the day after tomorrow?&amp;rdquo; we need &lt;em&gt;joint&lt;/em&gt; forecast distributions.&lt;/p&gt;&#xA;&lt;h2 id=&#34;joint-forecast-distributions&#34;&gt;Joint Forecast Distributions&lt;/h2&gt;&#xA;&lt;p&gt;Whereas marginal forecast distributions quantify the uncertainty of an outcome $Y_{t_i}$ at a single time period $t_i$, joint forecast distributions do so for the outcomes $Y_{t_i}$, $Y_{t_j}$, &amp;hellip; at multiple future time periods $t_i$, $t_j$, &amp;hellip; The graphs above have visualized the marginal distributions $P(Y_{T+1} | Y_{T}, Y_{T-1}, ...)$, $P(Y_{T+2} | Y_{T}, Y_{T-1}, ...)$, and so on. Since these are &lt;em&gt;separate&lt;/em&gt; distributions, it was illustrative to turn the ribbon graph into separate vertical lines, each representing one of the marginal distributions.&lt;/p&gt;&#xA;&lt;p&gt;Now, however, we turn our attention to &lt;em&gt;one&lt;/em&gt; big joint distribution over all future time periods of interest: $P(Y_{T+1}, Y_{T+2}, ... | Y_{T}, Y_{T-1}, ...)$. The main selling point of joint distributions is that they not only tell us how future periods relate to the past, but also describe how future periods relate to each other. Given their inherent high dimensionality (were often interested in more than 2 forecast horizons), however, it&amp;rsquo;s difficult to visualize the relationships implied by the joint distribution.&lt;/p&gt;&#xA;&lt;p&gt;You could, of course, summarize them as marginal forecasts and visualize them in the same way. But this hides most of the information contained in the joint distribution forecast. Instead we can, for example, visualize the two-dimensional joint distributions for forecast horizons 1 and 2, 2 and 3, and 1 and 3, respectively:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths/two_points_density-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;This reveals the correlation between outcomes at subsequent points in time. Given that our example time series exhibits strong autocorrelation, proper forecasts will project that autocorrelation into the future, and the estimated joint forecast distribution will represent it.&lt;/p&gt;&#xA;&lt;p&gt;Unsurprisingly, when forecasting, we usually aren&amp;rsquo;t able to derive a closed form definition of the joint distribution. A great way to represent the distribution is &lt;a href=&#34;https://betanalpha.github.io/assets/case_studies/sampling.html&#34;&gt;by drawing many samples from it&lt;/a&gt; instead. If we choose our forecast model wisely, it can generate these samples for us. We can then &lt;a href=&#34;https://en.wikipedia.org/wiki/Monte_Carlo_method#Definitions&#34;&gt;use the samples to answer questions&lt;/a&gt; for which we usually needed to compute integrals with the density function.&lt;/p&gt;&#xA;&lt;p&gt;Here is the same plot of the joint distributions, but now with the samples that I&amp;rsquo;m using in the background to generate the graphs&amp;mdash;I don&amp;rsquo;t know the joint distribution, the contour lines are an approximation of the true density computed from samples:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths/two_points_density_scatter-1.png&#34;&gt;&lt;/figure&gt;&lt;h2 id=&#34;joint-forecasts-as-sample-paths&#34;&gt;Joint Forecasts as Sample Paths&lt;/h2&gt;&#xA;&lt;p&gt;While the plots above are nice, the best way to think about and work with joint forecasts are sample paths. Sample paths are samples from the joint forecast distribution.&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt; They represent possible paths that the time series is expected to take into the future. Not every model can provide them, but often a model can generate a sample path iteratively starting at the last observation and then simulating an outcome over several forecast horizons. By drawing many sample paths from the model, we can infer which ranges of outcomes are likely.&lt;/p&gt;&#xA;&lt;p&gt;So let&amp;rsquo;s pick 10 sample paths from all the available ones, &amp;hellip;&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths/two_points_density_scatter_picked-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;&amp;hellip; and then render them as possible continuations of the original time series.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths/ggp_sample_paths-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Plotting a few sample paths is great to get a quick sense of possible outcomes and how well the forecast model represents the characteristics of the original series. The correlation between the different horizons becomes apparent, in contrast to the graphs of marginal distributions above.&lt;/p&gt;&#xA;&lt;p&gt;Plotting many sample paths on top of each other makes it harder to spot these correlations, but we get a sense of the overall possible ranges that can be achieved.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths/ggp_sample_paths_many-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;And we see that the extremal quantiles at the last horizon are usually only reached if the sample paths trended toward them at earlier points as well&amp;mdash;in contrast to what the marginal distributions might suggest.&lt;/p&gt;&#xA;&lt;p&gt;In fact, given sufficient samples, we can take this a step further and derive conditional sample paths: Demanding that we reach a point higher than at the last observation at forecast horizon 6 (this level is indicated by the horizontal line), let&amp;rsquo;s plot all sample paths that lead us there.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths/ggp_sample_paths_conditional-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;By conditioning on paths that reach a certain value at horizon 6, we filter out a large chunk of outcomes that were deemed likely before. The remaining sample paths illustrate the forecast distribution conditioned on a certain outcome at &lt;em&gt;also&lt;/em&gt; a future point, whereas usually forecast distributions are only ever presented conditioned on the past.&lt;/p&gt;&#xA;&lt;p&gt;This illustrates that we can answer hard questions in simple ways when we have access to sample paths.&lt;/p&gt;&#xA;&lt;h2 id=&#34;answering-hard-questions-by-summing-and-counting&#34;&gt;Answering Hard Questions by Summing and Counting&lt;/h2&gt;&#xA;&lt;p&gt;When you have quantiles of the marginal forecast distribution, then all you have are quantiles from the marginal forecast distribution. Samples from the marginal forecast distribution are a bit better, as they let you compute quantiles of arbitrary functions of your outcome at a single point in time. Samples from the joint distribution though are where it&amp;rsquo;s at: With them we can answer questions across arbitrary combinations of forecast horizons, arbitrary functions of those forecasts, at arbitrary quantiles. And since we&amp;rsquo;re working with samples, we don&amp;rsquo;t even have to compute integrals: All we need to do is to sum and to count, following the &lt;a href=&#34;https://betanalpha.github.io/assets/case_studies/sampling.html#31_The_Monte_Carlo_Method&#34;&gt;Monte-Carlo method&lt;/a&gt;&lt;sup id=&#34;fnref:3&#34;&gt;&lt;a href=&#34;#fn:3&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;3&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;&#xA;&lt;h3 id=&#34;scenario-1-forecast-of-costs-for-the-full-year&#34;&gt;Scenario 1: Forecast of Costs for the Full Year&lt;/h3&gt;&#xA;&lt;p&gt;Suppose the time series above represents the costs of a business division of the company you work for as data scientist. Your stakeholders asked for forecasts for the upcoming year and you provided them with the marginal quantile forecasts from the first graph above. They like those, but follow up with: &amp;ldquo;So what&amp;rsquo;s the 50% prediction interval for the costs we will generate in the entire year?&amp;rdquo;&lt;/p&gt;&#xA;&lt;p&gt;Oops! If all you had were marginal quantiles, you&amp;rsquo;d be hard pressed to provide an answer. Sure, you can sum up the mean predictions to get a mean prediction for the full year, but you can&amp;rsquo;t expect to sum up every month&amp;rsquo;s 25% quantile to get the 25% quantile for the full year&amp;mdash;quantiles aren&amp;rsquo;t additive.&lt;/p&gt;&#xA;&lt;p&gt;Luckily, you generated joint forecast distributions and saved them as sample paths. So all you need to do to find the answer is to sum up each sample path&amp;rsquo;s realizations before computing the quantiles of those sums. Based on the example below, there is a 50% probability that the total costs generated in the coming year will range from 1760 to 2145.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths/calculate_full_year.png&#34;&gt;&lt;/figure&gt;&lt;h3 id=&#34;scenario-2-out-of-stock-forecast&#34;&gt;Scenario 2: Out-of-Stock Forecast&lt;/h3&gt;&#xA;&lt;p&gt;Prolific data scientist that you are, you provided other stakeholders with monthly demand forecasts. &amp;ldquo;When do we need to create the next replenishment order for this product given that it takes two months for the order to arrive, a current inventory level of 900 units, and a service level of 90% (in one of 10 cases a stock-out is acceptable)?&amp;rdquo;, they ask you.&lt;/p&gt;&#xA;&lt;p&gt;Different from the first scenario where you always had to sum all 12 months, here you need to predict the optimal point in time to trigger the order. To do so, first compute the cumulative sum for each sample path. This way, you get the distribution of the &amp;ldquo;total sum until each point in time&amp;rdquo;, at each point in time.&lt;/p&gt;&#xA;&lt;p&gt;For each point in time, you want to know whether the 90% quantile is larger than the current stock, because as soon as this is the case, there is a larger than 90% probability that the current inventory will not suffice until the end of that month. Consequently, the order should be triggered 2 months before that point to ensure you don&amp;rsquo;t run out of stock with 90% probability.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths/calculate_order.png&#34;&gt;&lt;/figure&gt;&lt;h3 id=&#34;scenario-3-order-size&#34;&gt;Scenario 3: Order Size&lt;/h3&gt;&#xA;&lt;p&gt;&amp;ldquo;Assuming that we indeed trigger the order in April and that the entire inventory is used up when the delivery arrives: How much should we order to cover at least 3 months with 90% probability?&amp;rdquo;&lt;/p&gt;&#xA;&lt;p&gt;Since we are using sample paths to generate our answers, it is straightforward to answer questions which are concerned with an arbitrary time interval in the future. Instead of starting the sum at the first forecast horizon, take the sum over the three months in question (forecast horizons 6 through 8) and determine the quantile in question. To cover the demand of the three months in question with a probability of 90%, you need to order at least 554 units based on the sample paths shown below.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/25/use-sample-paths/calculate_order_size.png&#34;&gt;&lt;/figure&gt;&lt;h2 id=&#34;the-sum-of-quantiles-is-not-the-quantile-of-the-sums&#34;&gt;The Sum of Quantiles Is not the Quantile of the Sums&lt;/h2&gt;&#xA;&lt;p&gt;The reason all of this is even relevant is that the sum of quantiles is not the quantile of the sums. In scenario 1 above we solved the problem by computing the quantile of the sum. But we can&amp;rsquo;t answer the question by taking the sum of the quantiles. In contrast to the expected value, quantile functions are not additive.&lt;/p&gt;&#xA;&lt;p&gt;This implies that marginal forecast distributions in the form of quantiles will only ever be able to answer a limited set of questions. In contrast, joint distributions can be summarized to the marginal distributions and thus offer the full flexibility to answer any question concerning the future.&lt;/p&gt;&#xA;&lt;p&gt;There are important problems for which marginal forecasts suffice. Electricity load forecasting, for example. But to provide actionable recommendations in problems such as inventory management, one needs access to the joint forecast distribution.&lt;/p&gt;&#xA;&lt;p&gt;When reading papers and blog posts describing models designed for forecasting, the difference between marginal forecast distributions and joint forecast distributions is often swept under the carpet (by those that only offer marginal distributions). A showcase of this is the &lt;a href=&#34;https://arxiv.org/pdf/1711.11053.pdf&#34;&gt;Multi-Horizon Quantile Recurrent Forecaster paper&lt;/a&gt; by Wen et al. at Amazon. While they evaluate their model on an internal Amazon demand forecasting data set, they noticeably don’t use the same evaluation metric that a different Amazon team around Salinas et al. used in the &lt;a href=&#34;https://arxiv.org/pdf/1704.04110.pdf&#34;&gt;DeepAR paper&lt;/a&gt;. Salinas et al.&amp;rsquo;s $\rho$-risk metric evaluates the demand forecasts over a span of time points, very similar to the three scenarios described above. Thus unsurprisingly, they make a point of using sample paths to do so:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;To obtain such a quantile prediction from a set of sample paths, each realization is first summed in the given span. The samples of these sums then represent the estimated distribution for $Z_i(L, S)$ and we can take the $\rho$-quantile from the empirical distribution.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;To their credit, while Wen et al. don’t discuss this shortcoming in their MQ-RNN paper, they did publish a follow-up paper, &lt;a href=&#34;https://milets18.github.io/papers/milets18_paper_14.pdf&#34;&gt;Sample Path Generation for Probabilistic Demand Forecasting&lt;/a&gt; by Madeka et al., that tries to turn their neural network’s quantiles into sample paths. As part of this, they also describe in detail why their quantiles do not suffice for inter-temporal decision making:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;However, since quantiles aren’t additive, in order to predict the total demand for any wider future interval all required intervals are usually appended to the target vector during model training. The separate optimization of these overlapping intervals can lead to inconsistent forecasts, i.e. forecasts which imply an invalid joint distribution between different horizons. As a result, inter-temporal decision making algorithms that depend on the joint or step-wise conditional distribution of future demand cannot utilize these forecasts.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Choose your model based on the questions you want to answer.&lt;/p&gt;&#xA;&lt;h2 id=&#34;references&#34;&gt;References&lt;/h2&gt;&#xA;&lt;p&gt;Michael Betancourt (2021). &lt;strong&gt;Sampling&lt;/strong&gt;. Retrieved from &lt;a href=&#34;https://github.com/betanalpha/knitr_case_studies/tree/master/sampling&#34;&gt;https://github.com/betanalpha/knitr_case_studies/tree/master/sampling&lt;/a&gt;, commit eb27fbdb5063220f3d335024d2ae5e2bfdf70955.&lt;/p&gt;&#xA;&lt;p&gt;Dhruv Madeka, Lucas Swiniarski, Dean Foster, Leo Razoumov, Kari Torkkola, Ruofeng Wen (2018). &lt;strong&gt;Sample Path Generation for Probabilistic Demand Forecasting&lt;/strong&gt;. &lt;a href=&#34;https://milets18.github.io/papers/milets18_paper_14.pdf&#34;&gt;Link to PDF from 4th Workshop on Mining and Learning from Time Series&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;David Salinas, Valentin Flunkert, Jan Gasthaus (2017). &lt;strong&gt;DeepAR: Probabilistic Forecasting with Autoregressive Recurrent Networks&lt;/strong&gt;. &lt;a href=&#34;https://arxiv.org/abs/1704.04110&#34;&gt;https://arxiv.org/abs/1704.04110&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Syama Sundar Rangapuram, Matthias W. Seeger, Jan Gasthaus, Lorenzo Stella, Yuyang Wang, Tim Januschowski (2018). &lt;strong&gt;Deep State Space Models for Time Series Forecasting&lt;/strong&gt;. &lt;a href=&#34;https://papers.nips.cc/paper/2018/hash/5cf68969fb67aa6082363a6d4e6468e2-Abstract.html&#34;&gt;Advances in Neural Information Processing Systems 31 (NeurIPS 2018)&lt;/a&gt;. (Section 4.2 and the subsequent remarks are especially relevant).&lt;/p&gt;&#xA;&lt;p&gt;Ruofeng Wen, Kari Torkkola, Balakrishnan Narayanaswamy, Dhruv Madeka (2017). &lt;strong&gt;A Multi-Horizon Quantile Recurrent Forecaster&lt;/strong&gt;. &lt;a href=&#34;https://arxiv.org/abs/1711.11053&#34;&gt;https://arxiv.org/abs/1711.11053&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;h2 id=&#34;appendix-drawing-sample-paths&#34;&gt;Appendix: Drawing Sample Paths&lt;/h2&gt;&#xA;&lt;p&gt;Generating high-quality sample paths from a model is not always easy. For the examples above, instead of fitting a model, I used the function by which I generated the observed time series to also generate the &amp;ldquo;forecasted&amp;rdquo; sample paths as possible continuations of the observed series. This is as if I had chosen the correct model class and recovered the exact parameter of the data generating process. Also called cheating.&lt;/p&gt;&#xA;&lt;p&gt;It also shows, though, why it helps to be able to write down a data generating process when you want to generate sample paths. The one I used above is a random walk with drift and Student&amp;rsquo;s t-distributed noise with 4 degrees of freedom:&lt;/p&gt;&#xA;$$&#xA;y_t = y_{t-1} + \mu + \sigma \cdot \epsilon_t&#xA;$$$$&#xA;\epsilon_t \sim Student(4)&#xA;$$&lt;p&gt;The following function implements this model. $\mu$ / &lt;code&gt;mu&lt;/code&gt; regulates the drift of the series, whereas $\sigma$ / &lt;code&gt;sigma&lt;/code&gt; determines the size of the noise. We use a Student&amp;rsquo;s t-distributed noise to not have just a Normal distribution. The &lt;code&gt;initial_value&lt;/code&gt; is used to provide an overall level when generating the initial series, and to condition the forecasts on the input series.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;random_student_walk &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt;(n, mu, sigma, initial_value) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;NA&lt;/span&gt;, times &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; n)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y[1] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; initial_value &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; mu &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; sigma &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rt&lt;/span&gt;(n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#66d9ef&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;n) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;y[i] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; y[i&lt;span style=&#34;color:#ae81ff&#34;&gt;-1&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; mu &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; sigma &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rt&lt;/span&gt;(n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt;(y)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The observed series can then be generated using an initial value of 100:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;572&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;n_obs &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;60&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;y &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;random_student_walk&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; n_obs, mu &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, sigma &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;, initial_value &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;100&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;tail&lt;/span&gt;(y)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## [1] 145 143 147 151 148 154&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can generate one forecasted sample path by conditioning on the last observation (assuming really well estimated parameters and choice of model):&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;random_student_walk&lt;/span&gt;(n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;, mu &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, sigma &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;, initial_value &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; y[n_obs])&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;##  [1] 166 162 158 175 177 178 179 179 183 181 190 184&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And another one:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;random_student_walk&lt;/span&gt;(n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;, mu &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, sigma &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;, initial_value &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; y[n_obs])&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;##  [1] 145 145 146 147 141 139 135 135 162 166 164 161&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Third time&amp;rsquo;s a charm:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;random_student_walk&lt;/span&gt;(n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;, mu &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, sigma &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;, initial_value &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; y[n_obs])&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;##  [1] 162 153 155 157 152 152 149 158 160 173 182 184&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;Try &lt;code&gt;forecast::forecast(AirPassengers)&lt;/code&gt; to see what I mean.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:2&#34;&gt;&#xA;&lt;p&gt;Since the joint distribution is multidimensional across forecast horizons, a single sample is not a scalar but a vector. Many samples form a matrix. If we considered multiple time series, sample paths would form a tensor.&amp;#160;&lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:3&#34;&gt;&#xA;&lt;p&gt;Obviously we&amp;rsquo;re still computing integrals&amp;mdash;just instead of doing the math we &lt;a href=&#34;https://en.wikipedia.org/wiki/Numerical_integration&#34;&gt;approximate them by counting samples&lt;/a&gt;.&amp;#160;&lt;a href=&#34;#fnref:3&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>Be Skeptical of the t-SNE Bunny</title>
	  <link>https://minimizeregret.com/link/2022/07/11/tsne-bunny/</link>
	  <pubDate>Mon, 11 Jul 2022 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2022/07/11/tsne-bunny/</guid>
	  <description>&lt;p&gt;Matt Henderson on Twitter (&lt;a href=&#34;https://twitter.com/matthen2/status/1539376167257526272&#34;&gt;click through for the animation&lt;/a&gt;):&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Be skeptical of the clusters shown in t-SNE plots! Here we run t-SNE on a 3d shape - it quickly invents some odd clusters and structures that aren&amp;rsquo;t really present in the original bunny.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;What would happen if every machine learning method would come with a built-in visualization of the spurious results that it found?&lt;/p&gt;&#xA;&lt;p&gt;Never mind the the answer to that question. I think that this dimensionality reduction of a 3D bunny into two dimensions isn&amp;rsquo;t even all that bad&amp;mdash;the ears are still pretty cute. And it&amp;rsquo;s not like the original data had a lot more global and local structure once you consider that the bunny is not much more than noise in the shape of a rectangle with two ears that human eyes ascribe meaning to.&lt;/p&gt;&#xA;&lt;p&gt;I&amp;rsquo;m the first to admit that t-SNE, UMAP, and all kinds of other methods will produce clusters from whatever data you provide. But so will k-means always return &lt;code&gt;k&lt;/code&gt; clusters. One shouldn&amp;rsquo;t trust any model without some kind of evaluation of its results.&lt;/p&gt;&#xA;&lt;p&gt;If you don&amp;rsquo;t take them at face value, UMAP and Co. can be powerful tools &lt;a href=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/&#34;&gt;to explore data quickly and interactively&lt;/a&gt;. Look no further than the cool &lt;a href=&#34;https://www.youtube.com/watch?v=gDk7_f3ovIk&#34;&gt;workflows Vincent Warmerdam is building for annotating text&lt;/a&gt;.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Failure Modes of State Space Models</title>
	  <link>https://minimizeregret.com/post/2022/07/05/state-space-model-intricacies/</link>
	  <pubDate>Tue, 05 Jul 2022 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2022/07/05/state-space-model-intricacies/</guid>
	  <description>&lt;p&gt;State space models are great, but they will fail in predictable ways.&lt;/p&gt;&#xA;&lt;p&gt;Well, claiming that they &amp;ldquo;fail&amp;rdquo; is a bit unfair. They actually behave &lt;em&gt;exactly&lt;/em&gt; as they should given the input data. But if the input data fails to adhere to the Normal assumption or lacks stationarity, then this will affect the prediction derived from the state space models in perhaps unexpected yet deterministic ways.&lt;/p&gt;&#xA;&lt;p&gt;This article ensures that none of us is surprised by these &amp;ldquo;failure modes&amp;rdquo;.&lt;/p&gt;&#xA;&lt;h2 id=&#34;the-model&#34;&gt;The Model&lt;/h2&gt;&#xA;&lt;p&gt;To keep it instructive and because the analysis extends to the other standard model families of Innovation State Space Models (as in &lt;code&gt;forecast::ets()&lt;/code&gt;, for example) and Structural Time Series Models / Multiple Sources of Error State Space Models (as in &lt;code&gt;bsts::bsts()&lt;/code&gt;, for example), let&amp;rsquo;s consider the univariate local level model.&lt;/p&gt;&#xA;&lt;p&gt;Written as Structural Model, we have:&lt;/p&gt;&#xA;$$&#xA;y_t = l_{t-1} + \epsilon_t&#xA;$$$$&#xA;l_t = l_{t-1} + \eta_t&#xA;$$&lt;p&gt;for the measurement and the transition equation, respectively, where&lt;/p&gt;&#xA;$$&#xA;\epsilon_t \sim N(0, \sigma_{\epsilon})&#xA;$$$$&#xA;\eta_t \sim N(0, \sigma_{\eta}).&#xA;$$&lt;p&gt;The equivalent model in Innovation State Space form can be written as:&lt;/p&gt;&#xA;$$&#xA;y_t = l_{t-1} + \epsilon_t&#xA;$$$$&#xA;l_t = l_{t-1} + \alpha \cdot \epsilon_t&#xA;$$&lt;p&gt;where&lt;/p&gt;&#xA;$$&#xA;\epsilon_t \sim N(0, \sigma_{\epsilon}).&#xA;$$&lt;p&gt;In the following we will focus on the Innovation State Space Model formulation as we use the &lt;code&gt;forecast&lt;/code&gt; package to fit the model in our examples. However, the conclusions translate to the Structural Model as&lt;/p&gt;&#xA;$$&#xA;\alpha = - \frac{\sigma^2_{\eta}}{2\sigma^2_{\epsilon}} + \sqrt{(1 + \frac{\sigma^2_{\eta}}{2\sigma^2_{\epsilon}})^2-1}&#xA;$$&lt;p&gt;for $\alpha \in (0,1)$ as is the parameter space allowed by the &lt;code&gt;forecast::ets()&lt;/code&gt; implementation. For details, see chapter 13.4 of Hyndman et al. (2008).&lt;/p&gt;&#xA;&lt;p&gt;This local level model is equivalent to simple exponential smoothing. As such, it presents a trade-off between the two extremes of &lt;a href=&#34;https://otexts.com/fpp3/simple-methods.html#na%C3%AFve-method&#34;&gt;random walk&lt;/a&gt; and &lt;a href=&#34;https://otexts.com/fpp3/simple-methods.html#mean-method&#34;&gt;mean forecast&lt;/a&gt; models. The mean model implies a constant level that doesn&amp;rsquo;t change over time. The random walk model implies a level that adjusts fully to the most recent observation. The simple exponential smoothing forecast is a mix of the two: As $\alpha$ tends towards 1, the resulting model adjusts more to the most recent observations, thus behaving similar to the random walk. Inversely, as $\alpha$ tends to 0, the level is barely adjusted based on the most recent observations and equals instead the arithmetic mean of all observations.&lt;/p&gt;&#xA;&lt;p&gt;Equivalently: If $\sigma_{\epsilon}$ is larger then $\sigma_{\eta}$, the model will dominantly be driven by the observation noise and tend to the global mean. But if $\sigma_{\eta}$ is much larger than $\sigma_{\epsilon}$, the model is driven by the innovation noise: The level adjusts a lot and dominates the observation noise, thus favoring a model more similar to a random walk.&lt;/p&gt;&#xA;&lt;p&gt;These simple observations have strong implications as to how state space models behave in the presence of anomalies and structural breaks.&lt;/p&gt;&#xA;&lt;p&gt;But let&amp;rsquo;s first look at an example series produced by this model.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#&amp;#39; Sample a Local-Level Time Series via Multiple Sources of Error&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;gen_ssm_series &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt;(n, sigma_obs, sigma_level, l_0 &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  l_t &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rep&lt;/span&gt;(l_0, times &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; n &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  y_t &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;NA&lt;/span&gt;, times &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; n &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; (i &lt;span style=&#34;color:#66d9ef&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;(n&lt;span style=&#34;color:#ae81ff&#34;&gt;+1&lt;/span&gt;)) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;l_t[i] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; l_t[i&lt;span style=&#34;color:#ae81ff&#34;&gt;-1&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rnorm&lt;/span&gt;(n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, mean &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, sd &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; sigma_level)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;y_t[i] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; l_t[i&lt;span style=&#34;color:#ae81ff&#34;&gt;-1&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rnorm&lt;/span&gt;(n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, mean &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, sd &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; sigma_obs)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt;(y_t[&lt;span style=&#34;color:#ae81ff&#34;&gt;-1&lt;/span&gt;])&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We use $\sigma_{\epsilon} = 1$ and $\sigma_{\eta} = 1$ (equivalent to $\alpha = 0.62$) to generate a series based on equally strong measurement and transition noise.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;library&lt;/span&gt;(ggplot2)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;library&lt;/span&gt;(forecast)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;365&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;df &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;data.frame&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  index &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;80&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  value &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;gen_ssm_series&lt;/span&gt;(n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;80&lt;/span&gt;, sigma_obs &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, sigma_level &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ggp_base &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;ggplot&lt;/span&gt;(df, &lt;span style=&#34;color:#a6e22e&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; index, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; value)) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;geom_line&lt;/span&gt;(color &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;grey&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;geom_point&lt;/span&gt;(pch &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;21&lt;/span&gt;, color &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;, fill &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;, cex &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;labs&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Index&amp;#34;&lt;/span&gt;, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Value&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;print&lt;/span&gt;(ggp_base)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/05/state-space-model-intricacies/ggp_base-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Knowing the true underlying model, we can fit a local-level model using the &lt;code&gt;forecast&lt;/code&gt; package to try to recover the parameters.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ets_fit &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; forecast&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;ets&lt;/span&gt;(y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;value, model &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;ANN&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;print&lt;/span&gt;(ets_fit)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## ETS(A,N,N) &#xA;## &#xA;## Call:&#xA;##  forecast::ets(y = df$value, model = &amp;#34;ANN&amp;#34;) &#xA;## &#xA;##   Smoothing parameters:&#xA;##     alpha = 0.618 &#xA;## &#xA;##   Initial states:&#xA;##     l = -0.1009 &#xA;## &#xA;##   sigma:  1.6243&#xA;## &#xA;##      AIC     AICc      BIC &#xA;## 432.1522 432.4680 439.2983&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;With &lt;code&gt;alpha&lt;/code&gt; estimated as about the expected 0.62, the fitting procedure appears able to recover the underlying model well.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; Let&amp;rsquo;s take a quick look at the corresponding forecast:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ggp_base &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;autolayer&lt;/span&gt;(forecast&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;forecast&lt;/span&gt;(ets_fit, h &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/05/state-space-model-intricacies/ggp_base_w_forecast-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;The model can be interpreted as interpolation between random walk and mean forecast, so it&amp;rsquo;s expected to be close to the recent level of the series. Additionally, the prediction intervals are increasing in width, but not as fast as those of a random walk model would, and clearly not constant as those of a mean forecast as the local level is expected to fluctuate in the future.&lt;/p&gt;&#xA;&lt;p&gt;So far so good. Now how is this impacted by anomalies and structural breaks?&lt;/p&gt;&#xA;&lt;h2 id=&#34;cause-for-trouble-1-anomalies&#34;&gt;Cause for Trouble #1: Anomalies&lt;/h2&gt;&#xA;&lt;p&gt;Let&amp;rsquo;s consider an anomalous observation by adding it to the above series. To make the effect instructive, I won&amp;rsquo;t hold back&amp;mdash;let&amp;rsquo;s make it &lt;em&gt;really&lt;/em&gt; anomalous. This might look ridiculous, but believe me, this stuff exists.&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;df_w_anomaly &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; df&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;df_w_anomaly&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;value[55] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; df_w_anomaly&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;value[55] &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;100&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ggp_w_anomaly &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;ggplot&lt;/span&gt;(df_w_anomaly, &lt;span style=&#34;color:#a6e22e&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; index, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; value)) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;geom_line&lt;/span&gt;(color &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;grey&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;geom_point&lt;/span&gt;(pch &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;21&lt;/span&gt;, color &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;, fill &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;, cex &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;labs&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Index&amp;#34;&lt;/span&gt;, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Value&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;print&lt;/span&gt;(ggp_w_anomaly)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/05/state-space-model-intricacies/ggp_w_anomaly-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Let&amp;rsquo;s refit the model to consider the impact the anomaly has on the parameter estimates.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ets_fit_w_anomaly &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; forecast&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;ets&lt;/span&gt;(y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; df_w_anomaly&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;value, model &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;ANN&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;print&lt;/span&gt;(ets_fit_w_anomaly)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## ETS(A,N,N) &#xA;## &#xA;## Call:&#xA;##  forecast::ets(y = df_w_anomaly$value, model = &amp;#34;ANN&amp;#34;) &#xA;## &#xA;##   Smoothing parameters:&#xA;##     alpha = 1e-04 &#xA;## &#xA;##   Initial states:&#xA;##     l = 4.6512 &#xA;## &#xA;##   sigma:  11.4091&#xA;## &#xA;##      AIC     AICc      BIC &#xA;## 744.0427 744.3584 751.1887&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Remember that the original fit of the model above resulted in an &lt;code&gt;alpha&lt;/code&gt; value of &lt;code&gt;r round(ets_fit$par[&amp;quot;alpha&amp;quot;], 2)&lt;/code&gt; which reflected the underlying local-level model. In contrast, the &lt;code&gt;alpha&lt;/code&gt; value now dropped to &lt;code&gt;r round(ets_fit_w_anomaly$par[&amp;quot;alpha&amp;quot;], 2)&lt;/code&gt;: Instead of estimating a smooth interpolation of random walk and mean forecast, this &lt;code&gt;alpha&lt;/code&gt; parameter corresponds to an essentially pure mean forecast.&lt;/p&gt;&#xA;&lt;p&gt;This is immediately visible from the corresponding prediction intervals which are now constant:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ggp_w_anomaly &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;autolayer&lt;/span&gt;(forecast&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;forecast&lt;/span&gt;(ets_fit_w_anomaly, h &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/05/state-space-model-intricacies/ggp_w_anomaly_w_forecast-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Zooming in to roughly the scale of the original plot it&amp;rsquo;s also clear that the underlying Gaussian distributional assumption translates the anomaly into much larger prediction intervals:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ggp_w_anomaly &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;autolayer&lt;/span&gt;(forecast&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;forecast&lt;/span&gt;(ets_fit_w_anomaly, h &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;)) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;coord_cartesian&lt;/span&gt;(ylim &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;-2.5&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;15&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/05/state-space-model-intricacies/ggp_w_anomaly_w_forecast_zoomed-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Additionally, the forecast is clearly biased away from the most recent level as the current level of the series is not equal to its global average.&lt;/p&gt;&#xA;&lt;p&gt;Depending on the series itself and the size of the anomaly, a single anomaly leads the estimation procedure to choose an edge case of the structural time series parameter space, leading to poor forecasts.&lt;/p&gt;&#xA;&lt;p&gt;The reason for this is simple: The mean forecast has the overall smallest (squared) errors once the anomaly is added. By choosing a mean forecast rather than something closer to a random walk, we only make the error introduced by the anomaly once, whereas a random walk would make it twice: Once for the anomaly itself, and then a second time right after the anomaly when jumping back to the actual level.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;df_w_anomaly_residuals &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;data.frame&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  index &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rep&lt;/span&gt;(df_w_anomaly&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;index, times &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  residuals &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;df_w_anomaly&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;value &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;mean&lt;/span&gt;(df_w_anomaly&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;value),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;df_w_anomaly&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;value &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;  &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;NA&lt;/span&gt;, df_w_anomaly&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;value[&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;length&lt;/span&gt;(df_w_anomaly&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;index)])&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  Model &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Mean&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Random Walk&amp;#34;&lt;/span&gt;), &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;  each &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;length&lt;/span&gt;(df_w_anomaly&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;index))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;ggplot&lt;/span&gt;(df_w_anomaly_residuals, &lt;span style=&#34;color:#a6e22e&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; index, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; residuals)) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;geom_line&lt;/span&gt;(color &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;grey&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;geom_point&lt;/span&gt;(pch &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;21&lt;/span&gt;, color &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;, fill &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;, cex &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;labs&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Index&amp;#34;&lt;/span&gt;, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Residual&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;facet_wrap&lt;/span&gt;(&lt;span style=&#34;color:#f92672&#34;&gt;~&lt;/span&gt; Model)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/05/state-space-model-intricacies/df_w_anomaly_residuals-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Let&amp;rsquo;s move on to the other cause for trouble: structural breaks.&lt;/p&gt;&#xA;&lt;h2 id=&#34;cause-for-trouble-2-structural-breaks&#34;&gt;Cause for Trouble #2: Structural Breaks&lt;/h2&gt;&#xA;&lt;p&gt;While there are many kinds of structural breaks, I&amp;rsquo;m concerned with level shifts here. This is a bit simplistic but instructive: Let&amp;rsquo;s shift the last 25 observations upward by a constant to introduce the shift for this example.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;df_w_shift &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; df&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;df_w_shift&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;value[55&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;80&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; df_w_shift&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;value[55&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;80&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;25&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ggp_w_shift &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;ggplot&lt;/span&gt;(df_w_shift, &lt;span style=&#34;color:#a6e22e&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; index, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; value)) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;geom_line&lt;/span&gt;(color &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;grey&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;geom_point&lt;/span&gt;(pch &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;21&lt;/span&gt;, color &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;, fill &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;, cex &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;labs&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Index&amp;#34;&lt;/span&gt;, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Value&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;print&lt;/span&gt;(ggp_w_shift)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/05/state-space-model-intricacies/ggp_w_shift-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;As before, let&amp;rsquo;s refit the model to consider the impact the level shift has on the parameter estimates.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ets_fit_w_shift &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; forecast&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;ets&lt;/span&gt;(y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; df_w_shift&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;value, model &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;ANN&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;print&lt;/span&gt;(ets_fit_w_shift)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## ETS(A,N,N) &#xA;## &#xA;## Call:&#xA;##  forecast::ets(y = df_w_shift$value, model = &amp;#34;ANN&amp;#34;) &#xA;## &#xA;##   Smoothing parameters:&#xA;##     alpha = 0.7575 &#xA;## &#xA;##   Initial states:&#xA;##     l = -0.3653 &#xA;## &#xA;##   sigma:  3.2533&#xA;## &#xA;##      AIC     AICc      BIC &#xA;## 543.2815 543.5973 550.4275&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The structural break leads to a new estimate of &lt;code&gt;alpha&lt;/code&gt; at &lt;code&gt;r round(ets_fit_w_shift$par[&amp;quot;alpha&amp;quot;], 2)&lt;/code&gt;. Whereas the anomaly pushed the model towards a mean forecast, the level shift pushes it towards a random walk (though not all the way there) as that&amp;rsquo;s the model that adjusts quickest to the new level, thus avoiding the source of the single largest error in this data.&lt;/p&gt;&#xA;&lt;p&gt;Again, this is visible from the residuals of the two edge cases:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;df_w_shift_residuals &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;data.frame&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  index &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rep&lt;/span&gt;(df_w_shift&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;index, times &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  residuals &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;df_w_shift&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;value &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;mean&lt;/span&gt;(df_w_shift&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;value),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;df_w_shift&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;value &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;  &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;NA&lt;/span&gt;, df_w_shift&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;value[&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;length&lt;/span&gt;(df_w_shift&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;index)])&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  Model &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Mean&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Random Walk&amp;#34;&lt;/span&gt;), &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;  each &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;length&lt;/span&gt;(df_w_shift&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;index))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;ggplot&lt;/span&gt;(df_w_shift_residuals, &lt;span style=&#34;color:#a6e22e&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; index, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; residuals)) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;geom_line&lt;/span&gt;(color &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;grey&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;geom_point&lt;/span&gt;(pch &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;21&lt;/span&gt;, color &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;, fill &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;, cex &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;labs&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Index&amp;#34;&lt;/span&gt;, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Residual&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;facet_wrap&lt;/span&gt;(&lt;span style=&#34;color:#f92672&#34;&gt;~&lt;/span&gt; Model)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/05/state-space-model-intricacies/df_w_shift_residuals-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;While the residuals look reasonable, a random walk forecast is not really ideal for the data. It&amp;rsquo;s good to model the shift, yes&amp;mdash;but beyond that it leads to prediction intervals that are larger and growing faster than the period before the shift or the period after the shift would imply by themselves.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ggp_w_shift &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;autolayer&lt;/span&gt;(forecast&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;forecast&lt;/span&gt;(ets_fit_w_shift, h &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2022/07/05/state-space-model-intricacies/ggp_w_shift_w_forecast-1.png&#34;&gt;&lt;/figure&gt;&lt;h2 id=&#34;everything-works-as-intended&#34;&gt;Everything Works as Intended&lt;/h2&gt;&#xA;&lt;p&gt;At this point, let me just reiterate that everything here works as intended.&lt;/p&gt;&#xA;&lt;p&gt;Anomalies are just large observation noise so that&amp;rsquo;s what the model focuses on, resulting in a forecast dominated by observation noise&amp;mdash;the mean forecast.&lt;/p&gt;&#xA;&lt;p&gt;Level shifts are just large transition noise, so that&amp;rsquo;s what the model focuses on, resulting in a forecast dominated by transition noise&amp;mdash;the random walk forecast.&lt;/p&gt;&#xA;&lt;p&gt;If you use standard state space model implementations for your applications, you might as well write unit tests that check these behaviors and show them to your stakeholders. If your stakeholders&amp;rsquo; time series show these patterns, then your models will adjust their forecasts accordingly.&lt;/p&gt;&#xA;&lt;h2 id=&#34;everything-works-as-intended-and-yet&#34;&gt;Everything Works as Intended, and yet&amp;hellip;&lt;/h2&gt;&#xA;&lt;p&gt;These fits frustrate me. The time series look good for the most part! But a single anomaly, a single level shift can mess the forecast up and make the forecast distribution useless.&lt;/p&gt;&#xA;&lt;p&gt;With regard to anomalies, it would be nice to have model parameters that are robust against them but to reflect them properly in the uncertainty of the forecast. If we had fitted a model with &lt;code&gt;alpha&lt;/code&gt; close to the original one but larger prediction intervals due to a larger &lt;code&gt;sigma&lt;/code&gt;&amp;mdash;that&amp;rsquo;s one thing. But the impact on the &lt;code&gt;alpha&lt;/code&gt; and the corresponding bias of the forecast is not really acceptable. A low-hanging solution appears to be a different distributional assumption that replaces the Gaussian distribution as done in the &lt;a href=&#34;https://orbit-ml.readthedocs.io/en/latest/tutorials/dlt.html&#34;&gt;models of the Orbit package&lt;/a&gt;. While this may fix the distorted &lt;code&gt;sigma&lt;/code&gt; estimate, it won&amp;rsquo;t in general &amp;ldquo;fix&amp;rdquo; the smoothing parameters.&lt;/p&gt;&#xA;&lt;p&gt;The level shift is also a bummer. Even forgetting the first half of the series and only using the observations after the shift would lead to a more appropriate model fit! But forgetting history is not something the state space models do: All observations are used to evaluate the likelihood, always.&lt;/p&gt;&#xA;&lt;p&gt;Messy data needs messier models than the clean standard implementations with proven convergence behaviors.&lt;/p&gt;&#xA;&lt;h2 id=&#34;references&#34;&gt;References&lt;/h2&gt;&#xA;&lt;p&gt;Andrew C. Harvey (1990). &lt;strong&gt;Forecasting, Structural Time Series Models and the Kalman Filter&lt;/strong&gt;. &lt;a href=&#34;https://doi.org/10.1017/CBO9781107049994&#34;&gt;https://doi.org/10.1017/CBO9781107049994&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Rob Hyndman, Anne Koehler, Keith Ord, Ralph Snyder (2008). &lt;strong&gt;Forecasting with Exponential Smoothing: The State Space Approach&lt;/strong&gt;. &lt;a href=&#34;https://doi.org/10.1007/978-3-540-71918-2&#34;&gt;https://doi.org/10.1007/978-3-540-71918-2&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Edwin Ng, Zhishi Wang, Huigang Chen, Steve Yang, Slawek Smyl (2020). &lt;strong&gt;Orbit: Probabilistic Forecast with Exponential Smoothing&lt;/strong&gt;. &lt;a href=&#34;https://arxiv.org/abs/2004.08492&#34;&gt;https://arxiv.org/abs/2004.08492&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Steven L. Scott (2017). &lt;strong&gt;Fitting Bayesian structural time series with the bsts R package&lt;/strong&gt;. &lt;a href=&#34;https://www.unofficialgoogledatascience.com/2017/07/fitting-bayesian-structural-time-series.html&#34;&gt;https://www.unofficialgoogledatascience.com/2017/07/fitting-bayesian-structural-time-series.html&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;This is a lucky seed. Results will vary somewhat as you re-draw the series, of course. But most series will be recovered fairly well by the &lt;code&gt;forecast::ets()&lt;/code&gt; function.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:2&#34;&gt;&#xA;&lt;p&gt;The conclusions drawn from this example hold also for smaller anomalies, directionally.&amp;#160;&lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>Approach to Estimate Uncertainty Distributions of Walmart Sales</title>
	  <link>https://minimizeregret.com/link/2021/12/29/uncertainty-distributions-of-walmart-sales/</link>
	  <pubDate>Wed, 29 Dec 2021 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/link/2021/12/29/uncertainty-distributions-of-walmart-sales/</guid>
	  <description>&lt;blockquote&gt;&#xA;&lt;p&gt;We present our solution for the M5 Forecasting - Uncertainty competition. Our solution ranked 6th out of 909 submissions across all hierarchical levels and ranked first for prediction at the finest level of granularity (product-store sales, i.e. SKUs). The model combines a multi-stage state-space model and Monte Carlo simulations to generate the forecasting scenarios (trajectories). Observed sales are modelled with negative binomial distributions to represent discrete over-dispersed sales. Seasonal factors are hand-crafted and modelled with linear coefficients that are calculated at the store-department level.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;The approach chosen by this team of prior Lokad employees hits all the sweet spots. It’s simple, yet comes 6th in a Kaggle challenge, and produces multi-horizon sample paths.&lt;/p&gt;&#xA;&lt;p&gt;Having the write-up of a well-performing result available in this detail is great—they share some nuggets:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Considering the small search space, this optimisation is done via grid search.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Easy to do for a two-parameter model and a neat trick to get computational issues under control. Generally neat to also enforce additional prior knowledge via arbitrary constraints on the search space.&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;According to the M5 survey by Makridakis et al. [3], our solution had the best result at the finest level of granularity (level 12 in the competition), commonly referred to as product-store level or SKU level (Stock Keeping Unit). For store replenishment and numerous other problems, the SKU level is the most relevant level.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Good on them to point this out. Congrats!&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Forecasting Uncertainty Is Never Too Large</title>
	  <link>https://minimizeregret.com/note/2021/09/01/forecasting-uncertainty-is-never-too-large/</link>
	  <pubDate>Wed, 01 Sep 2021 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2021/09/01/forecasting-uncertainty-is-never-too-large/</guid>
	  <description>&lt;p&gt;Rob J. Hyndman gave a presentation titled “Uncertain futures: what can we forecast and when should we give up?” as part of the &lt;a href=&#34;https://acems.org.au/acems-public-lecture-series&#34;&gt;ACEMS public lecture series&lt;/a&gt; with &lt;a href=&#34;https://youtu.be/YOeDTt_JJx0&#34;&gt;recording available on Youtube&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;He makes an often underappreciated point &lt;a href=&#34;https://youtu.be/YOeDTt_JJx0?t=3027&#34;&gt;around minute 50&lt;/a&gt; of the talk:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;When the forecast uncertainty is too large to assist decision making? I don’t think that’s ever the case. Forecasting uncertainty being too large does assist decision making by telling the decision makers that the future is very uncertain and they should be planning for lots of different possible outcomes and not assuming just one outcome or another. And one of the problems we have in providing forecasts to decision makers is getting them to not focus in on the most likely outcome but to actually take into account the range of possibilities and to understand that futures are uncertain, that they need to plan for that uncertainty.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;</description>
	</item>
	<item>
	  <title>What Needs to Prove True for This to Work?</title>
	  <link>https://minimizeregret.com/post/2021/08/12/what-needs-to-prove-true-for-this-to-work/</link>
	  <pubDate>Thu, 12 Aug 2021 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2021/08/12/what-needs-to-prove-true-for-this-to-work/</guid>
	  <description>&lt;p&gt;Data science projects are a tricky bunch. They entice you with challenging problems and promise a huge return if successful. In contrast to more traditional software engineering projects, however, data science projects entail more upfront uncertainty: You’ll not know until you tried whether the technology is &lt;em&gt;good enough&lt;/em&gt; to solve the problem. Consequently, a data science endeavor fails more often, or doesn’t turn out to be the smash hit you and your stakeholders expected it to be.&lt;/p&gt;&#xA;&lt;p&gt;To get started working on projects that &lt;em&gt;are&lt;/em&gt; sucessful, you want to cut your losses on projects that will not work out; there is no honor in sticking with a problem just because you started. Don’t get caught up in a project that simmers on and on and doesn’t realize the value that was promised in the original slide deck. Even better: Don’t start the project that will not succeed.&lt;/p&gt;&#xA;&lt;p&gt;While this last recommendation is hard if not impossible to get right, ask yourself “What needs to prove true for this to work?” to make it a bit easier.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; Asking this question is a powerful way to drill into the assumptions that underlie any project—data science or not.&lt;/p&gt;&#xA;&lt;p&gt;By asking “What needs to prove true for this to work?” you as data scientist are explicit about the assumptions that are made on your contribution to a new project. You start validating whether you can deliver what others hope you will deliver.&lt;/p&gt;&#xA;&lt;p&gt;For example, let’s assume your task is to provide an image classification algorithm that improves over an existing method. The images are scans of parts of human patients and will need to be acquired. An implicit assumption is that you will evaluate and compare the performance of the algorithms. One easily neglects that this requires observations beyond those used to train the algorithm—potentially even quite many if you want to provide any useful level of certainty. Thus, before committing resources to the project, the required amount of samples can be determined and communicated.&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#xA;&lt;p&gt;For each project, you’ll encounter several assumptions that need to prove true. You can rank them by their importance and their uncertainty. Similarly to how an active learning algorithm selects the next observation to be labelled, you need to clarify the most important assumptions with the largest uncertainty first and then proceed to the next one. Or, if you can’t clarify them, choose a project for which the assumptions are not as uncertain.&lt;/p&gt;&#xA;&lt;p&gt;All this is encapsulated in data scientists’ natural drive to access a project’s data as soon as possible. Nothing reduces the general uncertainty more than having a look at the data you’re supposed to work with.&lt;/p&gt;&#xA;&lt;p&gt;There is a flip side to this: If you don’t yet have access to the data or when data acquisition is part of the project, you can still (or likely have to) formalize what you need the data to look like to deliver a useful product. You could, for example, simulate data to get a feeling for how the data can be analyzed, and to show at what noise level which model performance can be achieved.&lt;/p&gt;&#xA;&lt;p&gt;If you do have data, establish a human baseline performance. How well can you predict the data? If you can’t do it, why do you expect the algorithm to be able to do it? Think about it like an econometrics professor: Which theory would explain that your available features can predict the outcome?&lt;/p&gt;&#xA;&lt;p&gt;All this applies to every project you are about to embark on, but also to different steps within a project. To say it in the words of &lt;a href=&#34;https://www.amazon.com/How-Will-Measure-Your-Life/dp/0062102419&#34;&gt;Christensen, Allworth, and Dillon&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;[K]eep thinking about the most important assumptions that have to prove true, and how you can swiftly and inexpensively test if they are valid. Make sure you are being realistic about the path ahead of you.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;The first time I saw this was in the book &lt;a href=&#34;https://www.amazon.com/How-Will-Measure-Your-Life/dp/0062102419&#34;&gt;“How Will You Measure Your Life?” by Clayton M. Christensen, James Allworth, and Karen Dillon&lt;/a&gt;, which is excellent. They in turn attribute it to a process called “discovery-driven planning” by Ian MacMillan and Rita McGrath.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:2&#34;&gt;&#xA;&lt;p&gt;See the paper &lt;a href=&#34;https://arxiv.org/abs/1211.1323&#34;&gt;“Sample Size Planning for Classification Models”&lt;/a&gt; (2012) by Beleites et al. for an example of what this can look like.&amp;#160;&lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>Embedding Many Time Series via Recurrence Plots</title>
	  <link>https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/</link>
	  <pubDate>Sun, 14 Jun 2020 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/</guid>
	  <description>&lt;p&gt;&lt;em&gt;We demonstrate how recurrence plots can be used to embed a large set of time series via UMAP and HDBSCAN to quickly identify groups of series with unique characteristics such as seasonality or outliers. The approach supports exploratory analysis of time series via visualization that scales poorly when combined with large sets of related time series. We show how it works using a Walmart dataset of sales and a Citi Bike dataset of bike rides.&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;Traditional time series courses act as if we had plenty of time to take care of time series individually, one after another. In reality, though, we&amp;rsquo;re often faced with a large set of time series&amp;mdash;too large to look at each time series yet alone to check them repeatedly. This conflicts with the value that comes from visualizing time series to understand how they might need to be modeled. Randomly picking time series from the larger set is one way to deal with this issue. But I&amp;rsquo;ve also come to appreciate the following combination of tricks to quickly become aware of broader patterns and issues in a given set of data.&lt;/p&gt;&#xA;&lt;h2 id=&#34;recurrence-plots&#34;&gt;Recurrence Plots&lt;/h2&gt;&#xA;&lt;p&gt;Recurrence plots are not a new idea but I only became aware of them through the recent paper by &lt;a href=&#34;https://arxiv.org/abs/1904.08064&#34;&gt;Lia et al. (2020)&lt;/a&gt; who build on top of the original description by &lt;a href=&#34;https://iopscience.iop.org/article/10.1209/0295-5075/4/9/004&#34;&gt;Eckmann et al. (1987)&lt;/a&gt;. Given a time series with observations $y_1, ..., y_T$, the authors check for each combination of two observations $i$ and $j$ with $i &lt; j$ whether $|y_j - y_i| &lt; \epsilon$ with $\epsilon &gt; 0$. If this holds, then the observation has &lt;em&gt;recurred&lt;/em&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Given that many important patterns in time series can be described by how similar two points in the time series are to each other, plotting this recurrence information can be helpful to spot said patterns.&lt;/p&gt;&#xA;&lt;p&gt;I suppose that the binary measure was used in 1987 as it was easier to print a graph in black-and-white rather than shades of color. Nowadays though, we can look at the continuous version of the (absolute) difference between two points. Using all combinations of $i$ and $j$, we get following square recurrence plots:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-2-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Real time series, of course, do not have patterns this clean. This makes it difficult to draw conclusions from these graphs when they resemble the one on the right rather than the two on the left. Lia et al. (2020) thus took to Convolutional Neural Networks to analyze a large set of time series via recurrence plots.&lt;/p&gt;&#xA;&lt;p&gt;Lia et al. (2020) had a specific task that needed solving. However, when it comes to exploration of the data set only, &lt;a href=&#34;https://umap-learn.readthedocs.io/&#34;&gt;UMAP&lt;/a&gt; is a favorite of mine. Similarly to how &lt;a href=&#34;https://minimizeregret.com/post/2020/04/26/are-you-sure-this-embedding-is-good-enough/&#34;&gt;UMAP can embed the MNIST dataset&lt;/a&gt;, we can run UMAP on recurrence plots of many time series to project each time series onto a two-dimensional space. In this representation, time series with similar recurrence plots should be clustered and spotting commonalities among them becomes easier.&lt;/p&gt;&#xA;&lt;h2 id=&#34;umap-on-recurrence-plots&#34;&gt;UMAP on Recurrence Plots&lt;/h2&gt;&#xA;&lt;p&gt;To explore the usefulness of recurrence plots combined with UMAP, we consider two freely available data sets of many time series: 1) &lt;a href=&#34;https://s3.amazonaws.com/tripdata/index.html&#34;&gt;Daily number of rides starting from Citi Bike stations in New York&lt;/a&gt;, and 2) &lt;a href=&#34;https://www.kaggle.com/c/m5-forecasting-accuracy&#34;&gt;daily sales of products at 10 different Walmart locations in the US&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Both datasets consist of daily observations. Here, however, we load them aggregated to monthly values (&lt;code&gt;date&lt;/code&gt; is formatted as &lt;code&gt;YYYY-MM-01&lt;/code&gt;). The reason for this is mostly that the recurrence plots create large datasets: What previously was a time series of $N$ observations turns into at least $N^2/2$ observations. This is a major drawback to consider&amp;mdash;especially when going beyond a single series.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;glimpse&lt;/span&gt;(citibike)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## Observations: 46,170&#xA;## Variables: 3&#xA;## $ id    &amp;lt;dbl&amp;gt; 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72…&#xA;## $ date  &amp;lt;date&amp;gt; 2013-07-01, 2013-08-01, 2013-09-01, 2013-10-01, 2013-11-0…&#xA;## $ value &amp;lt;dbl&amp;gt; 3575, 3675, 3153, 3325, 2122, 1213, 924, 676, 1433, 2380, …&#xA;```r&#xA;glimpse(walmart)&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## Observations: 320,000&#xA;## Variables: 3&#xA;## $ id    &amp;lt;chr&amp;gt; &amp;#34;FOODS_1_012_CA_1_validation&amp;#34;, &amp;#34;FOODS_1_012_CA_1_validatio…&#xA;## $ date  &amp;lt;date&amp;gt; 2011-01-01, 2011-02-01, 2011-03-01, 2011-04-01, 2011-05-0…&#xA;## $ value &amp;lt;dbl&amp;gt; 0, 0, 0, 0, 0, 0, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 15, 89, 13…&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Note that for both datasets the observations are non-negative counts. This is what a randomly picked time series from the &lt;code&gt;citibike&lt;/code&gt; dataset looks like:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-5-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;And this is what sales for a randomly picked product at Walmart look like:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-6-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;The latter time series of Walmart highlight another detail that needs to be considered when using recurrence plots the way I do: Since we want to feed every pixel of the recurrence plot as a feature to UMAP, we need the same number of pixels for each time series. To achieve this when new Citi Bike stations are added over time and products are launched or discontinued at Walmart, I pad each time series with zeros as needed. At least in the case of count time series the zero is a natural choice. However, we will see in the following that even in this case the padding does have a strong impact on the results.&lt;/p&gt;&#xA;&lt;p&gt;To run the analysis, we first need to compute the recurrence plots for each time series of the dataset. We start by analyzing the Walmart dataset.&lt;/p&gt;&#xA;&lt;h3 id=&#34;umap-and-recurrence-plots-for-walmart-sales&#34;&gt;UMAP and Recurrence Plots for Walmart Sales&lt;/h3&gt;&#xA;&lt;p&gt;&lt;em&gt;In the following, I use a few convenience functions &lt;a href=&#34;https://github.com/timradtke/recur&#34;&gt;shared as tiny R package &lt;code&gt;recur&lt;/code&gt; on Github&lt;/a&gt;. They wrap in part functions from the &lt;code&gt;umap&lt;/code&gt; and &lt;code&gt;hdbscan&lt;/code&gt; package which do the actual work.&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;We take the dataset and compute the recurrence plot for each product. The &lt;a href=&#34;https://github.com/timradtke/recur/blob/master/R/measure.R&#34;&gt;&lt;code&gt;recur::measure()&lt;/code&gt;&lt;/a&gt; function does exactly this. Here, we return the recurrence pixels in a long table after having computed the full $N \times N$ matrix. Before computing the recurrence values, it min-max standardizes all values for each time series to values between 0 and 1.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;walmart_recur &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; recur&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;measure&lt;/span&gt;(walmart, shape &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;long&amp;#34;&lt;/span&gt;, size &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;square&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The recurrence plot of the product shown above looks as follows, with a large area reserved for the initial zero padding. The area with recurrence of points after the product started selling appears without obvious pattern and is limited to less than a quarter of the pixels.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-8-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;With the recurrence pixels for each time series in hand, we can pass them to UMAP to get a two-dimensional embedding of the time series. &lt;a href=&#34;https://github.com/timradtke/recur/blob/master/R/embed.R&#34;&gt;&lt;code&gt;recur::embed()&lt;/code&gt;&lt;/a&gt; is a convenience function that first calls &lt;code&gt;recur::measure()&lt;/code&gt; and then &lt;code&gt;umap::umap()&lt;/code&gt; to do this.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;walmart_embed &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; recur&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;embed&lt;/span&gt;(data &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; walmart)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Even before visualizing the embedding, we continue and cluster the products in the embedding space via HDBSCAN where the &lt;a href=&#34;https://github.com/timradtke/recur/blob/master/R/cluster.R&#34;&gt;&lt;code&gt;recur::cluster()&lt;/code&gt;&lt;/a&gt; function calls &lt;code&gt;dbscan::hdbscan()&lt;/code&gt; to do so.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;walmart_cluster &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; recur&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;cluster&lt;/span&gt;(walmart_embed)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-11-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;There appear to exist groups of time series that are &lt;em&gt;very&lt;/em&gt; different from the rest. If we focus on the central, largest set of time series, we see that they are mostly densely clustered.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-12-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Since this by itself might not be overly useful, we can instead look at the $n$ time series in cluster $k$ with the largest HDBSCAN cluster membership probability using the following function:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plot_top_n_from_cluster_k &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt;(clustered, data, n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;9&lt;/span&gt;, k &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  filtered &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; clustered&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;embedding[clustered&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;embedding&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;cluster &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; k,]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ordered &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; filtered&lt;span style=&#34;color:#a6e22e&#34;&gt;[order&lt;/span&gt;(filtered&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;membership_prob, decreasing &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;TRUE&lt;/span&gt;),]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  n &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;min&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;nrow&lt;/span&gt;(ordered), n)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  pp &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; data[data&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;id &lt;span style=&#34;color:#f92672&#34;&gt;%in%&lt;/span&gt; ordered&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;id[1&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;n],] &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#a6e22e&#34;&gt;ggplot&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; date, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; value)) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#a6e22e&#34;&gt;geom_line&lt;/span&gt;(color &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;grey&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#a6e22e&#34;&gt;geom_point&lt;/span&gt;() &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#a6e22e&#34;&gt;facet_wrap&lt;/span&gt;(&lt;span style=&#34;color:#f92672&#34;&gt;~&lt;/span&gt;id, scales &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;free&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#a6e22e&#34;&gt;labs&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Month&amp;#34;&lt;/span&gt;, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Sales&amp;#34;&lt;/span&gt;, &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9; title &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;paste0&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Top &amp;#34;&lt;/span&gt;, n, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34; Series in Cluster &amp;#34;&lt;/span&gt;, k))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt;(pp)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-14-1.png&#34;&gt;&#xA;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-14-2.png&#34;&gt;&#xA;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-14-3.png&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;If you would reproduce this at home and check more of the clusters, you would notice that many of the clusters consist of a set of time series that starts &lt;em&gt;after&lt;/em&gt; the earliest possible start date and thus has 0 sales for the first months. This can mean, of course, that a quarter or half or more of the values of the series are &lt;em&gt;exactly&lt;/em&gt; equal to other series with the same start date, while at the same time the importance of the differences between the actual observations is lessened by the min-max scaling. One example of this is the following cluster.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-15-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;So the method is certainly not perfect. But this drawback can also help to check your assumptions on whether the data is clean or not. It can also identify groups of time series that need a different modeling approach than others.&lt;/p&gt;&#xA;&lt;h3 id=&#34;umap-and-recurrence-plots-for-citi-bike-rides&#34;&gt;UMAP and Recurrence Plots for Citi Bike Rides&lt;/h3&gt;&#xA;&lt;p&gt;We can now apply the same methods to the Citi Bike data. An interesting differentiation to the Walmart data is that most time series here have a strong yearly seasonality. This, for example, is what the recurrence plot of Citi Bike station 360 looks like:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-16-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Compared to the plot of the Walmart product above, this is much closer to the idealized sine curve recurrence pattern!&lt;/p&gt;&#xA;&lt;p&gt;To analyze the entire dataset, we take the monthly observations of Citi Bike rides, compute the recurrence plots and pass them to UMAP and HDBSCAN, just as before.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;citibike_embed &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; recur&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;embed&lt;/span&gt;(data &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; citibike)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;citibike_cluster &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; recur&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;cluster&lt;/span&gt;(citibike_embed)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This time, the global dispersion in the UMAP embedding is not quite as extreme as for Walmart, and HDBSCAN is able to cluster the time series nicely.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-17-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;We peak into the clusters to reveal that yet again the clusters depend heavily on the dates at which the respective stations within the cluster were used for the first/last time, as this provides UMAP with the largest &lt;em&gt;global&lt;/em&gt; variation.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-20-1.png&#34;&gt;&#xA;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-20-2.png&#34;&gt;&#xA;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-20-3.png&#34;&gt;&#xA;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-20-4.png&#34;&gt;&lt;/p&gt;&#xA;&lt;h2 id=&#34;citi-bike-recurrence-plots-and-location-data&#34;&gt;Citi Bike Recurrence Plots and Location Data&lt;/h2&gt;&#xA;&lt;p&gt;A unique feature of the Citi Bike data is that each time series corresponds to a location in New York since the rides are counted for each station at which a ride starts. The coordinates of Citi Bike stations are also available with the ride data which let&amp;rsquo;s us make use of this information.&lt;/p&gt;&#xA;&lt;p&gt;We can take the cluster information that we used in the plot of the embedding above and project the stations with the cluster information onto their respective coordinate. The resulting plot reveals that Citi Bike rolled out its stations by distinct areas in New York. The largest cluster (pink) corresponds to the stations available from the start, which are located in Manhattan south of Central Park, as well as the parts of Brooklyn that connect to Manhattan via the Williamsburg Bridge, Brooklyn Bridge and Manhattan Bridge.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/14/embedding-many-time-series-via-recurrence-plots/unnamed-chunk-21-1.png&#34;&gt;&lt;/figure&gt;&lt;h2 id=&#34;references&#34;&gt;References&lt;/h2&gt;&#xA;&lt;p&gt;Citi Bike data available at &lt;a href=&#34;https://s3.amazonaws.com/tripdata/index.html&#34;&gt;https://s3.amazonaws.com/tripdata/index.html&lt;/a&gt;. See also &lt;a href=&#34;https://www.citibikenyc.com/system-data&#34;&gt;https://www.citibikenyc.com/system-data&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;The Walmart data set is part of the M5 competition hosted on Kaggle, see &lt;a href=&#34;https://www.kaggle.com/c/m5-forecasting-accuracy&#34;&gt;https://www.kaggle.com/c/m5-forecasting-accuracy&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Xixi Lia, Yanfei Kanga, Feng Li (2020). &lt;a href=&#34;https://arxiv.org/abs/1904.08064&#34;&gt;&lt;em&gt;Forecasting with Time Series Imaging&lt;/em&gt;&lt;/a&gt;, ArXiv e-prints 1802.03426.&lt;/p&gt;&#xA;&lt;p&gt;J.-P. Eckmann, S. Oliffson Kamphorst, D. Ruelle (1987). &lt;a href=&#34;https://iopscience.iop.org/article/10.1209/0295-5075/4/9/004&#34;&gt;&lt;em&gt;Recurrence Plots of Dynamical Systems&lt;/em&gt;&lt;/a&gt;, EPL (Europhysics Letters), Vol. 4, 9.&lt;/p&gt;&#xA;&lt;p&gt;L. McInnes, J. Healy, J. Melville (2018). &lt;a href=&#34;https://arxiv.org/abs/1802.03426&#34;&gt;&lt;em&gt;UMAP: Uniform Manifold Approximation and Projection for Dimension Reduction&lt;/em&gt;&lt;/a&gt;, ArXiv e-prints 1802.03426.&lt;/p&gt;&#xA;&lt;p&gt;L. McInnes, J. Healy, S. Astels. &lt;em&gt;The hdbscan Clustering Library&lt;/em&gt;.&lt;/p&gt;&#xA;&lt;p&gt;R. J.G.B. Campello, D. Moulavi, J. Sander (2013). &lt;em&gt;Density-Based Clustering Based on Hierarchical Density Estimates&lt;/em&gt;, PAKDD 2013, Part II, LNAI 7819, pp. 160-172.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Rediscovering Bayesian Structural Time Series</title>
	  <link>https://minimizeregret.com/post/2020/06/07/rediscovering-bayesian-structural-time-series/</link>
	  <pubDate>Sun, 07 Jun 2020 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2020/06/07/rediscovering-bayesian-structural-time-series/</guid>
	  <description>&lt;p&gt;&lt;em&gt;This article derives the Local-Linear Trend specification of the Bayesian Structural Time Series model family from scratch, implements it in Stan and visualizes its components via &lt;code&gt;tidybayes&lt;/code&gt;. To provide context, links to GAMs and the &lt;code&gt;prophet&lt;/code&gt; package are highlighted. The code is available &lt;a href=&#34;https://github.com/timradtke/bsts-stan&#34;&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;I tried to come up with a simple way to detect &amp;ldquo;outliers&amp;rdquo; in time series. Nothing special, no anomaly detection via variational auto-encoders, just finding values of low probability in a univariate time series. The week before, I had been toying with generalized additive models (GAMs). So it seemed like a good idea to fit a GAM with a spline-based smooth term to model the trend flexibly while other features take care of everything else (seasonality, &amp;hellip;). But really the flexible trend is nice and simple and you don&amp;rsquo;t even need to think too much:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;573&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;n &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;48&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;x &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;n&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;y &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rnorm&lt;/span&gt;(n, &lt;span style=&#34;color:#a6e22e&#34;&gt;tanh&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;), &lt;span style=&#34;color:#ae81ff&#34;&gt;0.25&lt;/span&gt; )&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;gam_fit &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; mgcv&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;gam&lt;/span&gt;(y &lt;span style=&#34;color:#f92672&#34;&gt;~&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;s&lt;/span&gt;(x))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/07/rediscovering-bayesian-structural-time-series/unnamed-chunk-3-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;This is all fine and well to get a nice in-sample fit which is all you need for standard outlier detection. For example, the &lt;code&gt;forecast&lt;/code&gt; package&amp;rsquo;s &lt;code&gt;tsoutliers()&lt;/code&gt; function relies on the non-parametric &lt;code&gt;stats::supsmu()&lt;/code&gt; function to fit a flexible trend.&lt;/p&gt;&#xA;&lt;p&gt;But then I went one step too far: &amp;ldquo;Wouldn&amp;rsquo;t it be nice to use a GAM to &lt;em&gt;forecast&lt;/em&gt; my time series as well?&amp;rdquo;&lt;/p&gt;&#xA;&lt;p&gt;If you&amp;rsquo;re familiar with spline functions you know this isn&amp;rsquo;t a question I should have asked myself: When using splines, you are limited to predicting new data that falls into the domain of your training data, i.e. the domain on which the splines were fitted. Once you leave this domain, there are no fitted splines and so your model can&amp;rsquo;t predict. You can always make assumptions, of course: A linear continuation, using the last value, or similar. But what about your prediction interval?&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/07/rediscovering-bayesian-structural-time-series/gam_plot_w_question.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;So I continued thinking: Maybe one could estimate how often the trend has adjusted in the past and use this to derive the prediction interval?&lt;/p&gt;&#xA;&lt;h2 id=&#34;trend-uncertainty-in-the-prophet-package&#34;&gt;Trend Uncertainty in the &lt;code&gt;prophet&lt;/code&gt; Package&lt;/h2&gt;&#xA;&lt;p&gt;In their &lt;a href=&#34;https://github.com/facebook/prophet&#34;&gt;&lt;code&gt;prophet&lt;/code&gt; package&lt;/a&gt;, Sean J. Taylor and Benjamin Letham specify their model &amp;ldquo;similar to a generalized additive model&amp;rdquo; (Taylor and Letham, 2018) as&lt;/p&gt;&#xA;$$&#xA;y(t) = g(t) + s(t) + h(t) + \epsilon.&#xA;$$&lt;p&gt;If we disregard the seasonal $s(t)$ and holiday components $h(t)$ for the moment, then this corresponds to the GAM formulation used above. A crucial addition of the &lt;code&gt;prophet&lt;/code&gt; package is how Taylor and Letham manage to incorporate uncertainty due to historical trend changes in the model&amp;rsquo;s prediction intervals (see chapter 3.1 in &lt;a href=&#34;https://peerj.com/preprints/3190.pdf&#34;&gt;their paper&lt;/a&gt;): In their trend specification,&lt;/p&gt;&#xA;$$&#xA;g(t) = (k + a(t)^T \delta )t + (m+a(t)^T\gamma),&#xA;$$&lt;p&gt;&#xA;$k$ corresponds to the linear growth rate but can be adjusted at changepoints $a(t)$ by different rate adjustments stored in the vector $\delta$. In their model specification for Bayesian inference, they pick $\delta_j \sim \text{Laplace}(0,\tau)$ as prior distribution for the amount by which the rate may change at a changepoint. This implies changes are generally more likely close to zero but can be large from time to time.&lt;/p&gt;&#xA;&lt;p&gt;It also suggests a convenient approach to forecast the trend uncertainty: They use this generative model to sample new changepoints in the future, as well as new changes in the growth rate via the above prior distribution: Take the estimated rate changes $\hat{\delta}_j$ and use them to get a &lt;a href=&#34;https://en.wikipedia.org/wiki/Laplace_distribution#Estimation_of_parameters&#34;&gt;maximum likelihood estimate of the Laplace distribution&amp;rsquo;s scale parameter&lt;/a&gt;: $\hat{\tau} = \frac{1}{S} \sum_{j = 1}^S |\hat{\delta}_j|$.&lt;/p&gt;&#xA;&lt;p&gt;This provides a way to forecast (the uncertainty of) the piece-wise constant growth rate: Sample new changepoints with linear trend changes according to the distribution fitted to historical growth rate changes.&lt;/p&gt;&#xA;&lt;p&gt;Could one go a step further and model a continously changing trend as well as its forecast uncertainty? Would this in particular be possible for the spline-based smooth term in the original GAM specified above?&lt;/p&gt;&#xA;&lt;p&gt;Roughly, this is the direction I went during a late evening whiteboard drawing session:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/07/rediscovering-bayesian-structural-time-series/whiteboard_trend_changes.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Don&amp;rsquo;t judge my drawing skills. I believe what I was trying to derive was some sort of local linear approximation to the smooth trend (you see the straight lines for every three observations). Each of the lines would be described by their own coefficients (see $a_0, b_0, a_1, b_1, ...$ in the lower left corner). And to be fancy, I wrote Random Walk prior to describe how much a coefficient may change from one time point to the next. This is actually not much different from the formulation used in the &lt;code&gt;prophet&lt;/code&gt; package: In my drawing, we have a potential changepoint every third observation and instead of changes drawn from a Normal distribution (random walk prior), &lt;code&gt;prophet&lt;/code&gt; assumes the Laplace.&lt;/p&gt;&#xA;&lt;p&gt;A little later that evening it hit me: I already know this! The model I was sketching is equivalent to the local-linear trend Bayesian structural time series model (&lt;a href=&#34;https://research.google/pubs/pub41335/&#34;&gt;Scott and Varian 2014&lt;/a&gt;)&amp;mdash;which itself is equivalent to a &lt;a href=&#34;https://arxiv.org/abs/1903.11309&#34;&gt;dynamic linear model&lt;/a&gt; or a &lt;a href=&#34;https://en.wikipedia.org/wiki/State-space_representation&#34;&gt;state-space model&lt;/a&gt; formulation. For the close cousin of the local-level model, look no further than me using it &lt;a href=&#34;https://minimizeregret.com/post/2020/01/18/the-causal-effect-of-new-years-resolutions/&#34;&gt;here&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Let&amp;rsquo;s have a closer look at this model that promises to be sufficiently flexible for many trends, can incorporate other features, is interpretable, and propagates the uncertainty from trend changes into the forecasts.&lt;/p&gt;&#xA;&lt;h2 id=&#34;bayesian-structural-time-series&#34;&gt;Bayesian Structural Time Series&lt;/h2&gt;&#xA;&lt;p&gt;We are looking for a flexible way of describing the trend $\mu_t$ of a time series. Assume that the observations of the time series are the sum of potentially several components: the trend, seasonality, regressors and noise:&lt;/p&gt;&#xA;$$&#xA;Y_t \sim \text{Normal}(\lambda_t, \sigma_Y), \quad \lambda_t = \mu_t + \tau_t + \beta^Tx_t&#xA;$$&lt;p&gt;While the seasonal and regressor components are certainly interesting, we&amp;rsquo;ll focus on the trend for now.&lt;/p&gt;&#xA;&lt;p&gt;Following my sketch above, what we expect to have is a trend $\mu_t$ that is a linear function of time, something along the lines of $\mu_t = \delta_t \cdot t$. This is equivalent to $\mu_t = \mu_{t-1} + \delta_t$ given that we only consider equal time steps of size 1. So we expect that the trend at the next step is the previous trend plus a smoothly changing difference $\delta_t$. Adding some additional noise to this relationship, we get&lt;/p&gt;&#xA;$$&#xA;\mu_t = \mu_{t-1} + \delta_t + \epsilon_{\mu,t}, \quad \epsilon_{\mu,t} \sim \text{Normal}(0, \sigma_\mu).&#xA;$$&lt;p&gt;&#xA;If the change $\delta_t$ at each time would be constant at $\delta$, then the trend would be equal to a linear regression against time. If we let $\delta_t$ change over time, however, the trend can become a rather flexible function of time&amp;mdash;while being locally linear in time.&lt;/p&gt;&#xA;&lt;p&gt;How do we let $\delta_t$ change over time? This is where the random walk prior from the whiteboard sketch comes in:&lt;/p&gt;&#xA;$$&#xA;\delta_t = \delta_{t-1} + \epsilon_{\delta,t}, \quad \epsilon_{\delta,t} \sim \text{Normal}(0, \sigma_\delta)&#xA;$$&lt;p&gt;Note that this model encompasses a few special cases. First, if $\delta_t$ was fixed at 0, then the trend $\mu_t$ would adjust according to a random walk and we would be looking at the local-level model (as used &lt;a href=&#34;https://minimizeregret.com/post/2020/01/18/the-causal-effect-of-new-years-resolutions/&#34;&gt;here&lt;/a&gt;). Second, if $\sigma_\delta$ was 0 and thus $\epsilon_{\delta,t}$ was 0, the trend $\mu_t$ would be changing according to the linear model&amp;mdash;either with some noise or in a straight line if $\sigma_{\mu}$ was 0.&lt;/p&gt;&#xA;&lt;h2 id=&#34;generating-a-time-series&#34;&gt;Generating a Time Series&lt;/h2&gt;&#xA;&lt;p&gt;Given this model, we can generate a time series that follows this model exactly. This provides us with additional intuition with regard to how the different parameters interact.&lt;/p&gt;&#xA;&lt;p&gt;Here, we generate $T=40$ observations. We draw $\sigma_{\mu}, \sigma_{\delta}$ and $\sigma_Y$ from Half-Normal prior distributions with different scale.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;4539&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;T &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;40&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;y &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;NA&lt;/span&gt;, T)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Draw standard normal errors which will be &lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# multiplied by the scale later&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mu_err &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rnorm&lt;/span&gt;(T, &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;delta_err &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rnorm&lt;/span&gt;(T, &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;s_obs &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;abs&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;rnorm&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;s_slope &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;abs&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;rnorm&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.5&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;s_level &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;abs&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;rnorm&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.5&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## [1] &amp;#34;Scale of Observation Noise: 10.33&amp;#34;&#xA;## [1] &amp;#34;Scale of Trend Noise: 0.06&amp;#34;&#xA;## [1] &amp;#34;Scale of Slope Noise: 0.68&amp;#34;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Note how the scale of the trend noise is quite small which means that we will not see a strong random walk behavior on the trend.&lt;/p&gt;&#xA;&lt;p&gt;Also note how we were able to sample the different noise values independently for each time step. We can add them up according to our model by iterating over the time steps. In the final step, we draw the observations from the Normal distribution with mean equal to the trend.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mu &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;NA&lt;/span&gt;, T)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;delta &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;NA&lt;/span&gt;, T)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mu[1] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; mu_err[1]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;delta[1] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; delta_err[1]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; (t &lt;span style=&#34;color:#66d9ef&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;T) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  mu[t] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; mu[t&lt;span style=&#34;color:#ae81ff&#34;&gt;-1&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; delta[t&lt;span style=&#34;color:#ae81ff&#34;&gt;-1&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; s_level &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; mu_err[t]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  delta[t] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; delta[t&lt;span style=&#34;color:#ae81ff&#34;&gt;-1&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; s_slope &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; delta_err[t]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;y &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rnorm&lt;/span&gt;(T, mu, s_obs)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The time series looks as follows:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/07/rediscovering-bayesian-structural-time-series/unnamed-chunk-8-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;But we can also stylize it differently. The next plot shows the trend $\mu_t$ as the grey line, the observations $y_t$ as dots with size scaled according to the size of the change $\delta_t$ in trend at time $t$.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/07/rediscovering-bayesian-structural-time-series/unnamed-chunk-9-1.png&#34;&gt;&lt;/figure&gt;&lt;h2 id=&#34;estimating-the-model&#34;&gt;Estimating the Model&lt;/h2&gt;&#xA;&lt;p&gt;While there may be more efficient inference methods for this model (Kalman filter) and the ready-to-use &lt;a href=&#34;https://cran.r-project.org/web/packages/bsts/index.html&#34;&gt;&lt;code&gt;bsts&lt;/code&gt;&lt;/a&gt; package, we &lt;em&gt;can&lt;/em&gt; take the specification above to define a Stan model and run Bayesian inference via Hamiltonian Monte-Carlo.&lt;/p&gt;&#xA;&lt;p&gt;A simple implementation of the local-linear trend model in Stan code looks quite similar to the R code we used to generate the example time series:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;library&lt;/span&gt;(rstan)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;rstan_options&lt;/span&gt;(auto_write &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;TRUE&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;options&lt;/span&gt;(mc.cores &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; parallel&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;detectCores&lt;/span&gt;())&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;llt_model &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;stan_model&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;local_linear_trend.stan&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;model_name &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;local_linear_trend&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## S4 class stanmodel &amp;#39;local_linear_trend&amp;#39; coded as follows:&#xA;## // https://discourse.mc-stan.org/t/bayesian-structural-time-series-modeling/2256/2&#xA;## &#xA;## data {&#xA;##   int &amp;lt;lower=0&amp;gt; T;&#xA;##   vector[T] y;&#xA;## }&#xA;## parameters {&#xA;##   vector[T] mu_err;&#xA;##   vector[T] delta_err;&#xA;##   real &amp;lt;lower=0&amp;gt; s_obs;&#xA;##   real &amp;lt;lower=0&amp;gt; s_slope;&#xA;##   real &amp;lt;lower=0&amp;gt; s_level;&#xA;## }&#xA;## transformed parameters {&#xA;##   vector[T] mu;&#xA;##   vector[T] delta;&#xA;##   mu[1] = mu_err[1];&#xA;##   delta[1] = delta_err[1];&#xA;##   for (t in 2:T) {&#xA;##     mu[t] = mu[t-1] + delta[t-1] + s_level * mu_err[t];&#xA;##     delta[t] = delta[t-1] + s_slope * delta_err[t];&#xA;##   }&#xA;## }&#xA;## model {&#xA;##   s_obs ~ normal(5,10);&#xA;##   s_slope ~ normal(0,1);&#xA;##   s_level ~ normal(0,1);&#xA;##   &#xA;##   mu_err ~ normal(0,1);&#xA;##   delta_err ~ normal(0,1);&#xA;##   &#xA;##   y ~ normal(mu, s_obs);&#xA;## }&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In the next step, we fit the model to the data generated above.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;llt_fit &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;sampling&lt;/span&gt;(object &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; llt_model, &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;data &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;list&lt;/span&gt;(T &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; T, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; y),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;chains &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;iter &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;4000&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;seed &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;357&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;verbose &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;TRUE&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;control &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;list&lt;/span&gt;(adapt_delta &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0.95&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For the three scale parameters, we get the following summary statistics of the samples from their posterior distributions:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;print&lt;/span&gt;(llt_fit, pars &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;s_obs&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;s_level&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;s_slope&amp;#34;&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## Inference for Stan model: local_linear_trend.&#xA;## 4 chains, each with iter=4000; warmup=2000; thin=1; &#xA;## post-warmup draws per chain=2000, total post-warmup draws=8000.&#xA;## &#xA;##         mean se_mean   sd 2.5%  25%  50%  75% 97.5% n_eff Rhat&#xA;## s_obs   9.20    0.01 1.13 7.27 8.41 9.10 9.89 11.64  7350    1&#xA;## s_level 0.74    0.01 0.57 0.03 0.29 0.63 1.06  2.12  6384    1&#xA;## s_slope 0.67    0.00 0.30 0.24 0.45 0.62 0.83  1.37  3830    1&#xA;## &#xA;## Samples were drawn using NUTS(diag_e) at Fri May  8 20:42:10 2020.&#xA;## For each parameter, n_eff is a crude measure of effective sample size,&#xA;## and Rhat is the potential scale reduction factor on split chains (at &#xA;## convergence, Rhat=1).&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;visualizing-the-fitted-model&#34;&gt;Visualizing the Fitted Model&lt;/h2&gt;&#xA;&lt;p&gt;We can use the excellent &lt;code&gt;tidybayes&lt;/code&gt; model by Matthew Kay to visualize the fitted model. First off, let&amp;rsquo;s look at the posterior distributions for the scale parameters for each of the four Markov chains:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/07/rediscovering-bayesian-structural-time-series/unnamed-chunk-16-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;While this is nice, wait until you see the following.&lt;/p&gt;&#xA;&lt;p&gt;We can visualize the estimated posterior distributions of the slope of the trend, $\delta_t$, over time. Larger values in absolute terms correspond to period where the overall level of the time series will change more rapidly, while positive values correspond to periods of increasing trend and a negative slope corresponds to periods of decreasing trend.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/07/rediscovering-bayesian-structural-time-series/unnamed-chunk-17-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;In just the same way, we can also visualize the posterior distribution of the trend $\mu_t$ over time. The trend increases as the slope above has positive values, then becomes constant as the slope hovers around 0, and changes to decreasing as the slope drops below 0.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/07/rediscovering-bayesian-structural-time-series/unnamed-chunk-18-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Given the posterior of the trend, we can also sample from our model (i.e., our data generating process) to get samples from the posterior predictive distribution. That is, we now look at the uncertainty of the observations instead of parameters. Note that the y-axis has the same scale as in the graph of the trend before. The different scale of the uncertainty is the result of the additional observation noise $\epsilon_{Y,t}$. We can say, for example, that the &lt;em&gt;trend&lt;/em&gt; is larger than 0 with probability larger than 99% at time step 20. However, &lt;em&gt;observations&lt;/em&gt; are still expected to be below 0 with about 10%.&lt;/p&gt;&#xA;&lt;p&gt;Additionally, we plot the actual observations again with size according to the median of $\delta_t$&amp;rsquo;s posterior distribution.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/07/rediscovering-bayesian-structural-time-series/unnamed-chunk-19-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Finally, we can generate and graph the forecast of the time series from the estimated model for the next 10 time steps. Note that all the uncertainty from the parameters&amp;rsquo; posterior distributions has been propagated into this forecast distribution.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/07/rediscovering-bayesian-structural-time-series/unnamed-chunk-20-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;We can generate new observations that continue the original time series according to the random sampling scheme above and compare against the forecast:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/07/rediscovering-bayesian-structural-time-series/unnamed-chunk-21-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;These realized values are of course only one of many possible future outcomes: Had I chosen a different seed, we would have sampled different observations from the same model.&lt;/p&gt;&#xA;&lt;p&gt;To illustrate the point that there are many different legitimate future outcomes, we can sample paths from our model and visualize those instead of the intervals.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/06/07/rediscovering-bayesian-structural-time-series/unnamed-chunk-22-1.gif&#34;&gt;&lt;/figure&gt;&lt;h2 id=&#34;where-to-go-from-here&#34;&gt;Where to Go From Here&lt;/h2&gt;&#xA;&lt;p&gt;So far I&amp;rsquo;ve only touched the surface of what&amp;rsquo;s possible with (Bayesian) structural time series models. But the derivation above has helped me to really come to grips with the idea and sold me on this way of phrasing the time series problem. As described in the &lt;a href=&#34;https://www.cambridge.org/core/books/forecasting-structural-time-series-models-and-the-kalman-filter/CE5E112570A56960601760E786A5E631#fndtn-information&#34;&gt;information on Andrew Harvey&amp;rsquo;s book&lt;/a&gt; on the topic:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Unlike the traditional ARIMA models, structural time series models consist explicitly of unobserved components, such as trends and seasonals, which have a direct interpretation.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Building on these first steps, we can go further. We could, for example, make use of the interpretability of this model family to &lt;em&gt;automatically&lt;/em&gt; provide explanations of the estimated models and their forecasts. This might support conversations with stakeholders.&lt;/p&gt;&#xA;&lt;p&gt;Another favorite of mine are prior-predictive checks. Combined with Bayesian structural time series models, we would check for a given specification of model and prior distributions which kind of time series that model generates&amp;mdash;similar to how we generated a single time series above. Based on this we could adjust the prior distributions to build models that correspond to our prior belief.&lt;/p&gt;&#xA;&lt;p&gt;An obvious next step would be the implementation of further variants of structural time series models: For example, models with different trend specifications, models that include seasonal components, or with distributions different from the Normal distribution used above. The &lt;code&gt;bsts&lt;/code&gt; package and &lt;a href=&#34;http://www.unofficialgoogledatascience.com/2017/07/fitting-bayesian-structural-time-series.html&#34;&gt;Steven L. Scott&amp;rsquo;s blog post&lt;/a&gt; on the topic are a great place to start on this.&lt;/p&gt;&#xA;&lt;p&gt;Lastly, the code to reproduce the examples above is &lt;a href=&#34;https://github.com/timradtke/bsts-stan&#34;&gt;available on Github&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;h2 id=&#34;references&#34;&gt;References&lt;/h2&gt;&#xA;&lt;p&gt;Steven L. Scott and Hal R. Varian (2014). Predicting the present with Bayesian structural time series. International Journal of Mathematical Modelling and Numerical Optimisation, vol. 5 (2014), pp. 4-23, &lt;a href=&#34;https://research.google/pubs/pub41335&#34;&gt;https://research.google/pubs/pub41335&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Kim Larsen (2016). Sorry ARIMA, but I’m Going Bayesian. Stitch Fix MultiThreaded blog, &lt;a href=&#34;https://multithreaded.stitchfix.com/blog/2016/04/21/forget-arima/&#34;&gt;https://multithreaded.stitchfix.com/blog/2016/04/21/forget-arima/&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Steven L. Scott (2017). Fitting Bayesian Structural Time Series with the bsts R Package. The Unofficial Google Data Science blog, &lt;a href=&#34;http://www.unofficialgoogledatascience.com/2017/07/fitting-bayesian-structural-time-series.html&#34;&gt;http://www.unofficialgoogledatascience.com/2017/07/fitting-bayesian-structural-time-series.html&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Sean J. Taylor and Benjamin Letham (2018). Forecasting at Scale, The American Statistician 72(1):37-45, &lt;a href=&#34;https://peerj.com/preprints/3190.pdf&#34;&gt;https://peerj.com/preprints/3190.pdf&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Andrew C. Harvey (1990). Forecasting, Structural Time Series Models and the Kalman Filter. Cambridge University Press, &lt;a href=&#34;https://doi.org/10.1017/CBO9781107049994&#34;&gt;https://doi.org/10.1017/CBO9781107049994&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Bob Carpenter, Andrew Gelman, Matthew D. Hoffman, Daniel Lee, Ben Goodrich, Michael Betancourt, Marcus Brubaker, Jiqiang Guo, Peter Li, and Allen Riddell (2017). Stan: A probabilistic programming language. Journal of Statistical Software 76(1). doi: &lt;a href=&#34;https://www.jstatsoft.org/article/view/v076i01&#34;&gt;10.18637/jss.v076.i01&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Stan Development Team (2018). RStan: the R interface to Stan. R package version 2.17.3. &lt;a href=&#34;http://mc-stan.org&#34;&gt;http://mc-stan.org&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Matthew Kay (2020). tidybayes: Tidy Data and Geoms for Bayesian Models. doi: 10.5281/zenodo.1308151, R package version 2.0.3.9000, &lt;a href=&#34;http://mjskay.github.io/tidybayes/&#34;&gt;http://mjskay.github.io/tidybayes/&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Steven L. Scott (2020). bsts: Bayesian Structural Time Series, R Package version 0.9.5, &lt;a href=&#34;https://cran.r-project.org/web/packages/bsts/index.html&#34;&gt;https://cran.r-project.org/web/packages/bsts/index.html&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Giovanni Petris (2020). dlm: Bayesian and Likelihood Analysis of Dynamic Linear Models, R Package version 1.1-5, &lt;a href=&#34;https://cran.r-project.org/web/packages/dlm/index.html&#34;&gt;https://cran.r-project.org/web/packages/dlm/index.html&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Stan Prior Choice Recommendations. &lt;a href=&#34;https://github.com/stan-dev/stan/wiki/Prior-Choice-Recommendations&#34;&gt;https://github.com/stan-dev/stan/wiki/Prior-Choice-Recommendations&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Notes by Ben Goodrich on implementing latent time series models in Stan on the Stan Discourse. &lt;a href=&#34;https://discourse.mc-stan.org/t/bayesian-structural-time-series-modeling/2256/2&#34;&gt;https://discourse.mc-stan.org/t/bayesian-structural-time-series-modeling/2256/2&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Marko Laine (2019). Introduction to Dynamic Linear Models for Time Series Analysis. arXiv Preprint, &lt;a href=&#34;https://arxiv.org/pdf/1903.11309.pdf&#34;&gt;https://arxiv.org/pdf/1903.11309.pdf&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Tim Radtke (2020). The Causal Effect of New Year&amp;rsquo;s Resolutions. Minimize Regret blog, &lt;a href=&#34;https://minimizeregret.com/post/2020/01/18/the-causal-effect-of-new-years-resolutions/&#34;&gt;https://minimizeregret.com/post/2020/01/18/the-causal-effect-of-new-years-resolutions/&lt;/a&gt;.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>The Causal Effect of New Year&#39;s Resolutions</title>
	  <link>https://minimizeregret.com/post/2020/01/18/the-causal-effect-of-new-years-resolutions/</link>
	  <pubDate>Sat, 18 Jan 2020 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2020/01/18/the-causal-effect-of-new-years-resolutions/</guid>
	  <description>&lt;p&gt;&lt;em&gt;We treat the turn of the year as an intervention to infer the causal effect of New Year&amp;rsquo;s resolutions on McFit&amp;rsquo;s Google Trend index. By comparing the observed values from the treatment period against predicted values from a counterfactual model, we are able to derive the overall lift induced by the intervention.&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;Throughout the year, people&amp;rsquo;s interest in a &lt;a href=&#34;https://www.mcfit.com&#34;&gt;McFit&lt;/a&gt; gym membership appears quite stable.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; The following graph shows the Google Trend for the search term &amp;ldquo;McFit&amp;rdquo; in Germany for April 2017 to until the week of December 17, 2017. Fluctuating somewhere around 40, the &amp;ldquo;McFit&amp;rdquo; search term is right in the middle of the 0 to 100 scale used for Google Trends.&lt;/p&gt;&#xA;&lt;p&gt;But as in many other countries, joining a gym is a typical New Year&amp;rsquo;s resolution in Germany. So we would expect to observe a jump in interest around the turn of the year, as people promise themselves to add a habit to their lives and to burn the calories they consumed during the holidays.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/01/18/the-causal-effect-of-new-years-resolutions/unnamed-chunk-2-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;To estimate the effect that New Year&amp;rsquo;s resolutions have on the interest in McFit, we will consider the turn of the year as an intervention. The intervention is followed by a period which we will call &lt;em&gt;treatment period&lt;/em&gt;. Since we observe a &amp;ldquo;treated&amp;rdquo; environment, we would expect to observe different behavior than in the &amp;ldquo;untreated&amp;rdquo; region before the intervention and thus a change in the Google Trend index. To estimate the change induced by the treatment, we not only need the actual observations from the treatment period, but also the counterfactual: What would we expect to happen in the absence of the intervention posed by New Year&amp;rsquo;s? Given that the time series appears to be reasonably forecast-able, a simple approach might be to take this time series&amp;rsquo; forecast as counterfactual. This approach and the discussion here is motivated by the paper &lt;a href=&#34;https://research.google/pubs/pub41854/&#34;&gt;Inferring causal impact using Bayesian structural time-series models&lt;/a&gt; by Brodersen et al. from 2015.&lt;/p&gt;&#xA;&lt;p&gt;To derive the counterfactual forecast, we thus fit a Bayesian structural time series model&amp;mdash;in this case we might use a local level model, in which the observations are generated by a Normal distribution with a mean that changes according to a random walk:&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#xA;$$&#xA;Y_t \sim N(\mu_t, \sigma)&#xA;$$&lt;p&gt;&#xA;&lt;/p&gt;&#xA;$$&#xA;\mu_{t+1} \sim N(\mu_t, \tau) &#xA;$$&lt;p&gt;Before we use this model to generate a counterfactual on which we base our causal effect inferences, we should ensure that we can actually rely on the model&amp;rsquo;s prediction.&lt;/p&gt;&#xA;&lt;h2 id=&#34;evaluation-of-forecast-model-in-untreated-period&#34;&gt;Evaluation of Forecast Model in Untreated Period&lt;/h2&gt;&#xA;&lt;p&gt;How well can this simple model predict the time series up to this point? To evaluate this, we train it on the first 26 observations in our time series and predict the remaining 12 from October until right before the actual intervention near the end of December. We can fit the above model quickly using the &lt;code&gt;bsts&lt;/code&gt; package in R:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;range&lt;/span&gt;(mcfit_2017_short&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;week)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## [1] &amp;#34;2017-04-02&amp;#34; &amp;#34;2017-09-24&amp;#34;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;library&lt;/span&gt;(bsts)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;state_spec_short &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;AddLocalLevel&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;list&lt;/span&gt;(), y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; mcfit_2017_short&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;trend)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;model_short &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;bsts&lt;/span&gt;(mcfit_2017_short&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;trend,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;state.specification &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; state_spec_short,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;niter &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;11000&lt;/span&gt;, ping &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pred_short &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;predict&lt;/span&gt;(model_short, horizon &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;, burn &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1000&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/01/18/the-causal-effect-of-new-years-resolutions/unnamed-chunk-5-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Given how fairly well the prediction intervals describe the actual observations (and since this here is just a blog post), we can be quite confident in the model&amp;rsquo;s predictions going forward.&lt;sup id=&#34;fnref:3&#34;&gt;&lt;a href=&#34;#fn:3&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#xA;&lt;h2 id=&#34;causal-effect-of-a-hypothetical-intervention&#34;&gt;Causal Effect of a Hypothetical Intervention&lt;/h2&gt;&#xA;&lt;p&gt;Since we already have a forecast for the untreated region, we can use this forecast, too, as counterfactual: We can &lt;em&gt;pretend&lt;/em&gt; that October 1 also has been an intervention date after which we might observe a treatment effect. Would our model conclude that there has not been any significant change? If it did find a significant change, then this might tell us that our model does not yet account for all behaviors of the time series besides the impact of New Year&amp;rsquo;s resolutions. In that case, we could not rely on it to provide a accurate counterfactual estimate for our period of interest.&lt;/p&gt;&#xA;&lt;p&gt;To derive the treatment effect on the untreated region, we subtract the observed trend values from the posterior predictive samples of our forecast model. This provides us not only with a point estimate but the uncertainty in our prediction is propagated as well. Since we are estimating a causal effect on an &lt;em&gt;untreated&lt;/em&gt; region, we expect to observe no large evidence for an effect different from zero. Indeed, as the graphs show below, we observe a posterior expectation of a total -20.4 additional Google Trend percentage points over the 12 weeks following October 1. With a 97.5% credible interval at [-64.3, 41.7] percentage points, there is a lot of probability on both sides of zero. This change in percentage points corresponds to an expected lift of -4.4% compared to the counterfactual estimate, with a [-13.9%, 9%] credible interval.&lt;/p&gt;&#xA;&lt;p&gt;We can thus be fairly confident in using this model to provide a counterfactual estimate when we move to the analysis of the actual intervention around New Year&amp;rsquo;s.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/01/18/the-causal-effect-of-new-years-resolutions/unnamed-chunk-7-1.png&#34;&gt;&lt;/figure&gt;&lt;h2 id=&#34;causal-effect-of-turn-of-the-year-intervention&#34;&gt;Causal Effect of Turn-of-the-Year Intervention&lt;/h2&gt;&#xA;&lt;p&gt;With the above analysis in hand, all we need is to take the time series up until the date of the intervention and to re-fit our model. It will provide us with the counterfactual estimate for the treated period. After that, we can observe the actual Google Trend for the treated period and compare against our counterfactual to derive the treatment effect in the same way as we did above.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;range&lt;/span&gt;(mcfit_2017&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;week)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## [1] &amp;#34;2017-04-02&amp;#34; &amp;#34;2017-12-17&amp;#34;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ss &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;AddLocalLevel&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;list&lt;/span&gt;(), y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; mcfit_2017&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;trend)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mcfit_model &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;bsts&lt;/span&gt;(mcfit_2017&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;trend,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;state.specification &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; ss,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;niter &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;11000&lt;/span&gt;, ping &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pred &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;predict&lt;/span&gt;(mcfit_model, horizon &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;, burn &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1000&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The model provides us with the following counterfactual forecast for the beginning of 2018:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/01/18/the-causal-effect-of-new-years-resolutions/unnamed-chunk-11-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;And this is how the counterfactual compares to the observed Google Trend index for McFit:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/01/18/the-causal-effect-of-new-years-resolutions/unnamed-chunk-12-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Wow, people really do like to make New Year&amp;rsquo;s resolutions! There is probably little discussion about whether &lt;em&gt;something&lt;/em&gt; changes after the intervention. But since we have a model at hand, let&amp;rsquo;s still quantify just how much changes.&lt;/p&gt;&#xA;&lt;p&gt;To estimate the treatment effect, we proceed as in our dry run above: We subtract the posterior predictive samples from the observed trend values. This let&amp;rsquo;s us make probabilistic statements about the size of the treatment effect. You can see the result in the graphs below. We observe a posterior expectation of 217.7 additional Google Trend percentage points over the 12 weeks following Christmas. This is a significant effect with a 97.5% credible interval at [165.7, 279] percentage points&amp;mdash;far, far away from zero.&lt;/p&gt;&#xA;&lt;p&gt;What becomes clear from the graph, however, is that this effect is mainly generated within the first 6 weeks after the intervention. After that, the actuals are again somewhat in line with our model (though all lie jointly above the expectation which is an event with somewhat small probability). After 6 weeks, we already have a posterior expectation of 202.8 additional Google Trend percentage points with a 97.5% credible interval at [177.5, 231.6] percentage points. This corresponds to an expected lift of 47.4% compared to the predicted trend, with a [41.5%, 54.1%] credible interval.&lt;/p&gt;&#xA;&lt;p&gt;In contrast, the pointwise treatment effect estimated in week 12 after the intervention is a difference of -0.5 [-8.6, 8.1] percentage points, corresponding to a tiny lift of -1.4% [-23.3%, 22%]s and thus no change compared to the counterfactual scenario.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/01/18/the-causal-effect-of-new-years-resolutions/unnamed-chunk-14-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;This has shown us that people&amp;rsquo;s interest in McFit increases suddenly around New Year&amp;rsquo;s as they make New Year&amp;rsquo;s resolutions&amp;mdash;and that this interest drops rapidly and disappears from February onwards as everyone has either signed up to McFit or lost sight of their resolutions.&lt;/p&gt;&#xA;&lt;p&gt;Or is there something else in play that could have an effect on people&amp;rsquo;s behavior?&lt;/p&gt;&#xA;&lt;h2 id=&#34;causal-effect-entanglement&#34;&gt;Causal Effect Entanglement&lt;/h2&gt;&#xA;&lt;p&gt;Alas, the turn of the year is not the only intervention: McFit builds on people&amp;rsquo;s good intentions with membership fee discounts and marketing campaigns.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/01/18/the-causal-effect-of-new-years-resolutions/mcfit_billboard.png&#34; alt=&#34;A McFit billboard as seen at Berlin walls.&#34;&gt;&lt;figcaption&gt;A McFit billboard as seen at Berlin walls.&lt;/figcaption&gt;&lt;/figure&gt;&lt;p&gt;The above image shows a large billboard advertising a McFit membership for only €4.90 per month if you subscribe until January 31, 2020.&lt;sup id=&#34;fnref:4&#34;&gt;&lt;a href=&#34;#fn:4&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;4&lt;/a&gt;&lt;/sup&gt; With the slogan &amp;ldquo;Für mehr wow!&amp;rdquo; (&amp;ldquo;For more wow!&amp;rdquo;), the marketing campaign makes it difficult to understand just how much of the Google Trend peak is caused by the turn of the year&amp;mdash;and how much should be attributed to the marketing.&lt;sup id=&#34;fnref:5&#34;&gt;&lt;a href=&#34;#fn:5&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;5&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#xA;&lt;p&gt;This is a problem for us since with price discounts and marketing campaigns in place, we can no longer claim confidently that people increasingly search for &amp;ldquo;McFit&amp;rdquo; &lt;em&gt;only&lt;/em&gt; because of New Year&amp;rsquo;s resolutions. Instead, the total number of increased searches will have multiple, entangled causes: Some people plan to become fit and think of the McFit brand. Others see a billboard with an enticing offer and want to subscribe online. Again others are comparing gyms to put their New Year&amp;rsquo;s resolutions in place and search for McFit because they were reminded of it by the marketing campaign. In a sense, New Year&amp;rsquo;s resolutions are no longer exogenous: They will exist in part because of the contemporaneous McFit marketing, which exists due to New Year&amp;rsquo;s resolutions and their effect.&lt;/p&gt;&#xA;&lt;p&gt;Consequently, we cannot conclude which effect is caused by the New Year&amp;rsquo;s intervention, which by the marketing campaign&amp;rsquo;s intervention, and which effect by the interaction of the previous two.&lt;/p&gt;&#xA;&lt;p&gt;What we need is an approach that let&amp;rsquo;s us disentangle these effects. Surely McFit as a business would be interested in disentangling them to estimate the return on their marketing investment! Maybe all their new customers would have subscribed even without a discount but solely due to their resolutions?&lt;/p&gt;&#xA;&lt;h3 id=&#34;geographic-experiments&#34;&gt;Geographic Experiments&lt;/h3&gt;&#xA;&lt;p&gt;One approach to entangle the effects could be the implementation of a geographic experiment in which certain regions are exposed to a marketing campaign while others are not. For out-of-home and online advertising, this might be feasible, though customers in areas without a campaign could still learn in other ways about the price reduction&amp;mdash;and the price reduction potentially has to hold for at least an entire country. McFit, however, is located in multiple countries. If these countries have a similar predisposition for New Year&amp;rsquo;s resolutions, it might be possible to compare effects across countries by introducing neither price reductions nor marketing campaigns in some of them.&lt;/p&gt;&#xA;&lt;p&gt;The following papers outline approaches along this line:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Alberto Abadie, Alexis Diamond, Jens Hainmueller (2009). &lt;a href=&#34;https://economics.mit.edu/files/11859&#34;&gt;Synthetic Control Methods for Comparative Case Studies: Estimating the Effect of California’s Tobacco Control Program&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;Alberto Abadie (2019). &lt;a href=&#34;https://economics.mit.edu/files/17847&#34;&gt;Using Synthetic Controls:&#xA;Feasibility, Data Requirements, and Methodological Aspects&lt;/a&gt;.&lt;/li&gt;&#xA;&lt;li&gt;Kay H. Brodersen, Fabian Gallusser, Jim Koehler, Nicolas Remy, Steven L. Scott (2015). &lt;a href=&#34;https://research.google/pubs/pub41854/&#34;&gt;Inferring causal impact using Bayesian structural time-series models&lt;/a&gt;.&lt;/li&gt;&#xA;&lt;li&gt;Jon Waver, Jim Koehler (2011). &lt;a href=&#34;https://research.google/pubs/pub38355/&#34;&gt;Measuring Ad Effectiveness Using Geo Experiments&lt;/a&gt;.&lt;/li&gt;&#xA;&lt;li&gt;Jon Waver, Jim Koehler (2012). &lt;a href=&#34;https://research.google/pubs/pub38356/&#34;&gt;Periodic Measurement of Advertising Effectiveness Using Multiple-Test-Period Geo Experiments&lt;/a&gt;.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;At least the current marketing campaign appears to be active in all countries, though, going from the McFit websites for different countries.&lt;/p&gt;&#xA;&lt;h3 id=&#34;time-varying-covariates-as-synthetic-controls&#34;&gt;Time-Varying Covariates as Synthetic Controls&lt;/h3&gt;&#xA;&lt;p&gt;Instead of the simple time series model used above, one could consider including time-varying covariates in the model that are able to predict the behavior of the McFit trend. Since we could train the model &lt;em&gt;before&lt;/em&gt; the intervention period, but compute the forecast &lt;em&gt;after&lt;/em&gt; the intervention period (when we want to infer the treatment effect based on observed values), we could even use features that are observed at the same time as the McFit trend. For example, one could consider using the Google Trend of other gym brands: We would expect them to have some New Year&amp;rsquo;s resolutions effect as well. Therefore, their combination could also predict the effect on McFit&amp;rsquo;s. The difference between the observed Google Trend index and this counterfactual could then be attributed to the fact that a marketing campaign was put in place at the same time (while we could still use the previous model as counterfactual for the new model and infer the impact of resolutions).&lt;/p&gt;&#xA;&lt;p&gt;This approach has been developed in: Kay H. Brodersen, Fabian Gallusser, Jim Koehler, Nicolas Remy, Steven L. Scott (2015). &lt;a href=&#34;https://research.google/pubs/pub41854/&#34;&gt;Inferring causal impact using Bayesian structural time-series models&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;The problem with this line of argument is that the Google Trend of other gym brands might or might not be impacted by McFit&amp;rsquo;s marketing campaign: Before they subscribe to McFit&amp;rsquo;s offer online, people might have a look at the website of FitX or John Reed.&lt;sup id=&#34;fnref:6&#34;&gt;&lt;a href=&#34;#fn:6&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;6&lt;/a&gt;&lt;/sup&gt; If that&amp;rsquo;s the case, then our covariate-based counterfactual estimate of the trend including the New Year&amp;rsquo;s resolution effect would be impacted by the treatment effect we try to infer.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/01/18/the-causal-effect-of-new-years-resolutions/unnamed-chunk-15-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;In a way, New Year&amp;rsquo;s resolutions create a similar environment for marketers as Google does with search engine marketing on brand name keywords: Companies pay the &amp;ldquo;&lt;a href=&#34;https://multithreaded.stitchfix.com/blog/2019/12/19/good-marketing-decisions/&#34;&gt;Google tax&lt;/a&gt;&amp;rdquo; to show their SEM advertisement at the top of the search results even though the user was searching for their brand already&amp;mdash;just so that other companies don&amp;rsquo;t attract the user away with their SEM advertisement. Similarly, gym brands could simply take the increased demand due to New Year&amp;rsquo;s resolutions without even having to advertise. But because someone &lt;em&gt;will&lt;/em&gt; advertise, all of them have to create a special offer. Especially when they&amp;rsquo;re not able to properly estimate the causal impact of their marketing, the situation might end up in a tragedy of the commons (with the increased market demand as common resource).&lt;/p&gt;&#xA;&lt;h3 id=&#34;forecast-model-with-yearly-seasonality&#34;&gt;Forecast Model with Yearly Seasonality&lt;/h3&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2020/01/18/the-causal-effect-of-new-years-resolutions/unnamed-chunk-16-1.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;In reality, we have access to more than just one year of data. If we knew that McFit did not run any advertising in the past years around New Year&amp;rsquo;s, we could estimate a time series model with seasonality in order to properly model the New Year&amp;rsquo;s resolutions counterfactual effect for the current year&amp;mdash;a marketing campaign in the current year would then be evaluated by the difference between the counterfactual forecast and the actuals. In our case however, I would assume that McFit has a marketing campaign every year around New Year&amp;rsquo;s.&lt;/p&gt;&#xA;&lt;p&gt;Additionally, we would need to assume that marketing campaigns from other companies are negligiable.&lt;/p&gt;&#xA;&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;&#xA;&lt;p&gt;The Google Trend data for McFit offers a natural intervention posed by the turn of the year which provides the opportunity to estimate a causal effect of New Year&amp;rsquo;s resolutions on human behavior. We observe a large increase in the index that subsequently reverts back to the counterfactual forecast within a few weeks. While we can estimate the effect after the intervention, we can not differentiate between New Year&amp;rsquo;s resolutions and the change due to a contemporaneous marketing campaign.&lt;/p&gt;&#xA;&lt;p&gt;Causal inference is hard, even when the observed effects are huge.&lt;/p&gt;&#xA;&lt;p&gt;The original data is available on Google Trends for the &lt;a href=&#34;https://trends.google.com/trends/explore/TIMESERIES/1579338000?hl=de&amp;amp;tz=-60&amp;amp;date=today+5-y&amp;amp;geo=DE&amp;amp;q=McFit&amp;amp;sni=3&#34;&gt;German &amp;ldquo;McFit&amp;rdquo; index&lt;/a&gt; and for &lt;a href=&#34;https://trends.google.com/trends/explore/TIMESERIES/1579338000?hl=de&amp;amp;tz=-60&amp;amp;date=today+5-y&amp;amp;geo=DE-BE&amp;amp;q=McFit,John+Reed,FitX&amp;amp;sni=3&#34;&gt;the comparison of three gym brands in Berlin&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;For those unfamiliar with German gym brands, &lt;a href=&#34;https://en.wikipedia.org/wiki/McFit&#34;&gt;according to Wikipedia&lt;/a&gt;, &amp;ldquo;[t]he McFIT GmbH is the largest fitness center chain in Germany&amp;rdquo;.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:2&#34;&gt;&#xA;&lt;p&gt;Note that this corresponds to an &lt;a href=&#34;https://en.wikipedia.org/wiki/Exponential_smoothing&#34;&gt;Exponential Smoothing&lt;/a&gt; model as long as $\sigma &gt; 0$ and $\tau &gt; 0$. See &lt;a href=&#34;http://www.unofficialgoogledatascience.com/2017/07/fitting-bayesian-structural-time-series.html&#34;&gt;this blog post by Steven L. Scott&lt;/a&gt;.&amp;#160;&lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:3&#34;&gt;&#xA;&lt;p&gt;Note that the last three observations from December show a bit of a dip potentially due to the Advent season. This could be accounted for if we used data from several years to account for seasonality. However, due to the way we consider the data for causal estimation, we act as if there is only this year&amp;mdash;else we would know about last year&amp;rsquo;s New Year effect, too.&amp;#160;&lt;a href=&#34;#fnref:3&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:4&#34;&gt;&#xA;&lt;p&gt;Of course, the reduced fee applies for the first 6 months after which the contract continues with a fee of at least €19.90 for another 14 months.&amp;#160;&lt;a href=&#34;#fnref:4&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:5&#34;&gt;&#xA;&lt;p&gt;Note that I actually don&amp;rsquo;t know whether there has been a marketing campaign in 2017/18, but let&amp;rsquo;s just assume that there has been.&amp;#160;&lt;a href=&#34;#fnref:5&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:6&#34;&gt;&#xA;&lt;p&gt;John Reed being owned by the same group as McFit.&amp;#160;&lt;a href=&#34;#fnref:6&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>satRday Berlin Presentation</title>
	  <link>https://minimizeregret.com/post/2019/06/16/satrday-berlin-presentation/</link>
	  <pubDate>Sun, 16 Jun 2019 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2019/06/16/satrday-berlin-presentation/</guid>
	  <description>&lt;p&gt;&lt;em&gt;My satRday Berlin slides on &amp;ldquo;Modeling Short Time Series&amp;rdquo; &lt;a href=&#34;https://minimizeregret.com/presentations/satrday_berlin_modeling_short_time_series.pdf&#34;&gt;are available here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;This saturday, June 15, Berlin had its first &lt;a href=&#34;https://satrdays.org&#34;&gt;satRday conference&lt;/a&gt;. I eagerly followed the hashtags of satRday Amsterdam last year and satRday Capetown the year before that on Twitter. Thanks to Noa Tamir, Jakob Graff, Steve Cunningham, &lt;a href=&#34;https://berlin2019.satrdays.org/#team&#34;&gt;and many others&lt;/a&gt;, we got a &lt;a href=&#34;https://berlin2019.satrdays.org&#34;&gt;conference in Berlin&lt;/a&gt; as well.&lt;/p&gt;&#xA;&lt;p&gt;When I saw the call for papers, I jumped at the opportunity to present, trying what it feels like to be on the other side of the microphone; being in the hashtag instead of following it. Thanks again to the organizers for having me, it has been a fantastic experience. The conference format is accessible and a lot of fun.&lt;/p&gt;&#xA;&lt;p&gt;You can find my slides from yesterday on &lt;a href=&#34;https://minimizeregret.com/presentations/satrday_berlin_modeling_short_time_series.pdf&#34;&gt;&amp;ldquo;Modeling Short Time Series&amp;rdquo; as PDF here&lt;/a&gt;. The slides are based &lt;a href=&#34;https://minimizeregret.com/short-time-series-prior-knowledge/&#34;&gt;on this blog post&lt;/a&gt; which provides more context.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2019/06/16/satrday-berlin-presentation/satrday_photo.png&#34; alt=&#34;Photo taken by Steve Cunningham.&#34;&gt;&lt;/figure&gt;</description>
	</item>
	<item>
	  <title>Modeling Short Time Series with Prior Knowledge</title>
	  <link>https://minimizeregret.com/post/2019/04/16/modeling-short-time-series-with-prior-knowledge/</link>
	  <pubDate>Tue, 16 Apr 2019 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2019/04/16/modeling-short-time-series-with-prior-knowledge/</guid>
	  <description>&lt;p&gt;I just published a longer case study, &lt;a href=&#34;https://minimizeregret.com/short-time-series-prior-knowledge/&#34;&gt;Modeling Short Time Series with Prior Knowledge: What ‘Including Prior Information’ really looks like&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;It is generally difficult to model time series when there is insuffient data to model a (suspected) long seasonality. We show how this difficulty can be overcome by learning a seasonality on a different, long related time series and transferring the posterior as a prior distribution to the model of the short time series. The result is a forecast that is believable and can be used for decisions in a business context. In contrast to traditional methods that are not able to incorporate the long seasonality, we observe a drastic increase in common evaluation metrics. Default models in the &lt;code&gt;forecast&lt;/code&gt; and &lt;code&gt;prophet&lt;/code&gt; R packages fail to produce good forecasts on this example.&lt;/p&gt;&#xA;&lt;p&gt;Find the &lt;a href=&#34;https://minimizeregret.com/short-time-series-prior-knowledge/&#34;&gt;case study here&lt;/a&gt;, and the &lt;a href=&#34;https://github.com/timradtke/short-time-series&#34;&gt;data and code on Github&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2019/04/16/modeling-short-time-series-with-prior-knowledge/final_forecast.png&#34;&gt;&lt;/figure&gt;</description>
	</item>
	<item>
	  <title>Problem Representations and Model-Based Machine Learning</title>
	  <link>https://minimizeregret.com/post/2019/02/24/problem-representations-and-model-based-machine-learning/</link>
	  <pubDate>Sun, 24 Feb 2019 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2019/02/24/problem-representations-and-model-based-machine-learning/</guid>
	  <description>&lt;p&gt;Back in 2003, Paul Graham, of Viaweb and Y Combinator fame, published an article entitled &amp;ldquo;&lt;a href=&#34;http://www.paulgraham.com/better.html&#34;&gt;Better Bayesian Filtering&lt;/a&gt;&amp;rdquo;. I was scrolling chronologically through &lt;a href=&#34;http://www.paulgraham.com/articles.html&#34;&gt;his essays archive&lt;/a&gt; the other day when this article stuck out to me (well, the &amp;ldquo;Bayesian&amp;rdquo; keyword). After reading the first few paragraphs, I was a little disappointed to realize the topic was Naive Bayes rather than Bayesian methods. But it turned out to be a tale of implementing a machine learning solution for a real world application before anyone dared to mention AI in the same sentence.&lt;/p&gt;&#xA;&lt;p&gt;Graham explains how he achieved much better results in filtering spam mail than the academic results published previously at AAAI 1998.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; Besides his access to more data, he attributes his results to how he prepared the data. He refrained from stemming words, using &amp;ldquo;mailing&amp;rdquo; and &amp;ldquo;mailed&amp;rdquo; instead of &amp;ldquo;mail&amp;rdquo;. He preserves case when tokenizing the text, treats exclamation points as part of words (&amp;ldquo;FREE!&amp;rdquo;) and keeps periods and commas between digits to get IP adresses and prices.&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;There is a lesson here for filter writers: don&amp;rsquo;t ignore data. You&amp;rsquo;d think this lesson would be too obvious to mention, but I&amp;rsquo;ve had to learn it several times.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;What Graham believes to be the most significant difference, however, is that he included the information available in the &lt;a href=&#34;https://en.wikipedia.org/wiki/Email#Message_header&#34;&gt;e-mails&amp;rsquo; message headers&lt;/a&gt; (e.g., sender, recipient, date and subject line). E-mails are not just text, there is more structure to an e-mail than that. Structure that humans use to work more efficiently with the text, structure that we recognize as important when scrolling through our inbox and identifying the messages we do need to respond to. We shouldn&amp;rsquo;t keep the machine from using this information.&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;I don&amp;rsquo;t think it&amp;rsquo;s a good idea to treat spam filtering as a straight text classification problem. You can use text classification techniques, but solutions can and should reflect the fact that the text is email, and spam in particular. Email is not just text; it has structure.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;I believe that there are many application specific cases in which simple solutions (such as Naive Bayes) can get you incredibly far if you can make the data accessible to the algorithm such that the algorithm can find the important bits that humans would pay attention to (if humans could browse through data quickly enough). I&amp;rsquo;m trying to think about this as more than just feature engineering. Architecture engineering belongs here just as much&amp;mdash;there is a reason that convolutions work well in image classification. By using convolutions, we provide prior (human) information and structure to the machine&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt; without which it would be even tougher to find reliable patterns. We don&amp;rsquo;t need to rely on a &lt;a href=&#34;https://en.wikipedia.org/wiki/Universal_approximation_theorem&#34;&gt;universal approximation theorem&lt;/a&gt; and pray that we&amp;rsquo;re able to search through an infinite function space if we can provide assumptions about the real world to our model (either through data preparation or architecture).&lt;/p&gt;&#xA;&lt;p&gt;François Chollet provides an example of feature engineering in his book &amp;ldquo;&lt;a href=&#34;https://www.manning.com/books/deep-learning-with-python&#34;&gt;Deep Learning with Python&lt;/a&gt;&amp;rdquo; that I think is an example of making a problem accessible to models by translating human structure into mathematical or machine structure. In his example, we&amp;rsquo;re &amp;ldquo;trying to develop a model that can take as input an image of a clock and can output the time of day&amp;rdquo;. Yes, you could (probably) use a CNN to solve this problem, but this would be wasteful of computational resources. Chollet continues:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;But if you already understand the problem at a high level (you understand how humans read time on a clock face), then you can come up with much better input features for a machine-learning algorithm: for instance, it’s easy to write a short Python script to follow the black pixels of the clock hands and output the (x, y) coordinates of the tip of each hand. Then a simple machine-learning algorithm can learn to associate these coordinates with the appropriate time of day.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;While this is much better than using a CNN, the beauty of this example problem is that it does not require any machine learning whatsoever. Doing a coordinate change from (x, y) coordinates to polar coordinates, you get the angle of each clock hand. From here, &amp;ldquo;a simple rounding operation and dictionary lookup are enough to recover the approximate time of day.&amp;rdquo;&lt;/p&gt;&#xA;&lt;p&gt;In a way, this is nothing else than using the perfect kernel in a support vector machine, or doing the representation learning for your neural network.&lt;sup id=&#34;fnref:3&#34;&gt;&lt;a href=&#34;#fn:3&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;3&lt;/a&gt;&lt;/sup&gt; If you can represent your problem well, it will be much simpler to solve it.&lt;/p&gt;&#xA;&lt;p&gt;In a different chapter, Chollet discusses a 1-day ahead temperature forecasting problem. As a simple baseline method, he uses the current temperature to predict the temperature 24 hours from now, given that there is a strong daily seasonality in the data.&lt;sup id=&#34;fnref:4&#34;&gt;&lt;a href=&#34;#fn:4&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;4&lt;/a&gt;&lt;/sup&gt; He then applies a very simple feed-forward neural network which fails to achieve a better performance. Chollet argues:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;This goes to show the merit of having this baseline in the first place: it turns out to be not easy to outperform. Your common sense contains a lot of valuable information that a machine-learning model doesn’t have access to.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;You may wonder, if a simple, well-performing model exists to go from the data to the targets (the common-sense baseline), why doesn’t the model you’re training find it and improve on it? Because this simple solution isn’t what your training setup is looking for. The space of models in which you’re searching for a solution&amp;ndash;that is, your hypothesis space&amp;ndash;is the space of all possible two-layer networks with the configuration you defined. These networks are already fairly complicated. When you’re looking for a solution with a space of complicated models, the simple, well-performing baseline may be unlearnable, even if it’s technically part of the hypothesis space. That is a pretty significant limitation of machine learning in general: unless the learning algorithm is hardcoded to look for a specific kind of simple model, parameter learning can sometimes fail to find a simple solution to a simple problem.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;He then goes on and achieves a mean absolute error of 2.35°C with an LSTM architecture compared to 2.57°C of the seasonal random walk model.&lt;/p&gt;&#xA;&lt;p&gt;In a similar manner, &lt;a href=&#34;ftp://ftp.idsia.ch/pub/juergen/icann2001predict.pdf&#34;&gt;Gers, Eck and Schmidhuber (2003)&lt;/a&gt; note that MLPs can be advantageous over LSTM architectures when time series can be forecasted based on direct access to a few past observations (similarly to ARIMA models):&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;A time window based MLP outperformed the LSTM pure-AR approach on certain time series prediction benchmarks solvable by looking at a few recent inputs only. Thus LSTM&amp;rsquo;s special strength, namely, to learn to remember single events for very long, unknown time periods, was not necessary here.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;I highlight this result as it implies that simpler solutions can suffice when the problem is represented well. Here, a forecast is possible at every time step based on a few lagged previous time steps. This representation can be difficult to find for potentially more powerful models, while constrained models may represent exactly this knowledge about the problem space.&lt;/p&gt;&#xA;&lt;p&gt;You need to find the right tool for your application. If you have knowledge about your problem, and you are able to put this knowledge into structure, then a model that can pick up this structure may be superior compared to the latest models from ICML, NeurIPS or ICLR.&lt;sup id=&#34;fnref:5&#34;&gt;&lt;a href=&#34;#fn:5&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;5&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#xA;&lt;p&gt;If we accept that there is structure in the real world that we would like to incorporate into our models before we let them adjust their predictions to those that are in agreement with the data we observe from the real world; then it is easy to recognize advantages of &lt;em&gt;&lt;a href=&#34;http://mbmlbook.com/Introduction.html&#34;&gt;model-based machine learning&lt;/a&gt;&lt;/em&gt; as described by Winn and Bishop in their same-titled book:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;[&amp;hellip;] the model-based approach seeks to create a bespoke solution tailored to each new application. Instead of having to transform your problem to fit some standard algorithm, in model-based machine learning you design the algorithm precisely to fit your problem.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;The core idea at the heart of model-based machine learning is that all the assumptions about the problem domain are made explicit in the form of a model. In fact, a model is just made up of this set of assumptions, expressed in a precise mathematical form.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;See also a &lt;a href=&#34;https://www.youtube.com/watch?v=8a7wBLg5Q8U&#34;&gt;presentation in which Christopher Bishop introduces the concept of model-based machine learning&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;Pantel and Lin (1998). SpamCop. &lt;a href=&#34;http://www.patrickpantel.com/download/papers/1998/aaai98.pdf&#34;&gt;&lt;em&gt;A Spam Classification &amp;amp; Organization Program&lt;/em&gt;&lt;/a&gt;. Sahami, Dumais, Heckerman, and Horvitz (1998). &lt;a href=&#34;https://www.aaai.org/Papers/Workshops/1998/WS-98-05/WS98-05-009.pdf&#34;&gt;&lt;em&gt;A Bayesian Approach to Filtering Junk E-Mail&lt;/em&gt;&lt;/a&gt;.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:2&#34;&gt;&#xA;&lt;p&gt;Goodfellow, Bengio, Courville (2016). &lt;a href=&#34;https://www.deeplearningbook.org/contents/convnets.html&#34;&gt;Deep Learning&lt;/a&gt;. Chapter 9, Convolutional Networks.&amp;#160;&lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:3&#34;&gt;&#xA;&lt;p&gt;Christopher Olah&amp;rsquo;s article on &lt;a href=&#34;http://colah.github.io/posts/2014-03-NN-Manifolds-Topology&#34;&gt;Neural Networks, Manifolds and Topology&lt;/a&gt; makes it very clear what I mean by representation learning.&amp;#160;&lt;a href=&#34;#fnref:3&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:4&#34;&gt;&#xA;&lt;p&gt;That is, he uses a &lt;a href=&#34;https://people.duke.edu/~rnau/411searw.htm&#34;&gt;seasonal random walk model&lt;/a&gt;.&amp;#160;&lt;a href=&#34;#fnref:4&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li id=&#34;fn:5&#34;&gt;&#xA;&lt;p&gt;Add some additional metaphor containing screws and a hammer.&amp;#160;&lt;a href=&#34;#fnref:5&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>Videos from PROBPROG 2018 Conference</title>
	  <link>https://minimizeregret.com/note/2018/11/11/probprog-videos/</link>
	  <pubDate>Sun, 11 Nov 2018 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2018/11/11/probprog-videos/</guid>
	  <description>&lt;p&gt;Videos of the talks given at the &lt;a href=&#34;https://probprog.cc&#34;&gt;International Conference on Probabilistic Programming&lt;/a&gt; (PROBPROG 2018) back in October were published a few days ago and are now &lt;a href=&#34;https://www.youtube.com/playlist?list=PL_PW0E_Tf2qvXBEpl10Y39RULTN-ExzZQ&#34;&gt;available on Youtube&lt;/a&gt;. I have not watched all presentations yet, but a lot of big names attended the conference so there should be something for everyone. In particular the talks by Brooks Paige (&lt;a href=&#34;https://youtu.be/X7ED8Fi1948&#34;&gt;&amp;ldquo;Semi-Interpretable Probabilistic Models&amp;rdquo;&lt;/a&gt;) and Michael Tingley (&lt;a href=&#34;https://youtu.be/gn6M8MX8jpI&#34;&gt;&amp;ldquo;Probabilistic Programming at Facebook&amp;rdquo;&lt;/a&gt;) made me curious to explore their topics more.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Videos from Exploration in RL Workshop at ICML</title>
	  <link>https://minimizeregret.com/note/2018/09/30/videos-from-exploration-in-rl-icml-workshop/</link>
	  <pubDate>Sun, 30 Sep 2018 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2018/09/30/videos-from-exploration-in-rl-icml-workshop/</guid>
	  <description>&lt;p&gt;One of the many fantastic workshops at ICML this year was the &lt;em&gt;Exploration in Reinforcement Learning&lt;/em&gt; workshop. All talks were recorded and are now &lt;a href=&#34;https://www.youtube.com/playlist?list=PLbSAfmOMweH3YkhlH0d5KaRvFTyhcr30b&#34;&gt;available on Youtube&lt;/a&gt;. Highlights include presentations by Ian Osband, Emma Brunskill, and Csaba Szepesvari, among others. You can find the workshop&amp;rsquo;s homepage &lt;a href=&#34;https://sites.google.com/view/erl-2018/&#34;&gt;here&lt;/a&gt; with more information and the accepted papers.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>SVD for a Low-Dimensional Embedding of Instacart Products</title>
	  <link>https://minimizeregret.com/post/2018/07/25/svd-instacart-product-embedding/</link>
	  <pubDate>Wed, 25 Jul 2018 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2018/07/25/svd-instacart-product-embedding/</guid>
	  <description>&lt;p&gt;&lt;em&gt;Building on the Instacart product recommendations based on Pointwise Mutual Information (PMI) in &lt;a href=&#34;https://minimizeregret.com/post/2018/06/17/instacart-products-bought-together/&#34;&gt;the previous article&lt;/a&gt;, we use Singular Value Decomposition to factorize the PMI matrix into a matrix of lower dimension (&amp;ldquo;embedding&amp;rdquo;). This allows us to identify groups of related products easily.&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;We finished &lt;a href=&#34;https://minimizeregret.com/post/2018/06/17/instacart-products-bought-together/&#34;&gt;the previous article&lt;/a&gt; with a long table where every row measured how surprisingly often two products were bought together according to the &lt;a href=&#34;https://www.instacart.com/datasets/grocery-shopping-2017&#34;&gt;Instacart Online Grocery Shopping&lt;/a&gt; dataset. The main point being that recommendations based on pointwise mutual information (PMI) are a strong first solution despite their simplicity. You can explore the results in &lt;a href=&#34;https://timradtke.shinyapps.io/instacart/&#34;&gt;a Shiny app&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;While the previous result could be implemented entirely in SQL, we will move beyond what is feasible in SQL very soon.&lt;/p&gt;&#xA;&lt;h2 id=&#34;goal-a-word2vec-style-representation-of-products&#34;&gt;Goal: A word2vec-style Representation of Products&lt;/h2&gt;&#xA;&lt;p&gt;Using &lt;a href=&#34;https://papers.nips.cc/paper/5021-distributed-representations-of-words-and-phrases-and-their-compositionality.pdf&#34;&gt;word2vec&lt;/a&gt; is all the rage to derive numeric vector representations of words in natural language processing. The result is be a $N\times K$ matrix of $N$ words represented by $K$ vectors. Since $K \ll N$, such a low-dimensional representation is extremely useful as input for subsequent tasks such as classification, or to perform word2vec&amp;rsquo;s hallmark word vector operations such as &lt;em&gt;zuckerberg - facebook + microsoft ~  nadella&lt;/em&gt;.&lt;/p&gt;&#xA;&lt;p&gt;In our case, we deal with $N=8979$ products, which is manageable. Still, by using a small $K = 64$ we are able to go from a $N \times N$ pointwise mutual information matrix to a small-ish set of vectors summarizing the information. And since we will be using &lt;a href=&#34;https://jeremykun.com/2016/04/18/singular-value-decomposition-part-1-perspectives-on-linear-algebra/&#34;&gt;singular value decomposition&lt;/a&gt; to do so, (hopefully most) vectors will even highlight different groups of related products! That&amp;rsquo;s a nice side effect when trying to derive higher level abstractions and patterns in the purchase data&amp;mdash;compared to individual product-to-product relations&amp;mdash;and comparable to topics derived from topic models like &lt;a href=&#34;http://www.jmlr.org/papers/volume3/blei03a/blei03a.pdf&#34;&gt;Latent Dirichlet Allocation&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;This blog post is inspired by Chris Moody&amp;rsquo;s &lt;a href=&#34;https://multithreaded.stitchfix.com/blog/2017/10/18/stop-using-word2vec/&#34;&gt;Stop Using word2vec&lt;/a&gt; on StichFix&amp;rsquo; MultiThreaded blog, in which Chris applied SVD in combination with PMI on the more typical NLP task of embedding words from the &lt;a href=&#34;https://cloud.google.com/bigquery/public-data/hacker-news&#34;&gt;Hacker News&lt;/a&gt; corpus. In the following, we will see that one can use the exact same method to derive a representation of items sold on Instacart.&lt;/p&gt;&#xA;&lt;h2 id=&#34;from-pmi-matrix-to-svd&#34;&gt;From PMI Matrix to SVD&lt;/h2&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://minimizeregret.com/post/2018/06/17/instacart-products-bought-together/&#34;&gt;The previous blog post&lt;/a&gt; introduced the Pointwise Mutual Information and how it can be applied to derive product recommendations for products that have been purchased in the same order. We finished with a long table called &lt;code&gt;pp_rec_smooth&lt;/code&gt; in which every row represented an observed combination of two products that have been purchased together a couple of times, as well as their corresponding pointwise mutual information.&lt;/p&gt;&#xA;&lt;table&gt;&#xA;&#x9;&lt;thead&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.x&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.y&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;pmi&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/thead&gt;&#xA;&#x9;&lt;tbody&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Birthday Candles&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Classic Lighters&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.69&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Birthday Candles&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Super Moist Chocolate Fudge Cake Mix&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.63&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Birthday Candles&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Creamy Classic Vanilla Frosting&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.55&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Birthday Candles&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Rich and Creamy Milk Chocolate Frosting&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.51&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Birthday Candles&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Funfetti Premium Cake Mix With Candy Bits&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.48&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;Consequently, the table is quite long with &lt;code&gt;r dim(pp_rec_smooth)[1]&lt;/code&gt; rows. This is still smaller than the $8979\cdot 8979 = 80622441$ row table for &lt;em&gt;all&lt;/em&gt; combinations of products. The latter, however, is exactly what we need for the matrix factorization through singular value decomposition: A $N \times N$ item-by-item pointwise mutual information matrix. We create this matrix by widening the long table; it&amp;rsquo;s not beautiful, but it does the job.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pmi_matrix &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; pp_rec_smooth &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;select&lt;/span&gt;(product_id.x, product_id.y, pmi) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  tidyr&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;spread&lt;/span&gt;(product_id.y, pmi)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pmi_matrix_product_ids &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; pmi_matrix[,&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pmi_matrix &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; pmi_matrix[,&lt;span style=&#34;color:#ae81ff&#34;&gt;-1&lt;/span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pmi_matrix[1&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## # A tibble: 5 x 5&#xA;##     `1`    `10`   `23`    `25`  `28`&#xA;##   &amp;lt;dbl&amp;gt;   &amp;lt;dbl&amp;gt;  &amp;lt;dbl&amp;gt;   &amp;lt;dbl&amp;gt; &amp;lt;dbl&amp;gt;&#xA;## 1  5.59  NA      NA     NA     NA   &#xA;## 2 NA      5.29  - 1.71 - 0.548 NA   &#xA;## 3 NA    - 1.51    5.89 - 1.36  NA   &#xA;## 4 NA    - 0.498 - 1.51   5.44  NA   &#xA;## 5 NA     NA      NA     NA      6.14&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;All &lt;code&gt;NA&lt;/code&gt;s in this table represent product combinations that have not been observed in the data and for which we consequently do not have a PMI value.&lt;/p&gt;&#xA;&lt;p&gt;The problem with the &lt;code&gt;NA&lt;/code&gt;s is that we cannot perform singular value decomposition on a matrix with missing entries. But assigning those entries any other value would give them meaning: A very negative value would convey a negative relationship, while setting all of them to zero implies no relationship. Neither is something we would like to simply assume in the following.&lt;/p&gt;&#xA;&lt;p&gt;A good alternative described in &lt;a href=&#34;https://web.stanford.edu/~jurafsky/slp3/15.pdf&#34;&gt;Jurafsky and Martin&lt;/a&gt; is to set not only the &lt;code&gt;NA&lt;/code&gt;s to zero, but also the negative PMI values. This can furthermore be justified by the observation that it is generally more difficult to observe a negative relationship using PMI&amp;mdash;as described in the previous blog post and by Jurafsky and Martin. Thus we proceed by replacing both &lt;code&gt;NA&lt;/code&gt;s and negative values by zero.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pmi_matrix&lt;span style=&#34;color:#a6e22e&#34;&gt;[is.na&lt;/span&gt;(pmi_matrix)] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pmi_matrix[pmi_matrix &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pmi_matrix[1&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## # A tibble: 5 x 5&#xA;##     `1`  `10`  `23`  `25`  `28`&#xA;##   &amp;lt;dbl&amp;gt; &amp;lt;dbl&amp;gt; &amp;lt;dbl&amp;gt; &amp;lt;dbl&amp;gt; &amp;lt;dbl&amp;gt;&#xA;## 1  5.59  0     0     0     0   &#xA;## 2  0     5.29  0     0     0   &#xA;## 3  0     0     5.89  0     0   &#xA;## 4  0     0     0     5.44  0   &#xA;## 5  0     0     0     0     6.14&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;colSums&lt;/span&gt;(pmi_matrix)[1&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;] &lt;span style=&#34;color:#75715e&#34;&gt;# not only the diagonal is positive&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;##        1       10       23       25       28 &#xA;## 281.6510 126.0473 162.0798 265.7752 323.0297&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The $N \times N$ matrix is now prepared for singular value decomposition. Though, it&amp;rsquo;s not actually a matrix yet. So let&amp;rsquo;s change that, and let&amp;rsquo;s use the fact that the matrix has become really sparse by converting the object into a sparse matrix.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pmi_matrix &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;as.matrix&lt;/span&gt;(pmi_matrix)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;row.names&lt;/span&gt;(pmi_matrix) &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; pmi_matrix_product_ids&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;product_id.x&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;colnames&lt;/span&gt;(pmi_matrix) &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; pmi_matrix_product_ids&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;product_id.x&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;library&lt;/span&gt;(Matrix)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pmi_sparse_matrix &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Matrix&lt;/span&gt;(pmi_matrix, sparse &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;TRUE&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pmi_sparse_matrix[1&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## 5 x 5 sparse Matrix of class &amp;#34;dgCMatrix&amp;#34;&#xA;##           1       10       23       25       28&#xA;## 1  5.587646 .        .        .        .       &#xA;## 10 .        5.291013 .        .        .       &#xA;## 23 .        .        5.891188 .        .       &#xA;## 25 .        .        .        5.442685 .       &#xA;## 28 .        .        .        .        6.135897&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To make use of the sparsity when performing singular value decomposition, we can for example use the &lt;code&gt;sparsesvd&lt;/code&gt; package. Here, we choose to find a representation of rank 64. This means in particular that we will have 64 vectors representing what was previously represented by 8979 vectors. Using the singular value decomposition, we arrange the available information in a more compact way; we lose some information, but hopefully retain most of it. Feel free to experiment with different values for the rank $K$. Problematic is that there is no good quantitative way of finding a good $K$. One could examine the eigenvalues, or check whether the vectors corresponding to the smallest eigenvalue for a given value of $K$ make some sense, and, if not, choose a smaller $K$ instead.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;library&lt;/span&gt;(sparsesvd)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;set.seed&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;80495&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pmi_svd &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;sparsesvd&lt;/span&gt;(pmi_sparse_matrix, &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9; rank &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;64&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The result looks as follows. We have the $N \times K$ matrix, &lt;code&gt;pmi_svd$u&lt;/code&gt;, as well as the corresponding $K$ singular values. If we plot them, we can see how much information is contained in just the first couple of dimensions.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;dim&lt;/span&gt;(pmi_svd&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;u)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## [1] 8979   64&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;working-with-the-svd&#34;&gt;Working with the SVD&lt;/h2&gt;&#xA;&lt;p&gt;Before we start using the result of the singular value decomposition, we add the product names to the rows of the left-singular vectors.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;products &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; readr&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;read_csv&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;products.csv&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;departments &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; readr&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;read_csv&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;departments.csv&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;id_name_dict &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;data.frame&lt;/span&gt;(product_id &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;as.numeric&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;row.names&lt;/span&gt;(pmi_sparse_matrix))) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;inner_join&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;select&lt;/span&gt;(products, product_id, &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;product_name, aisle_id, department_id)) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;left_join&lt;/span&gt;(departments)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;product_vectors &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; pmi_svd&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;u&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;row.names&lt;/span&gt;(product_vectors) &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; id_name_dict&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;product_name&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With the row names added, we can easily search for specific products as well as the products that are closest in the $K$-dimensional space as measured by a measure as simple as the dot product. That is, we&amp;rsquo;re starting to apply the typical techniques from word embeddings on our product matrix!&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# first a function to find the actual product names&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;search_product &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt;(products, name) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  products&lt;span style=&#34;color:#a6e22e&#34;&gt;[grepl&lt;/span&gt;(name, products, fixed &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;TRUE&lt;/span&gt;)]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# then the function that finds similar products by dot product&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;search_synonyms &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt;(word_vectors, selected_vector) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# https://juliasilge.com/blog/tidy-word-vectors/&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;require&lt;/span&gt;(broom)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  similarities &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; word_vectors &lt;span style=&#34;color:#f92672&#34;&gt;%*%&lt;/span&gt; selected_vector &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#a6e22e&#34;&gt;tidy&lt;/span&gt;() &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#a6e22e&#34;&gt;as_tibble&lt;/span&gt;() &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#a6e22e&#34;&gt;rename&lt;/span&gt;(product &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; .rownames,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;   similarity &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; unrowname.x.)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  similarities &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#a6e22e&#34;&gt;arrange&lt;/span&gt;(&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;similarity)    &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We search for products containing the word &lt;em&gt;Ketchup&lt;/em&gt; and then look for similar products, where the similarity is based on the dot product on a singular value decomposition of commonly bought together items.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;search_product&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;row.names&lt;/span&gt;(product_vectors), &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Ketchup&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;##  [1] &amp;#34;Reduced Sugar Tomato Ketchup&amp;#34; &amp;#34;Tomato Ketchup&amp;#34;              &#xA;##  [3] &amp;#34;Organic Ketchup Gluten Free&amp;#34;  &amp;#34;Organic Ketchup&amp;#34;             &#xA;##  [5] &amp;#34;Simply Heinz Tomato Ketchup&amp;#34;  &amp;#34;Organic Unsweetened Ketchup&amp;#34; &#xA;##  [7] &amp;#34;Classic Ketchup&amp;#34;              &amp;#34;Squeeze Tomato Ketchup&amp;#34;      &#xA;##  [9] &amp;#34;Ketchup&amp;#34;                      &amp;#34;Organic Tomato Ketchup&amp;#34;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;search_synonyms&lt;/span&gt;(product_vectors, &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;product_vectors[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Tomato Ketchup&amp;#34;&lt;/span&gt;,]) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt; &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;head&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  knitr&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;kable&lt;/span&gt;(digits &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;table&gt;&#xA;&#x9;&lt;thead&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;similarity&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/thead&gt;&#xA;&#x9;&lt;tbody&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Classic Hamburger Buns&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0025&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Dill Relish&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0025&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Bun Length Skinless Beef Franks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0022&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Sweet Relish&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0021&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Celery Salt&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0019&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Jumbo Beef Franks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0018&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Hot Dog Buns&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0017&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Classic Yellow Mustard&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0017&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Thousand Island Dressing&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0016&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Golden Crinkles French Fried Potatoes&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0016&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;search_product&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;row.names&lt;/span&gt;(product_vectors), &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Smoothie&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;tail&lt;/span&gt;()&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## [1] &amp;#34;Blueberry B Fruit Smoothie&amp;#34;                              &#xA;## [2] &amp;#34;Organic Original Acai Berry + Guarana Smoothie Packs&amp;#34;    &#xA;## [3] &amp;#34;Protein Smoothie Choc-a-lot Flavor&amp;#34;                      &#xA;## [4] &amp;#34;Power C Machine All Natural Fruit + Boost Juice Smoothie&amp;#34;&#xA;## [5] &amp;#34;Danimals Strawberry Explosion Flavored Smoothie&amp;#34;         &#xA;## [6] &amp;#34;Green Machine Juice Smoothie&amp;#34;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;search_synonyms&lt;/span&gt;(product_vectors, &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;product_vectors[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Green Machine Juice Smoothie&amp;#34;&lt;/span&gt;,]) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;head&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  knitr&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;kable&lt;/span&gt;(digits &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;table&gt;&#xA;&#x9;&lt;thead&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;similarity&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/thead&gt;&#xA;&#x9;&lt;tbody&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Berry Veggie Juice Smoothie&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0289&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Power C Machine All Natural Fruit + Boost Juice Smoothie&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0271&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;All Natural Berry Blast 100% Juice Smoothie&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0266&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Red Machine Juice Smoothie&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0263&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Blue Machine 100% Juice Smoothie&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0263&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Orange Mango Juice&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0260&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Protein Zone Protein Juice Smoothie&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0259&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Protein Zone Double Berry Juice Smoothie&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0258&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Strawberry Banana Juice&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0235&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Kale Blazer Juice&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0228&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;These results look quite good! But also similar to what we have been able to do based on the PMI alone.&lt;/p&gt;&#xA;&lt;h2 id=&#34;exploring-groups-of-products-via-dimensions&#34;&gt;Exploring Groups of Products via Dimensions&lt;/h2&gt;&#xA;&lt;p&gt;Something that the PMI values did not provide us with were &lt;strong&gt;groups of similar/frequently purchased together products&lt;/strong&gt;. Similarly to how we would derive topics when applying Latent Dirichlet Allocation on news articles, it would be super nice to build entire groups of bought-together products. In the case of Instacart, we might even deduct entire recipes. While no one gave singular value decomposition a metaphor as good as the topic model one, the individual singular vectors still allow us to find groups of related products. This is the case because related products should have similar values in certain dimensions. In the extreme, SVD compresses the information about a group of related products onto a single dimension in which these products have particularly high values.&lt;/p&gt;&#xA;&lt;p&gt;Thus we can find &amp;ldquo;important&amp;rdquo; groups of products by exploring for a given vector the products with the largest loading on that vector.&lt;/p&gt;&#xA;&lt;p&gt;The graph below contrasts singular vectors 7 and 8 from the &lt;code&gt;product_vectors&lt;/code&gt; matrix. We see that most observations are very close to 0 on both vectors, while comparatively few products are loaded in absolute terms on just one of the two vectors. This already indicates how the vectors split the work of representing the information that was previously spread across all 8979 vectors: By focusing on a few products per vector, we can more easily store the overall patterns in the data. If a single vector can contain information on about 140 products, then $64 \cdot 140 = 8979$ products can be represented in 64 dimensions.&lt;/p&gt;&#xA;&lt;p&gt;&lt;em&gt;Sorry, this plot has been lost to time and blog moves.&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;If the model works well, related products will load similarly on a given vector. Let&amp;rsquo;s check for example what kind of information vector 8 stores about products. To get an idea, let&amp;rsquo;s just look at the 20 most negatively loaded products on vector 8. You see the list of the products below. Given the many &amp;ldquo;vegan&amp;rdquo;, &amp;ldquo;beefless&amp;rdquo;, &amp;ldquo;dairy free&amp;rdquo;, &amp;ldquo;fishless&amp;rdquo; mentions in product names, it should be safe to say that vegan products tend to load negatively on vector 8. To hype it up a notch: &lt;strong&gt;Our model has learned the concept of a vegan lifestyle from products that were bought together!&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;table&gt;&#xA;&#x9;&lt;thead&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;department&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;X8&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/thead&gt;&#xA;&#x9;&lt;tbody&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Home Style Beefless Tips&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0939&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Golden Fishless Filet&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Mini Crispy Crabless Cakes&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0892&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Vegan Egg&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;dairy eggs&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0876&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Alfredo Cheezy Mac&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0854&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Vegan White Cheddar Mac &amp;amp; Cheese&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;dry goods pasta&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0847&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Chao, Vegan, Tomato Cayenne, Slices&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;dairy eggs&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0833&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Vegan Cheddar Mac &amp;amp; Cheese&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;dry goods pasta&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0824&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Lightly Seasoned Chick’n Scallopini&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0817&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Dairy Free New York Style Cheezecake&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0806&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Vegan Cheddar Flavor Squares&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0802&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Vegan Chao Creamy Original Cheese Slices&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;dairy eggs&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0791&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Veggie Loaf Mashed Potatoes and Vegetables&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0791&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Vegan Hickory &amp;amp; Sage Benevolent Bacon&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;deli&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0784&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Cheese Alternative, American Style, Slices&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;dairy eggs&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0782&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Milk Free Better Than Sour Cream&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;dairy eggs&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0770&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;The Ultimate Beefless Burger&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0764&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Vegan Shells &amp;amp; Creamy Sauce Mac &amp;amp; Cheese Macaroni &amp;amp; Cheese Organic&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;dry goods pasta&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0763&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Dairy Free Grated Parmesan Style Topping&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;dairy eggs&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0762&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Vegan Margherita Pizza With Daiya Cheeze&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.0759&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;We can also look at the other end of vector 8, by considering products that loaded positively. Here we find a selection of snacks. But not just any snacks; we have &lt;strong&gt;a selection of high protein snacks ranging from nut bars to turkey jerkey&lt;/strong&gt;.&lt;/p&gt;&#xA;&lt;table&gt;&#xA;&#x9;&lt;thead&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;department&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;X8&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/thead&gt;&#xA;&#x9;&lt;tbody&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Organic Almond Butter With Carob Macrobar&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0294&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Blueberry Pecan Plus Fiber Fruit &amp;amp; Nut Bar&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0299&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Macrobar Prolonged Powder Banana + Almond Butter&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0303&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Potato Chips Sea Salt &amp;amp; Vinegar&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0304&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Cranberry Almond Plus Fruit &amp;amp; Nut Bars&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0306&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Fruit &amp;amp; Nut Bar Blueberry Vanilla &amp;amp; Cashew&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0310&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Lightly Salted Chips&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0318&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Original Turkey Jerky&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0319&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Organic Coriander Seeds&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;pantry&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0320&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Dark Chocolate Cinnamon Pecan Bar&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0321&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Sweet Rejuvenation Cashew Butter Macrobar&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0333&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Green Dragon with Passion Fruit Tea&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;beverages&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0335&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Healthy Grains Granola Bar, Vanilla Blueberry&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0338&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Apple Cinnamon &amp;amp; Pecan Fruit &amp;amp; Nut Bar&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0345&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Organic Ground Cloves&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;pantry&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0359&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Dark Chocolate Chili Almond Nuts &amp;amp; Spices&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0360&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Turkey Bar, Almond + Cranberry&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0363&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Teriyaki Turkey Jerky&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0379&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Almond Walnut Macadamia Plus Bar&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0381&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Fruit &amp;amp; Nut Delight Bar&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;snacks&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.0391&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;h3 id=&#34;parallel-coordinates-plot&#34;&gt;Parallel Coordinates Plot&lt;/h3&gt;&#xA;&lt;p&gt;&lt;em&gt;Sorry, this plot has been lost to time and blog moves.&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;To take the previous plot to the next level&amp;mdash;or rather to the next dimension&amp;mdash;one can try to display several dimensions in a parallel coordinates plot like the one below. This plot is not ideal on a number of dimensions; it&amp;rsquo;s obviously prone to overplotting. And not being interactive, it&amp;rsquo;s difficult to find out whether there are items that load on two neighboring dimensions.&lt;/p&gt;&#xA;&lt;p&gt;But by adding a color for every department, one gets a sense of the fact that some dimensions load only on products from one or very few departments (e.g., dimensions 2, 3, or 6), while other dimensions (e.g., 7) bring together products from several departments. In the case of dimensions 2 and 3, we even have the lucky case that the same products load on neighboring dimensions. This should generally not happen too often, since it implies that similar information is stored twice. It does happen when the products in question carry a lot of information about the dataset, i.e., explain a lot of the variance. In this case, it creates a very explicit delineation of one category from all others: the &lt;em&gt;babies&lt;/em&gt; department.&lt;/p&gt;&#xA;&lt;p&gt;&lt;em&gt;Sorry, this plot has been lost to time and blog moves.&lt;/em&gt;&lt;/p&gt;&#xA;&lt;table&gt;&#xA;&#x9;&lt;thead&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;department&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;X2&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;X3&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/thead&gt;&#xA;&#x9;&lt;tbody&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Apple and Carrot Stage 2 Baby Food&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;babies&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.04&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Berry &amp;amp; Barley Organic Baby Food&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;babies&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.07&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.05&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Baby Food Pouch - Butternut Squash, Carrot &amp;amp; Chickpea&#x9;babies&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.04&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Squash &amp;amp; Sweet Peas Stage 2&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;babies&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.09&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Stage 2 Spinach, Apple &amp;amp; Kale&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;babies&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.08&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.05&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;While this effect is particularly drastic for the baby food, it is also observable in later dimensions for different product groups. For example, we could consider dimensions 13 and 14. They are not able to separate products quite as clearly, but some clusters are still visible. The products indicated in blue, for example, are a cluster of &lt;em&gt;frozen&lt;/em&gt; and foremost Indian meals. The orange cluster, on the other hand, groups different items used for baking. Note how the rug lines of dimension 13 show that this dimension alone is not able to separate blue from orange observations. It takes the second dimension to successfully perform a separation.&lt;/p&gt;&#xA;&lt;p&gt;&lt;em&gt;Sorry, this plot has been lost to time and blog moves.&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;Frozen meals, Indian dishes for the most part:&lt;/p&gt;&#xA;&lt;table&gt;&#xA;&#x9;&lt;thead&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;department&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;X13&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;X14&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/thead&gt;&#xA;&#x9;&lt;tbody&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Harvest Casserole Bowls&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.07&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Roasted Vegetable Tamale&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.07&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Gluten Free &amp;amp; Dairy Free Thai Red Curry With Organic Jasmin Rice &amp;amp; Vegetables&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.07&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Indian Vegetable Korma Entrée&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.07&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Light &amp;amp; Lean Sweet &amp;amp; Sour Asian Noodle Pasta&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&#x9;-0.07&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Light and Lean Meatless Swedish Meatballs With Broccoli And Pasta&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.07&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Thai Green Curry&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.07&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Indian Mattar Tofu&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.07&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Teriyaki Meal Bowls&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.07&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Black Bean Tamale Verde&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;frozen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.07&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;Advanced baking ingredients:&lt;/p&gt;&#xA;&lt;table&gt;&#xA;&#x9;&lt;thead&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;department&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;X13&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;X14&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/thead&gt;&#xA;&#x9;&lt;tbody&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Angel Flake Sweetened Coconut&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;pantry&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.01&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Nestle Toll House Premier White Chocolate Morsels&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;pantry&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.07&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.02&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Pure Granulated Sugar&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;pantry&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.01&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Light Corn Syrup with Real Vanilla&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;pantry&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.08&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.03&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Powdered Confectioners Sugar&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;pantry&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.08&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.01&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;Since this post would become even longer if I walked you through every interesting dimension, &lt;a href=&#34;https://timradtke.shinyapps.io/instacart_svd_app/&#34;&gt;I instead put the results in a small Shiny app&lt;/a&gt;. You can use it to explore more customer behavior patterns that this simple model was able to learn. For example, find a dimension for all-things burrito, as well as dairy free yogurts and milk in dimension 42.&lt;/p&gt;&#xA;&lt;h2 id=&#34;closing-thoughts&#34;&gt;Closing Thoughts&lt;/h2&gt;&#xA;&lt;p&gt;In this post we have derived a low dimensional representation of products sold through Instacart. Even though a singular value decomposition is a simple model, the derived vector representation seems useful upon inspection. In this post, we have focused our attention on exploring the resulting vectors and showing how they compress and represent information about certain groups of products that are purchased together by customers.&lt;/p&gt;&#xA;&lt;p&gt;We have seen that the singular vectors can be interpreted similarly to LDA&amp;rsquo;s topics given that a given group of products loads on a one or a few vectors. What is still to be explored in future blog posts is the ability to perform vector operations, i.e., operations such as &lt;em&gt;Hamburger Buns - Beef + Wiener ~ Hot Dog Buns&lt;/em&gt;. We have quickly shown how one can similar products to a given product through the dot product, but we have not used the cosine similarity yet. A future blog post will use the two dimensional graphs above to show how the dot product and cosine similarity can be used to explore the space of products.&lt;/p&gt;&#xA;&lt;p&gt;Until then, check out the product dimensions &lt;a href=&#34;https://timradtke.shinyapps.io/instacart_svd_app/&#34;&gt;in the Shiny app&lt;/a&gt; to find more than just the group of vegan products.&lt;/p&gt;&#xA;&lt;h2 id=&#34;references&#34;&gt;References&lt;/h2&gt;&#xA;&lt;p&gt;The Instacart Online Grocery Shopping Dataset 2017. Accessed from &lt;a href=&#34;https://www.instacart.com/datasets/grocery-shopping-2017&#34;&gt;https://www.instacart.com/datasets/grocery-shopping-2017&lt;/a&gt; on May 2, 2018.&lt;/p&gt;&#xA;&lt;p&gt;Chris Moody. &lt;a href=&#34;https://multithreaded.stitchfix.com/blog/2017/10/18/stop-using-word2vec/&#34;&gt;Stop Using word2vec&lt;/a&gt;. Blog post on Stitchfix&amp;rsquo; MultiThreaded blog, October 18, 2017.&lt;/p&gt;&#xA;&lt;p&gt;Julia Silge. &lt;a href=&#34;https://juliasilge.com/blog/tidy-word-vectors/&#34;&gt;Word Vectors with Tidy Data Principles&lt;/a&gt;. Blog post, October 30, 2017.&lt;/p&gt;&#xA;&lt;p&gt;Daniel Jurafsky and James H. Martin. &lt;a href=&#34;https://web.stanford.edu/~jurafsky/slp3/15.pdf&#34;&gt;Vector Semantics&lt;/a&gt;. Book chapter in &lt;em&gt;&lt;a href=&#34;https://web.stanford.edu/~jurafsky/slp3/&#34;&gt;Speech and Language Processing&lt;/a&gt;&lt;/em&gt;. Draft of August 7, 2017.&lt;/p&gt;&#xA;&lt;p&gt;Omer Levy and Yoav Goldberg. &lt;a href=&#34;https://papers.nips.cc/paper/5477-neural-word-embedding-as-implicit-matrix-factorization&#34;&gt;Neural Word Embedding as Implicit Matrix Factorization&lt;/a&gt;. In &lt;em&gt;Advances in Neural Information Processing Systems 27&lt;/em&gt; (NIPS 2014).&lt;/p&gt;&#xA;&lt;p&gt;Omer Levy, Yoav Goldberg and Ido Dagan. &lt;a href=&#34;http://www.aclweb.org/anthology/Q15-1016&#34;&gt;Improving Distributional Similarity with Lessons Learned from Word Embeddings&lt;/a&gt;. &lt;em&gt;Transactions of the Association for Computational Linguistics&lt;/em&gt;, vol. 3, pp. 211–225, 2015.&lt;/p&gt;&#xA;&lt;p&gt;Tomas Mikolov, Ilya Sutskever, Kai Chen, Greg Corrado and Jeffrey Dean. &lt;a href=&#34;https://arxiv.org/abs/1310.4546&#34;&gt;Distributed Representations of Words and Phrases and their Compositionality&lt;/a&gt;.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Pointwise Mutual Information for Instacart Product Recommendations</title>
	  <link>https://minimizeregret.com/post/2018/06/17/instacart-products-bought-together/</link>
	  <pubDate>Sun, 17 Jun 2018 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2018/06/17/instacart-products-bought-together/</guid>
	  <description>&lt;p&gt;&lt;em&gt;Using pointwise mutual information, we create highly efficient &amp;ldquo;customers who bought this item also bought&amp;rdquo; style product recommendations for more than 8000 Instacart products. The method can be implemented in a few lines of SQL yet produces high quality product suggestions. Check them out &lt;a href=&#34;https://timradtke.shinyapps.io/instacart/&#34;&gt;in this Shiny app&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;Back in school, I was a big fan of the Detective Conan anime. For whatever reason, one of the episodes stuck with me. &lt;a href=&#34;https://www.detectiveconanworld.com/wiki/A_Dangerous_Recipe&#34;&gt;In that episode&lt;/a&gt;, the protagonists &amp;ldquo;pick up receipts in a convenience store to guess what the people are buying for dinner.&amp;rdquo; While this leads them inadvertently to a crime they need to solve, we will rather stick with the idea of finding out which products appear together in customers&amp;rsquo; baskets.&lt;/p&gt;&#xA;&lt;p&gt;Based on the &lt;a href=&#34;https://www.instacart.com/datasets/grocery-shopping-2017&#34;&gt;Instacart Online Grocery Shopping&lt;/a&gt; dataset &lt;a href=&#34;https://tech.instacart.com/3-million-instacart-orders-open-sourced-d40d29ead6f2&#34;&gt;released a year ago&lt;/a&gt;, we analyze about 3 million orders of about 200,000 Instacart users. Similarly to how the detective boys used bought-together patterns to identify what customers were going to cook that evening, we&amp;rsquo;re going to find products that are bought together in order to create an effective, yet simple recommendation algorithm. So simple in fact, that the entire analysis could be productionized in plain SQL.&lt;/p&gt;&#xA;&lt;h2 id=&#34;instacart-data-set&#34;&gt;Instacart Data Set&lt;/h2&gt;&#xA;&lt;p&gt;From &lt;a href=&#34;https://en.wikipedia.org/wiki/Instacart&#34;&gt;Wikipedia&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Instacart is an American company that operates as a same-day grocery delivery service. Customers select groceries through a web application from various retailers and delivered by a personal shopper.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;The Instacart Online Grocery Shopping Dataset 2017 was made public by Instacart and can be downloaded &lt;a href=&#34;https://www.instacart.com/datasets/grocery-shopping-2017&#34;&gt;here&lt;/a&gt; and offers a unique ability to try out recommendation algorithms on customer basket data. Then Instacart&amp;rsquo;s VP Data Science, Jeremy Kun introduced the data set in a &lt;a href=&#34;https://tech.instacart.com/3-million-instacart-orders-open-sourced-d40d29ead6f2&#34;&gt;Medium post&lt;/a&gt;. The dataset contains information on the products contained in about 3 million orders made by 200,000 Instacart customers. It thus lends itself as a testbed for machine learning methods that one would tend to apply at ecommerce companies&amp;ndash;in particular those with a large variety of products, large basket sizes and returning customers.&lt;/p&gt;&#xA;&lt;h2 id=&#34;expected-result&#34;&gt;Expected Result&lt;/h2&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2018/06/17/instacart-products-bought-together/amazon_bought_together.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;In &lt;a href=&#34;https://tech.instacart.com/3-million-instacart-orders-open-sourced-d40d29ead6f2&#34;&gt;his blog post&lt;/a&gt;, Jeremy Kun highlights how Instacart uses the data for example to sort their Buy It Again listings, or to model the Frequently Bought With recommendations. Here I will restrict myself to recommendations in the style of the latter. The results of the algorithm should be able to run under a &amp;ldquo;Frequently Bought Together&amp;rdquo; or &amp;ldquo;Customers Who Bought This Item Also Bought&amp;rdquo; headline. Much like Amazon&amp;rsquo;s famous recommendations, or the ones that Instacart employs itself.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2018/06/17/instacart-products-bought-together/instacart_pita_bread.png&#34;&gt;&lt;/figure&gt;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2018/06/17/instacart-products-bought-together/instacart_pita_bought_together.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Take for example &lt;a href=&#34;https://www.instacart.com/store/items/item_2067636&#34;&gt;this Whole Foods pita bread&lt;/a&gt; offered on Instacart. Its page features recommendations for hummus and baba ghannouj under an &amp;ldquo;Often Bought With&amp;rdquo; headline, offering them as common &lt;em&gt;complements&lt;/em&gt;. This style of recommendation is the goal, where we find items that go well together based on past purchases made by all customers. Those recommendations serve as a simple way for customers to fill their baskets with items that increase the value of the items they have already added to their baskets. It also ensures that customers don&amp;rsquo;t forget to buy items they really ought to buy.&lt;/p&gt;&#xA;&lt;p&gt;This stands in contrast to &amp;ldquo;Similar Items&amp;rdquo; or &amp;ldquo;Related Items&amp;rdquo; recommendations that are often found on the same product detail pages. These recommendations usually aim at direct &lt;em&gt;substitutes&lt;/em&gt; to the product on the current detail page. On Instacart&amp;rsquo;s page for Whole Foods Market Organic Whole Wheat Pita Bread, I got served recommendations for a couple of other pita varieties, for example.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2018/06/17/instacart-products-bought-together/instacart_pita_similar_items.png&#34;&gt;&lt;/figure&gt;&lt;h2 id=&#34;methodology&#34;&gt;Methodology&lt;/h2&gt;&#xA;&lt;p&gt;So how exactly are we going to find the products that are often bought with pita bread? How do we know what customers who bought this item also bought?&lt;/p&gt;&#xA;&lt;p&gt;The naive approach would be to count the pure item co-occurrence in orders: For every item, count how often it has been in an order with the pita bread, then recommend the item with the highest count. While this might surface a good recommendation from time to time, it will mostly surface bananas and toilet paper. Bananas and toilet paper are examples of a few very common items which appear in a large share of orders without being related to any product in particular. They would dominate any raw co-occurrence count just by their own purchase probability.&lt;/p&gt;&#xA;&lt;p&gt;To account for this difficulty, we will make use of a simple trick from natural language processing: Pointwise Mutual Information.&lt;/p&gt;&#xA;&lt;h3 id=&#34;pointwise-mutual-information&#34;&gt;Pointwise Mutual Information&lt;/h3&gt;&#xA;&lt;p&gt;Pointwise Mutual Information is a measure of association from information theory and has found a popular application in natural language processing. There, it measures the association between a word and the word&amp;rsquo;s context, e.g. close words in a sentence (bi-grams, &lt;em&gt;n&lt;/em&gt;-grams, etc.). It does so by comparing how often the word and the context appear together against how often they would appear together were they independent events.&lt;/p&gt;&#xA;&lt;p&gt;Following &lt;a href=&#34;https://en.wikipedia.org/wiki/Pointwise_mutual_information&#34;&gt;Wikipedia&lt;/a&gt;, we have for the outcomes $x$ and $c$ of two discrete random variables $X$ and $C$:&lt;/p&gt;&#xA;$$&#xA;pmi(x;c) = \log \frac{p(x,c)}{p(x)p(c)}&#xA;$$&lt;p&gt;Here, the numerator describes the joint probability, while the denominator describes the joint probability under independence. Thus, were the two events independent, we would have $pmi(x;c) = \log(1) = 0$. Consequently, positive PMI values imply positive association between the events (e.g., the word and its context, or between two products).&lt;/p&gt;&#xA;&lt;p&gt;Similarly, negative PMI values should indicate negative relationships—but it’s generally not as easy to think in terms of words that do &lt;em&gt;not&lt;/em&gt; appear together. While it’s easy to come up with co-occuring words (Google &amp;amp; Facebook, Scrum &amp;amp; Agile, Obama &amp;amp; Merkel), I failed to quickly come up with examples for the opposite. It’s not how we’re tuned to think. Also, the necessary corpus to correctly measure the PMI of words that do not appear together is very, very large—because they don’t appear together (see &lt;a href=&#34;https://web.stanford.edu/~jurafsky/slp3/15.pdf&#34;&gt;this book chapter by Daniel Jurafsky and James H. Martin&lt;/a&gt;).&lt;/p&gt;&#xA;&lt;h2 id=&#34;implementation&#34;&gt;Implementation&lt;/h2&gt;&#xA;&lt;h3 id=&#34;data-preparation&#34;&gt;Data Preparation&lt;/h3&gt;&#xA;&lt;p&gt;We’ll now prepare the Instacart data and apply the PMI measure on the observed orders to find products that have been bought together.&lt;/p&gt;&#xA;&lt;p&gt;To follow along, download the csv files &lt;a href=&#34;https://www.instacart.com/datasets/grocery-shopping-2017&#34;&gt;from Instacart&lt;/a&gt;. First, we load some libraries and read the csv files. Note that these are the only packages we&amp;rsquo;ll need. This goes to show that we can do the same analysis in SQL, even though what follows is written in R.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;library&lt;/span&gt;(dplyr)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;library&lt;/span&gt;(readr)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# this is on order level&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;orders &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;read_csv&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;orders.csv&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# this is on product level&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;products &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;read_csv&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;products.csv&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# this is on order-product level&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;order_products &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;read_csv&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;order_products__prior.csv&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Note that we only read one of the available &lt;code&gt;order_products&lt;/code&gt; tables, since we will not perform an evaluation based on a test set. The &lt;code&gt;orders&lt;/code&gt; table, however, contains information on more orders than those contained in &lt;code&gt;order_products&lt;/code&gt;, so we slim it down:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# number of all orders&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;length&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;unique&lt;/span&gt;(orders&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;order_id))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## [1] 3421083&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# number of orders in our subset&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;length&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;unique&lt;/span&gt;(order_products&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;order_id))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## [1] 3214874&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# we focus on the &amp;#34;prior&amp;#34; evaluation set for now&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;orders &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; orders &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;filter&lt;/span&gt;(eval_set &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;prior&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;select&lt;/span&gt;(&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;eval_set)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the next few steps, we trim down the set of considered customers and products to only include those for which we have enough observations.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# first, get for every user his number of orders&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;users &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; orders &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;group_by&lt;/span&gt;(user_id) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;summarize&lt;/span&gt;(orders &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;n&lt;/span&gt;())&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;summary&lt;/span&gt;(users&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;orders)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. &#xA;##    3.00    5.00    9.00   15.59   19.00   99.00&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# drop all users who had a single order or a very large number of orders&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# (customers who only made one order might have bought &amp;#34;trial baskets&amp;#34;)&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;good_users &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; users &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;filter&lt;/span&gt;(orders &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;50&lt;/span&gt;, orders &lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;pull&lt;/span&gt;(user_id)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# filter for the corresponding orders&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;good_orders &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; orders &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;filter&lt;/span&gt;(user_id &lt;span style=&#34;color:#f92672&#34;&gt;%in%&lt;/span&gt; good_users)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# count for every user the number of different items he bought&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;product_by_customer_count &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; order_products &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;inner_join&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;select&lt;/span&gt;(good_orders, order_id, user_id)) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;distinct&lt;/span&gt;(user_id, product_id) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;count&lt;/span&gt;(product_id)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# A considered product should have been bought by &lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# at least 0.1% of the customers&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;product_threshold &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;length&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;unique&lt;/span&gt;(good_orders&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;user_id)) &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0.001&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;good_products &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; product_by_customer_count &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt; &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;filter&lt;/span&gt;(n &lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;=&lt;/span&gt; product_threshold) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;pull&lt;/span&gt;(product_id)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;op &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; order_products &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;select&lt;/span&gt;(order_id, product_id) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;inner_join&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;select&lt;/span&gt;(good_orders, order_id, user_id)) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;filter&lt;/span&gt;(product_id &lt;span style=&#34;color:#f92672&#34;&gt;%in%&lt;/span&gt; good_products)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# as last step, exclude all orders with a basket size of 1&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;op_size_one &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; op &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt; &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;group_by&lt;/span&gt;(order_id) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;summarize&lt;/span&gt;(basket_size &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;n&lt;/span&gt;()) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ungroup &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt; &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;filter&lt;/span&gt;(basket_size &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;pull&lt;/span&gt;(order_id)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;op &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; op &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt; &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;filter&lt;/span&gt;(&lt;span style=&#34;color:#f92672&#34;&gt;!&lt;/span&gt;(order_id &lt;span style=&#34;color:#f92672&#34;&gt;%in%&lt;/span&gt; op_size_one))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;After this initial data cleaning, let&amp;rsquo;s see how many orders, users, and products we are dealing here:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;length&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;unique&lt;/span&gt;(op&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;order_id))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## [1] 2315386&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;length&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;unique&lt;/span&gt;(op&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;user_id))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## [1] 194760&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;length&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;unique&lt;/span&gt;(op&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;product_id))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## [1] 8979&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So after dropping some customers and orders, we are left with about 200k users who bought about 9000 different products across 2.3 million orders. That should more than suffice to compute some PMI values.&lt;/p&gt;&#xA;&lt;h3 id=&#34;pointwise-mutual-information-for-instacart-products&#34;&gt;Pointwise Mutual Information for Instacart Products&lt;/h3&gt;&#xA;&lt;p&gt;To compute the PMI value for every product, we first of all need to count how often products appear together. For the dataset at hand, the following expansion of the &lt;code&gt;order_products&lt;/code&gt; table works fine (you should have quite some RAM though&amp;hellip;); for every order, we join every product against every product in the order. This makes our table much longer, so depending on the average basket size and number of orders in another dataset, it might be a prohibitively expensive computation. We then immediately count how often products appear together:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;op_pp &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;inner_join&lt;/span&gt;(op, op,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;by &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(order_id &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;order_id&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;count&lt;/span&gt;(product_id.x, product_id.y)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;dim&lt;/span&gt;(op_pp)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;## [1] 31708163        3&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Next, we need to count how often every product appears in orders, generally. This is used to compute the probabilities $p(x)$ and $p(c)$. We add these counts to the co-occurrence counts, and add the number of total orders in the &lt;code&gt;total_n&lt;/code&gt; column. At this point we have all ingredients to compute the empirical probabilities and the PMIs.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;product_count_train &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; op &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;count&lt;/span&gt;(product_id)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pp_common_count &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; op_pp &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;inner_join&lt;/span&gt;(product_count_train, by &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(product_id.x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;product_id&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;inner_join&lt;/span&gt;(product_count_train, by &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(product_id.y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;product_id&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;rename&lt;/span&gt;(common_n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; n.x, x_n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; n.y, y_n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; n) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# total number of orders considered&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;mutate&lt;/span&gt;(total_n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;length&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;unique&lt;/span&gt;(op&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;order_id)))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Computing the PMI is now as simple as dividing columns and taking the logarithm. We add the corresponding product names to analyze the results afterwards.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pp_pmi &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; pp_common_count &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;mutate&lt;/span&gt;(common_freq &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;log&lt;/span&gt;(common_n &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; total_n),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9; x_freq &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;log&lt;/span&gt;(x_n &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; total_n),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9; y_freq &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;log&lt;/span&gt;((y_n &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; total_n)),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9; pmi &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; common_freq &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; x_freq &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; y_freq)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pp_rec &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; pp_pmi &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;select&lt;/span&gt;(product_id.x, product_id.y, total_n, common_n, x_n, y_n, pmi) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;left_join&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;select&lt;/span&gt;(products, product_id, product_name), &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;by &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(product_id.x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;product_id&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;left_join&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;select&lt;/span&gt;(products, product_id, product_name), &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;by &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(product_id.y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;product_id&amp;#34;&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;detailed-look-at-recommendations&#34;&gt;Detailed Look at Recommendations&lt;/h3&gt;&#xA;&lt;p&gt;Given that we have not exactly fitted a model here, it&amp;rsquo;s not clear how to evaluate the results. We&amp;rsquo;re not explicitly optimizing for anything, so the following evaluation will be restricted to looking at some recommendations and judging whether the recommendations &amp;ldquo;make sense&amp;rdquo;.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#xA;&lt;p&gt;Given the large sortiment, I had to pick some products at random to evaluate the recommendations. Also, I had to pick products that I actually know&amp;ndash;I&amp;rsquo;m not living in the U.S., so what is &lt;em&gt;Glacier Freeze Frost&lt;/em&gt;?&lt;/p&gt;&#xA;&lt;p&gt;For a start, let&amp;rsquo;s act as if we are about to add Spicy Avocado Hummus to the cart. What could I buy with hummus? Apparently a lot of other hummus, yogurt, as well as crackers or chips:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pp_rec &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt; &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;filter&lt;/span&gt;(product_id.x &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;5973&lt;/span&gt;, product_id.x &lt;span style=&#34;color:#f92672&#34;&gt;!=&lt;/span&gt; product_id.y) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;arrange&lt;/span&gt;(&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;pmi) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;top_n&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;, pmi) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;select&lt;/span&gt;(product_name.x, product_name.y, pmi, common_n, x_n, y_n) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  knitr&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;kable&lt;/span&gt;(digits &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;table&gt;&#xA;&#x9;&lt;thead&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.x&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.y&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;pmi&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;common_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;x_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;y_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/thead&gt;&#xA;&#x9;&lt;tbody&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Spicy Avocado Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Organic Jalapeno Cilantro Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;4.70&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;118&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2541&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;982&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Spicy Avocado Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Organic Kale Pesto Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;4.47&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;97&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2541&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1015&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Spicy Avocado Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Organic Thai Coconut Curry Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;4.32&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;78&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2541&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;945&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Spicy Avocado Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Organic Sriracha Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;4.31&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;144&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2541&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1762&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Spicy Avocado Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Hummus, Hope, Original Recipe&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;3.71&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;206&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2541&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;4572&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Spicy Avocado Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Total 2% Lowfat Greek Yogurt with Honey&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;3.12&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;12&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2541&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;481&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Spicy Avocado Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Organic Jalapeno Crackers&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;3.11&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;12&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2541&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;489&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Spicy Avocado Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Chomperz Original Crunchy Seaweed Chips&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;3.09&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;17&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2541&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;707&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Spicy Avocado Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Teriyaki Turkey Jerky&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;3.06&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;11&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2541&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;472&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Spicy Avocado Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Soft Toothbrush&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;3.03&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;9&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2541&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;397&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;Observe how we don&amp;rsquo;t have the &lt;em&gt;Hummus, Hope, Original Recipe&lt;/em&gt; as the top recommended product even though the avocado hummus was bought most often with it. That is because the PMI takes into account how often the two products appear in orders independently. We see that the &lt;em&gt;Hummus, Hope, Original Recipe&lt;/em&gt; is quite popular, which is why the 206 common orders are not as impactful as the 118 orders together with &lt;em&gt;Organic Jalapeno Cilantro Hummus&lt;/em&gt; for the PMI. And so we want to rank the jalapeno hummus higher.&lt;/p&gt;&#xA;&lt;p&gt;Notice also how some recommendations are based on just 9, 11, 12, or 17 common orders. If we think about how many customers we have, 12 orders can be noise. The toothbrush, for example, does not look like a good recommendation. We will address this with a smoothing method in a minute.&lt;/p&gt;&#xA;&lt;p&gt;If we pick a different hummus, &lt;em&gt;Garlic Hummus&lt;/em&gt;, we get very different results. There is no other hummus recommended, and instead the recommendations focus on pita bread. But notice again how the PMI favors products with a small number of common orders.&lt;/p&gt;&#xA;&lt;table&gt;&#xA;&#x9;&lt;thead&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.x&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.y&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;pmi&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;common_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;x_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;y_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/thead&gt;&#xA;&#x9;&lt;tbody&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Whole Wheat Pita&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.76&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;23&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;489&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;White Pita&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.75&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;71&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1518&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Peanut Butter Dark Chocolate Fruit &amp;amp; Nut Protein Bars&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.62&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;15&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;368&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Gluten Free Black Bean and Quinoa Burrito&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.53&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;18&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;480&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Organic Spinach &amp;amp; Potatoes 2, 6 Months+&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.50&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;18&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;495&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Turkey Meatball Bites&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.50&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;13&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;358&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;100% Whole Wheat Hot Dog Buns&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.46&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;23&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;659&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Organic White Pita Bread&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.42&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;37&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1102&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Lotus Forbidden Rice Ramen&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.38&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;10&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;312&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Gochujang Fermented Garlic Chile Paste&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.20&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;7&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;260&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;Similarly, here are recommendations for products that go well with &lt;em&gt;Granny Smith Apples&lt;/em&gt;.&lt;/p&gt;&#xA;&lt;table&gt;&#xA;&#x9;&lt;thead&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.x&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.y&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;pmi&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;common_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;x_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;y_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/thead&gt;&#xA;&#x9;&lt;tbody&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Royal Gala Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.24&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;238&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2127&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Bag of Red Delicious Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.00&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;74&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;835&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Seedless Grapes Green&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1.98&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;34&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;392&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Dark Chocolate Chili Almond Nuts &amp;amp; Spices&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1.96&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;34&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;399&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Outshine Lime Fruit Bars&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1.95&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;55&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;651&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Golden Delicious Apple&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1.95&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;359&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;4258&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Braeburn Apple&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1.93&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;126&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1523&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Parmesan Deli Style Pretzel Crisps&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1.91&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;49&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;606&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Bag of Oranges&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1.89&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;131&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1657&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Whole Frozen Strawberries&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1.88&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;35&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;445&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;When you work yourself through a couple of examples, it might stand out to you that the PMI tends to favor products with a small probability, that is, rare products tend to be recommended more. This is not necessarily desired, in particular not from the standpoint of a business.&lt;/p&gt;&#xA;&lt;h3 id=&#34;context-distribution-smoothing&#34;&gt;Context Distribution Smoothing&lt;/h3&gt;&#xA;&lt;p&gt;As explained in &lt;a href=&#34;https://web.stanford.edu/~jurafsky/slp3/15.pdf&#34;&gt;Jurafsky and Martin (2017)&lt;/a&gt; citing &lt;a href=&#34;http://www.aclweb.org/anthology/Q15-1016&#34;&gt;Levy at al. (2015)&lt;/a&gt;, a simple way to address this bias is context distribution smoothing, where the context probability is raised to the power of $\alpha$, where $\alpha \in (0,1)$. Since, for example, $0.1^{0.75} \approx 0.1778$, doing so increases the probability of the context, and consequently decreases the PMI.&lt;/p&gt;&#xA;&lt;p&gt;While there is also an impact on events with larger probability, the effect on events with small probability can be more extreme as for example here, leading to a larger absolute discount of their PMI values:&lt;/p&gt;&#xA;$$&#xA;\log(0.25) - \log(0.5) - \log(0.3) \approx 0.511&#xA;$$&lt;p&gt;&#xA;&lt;/p&gt;&#xA;$$&#xA;\log(0.01) - \log(0.5) - \log(0.01) \approx 0.693&#xA;$$&lt;p&gt;&#xA;&lt;/p&gt;&#xA;$$&#xA;\log(0.25) - \log(0.5) - \log(0.3^{0.75}) \approx 0.210&#xA;$$&lt;p&gt;&#xA;&lt;/p&gt;&#xA;$$&#xA;\log(0.01) - \log(0.5) - \log(0.01^{0.75}) \approx -0.458&#xA;$$&lt;p&gt;It also implies that everything that would have been perfectly independent previously does now become negatively associated:&lt;/p&gt;&#xA;$$&#xA;\log(0.25) - \log(0.5) - \log(0.5) = 0&#xA;$$&lt;p&gt;&#xA;&lt;/p&gt;&#xA;$$&#xA;\log(0.25) - \log(0.5) - \log(0.5^{0.75}) \approx -0.173&#xA;$$&lt;p&gt;Setting this aside, the context distribution smoothing can help in many cases to make the top ranks more sensible by returning more mainstream results. We can add the exponent (here 0.75) and compare the results:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;context_exponent &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0.75&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pp_pmi_smooth &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; pp_common_count &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# smooth using the prior&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;mutate&lt;/span&gt;(common_freq &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;log&lt;/span&gt;(common_n &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; total_n),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9; x_freq &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;log&lt;/span&gt;(x_n &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; total_n),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9; y_freq &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;log&lt;/span&gt;((y_n &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; total_n)^context_exponent),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9; pmi &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; common_freq &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; x_freq &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; y_freq)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pp_rec_smooth &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; pp_pmi_smooth &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;select&lt;/span&gt;(product_id.x, product_id.y, total_n, common_n, x_n, y_n, pmi) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;left_join&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;select&lt;/span&gt;(products, product_id, product_name), &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;by &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(product_id.x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;product_id&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;left_join&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;select&lt;/span&gt;(products, product_id, product_name), &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;by &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(product_id.y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;product_id&amp;#34;&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For the apples we can observe that the seedless grapes, the &lt;em&gt;Dark Chocolate Chili Almond Nuts &amp;amp; Spices&lt;/em&gt;, as well as &lt;em&gt;Outshine Lime Fruit Bars&lt;/em&gt; have all been replaced by more apples, and the most frequently purchased item of them all: bananas.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pp_rec_smooth &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;filter&lt;/span&gt;(product_id.x &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;9387&lt;/span&gt;, product_id.x &lt;span style=&#34;color:#f92672&#34;&gt;!=&lt;/span&gt; product_id.y) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;arrange&lt;/span&gt;(&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;pmi) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;select&lt;/span&gt;(product_name.x, product_name.y, pmi, common_n, x_n, y_n) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;head&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;%&amp;gt;%&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  knitr&lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;kable&lt;/span&gt;(digits &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;table&gt;&#xA;&#x9;&lt;thead&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.x&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.y&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;pmi&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;common_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;x_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;y_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/thead&gt;&#xA;&#x9;&lt;tbody&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Royal Gala Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.49&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;238&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2127&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Golden Delicious Apple&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.38&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;359&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;4258&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Gala Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.37&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1061&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;18335&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Banana&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.25&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;8919&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;365728&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Red Delicious Apple&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.21&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;236&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;3062&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Mandarins Bag&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.20&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;295&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;4175&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Bosc Pear&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.15&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;301&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;4578&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Braeburn Apple&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.10&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;126&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1523&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Bag of Oranges&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.08&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;131&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1657&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Granny Smith Apples&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Organic Fuji Apple&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.08&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2156&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;27712&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;69495&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;We see a similar effect for the garlic hummus. Compared to the previous recommendations, we also observe that more of the recommended items now have larger &lt;code&gt;common_n&lt;/code&gt; values, i.e., by introducing the smoothing, we have implicitly ensured that the ranking relies more on common purchases.&lt;/p&gt;&#xA;&lt;table&gt;&#xA;&#x9;&lt;thead&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.x&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.y&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;pmi&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;common_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;x_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;y_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/thead&gt;&#xA;&#x9;&lt;tbody&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;White Pita&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.92&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;71&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;1518&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Sea Salt Pita Chips&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.90&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;351&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;13135&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Jalapeno Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.65&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;157&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6310&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Whole Wheat Pita&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.64&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;23&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;489&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Lemon Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.59&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;250&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;12625&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Organic White Pita Bread&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.51&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;37&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&#x9;1102&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Pita Chips Simply Naked&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.48&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;175&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;9110&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Organic Peeled Whole Baby Carrots&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.44&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;532&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;42519&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Peanut Butter Dark Chocolate Fruit &amp;amp; Nut Protein Bars&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.43&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;15&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;368&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Garlic Hummus&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;100% Whole Wheat Hot Dog Buns&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;0.42&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;23&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;6893&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;659&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;h3 id=&#34;why-pmi-and-not-common-order-count&#34;&gt;Why PMI and not Common Order Count?&lt;/h3&gt;&#xA;&lt;p&gt;To quickly show the impact of using pointwise mutual information to rank the recommendations instead of the raw count of common orders, consider the following example. If we use the pointwise mutual information to get products that are bought together with Birthday Candles, we will get the following items as the top recommendations. The lighter is a natural complement, and everything else is to prepare the cake on which the candles are placed:&lt;/p&gt;&#xA;&lt;table&gt;&#xA;&#x9;&lt;thead&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.x&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.y&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;pmi&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;common_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;x_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;y_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/thead&gt;&#xA;&#x9;&lt;tbody&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Birthday Candles&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Classic Lighters&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.69&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;4&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;208&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;331&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Birthday Candles&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Super Moist Chocolate Fudge Cake Mix&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.63&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;4&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;208&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;360&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Birthday Candles&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Creamy Classic Vanilla Frosting&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.55&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;3&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;208&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;273&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Birthday Candles&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Rich and Creamy Milk Chocolate Frosting&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.51&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;3&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;208&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;285&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Birthday Candles&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Funfetti Premium Cake Mix With Candy Bits&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;2.48&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;5&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;208&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;587&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;If we instead rank by the absolute count of common purchases, the recommended products would be the generally frequently purchased bananas, strawberries, etc., just as I alluded to in the beginning. This just goes to show that the raw count is not an alternative to come up with product recommendations.&lt;/p&gt;&#xA;&lt;table&gt;&#xA;&#x9;&lt;thead&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.x&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;product_name.y&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;pmi&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;common_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;x_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;th&gt;y_n&lt;/th&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/thead&gt;&#xA;&#x9;&lt;tbody&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Birthday Candles&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Banana&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.78&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;24&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;208&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;365728&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Birthday Candles&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Organic Strawberries&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.69&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;16&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;208&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;189410&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Birthday Candles&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Large Lemon&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-0.74&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;11&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;208&#x9;122928&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Birthday Candles&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Bag of Organic Bananas&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-1.66&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;8&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;208&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;274515&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&#x9;&#x9;&lt;tr&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Birthday Candles&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;Strawberries&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;-1.00&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;8&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;208&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;td&gt;113606&lt;/td&gt;&#xA;&#x9;&#x9;&#x9;&lt;/tr&gt;&#xA;&#x9;&lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;h2 id=&#34;closing-thoughts&#34;&gt;Closing Thoughts&lt;/h2&gt;&#xA;&lt;p&gt;Recommendations based on pointwise mutual information alone are of course not perfect. It&amp;rsquo;s easy to find cases in which seemingly random products are recommended based on a few common orders. It&amp;rsquo;s difficult to filter these cases out by setting some threshold on the common orders; three common orders can produce good recommendations depending on the product (just consider the Birthday Candles example above).&lt;/p&gt;&#xA;&lt;p&gt;More, since we&amp;rsquo;re not training a model and optimizing a metric, there is no scalable way of evaluating the result. Without picking a few example products and comparing recommendations, it&amp;rsquo;s difficult to, for example, pick the optimal smoothing exponent.&lt;/p&gt;&#xA;&lt;p&gt;But the PMI ranking serves as excellent baseline solution. Given that only four columns have to be counted, the above recommendations can be written in a couple of lines of SQL. It doesn&amp;rsquo;t take more than a morning to go from no recommendations to a good solution. The PMI gives a lot of bang for the buck.&lt;/p&gt;&#xA;&lt;p&gt;Not only that, but the PMI is also a natural starting point for word embedding models. As indicated in the references below, one could for example extend the ranking here to a full product-product PMI matrix. This high dimensional matrix could then be reduced into a lower dimensional embedding using something as simple as singular value decomposition (see &lt;a href=&#34;https://multithreaded.stitchfix.com/blog/2017/10/18/stop-using-word2vec/#back-4&#34;&gt;Chris Moody&amp;rsquo;s &amp;ldquo;Stop Using word2vec&amp;rdquo;&lt;/a&gt; post on the Stitchfix blog). A word embedding makes it easy to train other models, as for example a clustering to find groups of related products.&lt;/p&gt;&#xA;&lt;p&gt;In any case, take a look at the recommendations from the PMI ranking. I have published an interactive Shiny app which lets you select different products to simulate what could be presented on product display pages. The context smoothing parameter is adjustable as well. &lt;a href=&#34;https://timradtke.shinyapps.io/instacart/&#34;&gt;Try it out here&lt;/a&gt;. And next time your company needs product recommendations, try this as a cheap and good baseline.&lt;/p&gt;&#xA;&lt;h2 id=&#34;references&#34;&gt;References&lt;/h2&gt;&#xA;&lt;p&gt;The Instacart Online Grocery Shopping Dataset 2017. Accessed from &lt;a href=&#34;https://www.instacart.com/datasets/grocery-shopping-2017&#34;&gt;https://www.instacart.com/datasets/grocery-shopping-2017&lt;/a&gt; on May 2, 2018.&lt;/p&gt;&#xA;&lt;p&gt;Daniel Jurafsky and James H. Martin. &lt;a href=&#34;https://web.stanford.edu/~jurafsky/slp3/15.pdf&#34;&gt;Vector Semantics&lt;/a&gt;. Book chapter in &lt;em&gt;&lt;a href=&#34;https://web.stanford.edu/~jurafsky/slp3/&#34;&gt;Speech and Language Processing&lt;/a&gt;&lt;/em&gt;. Draft of August 7, 2017.&lt;/p&gt;&#xA;&lt;p&gt;Omer Levy and Yoav Goldberg. &lt;a href=&#34;https://papers.nips.cc/paper/5477-neural-word-embedding-as-implicit-matrix-factorization&#34;&gt;Neural Word Embedding&#xA;as Implicit Matrix Factorization&lt;/a&gt;. In &lt;em&gt;Advances in Neural Information Processing Systems 27&lt;/em&gt; (NIPS 2014).&lt;/p&gt;&#xA;&lt;p&gt;Omer Levy, Yoav Goldberg and Ido Dagan. &lt;a href=&#34;http://www.aclweb.org/anthology/Q15-1016&#34;&gt;Improving Distributional Similarity with Lessons Learned from Word Embeddings&lt;/a&gt;. &lt;em&gt;Transactions of the Association for Computational Linguistics&lt;/em&gt;, vol. 3, pp. 211–225, 2015.&lt;/p&gt;&#xA;&lt;p&gt;Chris Moody. &lt;a href=&#34;https://multithreaded.stitchfix.com/blog/2017/10/18/stop-using-word2vec/#back-4&#34;&gt;Stop Using word2vec&lt;/a&gt;. Blog post in &lt;em&gt;Stitchfix&amp;rsquo; MultiThreaded&lt;/em&gt; blog.&lt;/p&gt;&#xA;&lt;p&gt;Tomas Mikolov, Ilya Sutskever, Kai Chen, Greg Corrado and Jeffrey Dean. &lt;a href=&#34;https://arxiv.org/abs/1310.4546&#34;&gt;Distributed Representations of Words and Phrases and their Compositionality&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;&#xA;&lt;hr&gt;&#xA;&lt;ol&gt;&#xA;&lt;li id=&#34;fn:1&#34;&gt;&#xA;&lt;p&gt;An first alternative would be to compare recommendations we derive from this &amp;ldquo;training&amp;rdquo; against the product combinations that appear in a test set.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;/div&gt;&#xA;</description>
	</item>
	<item>
	  <title>Pokémon Recommendation Engine</title>
	  <link>https://minimizeregret.com/post/2017/07/01/pok%C3%A9mon-recommendation-engine/</link>
	  <pubDate>Sat, 01 Jul 2017 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2017/07/01/pok%C3%A9mon-recommendation-engine/</guid>
	  <description>&lt;p&gt;&lt;em&gt;Using t-SNE, I wrote a Shiny app that recommends similar Pokémon. Try it out &lt;a href=&#34;https://timradtke.shinyapps.io/poke_recommender&#34;&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;Needless to say, I was and still am a big fan of the Pokémon games. So I was very excited to see that a lot of the meta data used in Pokémon games is available on &lt;a href=&#34;https://github.com/PokeAPI/pokeapi&#34;&gt;Github due to the Pokémon API project&lt;/a&gt;. Data on Pokémon&amp;rsquo;s names, types, moves, special abilities, strengths and weaknesses is all cleanly organized in a few dozen csv files.&lt;/p&gt;&#xA;&lt;p&gt;And so a few lines of dplyr action later, I had imported all data relevant to me into R and organized into a neat data set with a little more than one hundred features for every Pokémon of the first six generations (721 to be exact).&lt;/p&gt;&#xA;&lt;p&gt;If you give a data set like this to a Pokémon nerd like me, what happens? Well, one of the big frustrations in the game can happen when for what ever reason you would like to use a specific Pokémon in your team, but you cannot catch it. Imagine that a &lt;a href=&#34;https://bulbapedia.bulbagarden.net/wiki/Butterfree&#34;&gt;Butterfree&lt;/a&gt; would be a great addition to your team because you want to have one team member with a lot of status type moves. But you&amp;rsquo;re playing the &lt;a href=&#34;https://bulbapedia.bulbagarden.net/wiki/Pok%C3%A9mon_Black_and_White_Versions&#34;&gt;Black edition&lt;/a&gt; in which Butterfree cannot be caught. So what Pokémon would be good alternatives?&lt;/p&gt;&#xA;&lt;p&gt;Here is where the &lt;a href=&#34;https://timradtke.shinyapps.io/poke_recommender&#34;&gt;Pokémon Recommendation Engine&lt;/a&gt; comes in. Just like Amazon recommends you similar products when you&amp;rsquo;re visiting a product page, and similar to how Spotify can recommend you music you&amp;rsquo;d might like, the Pokémon Recommendation Engine will suggest you good alternatives to a given Pokémon.&lt;/p&gt;&#xA;&lt;h4 id=&#34;how-the-recommendation-engine-is-built&#34;&gt;How the Recommendation Engine is Built&lt;/h4&gt;&#xA;&lt;p&gt;My goal was to get a measure of similarity between Pokémon based on which the recommendations can be made. Ideally, the recommendations would not only be based on straightforward characteristics like the evolution of the Pokémon or the type of the Pokémon; instead, it would be more interesting to get Pokémon recommended that would have similar characteristics in a Pokémon battle (sorry if I go too deep into the game here). Thus I made sure to include primarily battle related information like information on moves. Since moves have types as well, a lot of information about the Pokémon types would make its way into the data set anyway.&lt;/p&gt;&#xA;&lt;p&gt;I then used &lt;a href=&#34;https://en.wikipedia.org/wiki/T-distributed_stochastic_neighbor_embedding&#34;&gt;t-distributed Stochastic Neighbor Embedding&lt;/a&gt; (t-SNE) to embed the data into a two-dimensional space. Now, t-SNE is meant for visualization purposes. It is not meant to calculate distances between observations. Indeed, it can take a distance matrix as input. Furthermore, &lt;a href=&#34;http://distill.pub/2016/misread-tsne/&#34;&gt;one should in general not try to interpret the distance between groups of observations in the resulting t-SNE plane&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;That said, I still went ahead and calculated the Euclidean distance between the Pokémon in the resulting 2D plane. I had chosen a low perplexity value for the t-SNE which led to a result in which many clusters of a few Pokémon were generated. This means that if I want to find the six nearest neighbors of a given Pokémon, chances are that those are found within one of these groups. Based on this effect, t-SNE leads to a very efficient way to get recommendations &amp;ndash; at least for this small data set. Additionally, since the data is inherently interpretable, it was very easy to judge the result by the resulting t-SNE visualization. Indeed, for this minimum viable product, I restrained from using statistical measures. Instead, I plotted the Pokémon sprites, which are also included in the Pokémon API repository, and judged the groups through my years and years of experience as a PokeTrainer. After I played around with the perplexity value, I decided that the current solution was good enough for a first version.&lt;/p&gt;&#xA;&lt;p&gt;And so it was time to built a small Shiny app in which one can select a Pokémon and get six recommendations. The app is online, so &lt;a href=&#34;https://timradtke.shinyapps.io/poke_recommender/&#34;&gt;head over there&lt;/a&gt; and try for example the recommendations for Snorlax (#143).&lt;/p&gt;&#xA;&lt;p&gt;Here is a screenshot:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2017/07/01/pok%c3%a9mon-recommendation-engine/poke_recommender_screenshot.png&#34;&gt;&lt;/figure&gt;</description>
	</item>
	<item>
	  <title>Look At All These Links</title>
	  <link>https://minimizeregret.com/post/2017/01/25/look-at-all-these-links/</link>
	  <pubDate>Wed, 25 Jan 2017 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2017/01/25/look-at-all-these-links/</guid>
	  <description>&lt;p&gt;By now, some time has passed since NIPS 2016. Consequently, several recaps can be found on blogs. One of them is &lt;a href=&#34;http://blog.evjang.com/2017/01/nips2016.html&#34;&gt;this one by Eric Jang&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;If you want to make your first steps in putting some of the theory presented at NIPS into practice, why not take a look at &lt;a href=&#34;http://www.rblog.uni-freiburg.de/reinforcement-learning-in-r/&#34;&gt;this slide deck&lt;/a&gt; about reinforcement learning in R?&lt;/p&gt;&#xA;&lt;p&gt;The RStudio Conference also took place, and apparently has been a blast. You can find recaps &lt;a href=&#34;https://robinsones.github.io/RStudio-Conference-Recap/&#34;&gt;here&lt;/a&gt; and &lt;a href=&#34;http://www.gettinggeneticsdone.com/2017/01/rstudio-conference-2017-recap.html&#34;&gt;here&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;One presentation held at the RStudio conference was by Yihui Xie, about using R Markdown to create blogs and websites using the new &lt;code&gt;blogdown&lt;/code&gt; package. You can &lt;a href=&#34;https://slides.yihui.name/2017-rstudio-conf-blogdown-Yihui-Xie.html#1&#34;&gt;find his slides here&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Another &lt;a href=&#34;http://www.slideshare.net/hilaryparker/opinionated-analysis-development&#34;&gt;presentation at RStudio Conf was given by Hilary Parker&lt;/a&gt; of Stitchfix. The last time I wrote this kind of link collection, I linked to a &lt;a href=&#34;http://www.slideshare.net/Work-Bench/scaling-analysis-responsibly&#34;&gt;presentation she gave at the New York R Conference&lt;/a&gt;. This time around, the topic is similar: &lt;em&gt;Opinionated Analysis Development&lt;/em&gt;, which is mindset towards data analysis that could help to scale the work of a growing data science team.&lt;/p&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://thuijskens.github.io/2016/12/29/bayesian-optimisation/&#34;&gt;Intro to Bayesian hyper-parameter optimization&lt;/a&gt; using Gaussian Process priors in scikit-learn:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Bayesian optimization falls in a class of optimization algorithms called sequential model-based optimization (SMBO) algorithms. These algorithms use previous observations of the loss &lt;em&gt;f&lt;/em&gt;, to determine the next (optimal) point to sample &lt;em&gt;f&lt;/em&gt; for.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;The Data Science Lab at Johns Hopkins University has started a newsletter. &lt;a href=&#34;http://tinyletter.com/jhudatascience&#34;&gt;Subscribe here&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Yanir Seroussi has built a Bayesian A/B Test calculator which is available &lt;a href=&#34;https://yanirs.github.io/tools/split-test-calculator/&#34;&gt;here&lt;/a&gt;. Read &lt;a href=&#34;https://yanirseroussi.com/2016/06/19/making-bayesian-ab-testing-more-accessible/&#34;&gt;his corresponding blog post&lt;/a&gt; for more information.&lt;/p&gt;&#xA;&lt;p&gt;Justin O’Beirne, who led the development of cartography at Apple, writes &lt;a href=&#34;https://www.justinobeirne.com/essays/&#34;&gt;extremely detailed essays&lt;/a&gt; on Apple and Google Maps, which are beautiful to look at. I always found map apps to be such incredibly useful and magical technology. It’s fun to read opinions from someone who creates them and obsesses about their design. Also, if you’re into data visualization, make sure to check out his cartography reading list (O’Beirne: “One thing you’ll likely notice: almost none of the books are ‘cartography’ books.”).&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Multi-Armed Bandits at Tinder</title>
	  <link>https://minimizeregret.com/note/2016/12/14/multi-armed-bandits-at-tinder/</link>
	  <pubDate>Wed, 14 Dec 2016 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/note/2016/12/14/multi-armed-bandits-at-tinder/</guid>
	  <description>&lt;p&gt;In a &lt;a href=&#34;http://tech.gotinder.com/smart-photos-2/&#34;&gt;post on Tinder&amp;rsquo;s tech blog&lt;/a&gt;, Mike Hall presents a new application for multi-armed bandits. At Tinder, they started to use multi-armed bandits to optimize the photo of users that is shown first: While a user can have multiple photos in his profile, only one of them is shown first when another user swipes through the deck of user profiles. By employing an adapted epsilon-greedy algorithm, Tinder optimizes this photo for the &amp;ldquo;Swipe-Right-Rate&amp;rdquo;. Mike Hall about the project:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;It seems to fit our problem perfectly. Let’s discover which profile photo results in the most right swipes, without wasting views on the low performing ones. &amp;hellip;&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;We were off to a solid start with just a little tweaking and tuning. Now, we are able to leverage Tinder’s massive volume of swipes in order to get very good results in a relatively small amount of time, and we are convinced that Smart Photos will give our users a significant upswing in the number of right swipes they are receiving with more complex and fine-tuned algorithms as we move forward.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;</description>
	</item>
	<item>
	  <title>Look At All These Links</title>
	  <link>https://minimizeregret.com/post/2016/11/06/look-at-all-these-links/</link>
	  <pubDate>Sun, 06 Nov 2016 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2016/11/06/look-at-all-these-links/</guid>
	  <description>&lt;p&gt;At Airbnb, the &lt;a href=&#34;https://medium.com/airbnb-engineering/using-r-packages-and-education-to-scale-data-science-at-airbnb-906faa58e12d#.vkf36gpfg&#34;&gt;data science team has written their own R packages&lt;/a&gt; to scale with the company’s growth. The most basic achievement of the packages is the standardization of the work (ggplot and RMarkdown templates) and reduction of duplicate effort (importing data). New employees are introduced to the infrastructure with extensive workshops.&lt;/p&gt;&#xA;&lt;p&gt;This reminded me of a &lt;a href=&#34;http://www.slideshare.net/Work-Bench/scaling-analysis-responsibly&#34;&gt;presentation by Hilary Parker&lt;/a&gt; in April at the New York R Conference on &lt;em&gt;Scaling Analysis Responsibly&lt;/em&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Speaking of education within a company, &lt;a href=&#34;http://www.unofficialgoogledatascience.com/2016/10/practical-advice-for-analysis-of-large.html&#34;&gt;here is Patrick Riley’s most popular advice&lt;/a&gt; within Google on the analysis of large, complex data sets:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;To answer those questions, I put together a document shared Google-wide which I optimistically and simply titled “Good Data Analysis.” To my surprise, this document has been read more than anything else I’ve done at Google over the last eleven years. Even four years after the last major update, I find that there are multiple Googlers with the document open any time I check.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;&lt;a href=&#34;http://www.martingoodson.com/ten-ways-your-data-project-is-going-to-fail/&#34;&gt;Ten ways your data project is going to fail&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Given the &lt;a href=&#34;https://twitter.com/johnmyleswhite/status/793110465337372672?ref_src=twsrc%5Etfw&#34;&gt;Twitter thread by John Myles White&lt;/a&gt; that &lt;a href=&#34;https://www.timradtke.net/blog/sign-of-incompetence&#34;&gt;I linked to&lt;/a&gt; earlier this week, in which he recommends using “aggressive confidence intervals with nominal coverage that&amp;rsquo;s &amp;gt;= 99.9%”, &lt;a href=&#34;http://andrewgelman.com/2016/11/05/why-i-prefer-50-to-95-intervals/&#34;&gt;this short post&lt;/a&gt; by Andrew Gelman on 50% &lt;em&gt;uncertainty&lt;/em&gt; intervals is even better than it is on its own. Assuming that many humans think of a toin coss whenever they’re talking about uncertainty/chance, this might be the way to go.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Three Types of Cluster Reproducibility</title>
	  <link>https://minimizeregret.com/post/2016/06/14/three-types-of-cluster-reproducibility/</link>
	  <pubDate>Tue, 14 Jun 2016 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2016/06/14/three-types-of-cluster-reproducibility/</guid>
	  <description>&lt;p&gt;Christian Hennig provides a function called &lt;code&gt;clusterboot()&lt;/code&gt; in his R package &lt;code&gt;fpc&lt;/code&gt; which &lt;a href=&#34;https://minimizeregret.com/post/2016/05/30/assessing-the-quality-of-a-clustering-solution&#34;&gt;I mentioned before&lt;/a&gt; when talking about assessing the quality of a clustering. The function runs the same cluster algorithm on several bootstrapped samples of the data to make sure that clusters are reproduced in different samples; it validates the cluster stability.&lt;/p&gt;&#xA;&lt;p&gt;In a similar vein, the reproducibility of clusterings with subsequent use for marketing segmentation is discussed in &lt;a href=&#34;https://epub.ub.uni-muenchen.de/10960/1/tr063.pdf&#34;&gt;this paper&lt;/a&gt; by Dolnicar and Leisch. They describe three types of cluster solutions distinguished by their level of reproducibility. The authors argue that this kind of assessment is necessary so as to communicate the correct value of the results to the management. The marketing team is then able to work with the results to create an appropriate customer segmentation.&lt;/p&gt;&#xA;&lt;h2 id=&#34;natural-clustering&#34;&gt;Natural Clustering&lt;/h2&gt;&#xA;&lt;p&gt;The first and optimal type of clustering is what the authors call &lt;em&gt;natural clustering/segmentation&lt;/em&gt;. There exist (obvious) groups (“density clusters”) that are easily picked up by algorithms and persist over different samples. Basically, there exists some natural cause segmenting the observations. Imagine a 2D-plot where two very clearly different groups of observations are shown.&lt;/p&gt;&#xA;&lt;h2 id=&#34;reproducible-clustering&#34;&gt;Reproducible Clustering&lt;/h2&gt;&#xA;&lt;p&gt;The second type of clustering they call &lt;em&gt;reproducible clustering/segmentation&lt;/em&gt;:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Typically, consumer data contain some structure, but not density clusters. … In such situations true density clusters cannot be revealed but data structure can be used to derive stable, reproducible market segments.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;I&amp;rsquo;d describe the natural clustering as an idealized type of solution that usually doesn&amp;rsquo;t occur, and so a strongly reproducible clustering is the outcome one is looking for. There does not necessarily exist a natural distinction of segments, but the data may still be very structured. Alas, this is not always the case and leads to the third type.&lt;/p&gt;&#xA;&lt;h2 id=&#34;constructive-clustering&#34;&gt;Constructive Clustering&lt;/h2&gt;&#xA;&lt;p&gt;&lt;em&gt;Constructive clustering/segmentation&lt;/em&gt; is what the authors call cluster solutions which are not reproducible due to unstructured data, but which may inform market segmentation in a valuable manner anyways. The authors argue that an artificial segmentation can still be better than no segmentation, and so “while the cluster algorithm cannot reveal true groups of consumers, it can still help to create managerially useful subgroups of customers”.&lt;/p&gt;&#xA;&lt;p&gt;Working with this scaffolding, Dolnicar and Leisch go on to show how two questions about clusters can be examined. First, “Does the data set contain natural clusters?” and second, “Does the data set allow reproduction of similar segmentation solutions?”. To perform the explanatory analysis on articificial and real data sets, they have used the packages &lt;code&gt;flexmix&lt;/code&gt; and &lt;a href=&#34;https://cran.r-project.org/web/packages/flexclust/flexclust.pdf&#34;&gt;&lt;code&gt;flexclust&lt;/code&gt;&lt;/a&gt; by Leisch himself. The function &lt;code&gt;bootFlexclust()&lt;/code&gt; they use is similar to &lt;code&gt;clusterboot()&lt;/code&gt; in that it implements a repeated clustering on bootstrapped data.&lt;/p&gt;&#xA;&lt;p&gt;With regard to the bootstrapping for stability assessment, the authors seem to overlap with Christian Hennig&amp;rsquo;s recommendations. But I draw value from the way in which they interpret cluster solutions and prepare them for management (in their theoretical examples). This is something I don’t come across in my courses but as time goes on it will become increasingly important for me. Take for example this paragraph (which is sadly based on their aritificial data; also, they never explicitly go through a thought experiment in which they present a constructive clustering/segmentation, the weakest type, to management):&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;To continue the marketing management interpretation of the artificial example, we would suggest the following consequences for the two data sets: the square data would be classified as stable segmentation case with four market segments. In addition to the two segments mentioned above, this data set would include also highly price-elastic techno buffs and – every manager’s dream segment – the modest price-inelastic mobile phone users in the top right-hand corner who seemingly do not mind to pay a lot of money for a basic mobile phone with few features only. In the case of the circular data set, management has to be informed that natural segments do not exist and that the data set does not enable the researcher to determine stable segments of consumers. In this constructive segmentation case a number of solutions have to be computed, visualized, described and presented to management for comparative evaluation.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;</description>
	</item>
	<item>
	  <title>Assessing the Quality of a Clustering Solution</title>
	  <link>https://minimizeregret.com/post/2016/05/30/assessing-the-quality-of-a-clustering-solution/</link>
	  <pubDate>Mon, 30 May 2016 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2016/05/30/assessing-the-quality-of-a-clustering-solution/</guid>
	  <description>&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2016/05/30/assessing-the-quality-of-a-clustering-solution/cornflakes-clusters.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;During one of the talks at PyData Berlin, a presenter quickly mentioned a k-means clustering used to group similar clothing brands. She commented that it wasn’t perfect, but good enough and the result you would expect from a k-means clustering.&lt;/p&gt;&#xA;&lt;p&gt;There remains the question, however, how one can assess whether a clustering is “good enough”. In above case, the number of brands is rather small, and simply by looking at the groups one is able to assess whether the combination of Tommy Hilfiger and Marc O’Polo is sensible. This becomes more difficult, though, if you cluster hundreds or thousands of observations. Additionally, consider several clustering methods besides k-means from which to choose. In supervised learning, you always have labels to tell you the truth (for your training data), but in clustering, there is no objectively correct solution. How can we assess a cluster’s quality?&lt;/p&gt;&#xA;&lt;p&gt;In his &lt;a href=&#34;http://bactra.org/notebooks/clustering.html&#34;&gt;notebooks&lt;/a&gt;, Cosma Shalizi shares the frustration with assessing clustering quality:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;This [clustering] is an important subject, but one of the topics I most dislike teaching in data mining, because the students&amp;rsquo; natural question is always &amp;ldquo;how do I know when my clustering algorithm is giving me a good solution?&amp;rdquo;, and it&amp;rsquo;s very hard to give them a reasonable answer. I think this is because most other data-mining problems are basically predictive, and so one can ask how good the prediction is; what&amp;rsquo;s the best way to turn clustering into a prediction problem?&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;While Christian Hennig doesn&amp;rsquo;t answer how clustering can be turned into a prediction problem, he did propose methods for assessing the clustering quality in a recent talk at PyData London. His presentation is &lt;a href=&#34;https://www.youtube.com/watch?v=Mf6MqIS2ql4&#34;&gt;available on Youtube&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;For me, the most important takeaway from Hennig’s presentation is the fact that due to the lack of a “true” clustering, the researcher has to choose from different clustering goals for which to optimize. For example, he might optimize for between-cluster separation, within-cluster homogeneity, or uniform cluster sizes. Since some of the goals may be in conflict with each other, the goals have to be scored by importance, and the different measures of quality have to be weighted. As usual, it is important to know the question you would like to answer; only then you can argue whether your clustering delivers a valuable solution.&lt;/p&gt;&#xA;&lt;p&gt;In his presentation, Hennig refers to &lt;a href=&#34;http://onlinelibrary.wiley.com/doi/10.1111/j.1467-9876.2012.01066.x/abstract&#34;&gt;a paper he co-authored&lt;/a&gt; in which he describes diligently how he chooses between two different clustering methods in order to cluster individuals in a social stratification data set, and how he is able to compare the resulting clusters’ validity. The paper includes a chapter on the clustering philosophy he and Tim Liao applied in this specific case. They write:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;In most applications including social stratification the researcher may not be able to justify all the required decisions in detail; sometimes different aspects of what the researcher is interested in are in conflict, and there is feedback between the desired cluster concept and the data (the researcher may want very homogeneous but not too small clusters, or may have &lt;strong&gt;unrealistic expectations about existing separation between clusters&lt;/strong&gt;). Furthermore, it is hardly possible to anticipate the implications of all required tuning decisions for the given data. As mentioned before, researchers often want to check whether the found clusters carry more meaning than an optimal partition of homogeneous random data. Therefore it is not enough to specify a cluster concept and to select a method. Clustering results need to be validated further.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;The highlighted sentence expresses the frustration that clustering will entail if you start out with unrealistic expextations (as is so often the case). For example, if you set out to perform a clustering on a given data set for your thesis, and your clustering doesn’t bring up important clusters, you want to be smart about the communication of the result. Why didn’t it happen, which goals can still be achieved, which goals were unreasonable given the data. On the other hand, if your straightforward k-means returned interesting groupings of observations, you’d better be sure to communicate results which are valid and generalize. Else, you might find yourself making business decisions based on random allocations of brands.&lt;/p&gt;&#xA;&lt;p&gt;To that end, I recommend Hennig’s &lt;a href=&#34;https://cran.r-project.org/web/packages/fpc/fpc.pdf&#34;&gt;R package &lt;code&gt;fpc&lt;/code&gt;&lt;/a&gt; in which he has prepared several measures to support you with cluster validation.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Taxi Pulse of New York City</title>
	  <link>https://minimizeregret.com/post/2015/09/21/taxi-pulse-of-new-york-city/</link>
	  <pubDate>Mon, 21 Sep 2015 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2015/09/21/taxi-pulse-of-new-york-city/</guid>
	  <description>&lt;p&gt;I don&amp;rsquo;t know about you, but I think taxi data is fascinating. There is a lot you can do with the data sets as they usually contain observations on geolocation as well as time stamps besides other information, which makes them unique. Geolocation and timestamps alone, as well as the large number of observations in cities like New York enable you to create stunning visualizations that aren&amp;rsquo;t possible with any other set of data.&lt;/p&gt;&#xA;&lt;p&gt;For example, it is possible to visualize the streets of New York by simply plotting the locations at which taxis have picked up customers. You can find the version that I&amp;rsquo;ve created in R &lt;a href=&#34;http://www.timradtke.net/blog/nyc-taxi-map&#34;&gt;here&lt;/a&gt;, which is based on the ideas of two others who have written their visualizations in C and Python, respectively.&lt;/p&gt;&#xA;&lt;p&gt;Earlier this year, there have been two competitions on Kaggle for which the organization of ECML/PKDD provided a data set on taxis in Porto, Portugal. For the first competition users were asked to &lt;a href=&#34;https://www.kaggle.com/c/pkdd-15-taxi-trip-time-prediction-ii&#34;&gt;predict the remaining travel time&lt;/a&gt; of a taxi after a customer had been picked up. The second competition challenged Kaggle users to &lt;a href=&#34;https://www.kaggle.com/c/pkdd-15-predict-taxi-service-trajectory-i&#34;&gt;build a predictive framework able to infer the final destination of taxi rides&lt;/a&gt; based on their partial trajectories.&lt;/p&gt;&#xA;&lt;p&gt;The latest use case I&amp;rsquo;ve come across is a visual comparison of Uber usage in different cities created by Uber&amp;rsquo;s engineering team. You can find their graphics and the accompanying article on their &lt;a href=&#34;https://eng.uber.com/pulse-of-a-city/&#34;&gt;blog&lt;/a&gt;. Each of their graphics shows the amount of Uber trips depending on weekday and hour. The number of trips is compared within weekdays with high levels of usage displayed in a bright blue color.&lt;/p&gt;&#xA;&lt;p&gt;Of course, the data the engineering team used is private to Uber which restricts one from recreating the exact graphics. But since New York&amp;rsquo;s Taxi and Limousine Commission (TLC) has &lt;a href=&#34;http://www.nyc.gov/html/tlc/html/about/trip_record_data.shtml&#34;&gt;publicized their data on taxi usage&lt;/a&gt;, I&amp;rsquo;m able to create a corresponding graphic based on taxi cab usage.&lt;/p&gt;&#xA;&lt;p&gt;To do so, I access the TLC&amp;rsquo;s data set through Google BigQuery. That way, I don&amp;rsquo;t have to work with &lt;code&gt;.csv&lt;/code&gt; files that are several GB large. Instead, I can simply use a SQL query to extract the data in the format that I prefer. In this case, I&amp;rsquo;m especially interested in the timestamp describing the time at which the taxi rides occured. Since I only want to count each ride once, I don&amp;rsquo;t use both pickup and dropoff times but only pickup times. Furthermore, I would like to aggregate the rides on an hourly basis. Thus, I strip away the minutes and seconds from the timestamp using the &lt;code&gt;HOUR()&lt;/code&gt; function in BigQuery. Seperately, I extract the date of the ride with &lt;code&gt;DATE()&lt;/code&gt;. Using these two new columns, I can group the observations by date and hour in order to count the rides that occurred during an hour of a certain day. Additionally, I have restricted the observations I use on the area of New York City that I used for my previous graphics.&lt;/p&gt;&#xA;&lt;p&gt;As last time, I don&amp;rsquo;t download the data directly through the BigQuery web interface but use the &lt;code&gt;bigrquery&lt;/code&gt; package to access BigQuery through R instead. Proceeding this way, I can directly store the data in a data frame and continue to work with it.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;library&lt;/span&gt;(bigrquery)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;project &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;nyc-taxi-data-1048&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;taxiTimes &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;SELECT DATE(pickup_datetime) AS pickupDay, HOUR(pickup_datetime) AS&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#x9;&#x9;&#x9;  pickupHour, COUNT(*) AS num_pickups FROM [nyc-tlc:yellow.trips_2015] &#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#x9;&#x9;&#x9;  WHERE (pickup_latitude BETWEEN 40.61 AND 40.91)  AND &#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#x9;&#x9;&#x9;  (pickup_longitude BETWEEN -74.06 AND -73.77) GROUP BY pickupDay,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#x9;&#x9;&#x9;  pickupHour ORDER BY pickupDay, pickupHour;&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;taxi_df &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;query_exec&lt;/span&gt;(taxiTimes, project &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; project, max_pages &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Inf&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;dim&lt;/span&gt;(taxi_df) &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# 4344 3&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The resulting data frame has three variables and 4344 observations (one observation for every hour during the months from January to June of 2015).&lt;/p&gt;&#xA;&lt;p&gt;A quick look into my calendar reveals that the first day of 2015 has been a Thursday. Therefore I can add the name of the days to the data frame with the following commands:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;days_df &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;data.frame&lt;/span&gt;(days &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;rep&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Thursday&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Friday&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Saturday&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;   &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Sunday&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Monday&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Tuesday&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;   &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Wednesday&amp;#34;&lt;/span&gt;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9; each &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;24&lt;/span&gt;), length.out &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;4344&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;taxi_df &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;cbind&lt;/span&gt;(taxi_df,days_df)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now I have all information in the data frame that I would like to display in my graphic. The last thing to do before creating the actual visualization is to group the observations a second time, this time by weekday. While doing so, I take the mean of rides that have occured on a certain day during a certain hour. In the end, my data frame should therefore have 168 observations: 24 observations for each of the 7 days of the week.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;library&lt;/span&gt;(plyr)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;meanRidesDay &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt; (taxidf) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;mean&lt;/span&gt;(taxidf&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;num_pickups)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ridesPerWeekdayUTC &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;ddply&lt;/span&gt;(taxi_df, .(dayUTC, hourUTC), meanRidesDay)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# &amp;gt; head(ridesPerWeekdayUTC, 3)&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#    dayUTC hourUTC              V1&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# 1    Friday              0 17396.423&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# 2    Friday              1  11161.115&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# 3    Friday              2   7313.385&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So far, so good. The data frame is ready to be plotted. It contains three variables: The weekday, the hour, and the average number of rides that occured during the day and hour. It&amp;rsquo;s easy to now plot weekdays on the horizontal axis, hours on the vertical axis, and to use the average rides to create a density plot.&lt;/p&gt;&#xA;&lt;p&gt;The &lt;code&gt;ggplot2&lt;/code&gt; package provides the function to do so: &lt;code&gt;geom_tile()&lt;/code&gt;.  Given a data frame containing a row for each combination of the discrete value ranges on both axes (here, a combination of every day with every hour), it creates a rectangle for every observation so that the plotting area is filled with tiles, one tile per observation. The color of the tiles is determined by the third variable; here, the average number of rides. The more rides there are, the brighter the color appears.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ridesPerWeekdayUTC&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;hourUTC &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;as.factor&lt;/span&gt;(ridesPerWeekdayUTC&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;hourUTC)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;taxi_plot &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;ggplot&lt;/span&gt;(data &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; ridesPerWeekdayUTC, &lt;span style=&#34;color:#a6e22e&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; dayUTC, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; hourUTC))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;taxi_plot &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;geom_tile&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;aes&lt;/span&gt;(fill&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;V1)) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;scale_x_discrete&lt;/span&gt;(limits&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Monday&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Tuesday&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Wednesday&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Thursday&amp;#34;&lt;/span&gt;, &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Friday&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Saturday&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Sunday&amp;#34;&lt;/span&gt;)) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;scale_y_discrete&lt;/span&gt;(limits &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rev&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;levels&lt;/span&gt;(ridesPerWeekdayUTC&lt;span style=&#34;color:#f92672&#34;&gt;$&lt;/span&gt;hourUTC))) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;scale_fill_gradient&lt;/span&gt;(low&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;R #090A1C&amp;#34;&lt;/span&gt;, high&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;#1FBAD6&amp;#34;&lt;/span&gt;, guide&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;FALSE&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;ggtitle&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;New York&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;theme&lt;/span&gt;(axis.ticks &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_blank&lt;/span&gt;(), &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;panel.background &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_rect&lt;/span&gt;(fill&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;#09091A&amp;#34;&lt;/span&gt;, color&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;#09091A&amp;#34;&lt;/span&gt;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;panel.grid.major &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_blank&lt;/span&gt;(),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;panel.grid.minor &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_blank&lt;/span&gt;(),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;axis.text.x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_text&lt;/span&gt;(face&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;bold&amp;#34;&lt;/span&gt;, size&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;, color&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;#C0C0C8&amp;#34;&lt;/span&gt;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;axis.text.y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_text&lt;/span&gt;(face&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;bold&amp;#34;&lt;/span&gt;, size&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;, color&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;#C0C0C8&amp;#34;&lt;/span&gt;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;plot.title &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_text&lt;/span&gt;(face&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;bold&amp;#34;&lt;/span&gt;, size&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;40&lt;/span&gt;, color&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;#C0C0C8&amp;#34;&lt;/span&gt;, vjust&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1.1&lt;/span&gt;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;axis.title.x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_blank&lt;/span&gt;(),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;axis.title.y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_blank&lt;/span&gt;(),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;legend.title&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;element_blank&lt;/span&gt;(),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;plot.background &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_rect&lt;/span&gt;(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;  fill &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;#09091A&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;  colour &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;#09091A&amp;#34;&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The remaining options I use in combination with &lt;code&gt;ggplot()&lt;/code&gt; are simply needed to adjust the appearance of the plot to that of Uber&amp;rsquo;s template. Of course, I do not have Uber&amp;rsquo;s signature font. But there also isn&amp;rsquo;t an option in the &lt;code&gt;ggplot2&lt;/code&gt; package that allows me to move the x-axis labels to the top of the plot.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2015/09/21/taxi-pulse-of-new-york-city/taxi-pulse-ny.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Looking at the result, the similiarity to Uber&amp;rsquo;s plot is obvious. Even though I use data on taxis for the first half of 2015, I am able to recreate the pattern that Uber has observed for New York with their 2014 data. Between 1am and 5am on Monday through Friday, the color is mostly black as even in New York most people are asleep. But there is a shift of this pattern on the weekend such that the number of taxis between 1am and 4am is noticeably higher. At the same time, people use the taxi regularly in the late evening except for on Sundays where they may prefer to stay at home. The same pattern is visible in Uber&amp;rsquo;s graphic for New York. Striking in my version might be the dark color for 4pm which is in contrast to 3pm and 5pm. This difference does not exist in Uber&amp;rsquo;s version.&lt;/p&gt;&#xA;&lt;p&gt;Alas, I cannot create plots for different cities as Uber&amp;rsquo;s engineering team did. But I&amp;rsquo;m sure this hasn&amp;rsquo;t been the last time that I work with a taxi data set.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Analyzing Taxi Data to Create a Map of New York City</title>
	  <link>https://minimizeregret.com/post/2015/08/26/analyzing-taxi-data-to-create-a-map-of-new-york-city/</link>
	  <pubDate>Wed, 26 Aug 2015 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/post/2015/08/26/analyzing-taxi-data-to-create-a-map-of-new-york-city/</guid>
	  <description>&lt;p&gt;Yet another day was spent working on the &lt;a href=&#34;http://www.nyc.gov/html/tlc/html/about/trip_record_data.shtml&#34;&gt;taxi data provided by the NYC Taxi and Limousine Commission&lt;/a&gt; (TLC).&lt;/p&gt;&#xA;&lt;p&gt;My goal in working with the data was to create a plot that maps the streets of New York using the geolocation data that is provided for the taxis&amp;rsquo; pickup and dropoff locations as longitude and latitude values. So far, I had only used the dataset for January of 2015 to plot the locations; also, I hadn&amp;rsquo;t used the more than 12 million observations in January alone but a smaller sample (100000 to 500000 observations). The problem then however was that the sample included not enough observations for most streets outside of Manhattan Island such that their structure did not become visible. At the same time, the overplotting on Manhattan Island made it impossible to observe any structure for that part of the city.&lt;/p&gt;&#xA;&lt;p&gt;In order to change this, I had to move away from the &lt;code&gt;csv&lt;/code&gt; file I was using so far. To get better results, I needed to include &lt;em&gt;every&lt;/em&gt; observation in my analysis. Thereby, I were able to filter for every unique location registered during 2015. I would then plot every unique location once, instead of plotting every observation. This means, that I were able to include the rare observations for the outer parts of New York in the plot without having too many observations that would make plotting infeasible.&lt;/p&gt;&#xA;&lt;p&gt;Exactly this is the approach that &lt;a href=&#34;http://www.danielforsyth.me/mapping-nyc-taxi-data/&#34;&gt;Daniel Forsyth took for his plot&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;However, sticking with the monthly &lt;code&gt;csv&lt;/code&gt; files, each 2GB in size, was infeasible. It wouldn&amp;rsquo;t be effective to import them all, to sort through them, and to extract the unique observations. Instead, I followed Forsyth&amp;rsquo;s lead and utilized the fact that the TLC provides their data through Google BigQuery as well. Yet, while he had used Python to access and plot the data, I wanted to use R.&lt;/p&gt;&#xA;&lt;p&gt;To be clear, neither had I used &lt;a href=&#34;https://cloud.google.com/bigquery/&#34;&gt;Google BigQuery&lt;/a&gt; before, nor had I accessed it through R&amp;rsquo;s interface by using the API. Unsurprisingly though, I do have a Google account, so it was a breeze to log into Google BigQuery and to create my first project. Having a project in BigQuery is necessary to later access the data through R with the project ID as identifier. If you&amp;rsquo;re like me and using BigQuery for the first time, you might want to check whether the corresponding BigQuery API is turned on. To do so, click &amp;ldquo;API&amp;rsquo;s &amp;amp; Auth&amp;rdquo; on the left of your Google Developers Console, choose &amp;ldquo;BigQuery API&amp;rdquo; and click the button to switch it from &amp;ldquo;OFF&amp;rdquo; to &amp;ldquo;ON&amp;rdquo;.&lt;/p&gt;&#xA;&lt;p&gt;Afterwards, you&amp;rsquo;re set to continue in R. The currently easiest way to access BigQuery seems to be the package &lt;code&gt;bigrquery&lt;/code&gt; to be installed with &lt;code&gt;install.packages(&amp;quot;bigrquery&amp;quot;)&lt;/code&gt;. Since you already have a project with project ID, the next step is as easy as the example given in the &lt;a href=&#34;https://github.com/hadley/bigrquery&#34;&gt;package&amp;rsquo;s documentation&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;library&lt;/span&gt;(bigrquery)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;project &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;fantastic-voyage-389&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;# put your project ID here&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;sql &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;SELECT year, month, day, weight_pounds FROM &#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#x9;&#x9;[publicdata:samples.natality] LIMIT 5&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;query_exec&lt;/span&gt;(sql, project &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; project)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As you can see, one uses SQL queries to access data on BigQuery. You could also try to use the query stored in &lt;code&gt;sql&lt;/code&gt; above directly through &lt;a href=&#34;https://bigquery.cloud.google.com&#34;&gt;Google BigQuery and it&amp;rsquo;s &amp;ldquo;Compose Query&amp;rdquo;&lt;/a&gt; window. This provides an easy way to check your SQL query before using it in R.&lt;/p&gt;&#xA;&lt;p&gt;In order to access not data on body weights but the taxi locations, you can leave the first two lines  of code unchanged. Though you have to change the query itself, of course. Consequently, I was glad that &lt;a href=&#34;http://www.timradtke.net/blog/r-code-diary-15-are-mlb-teams-worth-their-money&#34;&gt;I&amp;rsquo;m not unfamiliar with SQL&lt;/a&gt;. On the other hand, Forsyth had used an SQL query, too, to access the data; for my purposes, I didn&amp;rsquo;t have to change his query at all.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;taxiQuery &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;SELECT ROUND(pickup_latitude, 4) as lat,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#x9;&#x9;&#x9;&#x9;ROUND(pickup_longitude, 4) as long, COUNT(*) as&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#x9;&#x9;&#x9;&#x9;num_pickups FROM [nyc-tlc:yellow.trips_2015] WHERE&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#x9;&#x9;&#x9;&#x9;(pickup_latitude BETWEEN 40.61 AND 40.91) AND&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#x9;&#x9;&#x9;&#x9;(pickup_longitude BETWEEN -74.06 AND -73.77 ) GROUP&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#x9;&#x9;&#x9;&#x9;BY lat, long&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;taxi_df &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;query_exec&lt;/span&gt;(taxiQuery, project &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; project, max_pages &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Inf&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As I&amp;rsquo;m not only requesting 5 values of body weights but 740879 locations, I had to set the &lt;code&gt;max_pages&lt;/code&gt; argument in &lt;code&gt;query_exec&lt;/code&gt; to &lt;code&gt;Inf&lt;/code&gt; so that the page limit of 10 with 10000 rows each (100000 observations overall) is lifted.&lt;/p&gt;&#xA;&lt;p&gt;The query above rounds both latitude and longitude to four decimal PLACES and stores how often each combination appears in &lt;code&gt;num_pickups&lt;/code&gt;. In order to zoom into downtown New York, the range of both latitude and longitude is limited to certain values which will be represented later in the plot. Due to the argument &lt;code&gt;GROUP BY lat, long&lt;/code&gt; not all observations are stored in the data frame &lt;code&gt;taxi_df&lt;/code&gt; but each pickup location only once. Instead of assigning a row to each of the millions and millions of taxi rides, the necessary information can be stored in less than 750000 rows. Also, if I do decide to incorporate the density of rides into my plot, I can use &lt;code&gt;num_pickups&lt;/code&gt; to do so.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;dim&lt;/span&gt;(taxi_df)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[1] &lt;span style=&#34;color:#ae81ff&#34;&gt;740879&lt;/span&gt;      &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The final data frame contains three variables with 740879 observations each.&lt;/p&gt;&#xA;&lt;p&gt;To create the map after the example of &lt;a href=&#34;http://i.imgur.com/ov6K6mt.jpg&#34;&gt;eck&lt;/a&gt; and &lt;a href=&#34;http://www.danielforsyth.me/mapping-nyc-taxi-data/&#34;&gt;Forsyth&lt;/a&gt;, I eventually chose to use the &lt;code&gt;ggplot2&lt;/code&gt; package, as it allows me to adjust certain settings as for example background color and transparency levels more easily or depending on &lt;code&gt;num_pickups&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Some experimenting later, I ended up with the following code to plot the pickup locations.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-r&#34; data-lang=&#34;r&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;require&lt;/span&gt;(ggplot2)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;taxi_plot &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;ggplot&lt;/span&gt;(data&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;taxi_df, &lt;span style=&#34;color:#a6e22e&#34;&gt;aes&lt;/span&gt;(x &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; long, y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; lat))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;taxi_plot &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;geom_point&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;aes&lt;/span&gt;(alpha &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;as.factor&lt;/span&gt;(num_pickups)),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;&#x9;&#x9;&#x9;   size&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;0.4&lt;/span&gt;, color&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;white&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;scale_alpha_discrete&lt;/span&gt;(range &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;0.1&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;), guide &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;FALSE&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;theme&lt;/span&gt;(panel.background &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_rect&lt;/span&gt;(fill&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;, color&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;  panel.grid.major &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_blank&lt;/span&gt;(), panel.grid.minor &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_blank&lt;/span&gt;(),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;  axis.text &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_blank&lt;/span&gt;(), axis.line &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_blank&lt;/span&gt;(),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;  axis.title &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_blank&lt;/span&gt;(), axis.ticks &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_blank&lt;/span&gt;(),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;  panel.border &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_blank&lt;/span&gt;(),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;  plot.background &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;element_rect&lt;/span&gt;(fill&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;, color&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;black&amp;#34;&lt;/span&gt;))&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I created the first layer of the plot using the longitude and latitude; after all, I would like to create a map. Since the map covers exclusively New York, the distortion due to cartesian coordinates should be minimal. As a second layer I added the scatter plot layer of pickup locations. The size of 0.4 is small enough to preserve a clear representation of streets. Furthermore, the transparency of points that are added to the plot is adjusted by the &lt;code&gt;alpha&lt;/code&gt; argument. Here, I did not set a fixed transparency for all locations. Instead, the transparency of each coordinate pair depends on how often a taxi picked up a customer on that location. Consequently, the airports and the streets of Manhattan ended up glowing. If it weren&amp;rsquo;t for the different &lt;code&gt;alpha&lt;/code&gt; values, one wouldn&amp;rsquo;t be able to recognize any street in Manhattan as there were too many observations.&lt;/p&gt;&#xA;&lt;p&gt;Lastly, I adjusted the theme of the plot such that there are neither axes nor legends in the final plot. Also, I set the background color to black.&lt;/p&gt;&#xA;&lt;p&gt;I&amp;rsquo;m pleased with the resulting image of New York.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;https://minimizeregret.com/post/2015/08/26/analyzing-taxi-data-to-create-a-map-of-new-york-city/taxi-map-ny.png&#34;&gt;&lt;/figure&gt;&lt;p&gt;Different from my &lt;a href=&#34;http://www.timradtke.net/blog/r-code-diary-16-nyc-taxi-trip-record-data&#34;&gt;first&lt;/a&gt; &lt;a href=&#34;http://www.timradtke.net/blog/r-code-diary-17&#34;&gt;tries&lt;/a&gt; is that not only Manhattan is recognizable, but also the streets in other boroughs, especially Queens and Brooklyn. Likewise, Manhattan is no longer a large glowing spot: the famous grid structure has become visible. I therefore managed to address the most critical features of the map.&lt;/p&gt;&#xA;&lt;p&gt;In the process, I realized that there is at least one big difference between &lt;a href=&#34;http://i.imgur.com/ov6K6mt.jpg&#34;&gt;eck&amp;rsquo;s image&lt;/a&gt; (and mine), and &lt;a href=&#34;http://www.danielforsyth.me/mapping-nyc-taxi-data/&#34;&gt;Forsyth&amp;rsquo;s&lt;/a&gt;. In eck&amp;rsquo;s and my case, a brigther white color represents points that were visited frequently by taxi drivers. That&amp;rsquo;s why on eck&amp;rsquo;s map, the streets in Manhattan as well as the airports are glowing. The airports in the case of Forsyth are glowing, too. But the streets in Manhattan are black with everything around them white. I&amp;rsquo;m wondering how he managed to plot them in black color and why he chose to do so. The effect looks nice.&lt;/p&gt;&#xA;&lt;p&gt;As an extension one could use the additionally provided dropoff dates and times to create an animated version of the plot. This would require one to use every observation, though, instead of only unique locations as this adds a third dimension to the plot.&lt;/p&gt;&#xA;&lt;p&gt;At this point, I have to thank to Hadley Wickham. Not only did he work on the &lt;code&gt;ggmap&lt;/code&gt; package that &lt;a href=&#34;http://www.timradtke.net/blog/r-code-diary-17&#34;&gt;I used last time&lt;/a&gt;. He extended the grammar of graphics to the &lt;a href=&#34;http://vita.had.co.nz/papers/layered-grammar.pdf&#34;&gt;layered grammar of graphics&lt;/a&gt; and is the author of the &lt;a href=&#34;http://ggplot2.org&#34;&gt;&lt;code&gt;ggplot2&lt;/code&gt; package&lt;/a&gt;. He has also written &lt;a href=&#34;http://amzn.com/0387981403?tag=ggplot2-20&#34;&gt;a book&lt;/a&gt; to accompany the package. He is the &lt;a href=&#34;https://en.wikipedia.org/wiki/Hadley_Wickham&#34;&gt;Chief Scientist at RStudio&lt;/a&gt;, which I use to write my code and to create my plots. Finally, Hadley Wickham is also the author of the &lt;a href=&#34;https://github.com/hadley/bigrquery&#34;&gt;&lt;code&gt;bigrquery&lt;/code&gt; package&lt;/a&gt;. I&amp;rsquo;m standing on the shoulders of giants, and today Hadley Wickham has been one of them.&lt;/p&gt;&#xA;</description>
	</item>
	<item>
	  <title>Shuffler</title>
	  <link>https://minimizeregret.com/shuffler/</link>
	  <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><author>Tim Radtke</author>
	  <guid>https://minimizeregret.com/shuffler/</guid>
	  <description>&lt;p&gt;Turn quantiles of marginal distribution forecasts returned by any model into sample paths from a joint forecast distribution using &lt;a href=&#34;https://github.com/timradtke/shuffler&#34;&gt;Shuffler&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;A limitation of most probabilistic time series (foundation) models is that they return forecasts in the form of quantiles of marginal distributions, only. While this suffices to answer some questions, many use cases require a joint distribution over the forecast horizon to answer more complex questions, such as&amp;hellip;&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&amp;ldquo;What is the probability that half of the product&amp;rsquo;s inventory will be consumed over the next 12 days?&amp;rdquo;&lt;/li&gt;&#xA;&lt;li&gt;&amp;ldquo;What is the probability that electricity prices will be higher than 200€/MWh for a period of six hours in the next 24 hours?&amp;rdquo;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Shuffler implements the copula-based approach by &lt;a href=&#34;https://minimizeregret.com/papers/baron-2025-efficiently-generating-correlated-sample-paths-from-multi-step-time-series-foundation-models&#34;&gt;Baron et al. (2025)&lt;/a&gt;, which injects an auto-correlation structure estimated from past observations into samples from the marginal distribution forecasted by any model, resulting in sample paths from a joint distribution forecast. The sample paths can then be aggregated to answer arbitrary questions.&lt;/p&gt;&#xA;&lt;h3 id=&#34;install-shuffler&#34;&gt;Install Shuffler&lt;/h3&gt;&#xA;&lt;p&gt;Install Shuffler from Github. Use either &lt;code&gt;pip&lt;/code&gt;&amp;hellip;&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pip install &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;git+https://github.com/timradtke/shuffler&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&amp;hellip; or use &lt;a href=&#34;https://docs.astral.sh/uv/&#34;&gt;&lt;code&gt;uv&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;uv pip install &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;git+https://github.com/timradtke/shuffler&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;get-started&#34;&gt;Get Started&lt;/h3&gt;&#xA;&lt;p&gt;Shuffler postprocesses quantile forecasts into sample paths. Therefore, you first forecast quantiles with some model and then you draw sample paths using Shuffler given the quantile forecasts. For the example here we&amp;rsquo;ll use Chronos-2, but you can use any model that returns its forecasts in the form of marginal quantiles.&lt;/p&gt;&#xA;&lt;p&gt;Start by importing some helper libraries and defining the quantile levels to forecast. Use as many as make sense given your model. Chronos-2 is trained to forecast 21 quantiles (see section 3.2 in &lt;a href=&#34;https://minimizeregret.com/papers/ansari-2025-chronos-2-from-univariate-to-universal-forecasting&#34;&gt;Chronos-2: From Univariate to Universal Forecasting&lt;/a&gt;), so use all of them.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; numpy &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; np&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; shuffler &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; sf&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# The following aren&amp;#39;t dependencies of shuffler:&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; pandas &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; pd&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; matplotlib.pyplot &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; plt&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; chronos &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; Chronos2Pipeline&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then reproduce the example from &lt;a href=&#34;https://github.com/amazon-science/chronos-forecasting/blob/main/README.md&#34;&gt;Chronos&amp;rsquo;s documentation&lt;/a&gt;, forecasting the next 24 hours of German electricity prices using some covariates.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;horizon &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;24&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;quantile_levels &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#ae81ff&#34;&gt;0.01&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.05&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.15&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.2&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.25&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.3&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.35&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.4&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.45&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#ae81ff&#34;&gt;0.5&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#ae81ff&#34;&gt;0.55&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.6&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.65&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.7&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.75&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.8&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.85&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.9&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.95&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0.99&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;context_df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pd&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;read_parquet(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;train.parquet&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;test_df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pd&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;read_parquet(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;test.parquet&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;future_df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; test_df&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;drop(columns&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;target&amp;#34;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pipeline &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; Chronos2Pipeline&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;from_pretrained(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;amazon/chronos-2&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;device_map&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;cpu&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pred_df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pipeline&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;predict_df(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;context_df,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;future_df&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;future_df,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;prediction_length&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;horizon,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;quantile_levels&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;quantile_levels,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;id_column&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;id&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;timestamp_column&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;timestamp&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;target&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;target&amp;#34;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Applying Shuffler is model-agnostic. Take the quantile forecasts (here we need to separate them from the other information in &lt;code&gt;pred_df&lt;/code&gt;) and pass them to Shuffler&amp;rsquo;s workhorse function, &lt;code&gt;draw_sample_paths()&lt;/code&gt;. Given past observations of the target time series, it fits a copula to that turns the marginal distributions specified by the provided quantile forecasts into a joint distribution over the forecast horizon. The returned object is a numpy array of sample paths drawn from the joint distribution, of shape (n_samples, horizon).&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Extract the quantile forecasts from `pred_df`, then&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# reshape them to a numpy array with shape&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# (len(quantile_levels), horizon).&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;quantile_column_names &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [str(x) &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; x &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; quantile_levels]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;quantile_forecasts &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; \&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;pred_df[quantile_column_names]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;to_numpy()&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;transpose()&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Fit a copula to learn a joint forecast distribution.&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;sample_paths &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; sf&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;draw_sample_paths(&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;quantile_forecasts&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;quantile_forecasts,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#75715e&#34;&gt;# Same as for Chronos:&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;quantile_forecast_levels&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;np&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;array(quantile_levels),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#75715e&#34;&gt;# Provide past observations:&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;y&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;context_df[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;target&amp;#34;&lt;/span&gt;]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;to_numpy(),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;horizon&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;horizon,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;size&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;n_samples,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;seed&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;128&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Finally, visualize the first three sample paths along with the observed data. Note the sample paths&amp;rsquo; smooth behavior, reminiscent of the observed data. Shuffler&amp;rsquo;s copula injects auto-correlation structure into the sample paths to ensure a realistic joint distribution even though we started from only marginal quantile forecasts.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;&#x9;&lt;img src=&#34;./sample_paths.png&#34; alt=&#34;A line chart visualizing the observed time series along with three sample path forecasts.&#34;&gt;&lt;/figure&gt;&lt;p&gt;Refer to &lt;a href=&#34;https://github.com/timradtke/shuffler/blob/main/docs/draw_sample_paths_using_chronos.ipynb&#34;&gt;the extended tutorial notebook&lt;/a&gt; for more details.&lt;/p&gt;&#xA;&lt;h3 id=&#34;references&#34;&gt;References&lt;/h3&gt;&#xA;&lt;p&gt;Shuffler is an implementation of the copula-based approach to transform marginal quantile forecasts into sample paths described in &lt;a href=&#34;https://minimizeregret.com/papers/baron-2025-efficiently-generating-correlated-sample-paths-from-multi-step-time-series-foundation-models&#34;&gt;Efficiently Generating Correlated Sample Paths from Multi-step Time Series Foundation Models&lt;/a&gt; (Baron et al., 2025).&lt;/p&gt;&#xA;&lt;p&gt;The approach relies on interpolating and extrapolating arbitrary quantiles of a given marginal distribution given only a limited number of its quantiles (in the above example, only 21 quantiles of the marginal distribution were known, not the full marginal distribution). Shuffler (just like Baron et al. (2025)) follows the inter/extrapolation approach described in section 3.2 of &lt;a href=&#34;https://minimizeregret.com/papers/park-2021-learning-quantile-functions-without-quantile-crossing-for-distribution-free-time-series-forecasting&#34;&gt;Learning Quantile Functions without Quantile Crossing for Distribution-free Time Series Forecasting&lt;/a&gt; (Park et al., 2021).&lt;/p&gt;&#xA;</description>
	</item>
  </channel>
</rss>