<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ruben van de Geer on Medium]]></title>
        <description><![CDATA[Stories by Ruben van de Geer on Medium]]></description>
        <link>https://medium.com/@rubenvandegeer?source=rss-816f756f83a3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BRsmyArNTGjdWvL_Lufm1A.jpeg</url>
            <title>Stories by Ruben van de Geer on Medium</title>
            <link>https://medium.com/@rubenvandegeer?source=rss-816f756f83a3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 01:06:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rubenvandegeer/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A New Method for Multi-Horizon Forecasting with Tree-based Models]]></title>
            <link>https://blog.ah.technology/a-new-method-for-multi-horizon-forecasting-with-tree-based-models-7276ae5f9636?source=rss-816f756f83a3------2</link>
            <guid isPermaLink="false">https://medium.com/p/7276ae5f9636</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[albert-heijn]]></category>
            <category><![CDATA[forecasting]]></category>
            <category><![CDATA[timeseries]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Ruben van de Geer]]></dc:creator>
            <pubDate>Mon, 11 Mar 2024 08:36:27 GMT</pubDate>
            <atom:updated>2024-04-08T08:13:01.271Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VCHv5eEgoC4W10g88u7hIA.jpeg" /><figcaption>Albert Heijn Distribution Centre</figcaption></figure><p>Recently, gradient-boosted decision trees (GBDT) dominated the fifth iteration of the world’s most <a href="https://en.wikipedia.org/wiki/Makridakis_Competitions">prestigious forecasting competition</a>, where the winners of both tracks (accuracy and uncertainty estimation) relied on GBDTs. The competition—designated M5—was hosted on Kaggle and revolved around a dataset with sales of roughly 42,000 hierarchical timeseries provided by Walmart. It’s therefore not surprising that, at Albert Heijn, GBDTs are our model of choice too, helping us to forecast 20,000,000 hierarchical timeseries of daily demand for our products.</p><p>Just like in the M5 competition, at Albert Heijn we forecast demand for different horizons, namely from one to fifty days ahead. In general, short-term and long-term forecasts are used to replenish, respectively, our stores and distribution centers with enough stock. One of the main challenges with using GBDTs for forecasting, however, is that they cannot <em>do</em> multi-horizon forecasting out of the box. To circumvent this, generally two methods exist to leverage GBDTs for multi-horizon forecasting: the direct and the recursive method.</p><h3>🤼 The challenges with the direct and the recursive method</h3><p>The <strong>recursive</strong> method works by making a forecast for tomorrow, then using this forecast as a feature to forecast for the day after tomorrow, etcetera. The <strong>direct</strong> method works by training a separate model for each horizon. It’s beyond the scope of this blog post to provide a detailed description of <em>how</em> these methods work exactly — there’s plenty of high-quality material out there. However, to appreciate our contribution, it <em>is</em> necessary to understand what the main drawbacks of both methods are.</p><p>The most prominent problem with the recursive method is its lack of robustness. By its nature, forecasting errors in short horizons propagate through higher horizons, thereby compounding from horizon to horizon and leading to poor forecasts for longer horizons. Also, we found that due to the recursive nature, it’s non-trivial to cross-validate the recursive method on a large scale.</p><p>Because of aforementioned reasons, at Albert Heijn we use the direct method. Although, in terms of performance, the direct method has served us well, clear downsides are the maintainability and the cost. After all, we require different models for different horizons and all these models take up valuable resources to train, monitor and maintain — it’s a burden really. In addition, as time progresses models for different horizons make forecasts for the same date and it may happen that “newer” forecasts deviate substantially from previous forecasts.</p><p>Therefore, to minimize maintenance and cost — yes, we are frugal and we are proud of it — we have been experimenting with a new single-model approach for multi-horizon forecasting with GBDTs. <strong>“Our” method is neither designed nor expected to achieve superior performance over the direct method</strong>, but it does offer all the (many) benefits of using a single model in production for all horizons.</p><h3>✨ The “new” method</h3><p>I’d like to start with a disclaimer: I’m not sure if this method, which I refer to as <em>macking </em>(a portmanteau of stacking and masking datasets), is actually new. I’ve done my fair share of research and pitched this idea to people who <em>know </em>forecasting, and I’ve concluded that macking is new. If this is not the case, please comment below and I will rectify accordingly.</p><p>Macking is inspired by the notion that GBDTs are very capable of handling missing values. Leveraging this observation, the idea is to treat features that are not available for the horizon at hand simply as missing. For example, if we forecast for the day after tomorrow, we simply treat the feature “yesterday’s sales” as missing (because “yesterday’s sales” refers to tomorrow’s sales, which haven’t been observed yet).</p><p>With macking, the training data is constructed to make sure the model can learn from different subsets of available features. We assume here that the data contains a record for each date and for each entity (e.g. item), for example:</p><pre>date       | item_id | store_id | sales<br>----------------------------------------<br>2024-01-01 |  1111   |   6789   |   5<br>2024-01-01 |  2222   |   6789   |   8<br>...</pre><p>Then, macking works by taking the training data for each horizon (as if you were using the Direct Method) and stacking these vertically — or, in SQL lingo, taking the “union all” of those datasets. The training data for each horizon is essentially the same, only features are masked that are not available for the horizon at hand.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*9V0ufpM2JSc_Mv6ulujwJA.png" /><figcaption>Figure 1: Graphical illustration of macking</figcaption></figure><p>Observe that each record in the original training dataset now occurs three times. For a discussion on how to manage memory consumption, please see the final section below.</p><p>To clarify the figure above, take for example the one-day lag of the target. This feature is available when forecasting horizon=1 (assuming that you forecast end of day), but not for horizon=2, because for the day after tomorrow, the one-day lag is tomorrow’s sales, which are not yet observed. So, for horizon=1 all the features are available, but not for horizon=2. Hence, when macking these, missing values will materialize in the rows that pertain to horizon=2 (and higher).</p><p>Needless to say, when making predictions, we simply set all the features that are unavailable for the corresponding horizon to null.</p><h3>🎛️ Balancing the data</h3><p>From experiments, we learned that the setup in Figure 1 favors longer horizons. This is because the “long” lags (e.g. “Feature n” in Figure 1) are never masked, while the “short” lags (e.g. “Feature 1” in Figure 1) are masked relatively frequently. Because of this, the model attributes too much value to the longer lags, resulting in inferior performance for the lower horizons (for which the shorter lags are particularly informative). To circumvent this, we only masked one-third of the records in the stacked dataset (and leaving the rest of the records unmasked).</p><h3>🔬 Empirical evaluation on the M5 data: Macking vs the Direct Method</h3><p>To validate macking, we rely on the data from the <a href="https://www.kaggle.com/competitions/m5-forecasting-accuracy">aforementioned M5 competition</a> and use the hyperparameters for LightGBM from the <a href="https://www.kaggle.com/competitions/m5-forecasting-accuracy/discussion/163684">winner’s submission</a>, which was based on <a href="https://www.kaggle.com/code/kyakovlev/m5-three-shades-of-dark-darker-magic/notebook">this notebook</a>. In the evaluation, we consider the horizons 1, 7, and 14. This means we compare one model that uses macking against three separate models (one for horizon 1, 7, and 14, respectively).</p><p>We use data of the years 2012 and 2013 and make forecasts every day from January 1, 2013 onwards and retrain the models every 60 days. So, on January 1, 2013 we make three forecasts (horizon 1, 7 and, 14), then on January 2, 2013 again, etc., etc.</p><p>The mean absolute error (MAE) across the different horizons is very similar for both methods: for horizon 1 it’s basically a tie, for horizon 7 the direct method is slightly better, and for horizon 14 macking is slightly better. Overall, based on the MAE there is no significant different in performance across the two methods.</p><p>This is a great result. It means we can get similar performance with fewer models (hence less maintenance, fewer cost) 🥳!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*ch1-Lilre8Fo886XispAhw.png" /><figcaption>Figure 2: Performance comparison between Macking and the Direct Method.</figcaption></figure><p>The code to reproduce this evaluation can be found on GitHub <a href="https://github.com/rubenvdg/blog-post-macking">here</a>. I took the opportunity to learn <a href="https://pola.rs/">Polars</a> for the feature engineering, which was an utter joy (and super performant).</p><h3>Thoughts on memory consumption</h3><p>Because we stack datasets for the different horizons, memory consumption is probably of immediate concern to some readers. After all, if you forecast, for example, up to 50 days in advance, this would imply that the required memory increases by a factor of 50. The following three methods can be used to mitigate this:</p><ul><li><strong>Clustering of horizons</strong>. At Albert Heijn, we forecast up to 50 days, but we do not use 50 separate models when using the Direct Method for this (for obvious reasons). Instead we typically cluster horizons 2 to 5 and 6 to 50, resulting in <strong>three </strong>models (horizon 1, horizon 2–5, and horizon 6–50). Note that, for example for the 6–50 model, we only use features that are available for horizon 50 (so for horizon 6, you won’t use all the data theoretically available).</li><li><strong>Downsampling</strong>. Because the data is at a very granular level, it often doesn’t hurt to downsample aggressively. We found that downsampling with 50% often doesn’t hurt forecasting performance at all — especially when downsampling outside of periods with events and holidays.</li><li><strong>Batch training</strong>. The lightgbm.Dataset class allows you to pass a list of input files, which will then be read iteratively thereby mitigating excessive memory usage. Thanks to Rogier van der Geer from Xebia Data for pointing this out!</li></ul><h3>Relevant material that might be of interest</h3><ul><li><a href="https://www.sciencedirect.com/science/article/pii/S0169207021001679">Scientific paper</a> on why trees work so well for timeseries forecasting.</li><li><a href="https://www.kaggle.com/competitions/m5-forecasting-accuracy">The M5 competition on Kaggle</a>.</li><li><a href="https://www.kaggle.com/competitions/m5-forecasting-accuracy/discussion/163684">Description of the winner’s solution</a>.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7276ae5f9636" width="1" height="1" alt=""><hr><p><a href="https://blog.ah.technology/a-new-method-for-multi-horizon-forecasting-with-tree-based-models-7276ae5f9636">A New Method for Multi-Horizon Forecasting with Tree-based Models</a> was originally published in <a href="https://blog.ah.technology/">AH Technology</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Monitoring and Optimizing Databricks Job Cluster Utilization]]></title>
            <link>https://blog.ah.technology/monitoring-and-optimizing-databricks-job-cluster-utilization-9a55e36e889e?source=rss-816f756f83a3------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a55e36e889e</guid>
            <category><![CDATA[databricks]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[retail-technology]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[spark]]></category>
            <dc:creator><![CDATA[Ruben van de Geer]]></dc:creator>
            <pubDate>Tue, 22 Aug 2023 10:58:39 GMT</pubDate>
            <atom:updated>2023-11-13T20:56:04.509Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XIw6iPDGoO6qSci4U0cToA.png" /></figure><p><em>⚠️ This blog post is only applicable to Databricks runtime &lt; 13.0, because </em><a href="https://docs.databricks.com/en/release-notes/runtime/13.0.html#ganglia-replaced-with-cluster-metrics"><em>Databricks is deprecating Ganglia</em></a><em>.</em></p><p>At Albert Heijn, we make over a billion forecasts per day to make sure we replenish our stores with enough — but not too much — stock. To make all these forecasts, we run thousands of Databricks jobs with ETL and ML workloads every day. Needless to say, to minimize both cost and our CO2 footprint, it is crucial to get the sizing of our Spark job clusters right. This blog post is about how we managed to do so at scale.</p><h3>Monitoring job cluster utilization</h3><p>It all starts with observability. The Spark UI natively offers a graph that depicts the cluster memory usage over time:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*yjcYXFv3YdaXtJH30aLHNw.png" /></figure><p>Our goal was, however, to monitor and optimize job cluster utilization systematically — not through visual inspection. Therefore, we were looking to obtain the raw data used to make the above figure. It turns out that you can query this data from the Ganglia API on the Spark driver of your cluster:</p><pre>import requests<br>metric = &quot;mem_report&quot;<br>payload = requests.get(f&quot;http://localhost/graph.php?c=cluster&amp;json=1&amp;r=4hr&amp;g={metric}&quot;)<br>mem_report = payload.json()</pre><p>From the memory report, you can easily distill the fields bmem_used and bmem_total to compute the memory allocated to and used by a specific job. We do this at the very end of each job. The appealing thing is that this captures the memory usage on a system level — including <strong>both</strong> PySpark workloads that run in the JVM and Python workloads that run outside of it.</p><h3>The right resources for each job</h3><p>It’s important to mention that our ML jobs recur on a frequent basis (mostly daily) and run on single-node clusters. This means that, for example, job XYZ on Monday, is very similar — in terms of memory requirements — to job XYZ running on Tuesday. To leverage this, we’ve used the following setup for our ML jobs.</p><p>We retrieve and store the single-node cluster memory consumption as follows — making it possible to optimize the cluster size in case the job runs again:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/1*w9s0xzhi5bpFdODSlSybww.png" /></figure><p>Then, in case an ML job runs again, it retrieves information (if available) and sizes the cluster right:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*Z3WgACNt_u5XaGO7bqoxfA.png" /></figure><p>As such, this is a data-driven and zero-ops way of provisioning your workloads appropriately.</p><p>We’re happy to hear your thoughts, questions or feedback for improvements below in the comments!</p><h3>Things that didn’t work</h3><ul><li>With <a href="https://spark.apache.org/docs/latest/monitoring.html"><strong>Spark’s built-in metrics</strong></a><strong> </strong>we were not able to observe memory usage outside the JVM (e.g. when training ML models in Python).</li><li>With Python<strong> </strong><a href="https://pypi.org/project/memory-profiler/"><strong>memory profilers</strong></a><strong> </strong>we were note able to observe memory usage inside the JVM (when running PySpark code).</li><li>By installing <a href="https://learn.microsoft.com/en-us/azure/azure-monitor/agents/log-analytics-agent"><strong>Azure Log Analytics Agent</strong></a> on the driver of each cluster, we were able to monitor the memory usage, but we were unable to match these metrics to specific Databricks jobs.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a55e36e889e" width="1" height="1" alt=""><hr><p><a href="https://blog.ah.technology/monitoring-and-optimizing-databricks-job-cluster-utilization-9a55e36e889e">Monitoring and Optimizing Databricks Job Cluster Utilization</a> was originally published in <a href="https://blog.ah.technology/">AH Technology</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>