BDS 761: Data Science and Machine Learning I


drawing

Topic 5: Regression - 2. Linear Algebra

Outline¶

  1. Multivariate Normal Distribution
  2. Regession, Linear Algebra style
  3. Multiple Regression
  4. Polynomial Regression

References¶

  • "An introduction to statistical learning: Python edition", G James, D Witten, T Hastie, R Tibshirani, J Taylor, Springer 2023. https://www.statlearning.com/
  • https://developers.google.com/machine-learning/crash-course/linear-regression

Multivariate Gaussian (for $n$ dimensions)¶

$$ f(\mathbf x) = \frac{1}{ \sqrt{(2 \pi)^n |\boldsymbol\Sigma|}} \exp \left(- \frac{1}{2} (\mathbf x - \boldsymbol \mu)^T \boldsymbol\Sigma^{-1} (\mathbf x - \boldsymbol \mu) \right) \text{, for } \mathbf x \in R^n $$

What is $(\mathbf x - \boldsymbol \mu)^T \boldsymbol\Sigma^{-1} (\mathbf x - \boldsymbol \mu)$?

In [5]:
def multivariate_normal(x, n, mean, cov):
    return (1./(np.sqrt((2*np.pi)**n * np.linalg.det(cov))) * np.exp(-1/2*(x - mean).T@np.linalg.inv(cov)@(x - mean)))


mean = np.array([35,70])
cov = 100*np.array([[1,.5],[.5,1]])
pic = np.zeros((100,100))
for x1 in np.arange(0,100):
    for x2 in np.arange(0,100):
        x = [x1,x2]
        pic[x1,x2] = multivariate_normal(x, 2, mean, cov)
        
plt.contour(pic);

Exercise¶

Generate 1000 2D random variables using numpy with mean (-1, 2) and standard deviations 3 for x and 1 for y

Compare their theoretical distribution to the scatterplot of points

Try covariances of 0, 0.5, and 0.9

Quiz: comprehension tests¶

Suppose we have a Gaussian random variable $ x \sim N(\mu = -1,\sigma^2 = 2)$

  1. Draw a plot of the distribution over $x$. Label the axes.

  2. Give a plot or manual list of several (10) plausible samples drawn from this distribution.

Now suppose we have a 2D random vector $\mathbf u$ (containing two random variables $x$ and $y$)

Suppose the stats are $\boldsymbol\mu = \begin{pmatrix}10\\15\end{pmatrix}$, $\boldsymbol\Sigma = \begin{pmatrix}1, &0\\0, &5\end{pmatrix}$

  1. Draw the general 2D shape of this distribution.

Regression, Linear Algebra style¶

Likelihood¶

The likelihood for a model sample (a.k.a. dataset) is defined as the function of model parameters using the data

$$ L(\beta_0, \beta_1, \sigma^2) = \prod_{i=1}^{m} P(Y = y_i \ |\ X=x_i) = \prod_{i=1}^{m} \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(y_i - (\beta_0 + \beta_1 x_i))^2}{2\sigma^2}\right) $$
\begin{align} &= \prod_{i=1}^m \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(y_i - (\beta_0 + \beta_1 x_i))^2}{2\sigma^2}\right) \\ &= \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(y_1 - (\beta_0 + \beta_1 x_1))^2}{2\sigma^2}\right) \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(y_2 - (\beta_0 + \beta_1 x_2))^2}{2\sigma^2}\right) ... \\ &= \left(\frac{1}{\sqrt{2\pi\sigma^2}}\right)^m \exp\left(\sum_{i=1}^m\frac{-(y_i - (\beta_0 + \beta_1 x_i))^2}{2\sigma^2}\right) \\ &= \left(\frac{1}{\sqrt{2\pi\sigma^2}}\right)^m \exp\left(-\frac{\sum_{i=1}^m(y_i - (\beta_0 + \beta_1 x_i))^2}{2\sigma^2}\right) \\ \end{align}
\begin{align} L(\beta_0, \beta_1, \sigma^2) = \prod_{i=1}^{n} P(Y = y_i \ |\ X=x_i) &= \left(\frac{1}{\sqrt{2\pi\sigma^2}}\right)^n \exp\left(-\frac{\sum_{i=1}^n(y_i - (\beta_0 + \beta_1 x_i))^2}{2\sigma^2}\right) \\ \end{align}

