Sveučilište u Zagrebu
Fakultet elektrotehnike i računarstva
http://www.fer.unizg.hr/predmet/su
Ak. god. 2015./2016.
(c) 2015 Jan Šnajder
Verzija: 0.3 (2015-11-09)
In [1]:
import scipy as sp
import scipy.stats as stats
import matplotlib.pyplot as plt
import pandas as pd
%pylab inline
Međutim, rješenja nema ili ono nije jedinstveno ako:
(1) $\mathbf{X}$ nije kvadratna, pa nema inverz. U pravilu:
(2) $\boldsymbol{X}$ jest kvadratna (tj. $N=(n+1)$), ali ipak nema inverz (ovisno o rangu matrice)
$\Rightarrow$ sustav je nekonzistentan
Jednakosti linearne algebre:
- $(A^\intercal)^\intercal = A$
- $(AB)^\intercal = B^\intercal A^\intercal$
Jednakosti linearne algebre:
- $\frac{\mathrm{d}}{\mathrm{d}x}x^\intercal A x=x^\intercal(A+A^\intercal)$
- $\frac{\mathrm{d}}{\mathrm{d}x}A x=A$
Zbog šuma u $\mathcal{D}$: $$ y^{(i)} = f(x^{(i)}) + \color{red}{\varepsilon} $$
Prepostavka: $$ \color{red}{\varepsilon}\ \sim\ \mathcal{N}(0, \sigma^2) $$
Posjedično: $$ \color{red}{y|x}\ \sim\ \mathcal{N}\big(f(x), \sigma^2\big) $$ odnosno $$ \color{red}{p(y|x)} = \mathcal{N}\big(f(x), \sigma^2\big) $$
Vrijedi $$\mathbb{E}[y|x] = \mu = f(x)$$
In [261]:
def f(x) : return 3*(x - 2)**2 + 1
x1 = 1
x2 = 2
x3 = 3
In [262]:
xs = linspace(0, 4)
y = f(xs)
plt.ylim(0,5)
plt.plot(xs, y)
plt.plot(x1, f(x1), 'ro')
plt.plot(x2, f(x2), 'go')
plt.plot(x3, f(x3), 'bo')
plt.show()
In [263]:
def phi(x): return sp.array([1, x, x**2])
In [264]:
phi(x1)
Out[264]:
In [265]:
phi(x2)
Out[265]:
In [266]:
phi(x3)
Out[266]:
In [267]:
xs1 = linspace(0, 5)
xs2 = linspace(0, 10)
X1, X2 = np.meshgrid(xs1, xs2)
In [268]:
phi_X = 3*X2 - 12*X1 + 13
In [286]:
plt.contour(X, Y, phi_X, levels=[1,4])
plt.scatter(phi(x1)[1], phi(x1)[2], c='r')
plt.scatter(phi(x2)[1], phi(x2)[2], c='g')
plt.scatter(phi(x3)[1], phi(x3)[2], c='b')
plt.legend()
plt.show()
Dizajn-matrica: $$ \boldsymbol{\Phi} = \begin{pmatrix} 1 & \phi_1(\mathbf{x}^{(1)}) & \dots & \phi_m(\mathbf{x}^{(1)})\ 1 & \phi_1(\mathbf{x}^{(2)}) & \dots & \phi_m(\mathbf{x}^{(2)})\ \vdots\ 1 & \phi_1(\mathbf{x}^{(N)}) & \dots & \phi_m(\mathbf{x}^{(N)})\
\begin{pmatrix} \mathbf{\phi}(\mathbf{x}^{(1)})^\intercal \\ \mathbf{\phi}(\mathbf{x}^{(2)})^\intercal \\ \vdots\\ \mathbf{\phi}(\mathbf{x}^{(N)})^\intercal \\ \end{pmatrix}_{N\times m} $$
Prije smo imali: $$ \mathbf{w} = (\mathbf{X}^\intercal\mathbf{X})^{-1}\mathbf{X}^\intercal\mathbf{y} = \color{red}{\mathbf{X}^{+}}\mathbf{y} $$ a sada imamo: $$ \mathbf{w} = (\boldsymbol{\Phi}^\intercal\boldsymbol{\Phi})^{-1}\boldsymbol{\Phi}^\intercal\mathbf{y} = \color{red}{\boldsymbol{\Phi}^{+}}\mathbf{y} $$ gdje $$ \boldsymbol{\Phi}^{+}=(\boldsymbol{\Phi}^\intercal\boldsymbol{\Phi})^{-1}\boldsymbol{\Phi}^\intercal $$