<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://tejalbarnwal.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://tejalbarnwal.github.io/" rel="alternate" type="text/html" /><updated>2026-11-28T05:31:49+00:00</updated><id>https://tejalbarnwal.github.io/feed.xml</id><title type="html">Tejal Barnwal</title><subtitle>Welcome to my homepage!&lt;br&gt; I am Tejal Barnwal &lt;br&gt; This website is a quick outlet for my thoughts. &lt;br&gt; You can also read more  &lt;a href=&quot;https://tejalbarnwal.github.io/about/&quot;&gt;about me&lt;/a&gt; &amp;  &lt;a href=&quot;https://tejalbarnwal.github.io/projects/&quot;&gt;my key projects&lt;/a&gt;.</subtitle><author><name>Tejal Barnwal</name><email>tejalbarnwal@gmail.com</email></author><entry><title type="html">The Cost of Deviations</title><link href="https://tejalbarnwal.github.io/blog/the-cost-of-deviations" rel="alternate" type="text/html" title="The Cost of Deviations" /><published>2026-11-16T00:00:00+00:00</published><updated>2026-11-16T00:00:00+00:00</updated><id>https://tejalbarnwal.github.io/blog/the-cost-of-deviations</id><content type="html" xml:base="https://tejalbarnwal.github.io/blog/the-cost-of-deviations"><![CDATA[<p>In <a href="/blog/scoring-regressions">Scoring Regressions</a> we <strong>scored</strong> a model that was already fitted: MAE and RMSE told us how its errors looked. But where did that fitted model come from in the first place? Somebody had to <span class="hl">choose</span> it, and that choice is made by an optimiser chasing an <strong>objective function</strong>.</p>

<p>So today, let’s try to understand <strong>how to fit a model</strong>. Spoiler: the way you decide to <em>grade</em> (or <em>weigh</em>) each deviation quietly decides which answer you get (and which data points get a say).</p>

<h2 class="purple" id="nine-measurements-and-one-typo">Nine measurements and one typo</h2>

<p>Let’s start with an example. Nine friends each measure the length of the same table and write down their result, in metres:</p>

\[1,\ 2,\ 3,\ 3.5,\ 3.5,\ 4,\ 4,\ 4,\ 1000\]

<p>The last one is obviously wrong. Somebody meant to type “1.000”, or “10”, or their pen slipped. Still, the question stands: <em>if you had to report one number for the table’s length, what would it be?</em></p>

<figure class="kfig">
  <img src="/imgs/l1-l2/part2-numberline.svg?v=2" alt="The nine measurements on a number line: median 3.5, mode 4, and the mean dragged to about 113.9 by the typo 1000" />
</figure>

<p>One could report the table’s length in three ways: the <strong>mean</strong>, the <strong>median</strong> or the <strong>mode</strong>. All three try to represent the size of the same table in the best possible way, but each in its own way, and each makes its own assumptions:</p>

<div class="kbox purple">
  <p class="kbox-title">Every answer is a belief</p>
  <ul>
    <li><strong>Mean (≈ 113.9):</strong> you choose to believe 1000 was a correctly taken, correctly written measurement.</li>
    <li><strong>Median (3.5):</strong> you choose to believe it’s okay to ignore the extremes, since the data might have been contaminated along the way.</li>
    <li><strong>Mode (4):</strong> you choose to believe the value the majority agrees on.</li>
  </ul>
</div>

<p>These aren’t arbitrary recipes then. Rather, each is the <span class="hl">best possible answer</span> under a different rule for grading deviations. We just took a glimpse of these rules. Now let’s see them in detail, predict how they’ll behave in situations more complex than this really simple one, and learn how to choose between them on purpose.</p>

<h2 id="turning-a-question-into-an-optimisation-problem">Turning a question into an optimisation problem</h2>

<p>Before we dive into the rules, let’s first understand how asking “what represents my data best?” gets translated into an optimisation problem. Mathematically it means: <strong>assign a number</strong> (a score) to every candidate answer, then put all effort into making that number as small as possible. It boils down to five ingredients. Every time you encounter something to optimise, ask the five questions below. Let’s go back to our previous example, the table and the nine friends’ measurements, and answer each one for it:</p>

<div class="ktable">

  <table>
    <thead>
      <tr>
        <th style="text-align: left">Ingredient</th>
        <th style="text-align: left">The question to ask</th>
        <th style="text-align: left">In the table example</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td style="text-align: left"><strong>Data</strong></td>
        <td style="text-align: left">What is fixed / given to me?</td>
        <td style="text-align: left">\(y_1, \dots, y_9 = 1, 2, \dots, 1000\)</td>
      </tr>
      <tr>
        <td style="text-align: left"><strong>Decision variable</strong></td>
        <td style="text-align: left">What am I free to choose?</td>
        <td style="text-align: left">the number \(c\)</td>
      </tr>
      <tr>
        <td style="text-align: left"><strong>Model</strong></td>
        <td style="text-align: left">What relates my decision variable to what I know? This can be looked at as my <strong>prediction model</strong>.</td>
        <td style="text-align: left">“the length is some number \(c\)”, so my prediction is \(\hat y = c\)</td>
      </tr>
      <tr>
        <td style="text-align: left"><strong>Residual</strong></td>
        <td style="text-align: left">How far does each data point deviate from my prediction?</td>
        <td style="text-align: left">\(r_i = y_i - \hat y = y_i - c\)</td>
      </tr>
      <tr>
        <td style="text-align: left"><strong>Objective</strong> (cost)</td>
        <td style="text-align: left">How do I score my residuals? How do I weigh different residuals?</td>
        <td style="text-align: left">\(J(c) = \sum_i \rho(r_i)\)</td>
      </tr>
    </tbody>
  </table>

</div>

<p>The word <strong>residual</strong> itself means <em>what is remaining</em>: the part of each data point that my prediction couldn’t explain. It’s the <span class="hl">deviation</span> of the data from the model.</p>

<p>The rule \(\rho(r)\) that turns one residual into a cost is the <span class="hl">loss</span>. That cost is my <strong>grade</strong> for that data point: a penalty, based on how far the prediction from my decision variable deviates from it. \(\rho\) could be an L2 function, squaring the residual, or an L1 function, taking its absolute value (much more on both soon).</p>

<p><strong>Why “loss”?</strong> A loss function isn’t something special to fitting models. It’s a general idea; Wikipedia defines it as <em>“a function that maps an event or values of one or more variables onto a real number intuitively representing some ‘cost’ associated with the event.”</em> Whenever we use one to grade residuals, to score them and penalise them in some way, we call it a loss function: its output measures how much the model is <em>losing</em>, how far it falls short of the ideal.</p>

<p>The objective adds up the losses, and <strong>optimisation</strong> means finding the decision variable that makes the objective as small as possible: its <strong>minimum</strong>. Sometimes we want to make something as <em>large</em> as possible instead, like profit or value. That’s fine too: every maximisation problem can be converted into a minimisation problem, and vice versa, because maximising \(f\) is the same as minimising \(-f\).</p>

<p style="font-style: italic;">Just a little thought: these questions aren’t only for maths. What is given to me? What is my situation? What am I free to choose? Asking that, instead of just complaining about the situation, is itself an optimisation problem. (But it’s so difficult to apply in life!)</p>

<p><strong>The anatomy, in general.</strong> In its simplest form, an optimisation problem minimises (or maximises) an objective \(f(\mathbf{x})\) over decision variables \(\mathbf{x} \in \mathbb{R}^n\). Real problems often come with rules the answer must obey, written as inequalities or equalities:</p>

<div class="kgrid">
  <div class="kbox green">
    <p class="kbox-title">Unconstrained</p>

\[\min_{\mathbf{x}}\ f(\mathbf{x})\]

    <p>\(\mathbf{x}\) may take any value; nothing restricts the answer.</p>
  </div>
  <div class="kbox pink">
    <p class="kbox-title">Constrained (standard form)</p>

\[\begin{aligned}
\min_{\mathbf{x}}\ \ &amp; f(\mathbf{x})\\
\text{subject to}\ \ &amp; g_i(\mathbf{x}) \le 0,\quad i = 1, \dots, m\\
&amp; h_j(\mathbf{x}) = 0,\quad j = 1, \dots, p
\end{aligned}\]

  </div>
</div>

<h2 class="green" id="three-examples-same-recipe">Three examples, same recipe</h2>