Consider the term inside the exponent

\begin{align} \frac{-1}{2\sigma^2} \sum_{i=1}^n(y_i - (\beta_0 + \beta_1 x_i))^2 = \frac{-1}{2\sigma^2}\sum_{i=1}^n \varepsilon_i^2 = \frac{-1}{2\sigma^2}\Vert \boldsymbol\varepsilon \Vert_2^2 \end{align}

Euclidean Norm ($L_2$)¶

$$ \|\boldsymbol\varepsilon\|_2 = \sqrt{\varepsilon_1^2+\varepsilon_2^2+\dotsb+\varepsilon_n^2} $$

"Length" of a vector (as opposed to length of the data structure, i.e. number of dimensions)

Linear System¶

Combine all the measurements into a vector equation $$\mathbf y = \beta_0 + \beta_1 \mathbf x + \boldsymbol\varepsilon$$

$$\text{where } \mathbf y = \begin{bmatrix} y_1 \\y_2 \\ \vdots \\ y_m \end{bmatrix}, \;\; \mathbf x = \begin{bmatrix} x_1 \\x_2 \\ \vdots \\ x_m \end{bmatrix}, \;\; \boldsymbol\varepsilon = \begin{bmatrix} \varepsilon_1 \\ \varepsilon_2 \\ \vdots \\ \varepsilon_m \end{bmatrix}$$

How does this model apply to our example? What is $x_i$?

\begin{align} \mathbf y &= \beta_0 + \beta_1 \mathbf x + \boldsymbol\varepsilon = \mathbf X \boldsymbol\beta + \boldsymbol\varepsilon \\ \end{align}$$\text{where } \boldsymbol{\beta} = \begin{pmatrix} \beta_0\\ \beta_1 \end{pmatrix}, \;\; \mathbf X = \begin{pmatrix} 1 & x_{1} \\ \vdots & \vdots \\ 1 & x_{m} \end{pmatrix}, $$

Regression as solving this system for $\boldsymbol\beta$ given data $\mathbf x$ and $\mathbf y$

Likelihood as a Gaussian¶

The product of Gaussian distributions is also Gaussian (over random variable $\mathbf y$)

\begin{align} L(\beta_0, \beta_1, \sigma^2) &= \left(\frac{1}{\sqrt{2\pi\sigma^2}}\right)^m \exp\left(-\frac{1}{2\sigma^2} \sum_{i=1}^m (y_i - (\beta_0 + \beta_1 x_i))^2\right) \\ &= \left(\frac{1}{\sqrt{2\pi\sigma^2}}\right)^m \exp \left(\frac{-1}{2\sigma^2} \Vert\mathbf y -\mathbf X \boldsymbol{\beta}\Vert_2^2 \right) \end{align}

what is the mean and covariance? (recall $ f(\mathbf x) = \frac{1}{ \sqrt{(2 \pi)^n |\boldsymbol\Sigma|}} \exp \left(- \frac{1}{2} (\mathbf x - \boldsymbol \mu)^T \boldsymbol\Sigma^{-1} (\mathbf x - \boldsymbol \mu) \right) \text{, for } \mathbf x \in R^n $)

Maximum Likelihood via Linear Algebra¶

Maximize (over $\boldsymbol{\beta}$) \begin{align} L(\beta_0, \beta_1, \sigma^2) &= \left(\frac{1}{\sqrt{2\pi\sigma^2}}\right)^m \exp \left(\frac{-1}{2\sigma^2} \Vert\mathbf y -\mathbf X \boldsymbol{\beta}\Vert_2^2 \right) \end{align}

