Polynomial_regression.md 21 KB

Polynomial regression

We'll use a Clifford algebra library. Very slow.

Rationale (in French for some reason):

Le système d'équations peut s'écrire :

<mrow class="MJX-TeXAtom-ORD">
  <mstyle displaystyle="true" scriptlevel="0">
    <msub>
      <mrow>
        <mo>(</mo>
        <mrow>
          <mi>a</mi>
          <mo>+</mo>
          <mi>b</mi>
          <msub>
            <mi>x</mi>
            <mrow class="MJX-TeXAtom-ORD">
              <mi>i</mi>
            </mrow>
          </msub>
          <mo>+</mo>
          <mi>c</mi>
          <msubsup>
            <mi>x</mi>
            <mrow class="MJX-TeXAtom-ORD">
              <mi>i</mi>
            </mrow>
            <mrow class="MJX-TeXAtom-ORD">
              <mn>2</mn>
            </mrow>
          </msubsup>
          <mo>=</mo>
          <msub>
            <mi>y</mi>
            <mrow class="MJX-TeXAtom-ORD">
              <mi>i</mi>
            </mrow>
          </msub>
        </mrow>
        <mo>)</mo>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
        <mo>=</mo>
        <mn>1</mn>
        <mo>&#x2026;<!-- … --></mo>
        <mi>N</mi>
      </mrow>
    </msub>
  </mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle \left(a+bx\_{i}+cx\_{i}^{2}=y\_{i}\right)\_{i=1\ldots N}}</annotation>

, où on cherche
<mrow class="MJX-TeXAtom-ORD">
  <mstyle displaystyle="true" scriptlevel="0">
    <mo stretchy="false">(</mo>
    <mi>a</mi>
    <mo>,</mo>
    <mi>b</mi>
    <mo>,</mo>
    <mi>c</mi>
    <mo stretchy="false">)</mo>
    <mo>&#x2208;<!-- ∈ --></mo>
    <msup>
      <mrow class="MJX-TeXAtom-ORD">
        <mi mathvariant="double-struck">R</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mn>3</mn>
      </mrow>
    </msup>
  </mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle (a,b,c)\in \mathbb {R} ^{3}}</annotation>

. On considère
<mrow class="MJX-TeXAtom-ORD">
  <mstyle displaystyle="true" scriptlevel="0">
    <msup>
      <mrow class="MJX-TeXAtom-ORD">
        <mi mathvariant="double-struck">R</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>N</mi>
      </mrow>
    </msup>
  </mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle \mathbb {R} ^{N}}</annotation>

et on répartit chaque équation sur chaque dimension:

<mrow class="MJX-TeXAtom-ORD">
  <mstyle displaystyle="true" scriptlevel="0">
    <mo stretchy="false">(</mo>
    <mi>a</mi>
    <mo>+</mo>
    <mi>b</mi>
    <msub>
      <mi>x</mi>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
      </mrow>
    </msub>
    <mo>+</mo>
    <mi>c</mi>
    <msubsup>
      <mi>x</mi>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mn>2</mn>
      </mrow>
    </msubsup>
    <mo stretchy="false">)</mo>
    <msub>
      <mrow class="MJX-TeXAtom-ORD">
        <mi mathvariant="bold">e</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
      </mrow>
    </msub>
    <mo>=</mo>
    <msub>
      <mi>y</mi>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
      </mrow>
    </msub>
    <msub>
      <mrow class="MJX-TeXAtom-ORD">
        <mi mathvariant="bold">e</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
      </mrow>
    </msub>
  </mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle (a+bx\_{i}+cx\_{i}^{2})\mathbf {e} \_{i}=y\_{i}\mathbf {e} \_{i}}</annotation>

Posons alors :