<div class="kgrid">
  <div class="kbox">
    <p class="kbox-title">1. The table, from earlier</p>
    <p><strong>Data:</strong> 9 measurements \(y_i\)<br />
<strong>Decision variable:</strong> \(c\), our reported length<br />
<strong>Model:</strong> \(\hat y = c\)<br />
<strong>Residual:</strong> \(r_i(c) = y_i - c\)<br />
<strong>Objective:</strong></p>

\[\min_c\ \sum_{i=1}^{9} \rho\big(r_i(c)\big)\]

    <p>\(\rho\) could be the squared (L2) loss, the absolute (L1) loss, …</p>
  </div>
  <div class="kbox purple">
    <p class="kbox-title">2. Fitting a line</p>
    <p><strong>Data:</strong> points \((x_i, y_i)\): \((0,0), (1,1), (2,2), (1,2)\)<br />
<strong>Decision variables:</strong> slope \(m\), intercept \(c\)<br />
<strong>Model:</strong> \(\hat y_i = m x_i + c\)<br />
<strong>Residual:</strong> \(r_i = y_i - (m x_i + c)\)<br />
<strong>Objective</strong> (say, using the squared loss function):</p>

\[\min_{m,\,c}\ \sum_i \big(y_i - (m x_i + c)\big)^2\]

  </div>
</div>

<div class="kbox orange">
  <p class="kbox-title">3. The knapsack problem</p>
  <p>There are \(n\) distinct items and a knapsack of capacity \(W\). Select a subset of items so that the total weight doesn’t exceed \(W\) and the total value is as large as possible.</p>

  <p><strong>Data:</strong> each item’s weight \(w_i\) and value \(v_i\); the capacity \(W\)<br />
<strong>Decision variables:</strong> a yes/no \(y_i \in \{0, 1\}\) for each item (1 = it goes in the bag)<br />
<strong>Model:</strong> total weight \(\sum_i w_i\, y_i\), total value \(\sum_i v_i\, y_i\)<br />
<strong>Residual:</strong> none here! There’s no measurement to match, so we grade the choice directly.<br />
<strong>Objective:</strong></p>

\[\max_{y}\ \sum_{i=1}^{n} v_i\, y_i \qquad \text{subject to} \qquad \sum_{i=1}^{n} w_i\, y_i \le W, \qquad y_i \in \{0, 1\}.\]

  <p>A different flavour: a <em>max</em>, a constraint, and decision variables that can only be 0 or 1, so there are no slopes to walk down. But the same five questions got us here!</p>
</div>

<h2 class="pink" id="slopes-and-walking-downhill">Slopes, and walking downhill</h2>

<p>Let’s open up the objective for our table, using the squared loss \(\rho(r) = r^2\), and simplify it:</p>

\[J(c) = \sum_{i=1}^{9} (y_i - c)^2 = \sum_{i=1}^{9} \big(y_i^2 - 2\,y_i\,c + c^2\big) = \underbrace{\textstyle\sum_i y_i^2}_{1{,}000{,}086.5} \;-\; 2c\underbrace{\textstyle\sum_i y_i}_{1025} \;+\; 9c^2 .\]

<p>(Check: \(1+2+3+3.5+3.5+4+4+4+1000 = 1025\), and the squares add to \(1+4+9+12.25+12.25+16+16+16+1{,}000{,}000\).) So</p>

\[\boxed{J(c) = 9c^2 - 2050\,c + 1{,}000{,}086.5}\]

<p>That’s a <strong>quadratic in \(c\)</strong>, and the number in front of \(c^2\) is positive, so it’s a parabola that opens upward: a bowl. Let’s plot it against every candidate \(c\):</p>

<figure class="kfig">
  <img src="/imgs/l1-l2/part2-costcurve.svg?v=2" alt="The squared-loss cost J(c) for the nine table measurements: a bowl-shaped curve with tangent slopes on each side and arrows pointing downhill toward the minimum at c = 113.9, the mean" />
  <figcaption>The squared-loss cost for the nine table measurements. At any point, the slope says which way is downhill. Notice where the bottom is: the typo has dragged it all the way to 113.9.</figcaption>
</figure>

<p>The best answer sits at its lowest point. How do you find it? There are two ways.</p>