by minimizing

$$\Vert\mathbf y -\mathbf X \boldsymbol{\beta}\Vert_2^2$$

The Least squared or least length solution

Linear systems¶

Consider minimizing $\Vert\mathbf y -\mathbf X \boldsymbol{\beta}\Vert_2^2$ as a way to solve the system:

$$ \mathbf y =\mathbf X \boldsymbol{\beta} $$

What does it mean to ignore the residual $\varepsilon$ like this.

Linear system cases¶

$$ \mathbf y =\mathbf X \boldsymbol{\beta} $$

CASE 1: $\mathbf X$ square, invertible (a.k.a. nonsingular). Can solve with inverse (generally never do this).

$$ \boldsymbol{\beta} = \mathbf X^{-1} \mathbf y $$

CASE 2: $\mathbf X$ "short and fat", more unknowns than equations, underdetermined. Solve with pseudoinverse.

$$ \boldsymbol{\beta} = \mathbf X^\dagger \mathbf y = \mathbf X^T \left(\mathbf X\mathbf X^T\right)^{-1} \mathbf y $$

CASE 3: $\mathbf X$ "tall and thin", more equations than unknowns, overdetermined. Solve with pseudoinverse.

$$ \boldsymbol{\beta} = \mathbf X^\dagger \mathbf y = \left(\mathbf X^T\mathbf X\right)^{-1} \mathbf X^T \mathbf y $$

These all minimize $\Vert\mathbf y -\mathbf X \boldsymbol{\beta}\Vert_2^2$ for their respective case.

Note that $\mathbf X^\dagger = \mathbf X^{-1}$ when the inverse exists. There are yet more nuances regarding how to handle small singular values and other numerical issues. Linear algebra libraries often have a robust pinv function in addition to other options like solve or a backslash operator "A\b"

Exercise: find $\boldsymbol\beta$ for a typical regression case analytically

Multiple Regression¶

Multi-variable Model¶

New model: $\mathbf y = \beta_0 + \beta_1 \mathbf x_{(1)} + \beta_2 \mathbf x_{(2)} +...+ \beta_n \mathbf x_{(n)} + \boldsymbol\varepsilon$

  • where $\mathbf y = \begin{bmatrix} y_1 \\y_2 \\ \vdots \\ y_m \end{bmatrix}$, $\mathbf x_{(i)} = \begin{bmatrix} x_{(i),1} \\x_{(i),2} \\ \vdots \\ x_{(i),m} \end{bmatrix}$, $\boldsymbol\varepsilon = \begin{bmatrix} \varepsilon_1 \\ \varepsilon_2 \\ \vdots \\ \varepsilon_m \end{bmatrix}$

E.g., predict temperature using: time of year, time of day, and lattitude, to get more accurate prediction.

How many equations and variables?

\begin{align} L(\beta_0, \beta_1, ..., \beta_n, \sigma^2) &= \prod_i^m \frac{1}{\sqrt{2\pi\sigma^2}} \exp{\left(\frac{-1}{2\sigma^2} \left(y_i - (\beta_0 + \beta_1 X_{i,1} + ... + \beta_n X_{i,n})\right)^2\right)} \\ &= \frac{1}{\sqrt{2\pi\sigma^2}^m} \exp{ \left( \sum_i^m\frac{-1}{2\sigma^2} \left(y_i - (\beta_0 + \beta_1 X_{i,1} + ... + \beta_n X_{i,n})\right)^2\right)} \\ &= \frac{1}{\sqrt{2\pi\sigma^2}^m} \exp{ \left(\frac{-1}{2\sigma^2} \Vert\mathbf y -\mathbf X \boldsymbol{\beta}\Vert_2^2 \right)} \\ &= \frac{1}{\sqrt{2\pi\sigma^2}^m} \exp{ \left(\frac{-1}{2} (\mathbf y -\mathbf X \boldsymbol{\beta})^T\boldsymbol\Sigma^{-1} (\mathbf y -\mathbf X \boldsymbol{\beta}) \right)} \\ \end{align}$$\text{Where } \boldsymbol{\beta} = \begin{pmatrix}\beta_0\\ \beta_1\\ \vdots \\ \beta_n \end{pmatrix}, \;\; \mathbf X = \begin{pmatrix}1 & X_{1,1} & X_{1,2} & \dots & X_{1,n}\\ 1 & X_{1,2} & X_{2,2} & \dots & X_{2,n} \\ \vdots & \vdots & \vdots & & \vdots \\ 1 & X_{m,1} & X_{m,2} & \dots & X_{m,n} \end{pmatrix}, \;\; \boldsymbol\Sigma^{-1} = \frac{1}{\sigma^2} \mathbf I $$