<mrow class="MJX-TeXAtom-ORD">
  <mstyle displaystyle="true" scriptlevel="0">
    <msub>
      <mrow class="MJX-TeXAtom-ORD">
        <mi mathvariant="bold">x</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mn>0</mn>
      </mrow>
    </msub>
    <mo>=</mo>
    <munderover>
      <mo>&#x2211;<!-- ∑ --></mo>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
        <mo>=</mo>
        <mn>1</mn>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>N</mi>
      </mrow>
    </munderover>
    <msub>
      <mrow class="MJX-TeXAtom-ORD">
        <mi mathvariant="bold">e</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
      </mrow>
    </msub>
    <mo>,</mo>
    <mspace width="thinmathspace" />
    <msub>
      <mrow class="MJX-TeXAtom-ORD">
        <mi mathvariant="bold">x</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mn>1</mn>
      </mrow>
    </msub>
    <mo>=</mo>
    <munderover>
      <mo>&#x2211;<!-- ∑ --></mo>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
        <mo>=</mo>
        <mn>1</mn>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>N</mi>
      </mrow>
    </munderover>
    <msub>
      <mi>x</mi>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
      </mrow>
    </msub>
    <msub>
      <mrow class="MJX-TeXAtom-ORD">
        <mi mathvariant="bold">e</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
      </mrow>
    </msub>
    <mo>,</mo>
    <mspace width="thinmathspace" />
    <msub>
      <mrow class="MJX-TeXAtom-ORD">
        <mi mathvariant="bold">x</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mn>2</mn>
      </mrow>
    </msub>
    <mo>=</mo>
    <munderover>
      <mo>&#x2211;<!-- ∑ --></mo>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
        <mo>=</mo>
        <mn>1</mn>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>N</mi>
      </mrow>
    </munderover>
    <msubsup>
      <mi>x</mi>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mn>2</mn>
      </mrow>
    </msubsup>
    <msub>
      <mrow class="MJX-TeXAtom-ORD">
        <mi mathvariant="bold">e</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
      </mrow>
    </msub>
    <mo>,</mo>
    <mspace width="thinmathspace" />
    <mrow class="MJX-TeXAtom-ORD">
      <mi mathvariant="bold">y</mi>
    </mrow>
    <mo>=</mo>
    <munderover>
      <mo>&#x2211;<!-- ∑ --></mo>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
        <mo>=</mo>
        <mn>1</mn>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>N</mi>
      </mrow>
    </munderover>
    <msub>
      <mi>y</mi>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
      </mrow>
    </msub>
    <msub>
      <mrow class="MJX-TeXAtom-ORD">
        <mi mathvariant="bold">e</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mi>i</mi>
      </mrow>
    </msub>
  </mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle \mathbf {x} \_{0}=\sum \_{i=1}^{N}\mathbf {e} \_{i},\,\mathbf {x} \_{1}=\sum \_{i=1}^{N}x\_{i}\mathbf {e} \_{i},\,\mathbf {x} \_{2}=\sum \_{i=1}^{N}x\_{i}^{2}\mathbf {e} \_{i},\,\mathbf {y} =\sum \_{i=1}^{N}y\_{i}\mathbf {e} \_{i}}</annotation>

Le système d'équations devient :

<mrow class="MJX-TeXAtom-ORD">
  <mstyle displaystyle="true" scriptlevel="0">
    <mi>a</mi>
    <msub>
      <mrow class="MJX-TeXAtom-ORD">
        <mi mathvariant="bold">x</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mn>0</mn>
      </mrow>
    </msub>
    <mo>+</mo>
    <mi>b</mi>
    <msub>
      <mrow class="MJX-TeXAtom-ORD">
        <mi mathvariant="bold">x</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mn>1</mn>
      </mrow>
    </msub>
    <mo>+</mo>
    <mi>c</mi>
    <msub>
      <mrow class="MJX-TeXAtom-ORD">
        <mi mathvariant="bold">x</mi>
      </mrow>
      <mrow class="MJX-TeXAtom-ORD">
        <mn>2</mn>
      </mrow>
    </msub>
    <mo>=</mo>
    <mrow class="MJX-TeXAtom-ORD">
      <mi mathvariant="bold">y</mi>
    </mrow>
  </mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle a\mathbf {x} \_{0}+b\mathbf {x} \_{1}+c\mathbf {x} \_{2}=\mathbf {y} }</annotation>

