{ "cells": [ { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "# Weighted Reconciliation with Spark\n", "\n", "## Feng Li\n", "\n", "### Guanghua School of Management\n", "### Peking University\n", "\n", "\n", "### [feng.li@gsm.pku.edu.cn](feng.li@gsm.pku.edu.cn)\n", "### Course home page: [https://feng.li/bdcf](https://feng.li/bdcf)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## MinT-WLS\n", "\n", "\n", "- MinT-WLS assumes a **diagonal forecast error covariance matrix** $ W $, where each diagonal element is the **variance of forecast errors** for each series (e.g., Region_Category). The formula becomes:\n", "\n", "$$\n", "\\tilde{y} = S (S^\\top W^{-1} S)^{-1} S^\\top W^{-1} \\hat{y}\n", "$$\n", "\n", "- $ \\hat{y} $: base forecasts (from ETS, etc.)\n", "- $ S $: summing matrix\n", "- $ W $: diagonal matrix of **forecast error variances** from the training residuals\n" ] }, { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## How to Approximate MinT-WLS in Spark?\n", "\n", "Since Spark is not optimized for full matrix ops, you can:\n", "\n", "- Estimate **error variance per Region_Category** using training residuals.\n", "- To compute forecast error variances for use in MinT-WLS, you need **in-sample forecasts** (i.e., forecasts over the training period, not just for the future 12 months). These are often called fitted values from the model.\n", "- Use the **inverse variance** as weights.\n", "- Perform a **weighted projection** manually (just like MinT-OLS, but with weights)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Setting default log level to \"WARN\".\n", "To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).\n", "25/03/26 21:51:06 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable\n" ] }, { "data": { "text/html": [ "\n", "
SparkSession - in-memory
\n", " \n", "SparkContext
\n", "\n", " \n", "\n", "v3.5.3
local[*]
Spark Forecasting