Linear Algebra view of Linear Regression¶

The ML solution to the multiple linear regression problem with Gaussian error

$$ \boldsymbol\beta^*= \arg\min\limits_{ \boldsymbol\beta} \Vert\mathbf y -\mathbf X \boldsymbol{\beta}\Vert_2^2 $$$$\text{where } \mathbf y = \begin{bmatrix} y_1 \\y_2 \\ \vdots \\ y_m \end{bmatrix},\;\; \boldsymbol{\beta} = \begin{pmatrix}\beta_0\\ \beta_1\\ \vdots \\ \beta_n \end{pmatrix}, \;\; \mathbf X = \begin{pmatrix}1 & X_{1,1} & X_{1,2} & \dots & X_{1,n}\\ 1 & X_{1,2} & X_{2,2} & \dots & X_{2,n} \\ \vdots & \vdots & \vdots & & \vdots \\ 1 & X_{m,1} & X_{m,2} & \dots & X_{m,n} \end{pmatrix}, \;\; \boldsymbol\varepsilon = \begin{bmatrix} \varepsilon_1 \\ \varepsilon_2 \\ \vdots \\ \varepsilon_m \end{bmatrix}$$

Is, in linear algebra terminology, this is the least-squares solution to the linear system:

$$ \mathbf y =\mathbf X \boldsymbol{\beta} $$

Machine Learning in extremely-small nutshell¶

  1. Starting from "labeled data" $(x_1, y_1), (x_2, y_2), ...$

  2. Pick a model, e.g., $y_i \approx \beta_0 + \beta_1 x_i \rightarrow \mathbf y \approx\mathbf X \boldsymbol{\beta} = f(\mathbf x;\boldsymbol\beta)$

  3. Pick a "Loss function" to apply to the residual, such as $\mathcal{L}(\mathbf y, f(\mathbf x;\boldsymbol\beta)) = \Vert \mathbf y - f(\mathbf x;\boldsymbol\beta) \Vert_2^2$

  4. "Fit" model to your data by using your favorite optimization technique to find parameters $\boldsymbol\beta$ that minimize the loss $\Vert \mathbf y - f(\mathbf x;\boldsymbol\beta) \Vert$

drawing

Least Squares¶

Motivation¶

  • The most commonly used optimization problem
  • Analytical solution in linear case
  • The most important deep learning loss function
  • Maximum likelihood solution in case of Gaussian error (good approximation for almost everything)

Background: From Linear Systems to Linear Algebra¶

$$ a x = b \quad \Rightarrow \quad x = \frac{b}{a}, \quad a \neq 0 $$

Two equations, two unknowns:

$$ \begin{cases} a_{11} x_1 + a_{12} x_2 = b_1 \\ a_{21} x_1 + a_{22} x_2 = b_2 \end{cases} $$

Solve by substitution ~ Gaussian elimination

General system ($m$ equations, $n$ unknowns):

$$ A x = b, \quad A \in \mathbb{R}^{m \times n}, \; x \in \mathbb{R}^n, \; b \in \mathbb{R}^m $$