<p><strong>By hand, with basic calculus.</strong> At the bottom of a smooth curve the slope is zero, so compute the derivative \(J'\), set it equal to 0, and check that the second derivative \(J''\) is positive, which means the curve is bending upward there (a bottom, not a top):</p>

\[J'(c) = 18c - 2050 = 0 \ \Rightarrow\ c = \frac{2050}{18} = \frac{1025}{9} \approx 113.9, \qquad\qquad J''(c) = 18 &gt; 0 \ \checkmark\]

<p>Here \(J''\) is positive <em>everywhere</em>, not just at that point: the whole curve is a bowl (it’s <strong>convex</strong>), so this is the one and only minimum. And look, \(1025/9\) is exactly the <strong>mean</strong> of the nine measurements.</p>

<p><strong>Numerically, by walking downhill.</strong> The <strong>slope</strong> (derivative) of the curve says how fast the cost changes as \(c\) increases. If it’s negative, increasing \(c\) lowers the cost; if it’s positive, decreasing \(c\) does. So a computer can find the minimum by repeatedly taking a small step <em>against</em> the slope, \(c_{k+1} = c_k - \gamma\, J'(c_k)\): <span class="hl">gradient descent</span>, literally walking downhill. This is what we use when the equation \(J' = 0\) is too hard to solve by hand.</p>

<p>Keep this picture. Everything that follows is about what different losses do to the shape of this curve, and especially to its <strong>slopes</strong>.</p>

<h2 id="two-ways-to-penalise-a-deviation">Two ways to penalise a deviation</h2>

<p>The two most common losses are:</p>

<div class="ktable">

  <table>
    <thead>
      <tr>
        <th style="text-align: left">Name</th>
        <th style="text-align: center">Loss \(\rho(r)\)</th>
        <th style="text-align: left">In words</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td style="text-align: left"><strong>L2</strong>, squared error</td>
        <td style="text-align: center">\(r^2\)</td>
        <td style="text-align: left">a deviation twice as large is penalised <strong>four times</strong> as much</td>
      </tr>
      <tr>
        <td style="text-align: left"><strong>L1</strong>, absolute error</td>
        <td style="text-align: center">\(\lvert r \rvert\)</td>
        <td style="text-align: left">a deviation twice as large is penalised <strong>twice</strong> as much</td>
      </tr>
    </tbody>
  </table>

</div>

<p>(The names come from the \(\ell_2\) and \(\ell_1\) norms. A norm is a way of measuring the size of a whole list of numbers: \(\lVert r \rVert_1 = \sum_i \lvert r_i \rvert\) and \(\lVert r \rVert_2 = \sqrt{\sum_i r_i^2}\). Yep, the same ones hiding inside MAE and RMSE in <a href="/blog/scoring-regressions">Scoring Regressions</a>!)</p>

<p>Let’s apply these two to our table and analyse how they behave. Model \(\hat y = c\), residuals \(r_i = y_i - c\), \(n = 9\). Take the derivative and set it to zero:</p>

<div class="kgrid">
  <div class="kbox green">
    <p class="kbox-title">L2: the mean pops out</p>

\[L_2(c) = \sum_i (y_i - c)^2 \quad \text{(a parabola in } c\text{)}\]

\[\frac{dL_2}{dc} = -2 \sum_i (y_i - c) = 0\]

\[\sum_i y_i - 9c = 0\]

\[\Rightarrow\ c = \frac{1}{9} \sum_i y_i = \textbf{mean}\]

  </div>
  <div class="kbox pink">
    <p class="kbox-title">L1: the median pops out</p>

\[L_1(c) = \sum_i \lvert y_i - c \rvert\]

\[\frac{dL_1}{dc} = -\sum_i \sgn(y_i - c) = 0\]

    <p>\(\sgn(\cdot)\) only takes values \(-1, 0, 1\), so choose \(c\) such that the <strong>number of points below \(c\) equals the number above</strong>.</p>

    <p>Sit with this for a while. Which number has half the data below it and half above it? Sort the measurements in ascending order and take the middle one:</p>

\[1,\ 2,\ 3,\ 3.5,\ \boxed{3.5},\ 4,\ 4,\ 4,\ 1000\]

    <p>That’s the <strong>median</strong>!</p>
  </div>
</div>

<p>You see, for L1, moving \(c\) one unit toward the side with more points always helps, until the count balances. <span class="hl">Magnitudes never enter the scene at all.</span></p>

<div class="kbox orange">
  <p class="kbox-title">For this problem, that is the entire reason the median is robust</p>
  <p>Change the last value from 1000 to \(10^6\). The mean moves enormously (from ≈ 113.9 to ≈ 111,114), but the median doesn’t budge from 3.5. The counting rule is the whole secret of the median’s toughness: <strong>how far</strong> the bad point lies never enters, only <strong>which side</strong> it’s on.</p>

  <p>Careful, though: this is for fitting a <em>single number</em>. When we fit a line, an extra \(x_i\) shows up in the pull, and the story changes (see “who grabs the line?” below).</p>
</div>

<div class="kgrid">
  <div class="kbox purple">
    <p class="kbox-title">Just a caution</p>
    <p>L1 optima might not be unique. For data \((-5, -5, 5, 5)\), every \(c \in [-5, 5]\) has two points on each side, so every one of them is a median and therefore optimal.</p>
  </div>
  <div class="kbox">
    <p class="kbox-title">Fun fact: and the mode?</p>
    <p>Penalise <em>any</em> miss by 1 and an exact hit by 0 (the “0–1 loss”). The best \(c\) is then the value hit most often: the mode, 4. Size doesn’t matter at all, only exact agreement.</p>
  </div>
</div>

<h2 class="purple" id="deviations-pull-the-force-picture">Deviations pull: the force picture</h2>

<p>Let’s try to analyse what’s happening. Call the slope of a point’s own loss its <span class="hl">pull</span> (or <strong>influence</strong>), \(\psi(r) = \rho'(r)\). Since \(J(c) = \sum_i \rho(y_i - c)\), the chain rule gives \(J'(c) = -\sum_i \psi(r_i)\), so gradient descent becomes</p>

\[c_{k+1} = c_k + \gamma \sum_i \psi(r_i): \qquad \text{move } c \text{ in the direction of the net pull!}\]

<figure class="kfig">
  <img src="/imgs/l1-l2/part2-pulls.svg?v=2" alt="Pull of one point versus its residual: a straight line 2r for L2 that grows forever, a step sign(r) for L1 that is capped at 1" />
</figure>

<p>You see, for L1 it never mattered what the value of \(r\) is, but for L2 it matters: a larger \(r\) makes a greater force, so the best value gets influenced by it.</p>

<div class="kbox green">
  <p class="kbox-title">A physics way to see it</p>
  <p>Picture \(c\) as a little bead on a rail, with every data point tied to it by a band that pulls the bead toward that point. Gradient descent keeps nudging the bead along the net force, and the bead settles where it feels <strong>zero total force</strong>: equilibrium, \(\sum_i \psi(r_i) = 0\). That’s exactly the “slope = 0” condition!</p>

  <ul>
    <li><strong>L2</strong> bands are ideal springs: stretch twice as far, pull twice as hard.</li>
    <li><strong>L1</strong> bands are strings with fixed tension: everyone pulls with strength 1, however far away.</li>
  </ul>
</div>

<div class="ktable">

  <table>
    <thead>
      <tr>
        <th style="text-align: center">Size of residual \(r\)</th>
        <th style="text-align: center">Pull under L1</th>
        <th style="text-align: center">Pull under L2</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td style="text-align: center">1</td>
        <td style="text-align: center">1</td>
        <td style="text-align: center">2</td>
      </tr>
      <tr>
        <td style="text-align: center">10</td>
        <td style="text-align: center">1</td>
        <td style="text-align: center">20</td>
      </tr>
      <tr>
        <td style="text-align: center">100</td>
        <td style="text-align: center">1</td>
        <td style="text-align: center">200</td>
      </tr>
      <tr>
        <td style="text-align: center">1000</td>
        <td style="text-align: center">1</td>
        <td style="text-align: center">2000</td>
      </tr>
    </tbody>
  </table>

</div>

<p><strong>L2:</strong> a point’s pull grows with its distance, without limit. A point a thousand units away pulls a thousand times harder than one a unit away. That’s why one typo dragged the average to 113.9.
<strong>L1:</strong> every point pulls with the same strength. One point, one vote; the majority decides.</p>

<div class="kbox purple">
  <p class="kbox-title">Worked example: who holds the vote?</p>
  <p>Three residuals of sizes 1, 1 and 100. Under L2 their pulls are 2, 2 and 200; under L1 they are 1, 1 and 1.</p>

  <div class="kvote">
  <div class="lab">L2</div>
  <div class="bar"><span style="width:1%;background:#3E9B4F"></span><span style="width:1%;background:#7CC487"></span><span style="width:98%;background:#E84470">the 100 holds 98% of the vote</span></div>
  <div class="lab">L1</div>
  <div class="bar"><span style="width:33.4%;background:#3E9B4F">1/3</span><span style="width:33.3%;background:#7CC487">1/3</span><span style="width:33.3%;background:#E84470">1/3</span></div>
</div>
</div>

<div class="kgrid">
  <div class="kbox">
    <p class="kbox-title">Key idea</p>
    <p>To predict how a loss will behave, look at its <strong>pull</strong> (its slope), not its <strong>cost</strong> (its value). Under L1 the typo still <em>costs</em> \(\lvert 1000 - 3.5 \rvert = 996.5\); it just can’t <em>pull</em> any harder than anyone else.</p>
  </div>
  <div class="kbox pink">
    <p class="kbox-title">Common pitfall</p>
    <p>“L1 ignores outliers” is not about adding up absolute values: \(\lvert 1 \rvert + \lvert 2 \rvert + \lvert 3 \rvert + \lvert 4 \rvert + \lvert 1000 \rvert = 1010\) is dominated by the outlier too. The robustness is in <em>where the best answer lands</em>, and it comes from the capped pull.</p>
  </div>
</div>

<h2 class="green" id="now-lets-fit-a-line">Now let’s fit a line</h2>

<p>Data \((x_i, y_i) = (0,0), (1,1), (2,2), (1,2)\); decision variables \(m, c\); model \(\hat y_i = m x_i + c\); residual \(r_i = y_i - (m x_i + c)\). Stack everything up:</p>

\[A = \begin{bmatrix} x_1 &amp; 1\\ x_2 &amp; 1\\ x_3 &amp; 1\\ x_4 &amp; 1 \end{bmatrix}, \qquad
\theta = \begin{bmatrix} m\\ c \end{bmatrix}, \qquad
b = \begin{bmatrix} y_1\\ y_2\\ y_3\\ y_4 \end{bmatrix}, \qquad
r = b - A\theta.\]

<div class="kbox green">
  <p class="kbox-title">L2: a closed-form answer</p>

\[F(\theta) = \lVert A\theta - b \rVert^2 = (A\theta - b)^\top (A\theta - b) = \theta^\top A^\top A\, \theta - 2\, b^\top A\, \theta + b^\top b\]

\[\frac{\partial F}{\partial \theta} = 2 A^\top A\, \theta - 2 A^\top b = 0 \quad\Longrightarrow\quad A^\top A\, \theta = A^\top b \quad\Longrightarrow\quad \boxed{\theta = (A^\top A)^{-1} A^\top b}\]

  <p>For our points: \(A^\top A = \begin{bmatrix} 6 &amp; 4\\ 4 &amp; 4 \end{bmatrix}\), \(A^\top b = \begin{bmatrix} 7\\ 5 \end{bmatrix}\), so \(m = 1\) and \(c = 0.25\).</p>
</div>

<div class="kbox orange">
  <p class="kbox-title">Doubt: when does \((A^\top A)^{-1}\) exist?</p>
  <p>\(A\) is \(N \times p\): \(N\) data points, \(p\) parameters (here \(4 \times 2\)). \(A^\top A\) is invertible exactly when the <strong>columns of \(A\) are linearly independent</strong>. Two things follow:</p>

  <ul>
    <li><strong>Why \(N \ge p\)?</strong> \(p\) independent columns need at least \(p\) rows. One point can’t pin down both a slope and an intercept: infinitely many lines pass through it.</li>
    <li><strong>Are the columns always independent in reality?</strong> Not always! If every \(x_i\) is the same, the \(x\) column is a multiple of the column of 1s, and every line through that vertical stack of points is equally good. Real noisy data is almost never <em>exactly</em> dependent, but <em>nearly</em> dependent columns make \(A^\top A\) almost singular and the answer very jumpy (more on taming that later in the series).</li>
  </ul>
</div>

<p><strong>Who pulls on what?</strong> Look at each parameter separately:</p>

\[\frac{\partial F}{\partial m} = -2 \sum_i x_i\, r_i, \qquad \frac{\partial F}{\partial c} = -2 \sum_i r_i.\]

<p>The pull on the <strong>slope</strong> is weighted by <strong>both</strong> the residual \(r_i\) and its \(x_i\). So in this case every point except \((0,0)\) has a say on the slope, as its \(x_i = 0\). The pull on the <strong>intercept</strong> only sees the residuals. In general, for any parameters \(\theta\) and any loss, \(\nabla_\theta J = \sum_i \psi(r_i)\, \nabla_\theta r_i\): the pull \(\psi\) times how much that parameter moves that residual.</p>

<div class="kbox pink">
  <p class="kbox-title">L1 (least absolute deviation)</p>

\[G(m, c) = \sum_i \big\lvert y_i - (m x_i + c) \big\rvert, \qquad \frac{\partial G}{\partial m} = -\sum_i x_i \sgn(r_i), \qquad \frac{\partial G}{\partial c} = -\sum_i \sgn(r_i).\]

  <p>Setting these to zero is a <strong>median-type</strong> relation: for \(c\), the signs must balance (as many points above the line as below); for \(m\), the signs must balance <em>weighted by \(x_i\)</em>, so the bigger the \(x_i\), the more weight its vote has.</p>
</div>

<figure class="kfig">
  <img src="/imgs/l1-l2/part2-linefit.svg?v=2" alt="L1 fit y = x passes through three points; L2 fit y = x + 0.25 is lifted toward the odd point (1, 2)" />
  <figcaption>L1 fits three points exactly and leaves the whole deviation on the odd one. L2 lifts the entire line by 0.25 so every point shares the blame.</figcaption>
</figure>

<div class="ktable">

  <table>
    <thead>
      <tr>
        <th style="text-align: left"> </th>
        <th style="text-align: left">Balance conditions at the optimum</th>
        <th style="text-align: left">What has to cancel</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td style="text-align: left"><strong>L2</strong></td>
        <td style="text-align: left">\(\sum_i r_i = 0\) and \(\sum_i x_i r_i = 0\)</td>
        <td style="text-align: left">residual <strong>sizes</strong></td>
      </tr>
      <tr>
        <td style="text-align: left"><strong>L1</strong></td>
        <td style="text-align: left">\(\sum_i \sgn(r_i) = 0\) and \(\sum_i x_i \sgn(r_i) = 0\)</td>
        <td style="text-align: left">residual <strong>signs</strong></td>
      </tr>
    </tbody>
  </table>

</div>

<div class="kbox">
  <p class="kbox-title">The takeaway</p>
  <p>Both fits balance the residuals against \(1\) and \(x\). L2 balances their <strong>sizes</strong>, so the error spreads out; L1 balances only their <strong>signs</strong>, so the error can concentrate on a few points.</p>
</div>

<h2 class="orange" id="one-more-who-grabs-the-line">One more: who grabs the line?</h2>

<p>Let’s take one more example to see the influence of both \(x_i\) and \(r_i\). Points: \((0,0), (1,1), (2,2)\), plus two troublemakers: \((1,10)\), which is far off in \(y\), and \((10,1)\), which is far off in \(x\).</p>

<div class="kbox purple">
  <p class="kbox-title">My prediction (before computing anything!)</p>
  <p><strong>L2</strong> will be torn apart between \((1,10)\) and \((10,1)\), with their big residuals dominating.
<strong>L1</strong> will be solely dominated by \((10,1)\): its pull on the slope is \(x_i \sgn(r_i)\), and \(x_i = 10\) is a big vote.</p>
</div>

<figure class="kfig">
  <img src="/imgs/l1-l2/part2-leverage.svg?v=2" alt="With outliers (1,10) and (10,1), the L2 line tilts down to y ≈ -0.21x + 3.40 while the L1 line becomes flat at y = 1, ignoring the y = x trend" />
</figure>

<p>Prediction confirmed ✓! L2 gets torn: \((1,10)\) lifts it and \((10,1)\) tilts it downward, so it fits nobody well. L1 completely ignores \((1,10)\) (its residual is 9, but it only gets one vote), yet it <strong>gets captured by \((10,1)\)</strong>: the best L1 line is the flat \(y = 1\), passing through \((1,1)\) and \((10,1)\) and ignoring the clear \(y = x\) trend.</p>

<div class="kbox pink">
  <p class="kbox-title">Key idea: leverage</p>
  <p>Under L1, a big <strong>residual</strong> buys a point no extra pull: everyone gets one vote. But on the <strong>slope</strong>, each vote is multiplied by that point’s \(x_i\) (remember \(\partial G / \partial m = -\sum_i x_i \sgn(r_i)\)). So a point far out in \(x\), called a <span class="hl">leverage point</span>, gets a big vote on the slope, and that’s true under <em>any</em> loss \(\rho\). Changing the loss can’t fix it; you need methods that also limit how much a far-away \(x\) can count.</p>

  <p>This is a different problem from having “many bad points”. Bad points have wrong \(y\) values, so their residuals are big, and L1 handles them by majority vote. A leverage point’s trouble is its \(x\) value: its <em>lever arm</em> is long, so even one vote from it moves the line a lot.</p>
</div>

<h2 class="pink" id="rare-event-or-broken-data">Rare event or broken data?</h2>

<p>So L2 is fragile and L1 is tough. Should we always use L1? No, and the reason is the most important judgment in this whole article.</p>

<ul>
  <li>A <strong>rare event</strong> is unusual but genuine: a real reading from the same process, just far from typical.</li>
  <li>An <strong>outlier</strong> comes from a <em>different</em> process: a typo, a sensor glitch, a mislabelled example.</li>
</ul>

<p>The same number can be either, and you can’t tell from the number alone. A robot that is 50 cm off course might have a faulty sensor, or might really be 50 cm off course and about to hit a wall.</p>

<div class="kbox">
  <p class="kbox-title">Key idea</p>
  <p>Ask <strong>why</strong> a deviation is large, not <em>whether</em> it is large. If large deviations are real and costly, you want the answer to work hard to avoid them: L2 (or something even stronger). If they might be fake, you want to limit their pull: L1, or the compromise coming up next.</p>
</div>

<div class="kgrid">
  <div class="kbox green">
    <p class="kbox-title">Worked example: collision avoidance</p>
    <p>Which set of errors (in cm) is better?<br />
<strong>A.</strong> \([1,1,1,1,1,1,1,1,1,1]\): ten predictions, each off by 1 cm<br />
<strong>B.</strong> \([0,0,0,0,0,0,0,0,0,10]\): nine perfect, one off by 10 cm</p>

    <p>The single 10 cm miss is the one that hits the wall, so A is better. L2 agrees: A costs \(10 \times 1^2 = 10\), B costs \(10^2 = 100\). L1 can’t tell them apart: 10 vs 10.</p>
  </div>
  <div class="kbox orange">
    <p class="kbox-title">Worked example: which miss is real?</p>
    <p>100 predictions each off by 10 cm, or 99 perfect and one off by 5 m? L1: 10 m vs 5 m, so it prefers the second. L2: 1 m² vs 25 m², so it prefers the first. Which is right depends on whether that 5 m miss is real (use L2) or a glitch (use Huber).</p>
  </div>
</div>

<h2 class="green" id="huber-trust-small-deviations-distrust-large-ones">Huber: trust small deviations, distrust large ones</h2>

<p>Huber’s loss is quadratic for small residuals and linear for large ones, joined smoothly at a threshold \(\delta\):</p>

\[\rho_\delta(r) = \begin{cases} \tfrac12 r^2 &amp; \lvert r \rvert \le \delta \\ \delta \lvert r \rvert - \tfrac12 \delta^2 &amp; \lvert r \rvert &gt; \delta \end{cases}
\qquad\qquad
\text{pull} \quad \psi_\delta(r) = \begin{cases} r &amp; \lvert r \rvert \le \delta \\ \pm\delta &amp; \lvert r \rvert &gt; \delta \end{cases}\]

<p>(The \(\tfrac12\) in front of \(r^2\) is a convention that makes the pull exactly \(r\); it changes nothing about where the minimum is. From here on, L2 means \(\tfrac12 r^2\) too.)</p>

<figure class="kfig">
  <img src="/imgs/l1-l2/part2-huber.svg?v=2" alt="Left: the L2, L1 and Huber losses. Right: their pulls; L2 grows forever, L1 is a step, Huber grows like L2 and then is capped" />
  <figcaption>Left: how each rule penalises a residual. Right: how hard each residual pulls. Huber's pull grows like L2's, then stops growing.</figcaption>
</figure>

<div class="kbox">
  <p class="kbox-title">Key idea</p>
  <p>Huber’s sentence: <em>“Inside \(\pm\delta\) I treat deviations as genuine; beyond it, a deviation’s size buys it no extra pull.”</em> A wild point still pulls, but with its strength capped at \(\delta\). It <strong>caps the pull</strong>; it doesn’t delete the point.</p>
</div>

<ul>
  <li>As \(\delta \to \infty\), Huber becomes L2; as \(\delta \to 0\), it becomes (a scaled) L1. \(\delta\) is a <strong>dial</strong> between the two.</li>
  <li><strong>How big should \(\delta\) be?</strong> It’s measured relative to the typical size \(\sigma\) of the genuine noise, and the usual choice is \(\delta \approx 1.345\,\sigma\). (On our nine measurements, Huber lands at \(c \approx 3.5\), right next to the median.)</li>
</ul>

<div class="ktable">

  <table>
    <thead>
      <tr>
        <th style="text-align: left">Loss</th>
        <th style="text-align: left">\(\rho(r)\)</th>
        <th style="text-align: left">Pull \(\psi(r)\)</th>
        <th style="text-align: left">A far outlier’s pull</th>
        <th style="text-align: center">Convex?</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td style="text-align: left">L2</td>
        <td style="text-align: left">\(\tfrac12 r^2\)</td>
        <td style="text-align: left">\(r\)</td>
        <td style="text-align: left">unbounded</td>
        <td style="text-align: center">yes</td>
      </tr>
      <tr>
        <td style="text-align: left">L1</td>
        <td style="text-align: left">\(\lvert r \rvert\)</td>
        <td style="text-align: left">\(\sgn(r)\)</td>
        <td style="text-align: left">capped at 1</td>
        <td style="text-align: center">yes</td>
      </tr>
      <tr>
        <td style="text-align: left">Huber</td>
        <td style="text-align: left">\(\tfrac12 r^2\) if \(\lvert r \rvert \le \delta\), else \(\delta \lvert r \rvert - \tfrac12 \delta^2\)</td>
        <td style="text-align: left">\(r\) inside, \(\delta \sgn(r)\) outside</td>
        <td style="text-align: left">capped at \(\delta\)</td>
        <td style="text-align: center">yes</td>
      </tr>
    </tbody>
  </table>

</div>

<h2 id="choosing-a-rule-on-purpose">Choosing a rule on purpose</h2>

<div class="ktable">

  <table>
    <thead>
      <tr>
        <th style="text-align: left">Situation</th>
        <th style="text-align: left">Choice, and why</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td style="text-align: left">Clean data, small genuine noise</td>
        <td style="text-align: left"><strong>L2</strong>: large deviations are real and worth fixing first</td>
      </tr>
      <tr>
        <td style="text-align: left">Mostly clean, some bad points (a few wrong \(y\) values)</td>
        <td style="text-align: left"><strong>Huber</strong>: a safe default with capped pull</td>
      </tr>
      <tr>
        <td style="text-align: left">Many bad points (lots of wrong \(y\) values)</td>
        <td style="text-align: left"><strong>L1</strong>: majority rules</td>
      </tr>
      <tr>
        <td style="text-align: left">Catastrophes matter most, and are real</td>
        <td style="text-align: left"><strong>L2</strong>, a higher power (\(r^4\)), the <strong>worst case</strong> \(\max_i \lvert r_i \rvert\), or a hard limit written as a constraint, e.g. \(\lvert r_i \rvert \le 5\) cm for every \(i\) (no prediction may ever be off by more than 5 cm)</td>
      </tr>
      <tr>
        <td style="text-align: left">Outliers in \(x\) (leverage): a point whose \(x\) is far from the rest</td>
        <td style="text-align: left">no choice of \(\rho\) helps, because \(x_i\) multiplies its pull on the slope (see “who grabs the line?”): limit the influence of \(x\) too</td>
      </tr>
    </tbody>
  </table>

</div>

<p>Requirements stated in words translate into objectives too. The short version: “must” and “never” become <span class="hl">constraints</span>; “prefer” and “less” become <span class="hl">penalties</span>.</p>

<div class="krule">
  <div class="big">A rule: ask <em>why</em> a deviation is large</div>
  <div class="small">Not whether it is large. The answer picks your loss: L2 if it's real, Huber or L1 if it might be fake.</div>
</div>

<div class="kbox">
  <p class="kbox-title">Coming later</p>
  <p>Some requirements, like “a simple model”, ask us to penalise the <em>parameters</em> themselves, not just the deviations. Same L1 and L2, brand new job: <strong>regularisation</strong>. And why do squares secretly go with Gaussian noise and absolute values with Laplace noise? That waits for a later post on <strong>estimation</strong>. <strong>Let’s find out!!</strong></p>
</div>]]></content><author><name>Tejal Barnwal</name><email>tejalbarnwal@gmail.com</email></author><category term="maths" /><category term="L1 &amp; L2, All Around Us" /><category term="Optimisation" /><summary type="html"><![CDATA[How the way you grade each deviation quietly decides which answer you get, and which data points get a say.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://tejalbarnwal.github.io/imgs/thumbs/the-cost-of-deviations.svg" /><media:content medium="image" url="https://tejalbarnwal.github.io/imgs/thumbs/the-cost-of-deviations.svg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Scoring Regressions</title><link href="https://tejalbarnwal.github.io/blog/scoring-regressions" rel="alternate" type="text/html" title="Scoring Regressions" /><published>2026-10-21T00:00:00+00:00</published><updated>2026-10-21T00:00:00+00:00</updated><id>https://tejalbarnwal.github.io/blog/scoring-regressions</id><content type="html" xml:base="https://tejalbarnwal.github.io/blog/scoring-regressions"><![CDATA[<p>We are surrounded by a plethora of engineering marvels and challenges, offering great examples and potential for improvement and optimisation: from something as simple as people’s health schedules to determining the shape of a giant A380 aircraft wing. The modern world works on one word doing all the heavy lifting for us: <span class="hl">OPTIMISATION</span>. The word itself reflects the idea of improving, and continually improving, toward perfection, much like our human instincts for many things.</p>

<p>And once you start looking, two characters keep showing up all over this world: <strong>L1</strong> and <strong>L2</strong>. This series is going to take a short tour (sometimes detours XD) through the four places I keep bumping into them.</p>

<div class="kbox orange">
  <p class="kbox-title">TL;DR of the series</p>
  <p>The same two norms wear four different hats:</p>

  <ol>
    <li><strong>Measuring error</strong> (this post): MAE and RMSE score a model that’s already fitted.</li>
    <li><strong>Choosing a loss</strong> (<a href="/blog/the-cost-of-deviations">The Cost of Deviations</a>): the rule the optimiser uses to fit the model in the first place.</li>
    <li><strong>Regularisation</strong>: still a loss, but charged on the model’s <em>parameters</em>, with a more nuanced meaning.</li>
    <li><strong>Estimation</strong>: L2 meets probability, in things like the Kalman filter and its robust cousins.</li>
  </ol>

  <p>Choosing an objective function is choosing, in a way, <strong>what kind of errors made by the model deserve the optimiser’s attention, and by how much</strong>. Looking at the behaviour of the objective function tells you a lot about what your model assumes.</p>
</div>

<h2 class="purple" id="first-scoring-vs-shaping">First: scoring vs. shaping</h2>

<p>Before we go deep into choosing which function to use for minimisation, let us first talk about how to <strong>score</strong> the errors our model’s output is gonna give.</p>

<ul>
  <li>A <span class="hl">metric</span> reports a model’s error. Metrics describe a finished, fitted model. <strong>(This post.)</strong></li>
  <li>A <span class="hl">loss</span> is the function we choose to shape the model. <strong>(Next post: <a href="/blog/the-cost-of-deviations">The Cost of Deviations</a>.)</strong></li>
</ul>

<p>Two of the most common metrics are <strong>MAE</strong> and <strong>RMSE</strong>. For an error vector \(e \in \mathbb{R}^n\) given by \((e_1, e_2, \dots, e_n)\):</p>

<div class="kgrid">
  <div class="kbox green">
    <p class="kbox-title">MAE: Mean Absolute Error</p>

\[\text{MAE} = \frac{1}{n}\sum_{i=1}^{n} \lvert e_i \rvert = \frac{1}{n}\lVert e \rVert_1\]

    <p>Drop the minus signs, then take the average. Every unit of error counts the same.</p>
  </div>
  <div class="kbox pink">
    <p class="kbox-title">RMSE: Root Mean Squared Error</p>

\[\text{RMSE} = \sqrt{\frac{1}{n}\sum_{i=1}^{n} e_i^2} = \frac{\lVert e \rVert_2}{\sqrt{n}}\]

    <p>Square every error, average, then square-root back to normal units. Big errors count a <strong>lot</strong> more.</p>
  </div>
</div>

<p>We will try to go beyond these mere formula definitions and understand qualitatively what implications they have. Now, let’s look at some examples.</p>

<h2 id="five-models-five-mistakes-each">Five models, five mistakes each</h2>

<p>There are 5 fitted models, and here are the errors each one makes on the same 5 data points:</p>

<div class="ktable">

  <table>
    <thead>
      <tr>
        <th style="text-align: center">Model</th>
        <th style="text-align: left">Errors</th>
        <th style="text-align: center">MAE</th>
        <th style="text-align: center">RMSE</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td style="text-align: center"><strong>A</strong></td>
        <td style="text-align: left">3, 3, 3, 3, 3</td>
        <td style="text-align: center">3</td>
        <td style="text-align: center">3</td>
      </tr>
      <tr>
        <td style="text-align: center"><strong>B</strong></td>
        <td style="text-align: left">1, 2, 3, 4, 5</td>
        <td style="text-align: center">3</td>
        <td style="text-align: center">\(\sqrt{11} \approx 3.32\)</td>
      </tr>
      <tr>
        <td style="text-align: center"><strong>C</strong></td>
        <td style="text-align: left">0, 0, 0, 0, 15</td>
        <td style="text-align: center">3</td>
        <td style="text-align: center">\(\sqrt{45} \approx 6.71\)</td>
      </tr>
      <tr>
        <td style="text-align: center"><strong>D</strong></td>
        <td style="text-align: left">1, 1, 3, 3, 5</td>
        <td style="text-align: center">2.6</td>
        <td style="text-align: center">3</td>
      </tr>
      <tr>
        <td style="text-align: center"><strong>E</strong></td>
        <td style="text-align: left">0, 0, 0, 0, \(\sqrt{45}\)</td>
        <td style="text-align: center">\(\approx 1.34\)</td>
        <td style="text-align: center">3</td>
      </tr>
    </tbody>
  </table>

</div>

<h2 class="pink" id="story-1-same-mae-different-vibes">Story 1: Same MAE, different vibes</h2>

<div class="kbars">
  <div class="kbm c-green">
    <div class="name">Model A</div>
    <div class="plot"><span style="height:20%"></span><span style="height:20%"></span><span style="height:20%"></span><span style="height:20%"></span><span style="height:20%"></span><div class="mae" style="bottom:20%"><b>MAE 3</b></div></div>
    <div class="cap">RMSE = 3</div>
  </div>
  <div class="kbm c-orange">
    <div class="name">Model B</div>
    <div class="plot"><span style="height:6.7%"></span><span style="height:13.3%"></span><span style="height:20%"></span><span style="height:26.7%"></span><span style="height:33.3%"></span><div class="mae" style="bottom:20%"><b>MAE 3</b></div></div>
    <div class="cap">RMSE &asymp; 3.32</div>
  </div>
  <div class="kbm c-pink">
    <div class="name">Model C</div>
    <div class="plot"><span style="height:0"></span><span style="height:0"></span><span style="height:0"></span><span style="height:0"></span><span style="height:100%"></span><div class="mae" style="bottom:20%"><b>MAE 3</b></div></div>
    <div class="cap">RMSE &asymp; 6.71</div>
  </div>
</div>

<p>Look at A, B and C. All have the same MAE, but looking at the error values, they still feel different. A is steady. B wobbles. C is perfect four times, then makes one huge blunder.</p>

<p>If we look at RMSE, it captures this quite interestingly well: \(\text{RMSE}_A = 3 &lt; \text{RMSE}_B \approx 3.32 &lt; \text{RMSE}_C \approx 6.71\). The larger the variation, the more RMSE picks it up. RMSE is exactly meant to capture this difference. <span class="hl">RMSE dislikes big mistakes being made.</span></p>

<div class="kbox orange">
  <p class="kbox-title">Fun fact: why does this happen?</p>
  <p>There’s a neat identity hiding here:</p>

\[\text{RMSE}^2 = \text{MAE}^2 + \big(\text{spread of the error sizes } \lvert e_i \rvert\big)^2\]

  <p>where “spread” is the standard deviation. So RMSE is MAE <strong>plus a penalty for uneven mistakes</strong>. For model C: \(3^2 + 6^2 = 45\), and \(\sqrt{45} \approx 6.71\).</p>
</div>

<h2 class="green" id="story-2-same-rmse-different-mae">Story 2: Same RMSE, different MAE</h2>

<div class="kbars">
  <div class="kbm c-green">
    <div class="name">Model A</div>
    <div class="plot"><span style="height:20%"></span><span style="height:20%"></span><span style="height:20%"></span><span style="height:20%"></span><span style="height:20%"></span><div class="mae" style="bottom:20%"><b>MAE 3</b></div></div>
    <div class="cap">RMSE = 3</div>
  </div>
  <div class="kbm c-blue">
    <div class="name">Model D</div>
    <div class="plot"><span style="height:6.7%"></span><span style="height:6.7%"></span><span style="height:20%"></span><span style="height:20%"></span><span style="height:33.3%"></span><div class="mae" style="bottom:17.3%"><b>MAE 2.6</b></div></div>
    <div class="cap">RMSE = 3</div>
  </div>
  <div class="kbm c-purple">
    <div class="name">Model E</div>
    <div class="plot"><span style="height:0"></span><span style="height:0"></span><span style="height:0"></span><span style="height:0"></span><span style="height:44.7%"></span><div class="mae" style="bottom:8.9%"><b>MAE 1.34</b></div></div>
    <div class="cap">RMSE = 3</div>
  </div>
</div>

<p>Now look at A, D and E. All have the same RMSE. But when you look at the data, they clearly differ in how many outliers they have: A is steady 3s (MAE 3), D is a mix (MAE 2.6), and E is four perfect predictions plus one big miss (MAE ≈ 1.34). MAE sees that E is mostly good: <span class="hl">it counts each mistake at its actual size</span>, so a rare outlier doesn’t take over the score.</p>

<div class="kbox purple">
  <p class="kbox-title">A good interpretation</p>
  <p><strong>MAE:</strong> the average error size.<br />
<strong>RMSE:</strong> the average mistake size, where <strong>big mistakes weigh more</strong>.</p>
</div>

<h2 class="orange" id="who-should-be-graded-how">Who should be graded how?</h2>

<div class="kgrid">
  <div class="kbox pink">
    <p class="kbox-title">A surgeon: use RMSE</p>
    <p>A practising surgeon should be judged on RMSE, not MAE. One mistake can be fatal.</p>
  </div>
  <div class="kbox green">
    <p class="kbox-title">Airline delays: use MAE</p>
    <p>Delays in airline operations can be assessed using MAE. A 10-minute delay is a 10-minute delay.</p>
  </div>
</div>

<h2 class="purple" id="the-bounds-and-what-the-gap-tells-us">The bounds, and what the gap tells us</h2>

\[\text{MAE} \;\le\; \text{RMSE} \;\le\; \sqrt{n}\cdot\text{MAE}\]

<ul>
  <li><strong>RMSE = MAE</strong> holds when every error has the same absolute value: \([-5, 5]\), \([5, 5]\) and \([-5, -5]\) all count as “all fives”. That’s model A.</li>
  <li><strong>RMSE = \(\sqrt{n}\,\)MAE</strong> when all errors except one are zero. That’s models C and E (here \(n = 5\), so \(\sqrt{5} \approx 2.24\)).</li>
</ul>

<p>Both MAE and RMSE indicate the typical size of the error in their own ways. But their ratio, somewhere in \([1, \sqrt{n}]\), tells you how much the error magnitudes vary and how concentrated they are around a particular value:</p>

<div class="kratio">
  <span class="m" style="left:0%">A</span>
  <span class="m" style="left:8.6%; top:-50px">B</span>
  <span class="m" style="left:12.4%">D</span>
  <span class="m" style="left:100%">C, E</span>
</div>
<div class="kratio-labels">
  <div><strong>RMSE/MAE = 1</strong><br />all mistakes the same size</div>
  <div><strong>RMSE/MAE = &radic;n</strong><br />one big mistake, the rest zero</div>
</div>

<div class="kgrid">
  <div class="kbox">
    <p class="kbox-title">Quick read</p>
    <p>RMSE ≈ MAE ⇒ <strong>consistent errors</strong><br />
RMSE ≫ MAE ⇒ <strong>uneven errors</strong>, usually a few big ones</p>
  </div>
  <div class="kbox pink">
    <p class="kbox-title">Side note: squaring doesn’t always penalise more</p>
    <p>Note that \(0.5^2 = 0.25\) but \(3^2 = 9\). RMSE’s squaring rule penalises errors smaller than 1 less and errors larger than 1 more, so it penalises large deviations <strong>disproportionately</strong> more than small ones.</p>
  </div>
</div>

<h2 id="so-when-do-you-want-which-metric">So, when do you want which metric?</h2>

<div class="kgrid">
  <div class="kbox green">
    <p class="kbox-title">Pick MAE when…</p>
    <ul>
      <li>every unit of error matters equally</li>
      <li>you want robustness to outlier-like situations</li>
      <li>you like median-like behaviour (more in <a href="/blog/the-cost-of-deviations">The Cost of Deviations</a>!)</li>
      <li>large mistakes aren’t catastrophic (nothing safety-critical)</li>
    </ul>

    <p><strong>e.g.</strong> predicting arrival times for fixed-schedule flights</p>
  </div>
  <div class="kbox pink">
    <p class="kbox-title">Pick RMSE when…</p>
    <ul>
      <li>large errors should count extra</li>
      <li>you want to punish and measure outliers as well</li>
    </ul>

    <p><strong>e.g.</strong> collision avoidance; position estimation, so we know how badly a model behaves over the entire course</p>
  </div>
</div>

<div class="krule">
  <div class="big">A rule: report both!</div>
  <div class="small">The gap between these two numbers tells a more interesting story than either metric alone.</div>
</div>

<div class="kbox">
  <p class="kbox-title">Up next: The Cost of Deviations</p>
  <p>This article was about scoring mistakes made by a given fitted model: metrics for assessing how good a fitted model is. But how do we find a good model to fit in the first place?! <strong><a href="/blog/the-cost-of-deviations">Let’s find out!!</a></strong></p>
</div>]]></content><author><name>Tejal Barnwal</name><email>tejalbarnwal@gmail.com</email></author><category term="maths" /><category term="L1 &amp; L2, All Around Us" /><category term="Optimisation" /><summary type="html"><![CDATA[Two numbers that score a fitted model's errors, MAE and RMSE, and what the gap between them tells you.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://tejalbarnwal.github.io/imgs/thumbs/scoring-regressions.svg" /><media:content medium="image" url="https://tejalbarnwal.github.io/imgs/thumbs/scoring-regressions.svg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Documentation for min-snap trajectory</title><link href="https://tejalbarnwal.github.io/blog/min-snap-traj" rel="alternate" type="text/html" title="Documentation for min-snap trajectory" /><published>2024-06-24T00:00:00+00:00</published><updated>2024-06-24T00:00:00+00:00</updated><id>https://tejalbarnwal.github.io/blog/min-snap-traj</id><content type="html" xml:base="https://tejalbarnwal.github.io/blog/min-snap-traj"><![CDATA[<p><br />
<br /></p>
<div style="text-align: center;">
<!-- <a href="https://tejalbarnwal.github.io/pdfs/tejalbarnwal_resume.pdf">
    <img src="../pdfs/pdf.svg" alt="CV" title="Dowload CV" height="30px"> &nbsp;
    <span style="color:blue; font-size:1.25em">Curriculum Vitae </span>
</a> -->

<iframe src="https://docs.google.com/gview?url=https://tejalbarnwal.github.io/pdfs/min-snap-trajectory.pdf&amp;embedded=true" width="75%" height="600px"></iframe>
</div>]]></content><author><name>Tejal Barnwal</name><email>tejalbarnwal@gmail.com</email></author><category term="engineering" /><category term="Documentation" /><summary type="html"><![CDATA[Derivations for min-snap trajectory]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://tejalbarnwal.github.io/imgs/thumbs/min-snap-trajectory.svg" /><media:content medium="image" url="https://tejalbarnwal.github.io/imgs/thumbs/min-snap-trajectory.svg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Sublime and Tmux Shortcuts</title><link href="https://tejalbarnwal.github.io/blog/sublime-tutorial" rel="alternate" type="text/html" title="Sublime and Tmux Shortcuts" /><published>2021-12-20T00:00:00+00:00</published><updated>2021-12-20T00:00:00+00:00</updated><id>https://tejalbarnwal.github.io/blog/sublime-tutorial</id><content type="html" xml:base="https://tejalbarnwal.github.io/blog/sublime-tutorial"><![CDATA[<h1 id="sublime-shortcuts-to-use">Sublime Shortcuts to use</h1>

<ul>
  <li>Cntrl + mouse right click : multiple cursor</li>
  <li>Cntrl + d : select next occurence of word that you had selected</li>
  <li>Slect on thing hold on cntrl and select other : now we can copy paste multiple</li>
  <li>Cntrl + p : go to anything ( get preview of files that you had visited earlier)</li>
  <li>Cntrol + p + : -&gt; allow to navigate tot he line number</li>
  <li>Cntrl +  shift + p -&gt; set the syntax of the file that you want to</li>
  <li>F3 : find next</li>
  <li>Shift + F3 : find previous</li>
  <li>cntrl + shift + F : find in files <br />
Other Resources : <br />
<a href="https://w3codemasters.in/sublime-text-split-screen/">Other short notes 1</a> <br />
<a href="https://generalassemb.ly/blog/sublime-text-3-tips-tricks-shortcuts/#:~:text=If%20you%20want%20to%20split,new%20group%20inside%20a%20pane.">Short Notes 2</a></li>
</ul>

<h1 id="tmux-shortcuts-to-use-based-on-my-tmuxconf">Tmux Shortcuts to use (based on my <code class="language-plaintext highlighter-rouge">~/.tmux.conf</code>)</h1>

<ol>
  <li>Start new with session name: <code class="language-plaintext highlighter-rouge">tmux new -s myname</code></li>
  <li>Attach to the named session: <code class="language-plaintext highlighter-rouge">tmux a -t myname</code></li>
  <li>List sessions: <code class="language-plaintext highlighter-rouge">tmuz ls</code></li>
  <li>Kill session: <code class="language-plaintext highlighter-rouge">tmux kill-session -t myname</code></li>
  <li>Prefix I use: <code class="language-plaintext highlighter-rouge">C-a</code>
    <ul>
      <li>Shortcuts with Windows:</li>
    </ul>
    <ul>
      <li>Create window:<code class="language-plaintext highlighter-rouge">c</code></li>
      <li>List Windows: <code class="language-plaintext highlighter-rouge">w</code></li>
      <li>Next Window: <code class="language-plaintext highlighter-rouge">n</code></li>
      <li>Previous Window: <code class="language-plaintext highlighter-rouge">p</code></li>
      <li>Find Window: <code class="language-plaintext highlighter-rouge">f</code></li>
      <li>Rename Window: <code class="language-plaintext highlighter-rouge">,</code></li>
      <li>Kill Window: <code class="language-plaintext highlighter-rouge">&amp;</code></li>
    </ul>
  </li>
</ol>

<ul>
  <li>Shortcuts with panes:
    <ul>
      <li>Vertical Split: <code class="language-plaintext highlighter-rouge">|</code></li>
      <li>Horizontal Split: <code class="language-plaintext highlighter-rouge">_</code></li>
    </ul>
  </li>
  <li>Switch Panes with <code class="language-plaintext highlighter-rouge">Alt+ &lt;arror_keys&gt;</code></li>
  <li>Incse you face incompatiblity with agnoster theme and tmux, use <code class="language-plaintext highlighter-rouge">tmux -u</code> to intiate tmux</li>
</ul>

<h2 id="resources">Resources</h2>
<ol>
  <li>https://gist.github.com/MohamedAlaa/2961058</li>
  <li>https://tmuxcheatsheet.com/</li>
  <li>https://www.hamvocke.com/blog/a-guide-to-customizing-your-tmux-conf/</li>
  <li>https://github.com/gpakosz/.tmux/blob/master/.tmux.conf</li>
</ol>]]></content><author><name>Tejal Barnwal</name><email>tejalbarnwal@gmail.com</email></author><category term="engineering" /><category term="Software Development" /><summary type="html"><![CDATA[Everyday key shortcuts that I use]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://tejalbarnwal.github.io/imgs/thumbs/sublime-tmux-shortcuts.svg" /><media:content medium="image" url="https://tejalbarnwal.github.io/imgs/thumbs/sublime-tmux-shortcuts.svg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">उलझन</title><link href="https://tejalbarnwal.github.io/blog/kashmakash" rel="alternate" type="text/html" title="उलझन" /><published>2020-12-23T00:00:00+00:00</published><updated>2020-12-23T00:00:00+00:00</updated><id>https://tejalbarnwal.github.io/blog/kashmakash</id><content type="html" xml:base="https://tejalbarnwal.github.io/blog/kashmakash"><![CDATA[<p>This poem, written after a difficult experience explores self-awareness, emotional vulnerability, and the acceptance of imperfections. It quietly touches on the internal conflict of emotions and the inevitability of self-doubt.</p>

<p><img src="../imgs/poem_1.png" alt="drawing" width="500" /></p>]]></content><author><name>Tejal Barnwal</name><email>tejalbarnwal@gmail.com</email></author><category term="poetry" /><category term="Poetry" /><summary type="html"><![CDATA[This poem quietly touches on the internal conflict of emotions and the inevitability of self-doubt]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://tejalbarnwal.github.io/imgs/poems/uljhan.webp" /><media:content medium="image" url="https://tejalbarnwal.github.io/imgs/poems/uljhan.webp" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Shadow Drums</title><link href="https://tejalbarnwal.github.io/blog/shadowdrums" rel="alternate" type="text/html" title="Shadow Drums" /><published>2019-11-20T00:00:00+00:00</published><updated>2019-11-20T00:00:00+00:00</updated><id>https://tejalbarnwal.github.io/blog/shadowdrums</id><content type="html" xml:base="https://tejalbarnwal.github.io/blog/shadowdrums"><![CDATA[]]></content><author><name>Tejal Barnwal</name><email>tejalbarnwal@gmail.com</email></author><category term="engineering" /><category term="Documentation" /><summary type="html"><![CDATA[Using LDR with Arduino]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://tejalbarnwal.github.io/imgs/thumbs/shadow-drums.webp" /><media:content medium="image" url="https://tejalbarnwal.github.io/imgs/thumbs/shadow-drums.webp" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">पाकर खोना</title><link href="https://tejalbarnwal.github.io/blog/paakarkhona" rel="alternate" type="text/html" title="पाकर खोना" /><published>2019-02-28T00:00:00+00:00</published><updated>2019-02-28T00:00:00+00:00</updated><id>https://tejalbarnwal.github.io/blog/paakarkhona</id><content type="html" xml:base="https://tejalbarnwal.github.io/blog/paakarkhona"><![CDATA[<p>I wrote this poem on National Science Day, 28th February 2019, during my JEE preparation days. It reflects the internal conflict of getting swept into the race, even when I didn’t want to be part of the competition. Despite my reluctance, I found myself entangled in the very system I wished to avoid. The poem captures that journey—of struggling, adapting, and sometimes losing parts of myself along the way.</p>

<p>P.S.: <em>Favourite uncles</em> refers to the scientists who’ve always inspired me with their unique perspectives on life—like Feynman uncle, whose curiosity and enthusiasm for discovery and for life never fails to amaze me!</p>

<div class="poem-pair">

  <figure class="poem-art">
  <img src="/imgs/poems/paakar-khona.webp" alt="At the Crossroads of Curiosity: a girl sits on a hill where the path splits, one way toward a crowd marching to a school under a clock and a trophy, the other toward stars, atoms, books and a robot arm" loading="lazy" />
</figure>

  <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>एक राह मैंने चुनी थी
उस राह पर मैं चली भी
कहीं गिर गई, कहीं भटक गई, कहीं संघर्ष की
ना जाने कैसे, ना चाहते हुए भी मैं उसमें घुल गई

अब फिल्में देखते, गाने सुनते, YouTube पर
खुद को ज़्यादा पाती हूँ
कभी जिससे दूर का रिश्ता रखा करती थी
ना जाने कैसे आज उन्हें अपने क़रीब पाती हूँ

खुद को पढ़ाना, खुद से सीखना
देर बाद, भूल गई अपने आप से मिलना
Favourite uncles भी सिर्फ़ कभी-कभी याद आते
ना जाने कैसे मैं भूल गई उनकी बातें

जिज्ञासा दिल में, दिल से करने वाली चीज़ों में
आज भी वही लौ जल रही है
सवाल ही सवाल, जवाब नहीं है अब
ना जाने ज़िंदगी जीने की परिभाषा बदल गई कब

खुद को कई बार पाया भी और खोया भी
ढूंढ़ा भी, थोंपा भी और समझाया भी
पर क्यों मैं समझ नहीं पा रही
ना जाने क्यों, कहाँ, कैसे हो गई हूँ मैं ऐसे!

</code></pre></div>  </div>

</div>]]></content><author><name>Tejal Barnwal</name><email>tejalbarnwal@gmail.com</email></author><category term="poetry" /><category term="Poetry" /><summary type="html"><![CDATA[I wrote this poem on National Science Day, 28th February 2019, during my JEE preparation days. It reflects the internal conflict of getting swept into the race, even when I didn’t want to be part of the competition. Despite my reluctance, I found myself entangled in the very system I wished to avoid. The poem captures that journey—of struggling, adapting, and sometimes losing parts of myself along the way.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://tejalbarnwal.github.io/imgs/poems/paakar-khona.webp" /><media:content medium="image" url="https://tejalbarnwal.github.io/imgs/poems/paakar-khona.webp" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">चलते चलो</title><link href="https://tejalbarnwal.github.io/blog/haarnamanna" rel="alternate" type="text/html" title="चलते चलो" /><published>2012-02-01T00:00:00+00:00</published><updated>2012-02-01T00:00:00+00:00</updated><id>https://tejalbarnwal.github.io/blog/haarnamanna</id><content type="html" xml:base="https://tejalbarnwal.github.io/blog/haarnamanna"><![CDATA[<p>This is a poem I wrote in fifth grade. Back then, I didn’t fully grasp the message it conveys, but today I realize its significance! Please forgive the innocent mistakes I made. :p</p>

<div class="poem-pair">

  <figure class="poem-art">
  <img src="/imgs/poems/chalte-chalo.webp" alt="Perseverance Beneath a Golden Sunrise: a schoolgirl with a backpack climbs a rocky path toward a sunrise and a signpost, leaving storm clouds, thorns and a broken trophy behind" loading="lazy" />
</figure>

  <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>हार न मानना तुम कभी
बस चलते चलो, चलते चलो

दुःख आएंगे आने दो
मुसीबत को आसान करो
हार न मानना तुम कभी
बस चलते चलो, चलते चलो

हार गए तो क्या हुआ
आगे बढ़ते चलो
सर झुकाना मत कभी
आगे बढ़ते चलो

हार न मानना तुम कभी
बस चलते चलो, चलते चलो
</code></pre></div>  </div>

</div>]]></content><author><name>Tejal Barnwal</name><email>tejalbarnwal@gmail.com</email></author><category term="poetry" /><category term="Poetry" /><summary type="html"><![CDATA[This is a poem I wrote in fifth grade. Back then, I didn’t fully grasp the message it conveys, but today I realize its significance! Please forgive the innocent mistakes I made. :p]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://tejalbarnwal.github.io/imgs/poems/chalte-chalo.webp" /><media:content medium="image" url="https://tejalbarnwal.github.io/imgs/poems/chalte-chalo.webp" xmlns:media="http://search.yahoo.com/mrss/" /></entry></feed>