This MathML example demontrates how to fit the ordinary least squares line fit to a set of 2D data points..
<!doctype html>
<html>
<head>
<title>XoaX.net's MathML: Ordinary Least Squares Line</title>
<style>
th, td {
text-align: center;
border:1px black solid;
padding:5px;
}
table {
background-color:white;
border:1px black solid;
border-spacing: 10px;
border-collapse:collapse;
}
</style>
</head>
<body>
<ul>
<li>To use ordinary least squares to find the line <math><mi>y</mi><mo>=</mo><mi>m</mi><mo>⁢</mo><mi>x</mi><mo>+</mo><mi>b</mi></math> that minimizes the distance in the y coordinate<ul>
<li>We begin with a set of n points given by </li>
<math>
<mrow>
<mtext>(</mtext><msub><mi>x</mi><mn>0</mn></msub><mo>,</mo><msub><mi>y</mi><mn>0</mn></msub><mtext>)</mtext><mo>,</mo>
<mtext>(</mtext><msub><mi>x</mi><mn>1</mn></msub><mo>,</mo><msub><mi>y</mi><mn>1</mn></msub><mtext>)</mtext><mo>,</mo>
<mtext>(</mtext><msub><mi>x</mi><mn>2</mn></msub><mo>,</mo><msub><mi>y</mi><mn>2</mn></msub><mtext>)</mtext>
<mo>,</mo><mo>…</mo><mo>,</mo>
<mtext>(</mtext><msub><mi>x</mi><mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow></msub><mo>,</mo>
<msub><mi>y</mi><mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow></msub><mtext>)</mtext>
</mrow>
</math>
<li>To find the least squares line, we want to minimize this function</li>
<math>
<mi>F</mi><mtext>(</mtext><mi>m</mi><mo>,</mo><mi>b</mi><mtext>)</mtext><mo>=</mo>
<munderover>
<mo>∑</mo>
<mrow>
<mi>i</mi><mo>=</mo><mn>0</mn>
</mrow>
<mrow>
<mi>n</mi><mo>-</mo><mn>1</mn>
</mrow>
</munderover>
<msup>
<mrow><mtext>[</mtext>
<msub><mi>y</mi><mi>i</mi></msub><mo>-</mo>
<mrow><mtext>(</mtext><mi>m</mi><msub><mi>x</mi><mi>i</mi></msub><mo>+</mo><mi>b</mi><mtext>)</mtext></mrow>
<mtext>]</mtext></mrow><mn>2</mn>
</msup>
</math>
<li>To minimize the function, we need the partial derivatives to be zero.</li>
<math>
<mtable>
<mtr>
<mtd><mfrac><mo>∂</mo><mrow><mo>∂</mo><mi>b</mi></mrow></mfrac>
<mi>F</mi><mtext>(</mtext><mi>m</mi><mo>,</mo><mi>b</mi><mtext>)</mtext></mtd>
<mtd>
<mo>=</mo></mtd>
<mtd>
<mo>-</mo>
<mn>2</mn>
<munderover>
<mo>∑</mo>
<mrow>
<mi>i</mi><mo>=</mo><mn>0</mn>
</mrow>
<mrow>
<mi>n</mi><mo>-</mo><mn>1</mn>
</mrow>
</munderover>
<mrow><mtext>[</mtext>
<msub><mi>y</mi><mi>i</mi></msub><mo>-</mo>
<mrow><mtext>(</mtext><mi>m</mi><msub><mi>x</mi><mi>i</mi></msub><mo>+</mo><mi>b</mi><mtext>)</mtext></mrow>
<mtext>]</mtext></mrow>
</mtd>
</mtr>
<mtr>
<mtd><mfrac><mo>∂</mo><mrow><mo>∂</mo><mi>m</mi></mrow></mfrac>
<mi>F</mi><mtext>(</mtext><mi>m</mi><mo>,</mo><mi>b</mi><mtext>)</mtext></mtd>
<mtd><mo>=</mo></mtd>
<mtd>
<mo>-</mo>
<mn>2</mn>
<munderover>
<mo>∑</mo>
<mrow>
<mi>i</mi><mo>=</mo><mn>0</mn>
</mrow>
<mrow>
<mi>n</mi><mo>-</mo><mn>1</mn>
</mrow>
</munderover>
<msub><mi>x</mi><mi>i</mi></msub>
<mrow><mtext>[</mtext>
<msub><mi>y</mi><mi>i</mi></msub><mo>-</mo>
<mrow><mtext>(</mtext><mi>m</mi><msub><mi>x</mi><mi>i</mi></msub><mo>+</mo><mi>b</mi><mtext>)</mtext></mrow>
<mtext>]</mtext></mrow>
</mtd>
</mtr>
</mtable>
</math>
<li>This can be rewritten as the following system of equations</li>
<math>
<mtable>
<mtr>
<mtd>
<mi>b</mi><mo>⁢</mo><mi>n</mi>
<mo>+</mo>
<mi>m</mi><mo>⁢</mo>
<munderover>
<mo>∑</mo>
<mrow>
<mi>i</mi><mo>=</mo><mn>0</mn>
</mrow>
<mrow>
<mi>n</mi><mo>-</mo><mn>1</mn>
</mrow>
</munderover>
<msub><mi>x</mi><mi>i</mi></msub>
</mtd>
<mtd><mo>=</mo></mtd>
<mtd>
<munderover>
<mo>∑</mo>
<mrow>
<mi>i</mi><mo>=</mo><mn>0</mn>
</mrow>
<mrow>
<mi>n</mi><mo>-</mo><mn>1</mn>
</mrow>
</munderover>
<msub><mi>y</mi><mi>i</mi></msub>
</mtd>
</mtr>
<mtr>
<mtd>
<mi>b</mi><mo>⁢</mo>
<munderover>
<mo>∑</mo>
<mrow>
<mi>i</mi><mo>=</mo><mn>0</mn>
</mrow>
<mrow>
<mi>n</mi><mo>-</mo><mn>1</mn>
</mrow>
</munderover>
<msub><mi>x</mi><mi>i</mi></msub>
<mo>+</mo>
<mi>m</mi><mo>⁢</mo>
<munderover>
<mo>∑</mo>
<mrow>
<mi>i</mi><mo>=</mo><mn>0</mn>
</mrow>
<mrow>
<mi>n</mi><mo>-</mo><mn>1</mn>
</mrow>
</munderover>
<msup><msub><mi>x</mi><mi>i</mi></msub><mn>2</mn></msup>
</mtd>
<mtd><mo>=</mo></mtd>
<mtd>
<munderover>
<mo>∑</mo>
<mrow>
<mi>i</mi><mo>=</mo><mn>0</mn>
</mrow>
<mrow>
<mi>n</mi><mo>-</mo><mn>1</mn>
</mrow>
</munderover>
<msub><mi>x</mi><mi>i</mi></msub>
<msub><mi>y</mi><mi>i</mi></msub>
</mtd>
</mtr>
</mtable>
</math>
<li>Writing this as a matrix equation, we have</li>
<math>
<mo>[</mo>
<mtable>
<mtr>
<mtd><msub><mi>a</mi><mrow><mn>1</mn><mn>1</mn></mrow></msub></mtd>
<mtd><msub><mi>a</mi><mrow><mn>1</mn><mn>2</mn></mrow></msub></mtd>
</mtr>
<mtr>
<mtd><msub><mi>a</mi><mrow><mn>2</mn><mn>1</mn></mrow></msub></mtd>
<mtd><msub><mi>a</mi><mrow><mn>2</mn><mn>2</mn></mrow></msub></mtd>
</mtr>
</mtable>
<mo>]</mo>
<mo>[</mo>
<mtable>
<mtr><mtd><mi>b</mi></mtd></mtr>
<mtr><mtd><mi>m</mi></mtd></mtr>
</mtable>
<mo>]</mo>
<mo>=</mo>
<mo>[</mo>
<mtable>
<mtr><mtd><msub><mi>c</mi><mn>1</mn></msub></mtd></mtr>
<mtr><mtd><msub><mi>c</mi><mn>2</mn></msub></mtd></mtr>
</mtable>
<mo>]</mo>
</math>
<div>where the labels correspond to the values in the equations (i.e.
<math>
<msub><mi>a</mi><mrow><mn>1</mn><mn>1</mn></mrow></msub>
<mo>=</mo><mi>n</mi>
</math>)</div>
<li>The solution is given by finding the inverse</li>
<math>
<mo>[</mo>
<mtable>
<mtr><mtd><mi>b</mi></mtd></mtr>
<mtr><mtd><mi>m</mi></mtd></mtr>
</mtable>
<mo>]</mo>
<mo>=</mo>
<mfrac>
<mn>1</mn>
<mrow>
<msub><mi>a</mi><mrow><mn>1</mn><mn>1</mn></mrow></msub>
<msub><mi>a</mi><mrow><mn>2</mn><mn>2</mn></mrow></msub>
<mo>-</mo>
<msub><mi>a</mi><mrow><mn>1</mn><mn>2</mn></mrow></msub>
<msub><mi>a</mi><mrow><mn>1</mn><mn>2</mn></mrow></msub>
</mrow>
</mfrac>
<mo>[</mo>
<mtable>
<mtr>
<mtd><msub><mi>a</mi><mrow><mn>2</mn><mn>2</mn></mrow></msub></mtd>
<mtd><mo>-</mo><msub><mi>a</mi><mrow><mn>1</mn><mn>2</mn></mrow></msub></mtd>
</mtr>
<mtr>
<mtd><mo>-</mo><msub><mi>a</mi><mrow><mn>2</mn><mn>1</mn></mrow></msub></mtd>
<mtd><msub><mi>a</mi><mrow><mn>1</mn><mn>1</mn></mrow></msub></mtd>
</mtr>
</mtable>
<mo>]</mo>
<mo>[</mo>
<mtable>
<mtr><mtd><msub><mi>c</mi><mn>1</mn></msub></mtd></mtr>
<mtr><mtd><msub><mi>c</mi><mn>2</mn></msub></mtd></mtr>
</mtable>
<mo>]</mo>
</math>
<li>This gives the solution</li>
<math>
<mtable>
<mtr>
<mtd><mi>b</mi></mtd>
<mtd><mo>=</mo></mtd>
<mtd><mfrac>
<mrow>
<msub><mi>a</mi><mrow><mn>2</mn><mn>2</mn></mrow></msub>
<msub><mi>c</mi><mn>1</mn></msub>
<mo>-</mo>
<msub><mi>a</mi><mrow><mn>1</mn><mn>2</mn></mrow></msub>
<msub><mi>c</mi><mn>2</mn></msub>
</mrow>
<mrow>
<msub><mi>a</mi><mrow><mn>1</mn><mn>1</mn></mrow></msub>
<msub><mi>a</mi><mrow><mn>2</mn><mn>2</mn></mrow></msub>
<mo>-</mo>
<msub><mi>a</mi><mrow><mn>1</mn><mn>2</mn></mrow></msub>
<msub><mi>a</mi><mrow><mn>1</mn><mn>2</mn></mrow></msub>
</mrow>
</mfrac></mtd>
<mtd><mo>=</mo></mtd>
<mtd><mfrac>
<mrow>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msup><msub><mi>x</mi><mi>i</mi></msub><mn>2</mn></msup>
<mo>⁢</mo>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msub><mi>y</mi><mi>i</mi></msub>
<mo>-</mo>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msub><mi>x</mi><mi>i</mi></msub>
<mo>⁢</mo>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msub><mi>x</mi><mi>i</mi></msub><msub><mi>y</mi><mi>i</mi></msub>
</mrow>
<mrow>
<mi>n</mi><mo>⁢</mo>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msup><msub><mi>x</mi><mi>i</mi></msub><mn>2</mn></msup>
<mo>-</mo>
<msup><mrow><mo>(</mo><munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msub><mi>x</mi><mi>i</mi></msub><mo>)</mo></mrow><mn>2</mn></msup>
</mrow>
</mfrac></mtd>
</mtr>
<mtr>
<mtd><mi>m</mi></mtd>
<mtd><mo>=</mo></mtd>
<mtd><mfrac>
<mrow>
<msub><mi>a</mi><mrow><mn>1</mn><mn>1</mn></mrow></msub><mo>⁢</mo>
<msub><mi>c</mi><mn>2</mn></msub>
<mo>-</mo>
<msub><mi>a</mi><mrow><mn>2</mn><mn>1</mn></mrow></msub><mo>⁢</mo>
<msub><mi>c</mi><mn>1</mn></msub>
</mrow>
<mrow>
<msub><mi>a</mi><mrow><mn>1</mn><mn>1</mn></mrow></msub><mo>⁢</mo>
<msub><mi>a</mi><mrow><mn>2</mn><mn>2</mn></mrow></msub>
<mo>-</mo>
<msub><mi>a</mi><mrow><mn>1</mn><mn>2</mn></mrow></msub><mo>⁢</mo>
<msub><mi>a</mi><mrow><mn>1</mn><mn>2</mn></mrow></msub>
</mrow>
</mfrac></mtd>
<mtd><mo>=</mo></mtd>
<mtd><mfrac>
<mrow>
<mi>n</mi><mo>⁢</mo>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msub><mi>x</mi><mi>i</mi></msub><msub><mi>y</mi><mi>i</mi></msub>
<mo>-</mo>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msub><mi>x</mi><mi>i</mi></msub>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msub><mi>y</mi><mi>i</mi></msub>
</mrow>
<mrow>
<mi>n</mi><mo>⁢</mo>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msup><msub><mi>x</mi><mi>i</mi></msub><mn>2</mn></msup>
<mo>-</mo>
<msup><mrow><mo>(</mo><munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msub><mi>x</mi><mi>i</mi></msub><mo>)</mo></mrow><mn>2</mn></msup>
</mrow>
</mfrac></mtd>
</mtr>
</mtable>
</math>
<li>For the homogeneous equation
<math>
<mi>A</mi><mo>⁢</mo><mi>x</mi><mo>+</mo>
<mi>B</mi><mo>⁢</mo><mi>y</mi><mo>+</mo>
<mi>C</mi><mo>=</mo><mn>0</mn></math>,
we have</li>
<math>
<mtable>
<mtr><mtd><mi>A</mi></mtd><mtd><mo>=</mo></mtd>
<mtd>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msub><mi>x</mi><mi>i</mi></msub>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msub><mi>y</mi><mi>i</mi></msub>
<mo>-</mo>
<mi>n</mi><mo>⁢</mo>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover>
<msub><mi>x</mi><mi>i</mi></msub><msub><mi>y</mi><mi>i</mi></msub>
</mtd>
</mtr>
<mtr><mtd><mi>B</mi></mtd><mtd><mo>=</mo></mtd>
<mtd>
<mi>n</mi><mo>⁢</mo>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msup><msub><mi>x</mi><mi>i</mi></msub><mn>2</mn></msup>
<mo>-</mo>
<msup><mrow><mo>(</mo><munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msub><mi>x</mi><mi>i</mi></msub><mo>)</mo></mrow><mn>2</mn></msup>
</mtd>
</mtr>
<mtr><mtd><mi>C</mi></mtd><mtd><mo>=</mo></mtd>
<mtd>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msub><mi>x</mi><mi>i</mi></msub>
<mo>⁢</mo>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msub><mi>x</mi><mi>i</mi></msub><msub><mi>y</mi><mi>i</mi></msub>
<mo>-</mo>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msup><msub><mi>x</mi><mi>i</mi></msub><mn>2</mn></msup>
<mo>⁢</mo>
<munderover>
<mo>∑</mo>
<mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow>
<mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow>
</munderover><msub><mi>y</mi><mi>i</mi></msub>
</mtd>
</mtr>
</mtable>
</math>
<li>Using vector notation and setting
<math>
<mover><mn>1</mn><mo>→</mo></mover><mo>=</mo>
<mtext>(</mtext><mn>1</mn><mo>,</mo><mn>1</mn><mo>,</mo><mo>…</mo><mn>1</mn><mtext>)</mtext>
</math>, we have
</li>
<math>
<mtable>
<mtr>
<mtd><mi>A</mi></mtd><mtd><mo>=</mo></mtd>
<mtd>
<mo>(</mo><mover><mn>1</mn><mo>→</mo></mover><mo>⋅</mo><mover><mi>x</mi><mo>→</mo></mover></mover><mo>)</mo><mo>⁢</mo>
<mo>(</mo><mover><mn>1</mn><mo>→</mo></mover><mo>⋅</mo><mover><mi>y</mi><mo>→</mo></mover><mo>)</mo><mo>-</mo>
<mo>(</mo><mover><mn>1</mn><mo>→</mo></mover><mo>⋅</mo><mover><mi>1</mi><mo>→</mo></mover></mover><mo>)</mo><mo>⁢</mo>
<mo>(</mo><mover><mn>x</mn><mo>→</mo></mover><mo>⋅</mo><mover><mi>y</mi><mo>→</mo></mover><mo>)</mo>
</mtd>
</mtr>
<mtr>
<mtd><mi>B</mi></mtd><mtd><mo>=</mo></mtd>
<mtd>
<mo>(</mo><mover><mn>1</mn><mo>→</mo></mover><mo>⋅</mo><mover><mi>1</mi><mo>→</mo></mover></mover><mo>)</mo><mo>⁢</mo>
<mo>(</mo><mover><mn>x</mn><mo>→</mo></mover><mo>⋅</mo><mover><mi>x</mi><mo>→</mo></mover><mo>)</mo><mo>-</mo>
<mo>(</mo><mover><mn>1</mn><mo>→</mo></mover><mo>⋅</mo><mover><mi>x</mi><mo>→</mo></mover></mover><mo>)</mo><mo>⁢</mo>
<mo>(</mo><mover><mn>1</mn><mo>→</mo></mover><mo>⋅</mo><mover><mi>x</mi><mo>→</mo></mover><mo>)</mo>
</mtd>
</mtr>
<mtr>
<mtd><mi>C</mi></mtd><mtd><mo>=</mo></mtd>
<mtd>
<mo>(</mo><mover><mn>1</mn><mo>→</mo></mover><mo>⋅</mo><mover><mi>x</mi><mo>→</mo></mover></mover><mo>)</mo><mo>⁢</mo>
<mo>(</mo><mover><mn>x</mn><mo>→</mo></mover><mo>⋅</mo><mover><mi>y</mi><mo>→</mo></mover><mo>)</mo><mo>-</mo>
<mo>(</mo><mover><mn>x</mn><mo>→</mo></mover><mo>⋅</mo><mover><mi>x</mi><mo>→</mo></mover></mover><mo>)</mo><mo>⁢</mo>
<mo>(</mo><mover><mn>1</mn><mo>→</mo></mover><mo>⋅</mo><mover><mi>y</mi><mo>→</mo></mover><mo>)</mo>
</mtd>
</mtr>
</mtable>
</math>
</ul></li>
</ul>
</body>
</html>© 20072025 XoaX.net LLC. All rights reserved.