Cases¶

  • $m = n$: square system, exact solution if $\det(A) \neq 0$.
  • $m > n$: overdetermined, may have no exact solution → least squares.
  • $m < n$: underdetermined, infinite solutions → choose minimum-norm.

Linear algebra tools:¶

  • Matrix inverse: $x = A^{-1} b$ (when invertible).
  • Factorizations: LU, QR, SVD (numerically stable methods).
  • Pseudoinverse: $x = A^+ b$, general formula for all cases.

Example:¶

$$ \begin{cases} 2x_1 + 1x_2 = 5 \\ 1x_1 + 3x_2 = 6 \end{cases} $$$$ A = \begin{bmatrix} 2 & 1 \\ 1 & 3 \end{bmatrix}, \quad b = \begin{bmatrix} 5 \\ 6 \end{bmatrix} $$

Inverse¶

$$ A^{-1} = \frac{1}{\det(A)} \begin{bmatrix} a_{22} & -a_{12} \\ -a_{21} & a_{11} \end{bmatrix}, \quad \det(A) = (2)(3) - (1)(1) = 5 $$$$ A^{-1} = \tfrac{1}{5} \begin{bmatrix} 3 & -1 \\ -1 & 2 \end{bmatrix} $$

Solution¶

$$ x = A^{-1} b = \tfrac{1}{5} \begin{bmatrix} 3 & -1 \\ -1 & 2 \end{bmatrix} \begin{bmatrix} 5 \\ 6 \end{bmatrix} = \begin{bmatrix} 1.8 \\ 1.4 \end{bmatrix} $$

Solving with NumPy¶

In [5]:
import numpy as np

A = np.array([[2, 1],
              [1, 3]])
b = np.array([5, 6])

x = np.linalg.inv(A) @ b
print(x)  # [1.8 1.4]
[1.8 1.4]

NumPy Inverse¶

In [91]:
A = np.array([[1,2],[4,5]])
iA = np.linalg.inv(A)
print(iA)
print(np.matmul(A,iA))
[[-1.66666667  0.66666667]
 [ 1.33333333 -0.33333333]]
[[1. 0.]
 [0. 1.]]
In [95]:
A = np.random.randint(0, 10, size=(3, 3))
A_inv = np.linalg.inv(A)
print(A)
print(A_inv)
print(np.matmul(A,A_inv))
[[2 6 4]
 [5 7 1]
 [5 3 2]]
[[-1.25000000e-01 -1.11022302e-17  2.50000000e-01]
 [ 5.68181818e-02  1.81818182e-01 -2.04545455e-01]
 [ 2.27272727e-01 -2.72727273e-01  1.81818182e-01]]
[[ 1.00000000e+00  0.00000000e+00 -1.11022302e-16]
 [-2.77555756e-17  1.00000000e+00 -8.32667268e-17]
 [-5.55111512e-17  0.00000000e+00  1.00000000e+00]]

Recall that "hard zeros" rarely exist in real world due to limited numerical precision.

Ex: Underdetermined System $m<n$¶

$$ A x = b, \quad A = \begin{bmatrix} 1 & 2 & 1 \\ 0 & 1 & 1 \end{bmatrix}, \quad b = \begin{bmatrix} 4 \\ 2 \end{bmatrix}, \quad x = \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix}. $$

Matrix is "Short and fat" ~ More unknowns than equations

$$ x_1 + 2x_2 + x_3 = 4, $$$$ x_2 + x_3 = 2. $$

...solving by hand¶

$$ x_2 = 2 - x_3. $$

Substitute into the first:

$$ x_1 + 2(2 - x_3) + x_3 = 4, $$$$ x_1 + 4 - 2x_3 + x_3 = 4, $$$$ x_1 - x_3 = 0 \quad \Rightarrow \quad x_1 = x_3. $$

Let $t = x_3$. Then

$$ x_1 = t, \quad x_2 = 2 - t, \quad x_3 = t. $$$$ x = \begin{bmatrix} t \\ 2 - t \\ t \end{bmatrix}, \quad t \in \mathbb{R}. $$