.

D'où :

<mrow class="MJX-TeXAtom-ORD">
  <mstyle displaystyle="true" scriptlevel="0">
    <mrow class="MJX-TeXAtom-ORD">
      <mtable columnalign="right left right left right left right left right left right left" rowspacing="3pt" columnspacing="0em 2em 0em 2em 0em 2em 0em 2em 0em 2em 0em" displaystyle="true">
        <mtr>
          <mtd>
            <mi>a</mi>
            <mo>=</mo>
            <mrow class="MJX-TeXAtom-ORD">
              <mi mathvariant="bold">y</mi>
            </mrow>
            <mo>&#x2227;<!-- ∧ --></mo>
            <msub>
              <mrow class="MJX-TeXAtom-ORD">
                <mi mathvariant="bold">x</mi>
              </mrow>
              <mrow class="MJX-TeXAtom-ORD">
                <mn>1</mn>
              </mrow>
            </msub>
            <mo>&#x2227;<!-- ∧ --></mo>
            <msub>
              <mrow class="MJX-TeXAtom-ORD">
                <mi mathvariant="bold">x</mi>
              </mrow>
              <mrow class="MJX-TeXAtom-ORD">
                <mn>2</mn>
              </mrow>
            </msub>
            <mrow class="MJX-TeXAtom-ORD">
              <mo>/</mo>
            </mrow>
            <mo stretchy="false">(</mo>
            <msub>
              <mrow class="MJX-TeXAtom-ORD">
                <mi mathvariant="bold">x</mi>
              </mrow>
              <mrow class="MJX-TeXAtom-ORD">
                <mn>0</mn>
              </mrow>
            </msub>
            <mo>&#x2227;<!-- ∧ --></mo>
            <mrow class="MJX-TeXAtom-ORD">
              <msub>
                <mi mathvariant="bold">x</mi>
                <mrow class="MJX-TeXAtom-ORD">
                  <mn mathvariant="bold">1</mn>
                </mrow>
              </msub>
            </mrow>
            <mo>&#x2227;<!-- ∧ --></mo>
            <mrow class="MJX-TeXAtom-ORD">
              <msub>
                <mi mathvariant="bold">x</mi>
                <mrow class="MJX-TeXAtom-ORD">
                  <mn mathvariant="bold">2</mn>
                </mrow>
              </msub>
            </mrow>
            <mo stretchy="false">)</mo>
          </mtd>
        </mtr>
        <mtr>
          <mtd>
            <mi>b</mi>
            <mo>=</mo>
            <mrow class="MJX-TeXAtom-ORD">
              <mi mathvariant="bold">y</mi>
            </mrow>
            <mo>&#x2227;<!-- ∧ --></mo>
            <msub>
              <mrow class="MJX-TeXAtom-ORD">
                <mi mathvariant="bold">x</mi>
              </mrow>
              <mrow class="MJX-TeXAtom-ORD">
                <mn>2</mn>
              </mrow>
            </msub>
            <mo>&#x2227;<!-- ∧ --></mo>
            <msub>
              <mrow class="MJX-TeXAtom-ORD">
                <mi mathvariant="bold">x</mi>
              </mrow>
              <mrow class="MJX-TeXAtom-ORD">
                <mn>0</mn>
              </mrow>
            </msub>
            <mrow class="MJX-TeXAtom-ORD">
              <mo>/</mo>
            </mrow>
            <mo stretchy="false">(</mo>
            <msub>
              <mrow class="MJX-TeXAtom-ORD">
                <mi mathvariant="bold">x</mi>
              </mrow>
              <mrow class="MJX-TeXAtom-ORD">
                <mn>1</mn>
              </mrow>
            </msub>
            <mo>&#x2227;<!-- ∧ --></mo>
            <mrow class="MJX-TeXAtom-ORD">
              <msub>
                <mi mathvariant="bold">x</mi>
                <mrow class="MJX-TeXAtom-ORD">
                  <mn mathvariant="bold">2</mn>
                </mrow>
              </msub>
            </mrow>
            <mo>&#x2227;<!-- ∧ --></mo>
            <mrow class="MJX-TeXAtom-ORD">
              <msub>
                <mi mathvariant="bold">x</mi>
                <mrow class="MJX-TeXAtom-ORD">
                  <mn mathvariant="bold">0</mn>
                </mrow>
              </msub>
            </mrow>
            <mo stretchy="false">)</mo>
          </mtd>
        </mtr>
        <mtr>
          <mtd>
            <mi>c</mi>
            <mo>=</mo>
            <mrow class="MJX-TeXAtom-ORD">
              <mi mathvariant="bold">y</mi>
            </mrow>
            <mo>&#x2227;<!-- ∧ --></mo>
            <msub>
              <mrow class="MJX-TeXAtom-ORD">
                <mi mathvariant="bold">x</mi>
              </mrow>
              <mrow class="MJX-TeXAtom-ORD">
                <mn>0</mn>
              </mrow>
            </msub>
            <mo>&#x2227;<!-- ∧ --></mo>
            <msub>
              <mrow class="MJX-TeXAtom-ORD">
                <mi mathvariant="bold">x</mi>
              </mrow>
              <mrow class="MJX-TeXAtom-ORD">
                <mn>1</mn>
              </mrow>
            </msub>
            <mrow class="MJX-TeXAtom-ORD">
              <mo>/</mo>
            </mrow>
            <mo stretchy="false">(</mo>
            <msub>
              <mrow class="MJX-TeXAtom-ORD">
                <mi mathvariant="bold">x</mi>
              </mrow>
              <mrow class="MJX-TeXAtom-ORD">
                <mn>2</mn>
              </mrow>
            </msub>
            <mo>&#x2227;<!-- ∧ --></mo>
            <mrow class="MJX-TeXAtom-ORD">
              <msub>
                <mi mathvariant="bold">x</mi>
                <mrow class="MJX-TeXAtom-ORD">
                  <mn mathvariant="bold">0</mn>
                </mrow>
              </msub>
            </mrow>
            <mo>&#x2227;<!-- ∧ --></mo>
            <mrow class="MJX-TeXAtom-ORD">
              <msub>
                <mi mathvariant="bold">x</mi>
                <mrow class="MJX-TeXAtom-ORD">
                  <mn mathvariant="bold">1</mn>
                </mrow>
              </msub>
            </mrow>
            <mo stretchy="false">)</mo>
          </mtd>
        </mtr>
      </mtable>
    </mrow>
  </mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle {\begin{aligned}a=\mathbf {y} \land \mathbf {x} \_{1}\land \mathbf {x} \_{2}/(\mathbf {x} \_{0}\land \mathbf {x\_{1}} \land \mathbf {x\_{2}} )\\b=\mathbf {y} \land \mathbf {x} \_{2}\land \mathbf {x} \_{0}/(\mathbf {x} \_{1}\land \mathbf {x\_{2}} \land \mathbf {x\_{0}} )\\c=\mathbf {y} \land \mathbf {x} \_{0}\land \mathbf {x} \_{1}/(\mathbf {x} \_{2}\land \mathbf {x\_{0}} \land \mathbf {x\_{1}} )\\\end{aligned}}}</annotation>

use MultiVector;

constant @x1 = <0 1 2 3 4 5 6 7 8 9 10>;
constant @y = <1 6 17 34 57 86 121 162 209 262 321>;

constant $x0 = [+] @e[^@x1];
constant $x1 = [+] @x1 Z* @e;
constant $x2 = [+] @x1 »**» 2  Z* @e;

constant $y  = [+] @y Z* @e;

.say for
  $y∧$x1∧$x2/($x0∧$x1∧$x2),
  $y∧$x2∧$x0/($x1∧$x2∧$x0),
  $y∧$x0∧$x1/($x2∧$x0∧$x1);

Output:

1
2
3