Recap¶

The underdetermined system: $$ \begin{bmatrix} 1 & 2 & 1 \\ 0 & 1 & 1 \end{bmatrix} x = \begin{bmatrix} 4 \\ 2 \end{bmatrix} $$ has an infinite set of solutions described by $$ x = \begin{bmatrix} t \\ 2 - t \\ t \end{bmatrix}, \quad t \in \mathbb{R}. $$

pick a couple values for $t$ and check it works

How do we deal with this situation?

Ex: Overdetermined system $m>n$¶

Consider

$$ A=\begin{bmatrix} 1&1\\[2pt] 1&2\\[2pt] 1&3 \end{bmatrix}\in\mathbb{R}^{3\times 2},\qquad b=\begin{bmatrix} 1\\[2pt]2\\[2pt]2 \end{bmatrix},\qquad x=\begin{bmatrix}x_1\\ x_2\end{bmatrix}. $$

Can this be solved?

Least Squares¶

In the real world, nothing is exact (precision class) and we usually will be happy with a decent workable solution.

View geometrically. A vector as a point in high dimensions.

Solve $A x = b$ by finding $x$ so that $A x$ lies exactly (or as close as possible) to $b$.

Underdetermined case: we have infinite solutions and must pick best one of them. So we pick:

$$ x^\ast=\arg\min_{x}\|x\|_2^2 \text{ such that } Ax=b $$

The solution with the least length

Overdetermined case: we (probably) have no solutions and must pick the closest thing to a solution we can get. So we pick:

$$ x^\ast=\arg\min_{x}\|Ax-b\|_2^2. $$

The vector $Ax-b$ is called the residual. We want the residual with the least length.

Pseudoinverse $A^\dagger$¶

The previous optimization problems can be solved analytically (set derivative equal to zero).

Note we define the pseudoinverse depending on the situation

Underdetermined case ($m < n$)

$$ x^\ast = A^T (A A^T)^{-1} b = A^\dagger b $$

Overdetermined case ($m > n$)

$$ x^\ast = (A^T A)^{-1} A^T b = A^\dagger b $$

Exercise:¶

use numpy pseudoinverse to solve the previous 2x2, 2x3, and 3x2 examples

Verify pseudoinverse equation for 2x3 case

One more trick...¶

Consider the following bad system of equations

In [4]:
import numpy as np

A = np.array([[1, 1],
              [2, 2]])
b = np.array([3, 6])

np.linalg.inv(A)
---------------------------------------------------------------------------
LinAlgError                               Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_18984\2343809015.py in <module>
      5 b = np.array([3, 6])
      6 
----> 7 np.linalg.inv(A)

<__array_function__ internals> in inv(*args, **kwargs)

~\anaconda3\lib\site-packages\numpy\linalg\linalg.py in inv(a)
    543     signature = 'D->D' if isComplexType(t) else 'd->d'
    544     extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
--> 545     ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
    546     return wrap(ainv.astype(result_t, copy=False))
    547 

~\anaconda3\lib\site-packages\numpy\linalg\linalg.py in _raise_linalgerror_singular(err, flag)
     86 
     87 def _raise_linalgerror_singular(err, flag):
---> 88     raise LinAlgError("Singular matrix")
     89 
     90 def _raise_linalgerror_nonposdef(err, flag):

LinAlgError: Singular matrix

Somehow the pseudoinverse worked

In [5]:
# Pseudoinverse solution (via SVD under the hood)
x_pinv = np.linalg.pinv(A) @ b
print("Pseudoinverse solution (minimum norm):", x_pinv)

# Check residual
print("Residual:", np.linalg.norm(A @ x_pinv - b))
Pseudoinverse solution (minimum norm): [1.5 1.5]
Residual: 2.9790409838967277e-15

how did it do that?

In [6]:
# Normal equations attempt (will fail because A^T A is singular)
print("A^T A determinant:", np.linalg.det(A.T @ A))  # should be 0
A^T A determinant: 0.0

Lab: Least-squares regression various ways¶

Load Boston house prices dataset.

Formulate linear system and try using inverse and pseudoinverse to solve.

Regression: $ \mathbf A \boldsymbol{\beta} \rightarrow \text{target} $

Linear Algebra: solve $ \mathbf A \boldsymbol{\beta} = \text{target} $ for unknown $\boldsymbol{\beta}$.

In [2]:
from sklearn.datasets import load_breast_cancer
bc = load_breast_cancer()
print(dir(bc))
print(bc.data.shape)
print(bc.feature_names)
#print(bc.DESCR)
print(bc.target_names)
['DESCR', 'data', 'data_module', 'feature_names', 'filename', 'frame', 'target', 'target_names']
(569, 30)
['mean radius' 'mean texture' 'mean perimeter' 'mean area'
 'mean smoothness' 'mean compactness' 'mean concavity'
 'mean concave points' 'mean symmetry' 'mean fractal dimension'
 'radius error' 'texture error' 'perimeter error' 'area error'
 'smoothness error' 'compactness error' 'concavity error'
 'concave points error' 'symmetry error' 'fractal dimension error'
 'worst radius' 'worst texture' 'worst perimeter' 'worst area'
 'worst smoothness' 'worst compactness' 'worst concavity'
 'worst concave points' 'worst symmetry' 'worst fractal dimension']
['malignant' 'benign']
In [ ]:
 

Nonlinear regression¶

Fitting a line works in some cases, but other kinds of curves might work better elsewhere

drawing

Suppose we know a relationship is quadratic. What can we do?

If we know the relationship is quadratic, we could just take the square root and make it linear.

Nonlinear regression (easy way) - Linear regression after nonlinear transformation¶

Old: \begin{align} \mathbf y = \beta_0 + \beta_1 \mathbf x_{(1)} + \beta_2 \mathbf x_{(2)} +...+ \beta_n \mathbf x_{(n)} + \varepsilon , \; \; \; \mathbf x_{(i)} = \begin{bmatrix} x_{(i),1} \\x_{(i),2} \\ \vdots \\ x_{(i),m} \end{bmatrix} \end{align}

New: \begin{align} \mathbf y = \beta_0 + \beta_1 \mathbf x'_{(1)} + \beta_2 \mathbf x'_{(2)} +...+ \beta_n \mathbf x'_{(n)} + \varepsilon, \;\;\; \mathbf x'_{(i)} = \begin{bmatrix} f_{(i)}(x_1) \\f_{(i)}(x_2) \\ \vdots \\ f_{(i)}(x_m) \end{bmatrix} \end{align}

...Feature Engineering, Kernel methods, ...

Polynomial regression - simple powers of data¶

New model: $\mathbf y = \beta_0 + \beta_1 \mathbf x'_{(1)} + \beta_2 \mathbf x'_{(2)} +...+ \beta_n \mathbf x'_{(n)} + \varepsilon$

$$\mathbf x'_{(i)} = \begin{bmatrix} f_{(i)}(x_1) \\f_{(i)}(x_2) \\ \vdots \\ f_{(i)}(x_m) \end{bmatrix} = \begin{bmatrix} x_{1}^i \\x_{2}^i \\ \vdots \\ x_{m}^i \end{bmatrix}$$

Exercise: write the model equations out for 3D case, i.e. $\bf x$ is just $x_1$, $x_2$, and $x_3$.

Exercise¶

Use numpy polyfit and polyval to plot the residual versus polynomial order

In [26]:
show_polyfit_ho_example_results(dosage,conc_noisy,(1,2,3,4,5,6));
order: 1, residual: 56.6204627002199
order: 2, residual: 43.28483436298878
order: 3, residual: 38.90189854595423
order: 4, residual: 21.166171682768383
order: 5, residual: 13.416800916508517
order: 6, residual: 1.8203750586044797e-11