some slides done

This commit is contained in:
Pierre-Edouard Portier 2022-03-06 22:57:04 +01:00
parent dca76f831a
commit cb4a0b3ef3
5 changed files with 356 additions and 0 deletions

127
03_tikhonov_slides.Rmd Normal file
View File

@ -0,0 +1,127 @@
---
title: "03 Régularisation de Tikhonov"
author: Pierre-Edouard Portier
date: mars 2022
output: beamer_presentation
---
```{r, include=FALSE}
source("01_intro.R", local = knitr::knit_global())
source("03_tikhonov.R", local = knitr::knit_global())
```
# Problèmes linéaires mal posés
- Pas de solution unique pour $\mathbf{X}\boldsymbol\beta=\mathbf{y}$ avec $n<p$
- Avec $n \geq p$, colinéarités $\leadsto$ solution _irrégulière_
\[
\left( \begin{array}{cc}
1 & 1 \\
1 & 1.00001 \\
\end{array} \right)
\left( \begin{array}{c}
\beta_1 \\ \beta_2
\end{array} \right)
=
\left( \begin{array}{c}
1 \\ 0.99
\end{array} \right)
\]
- Solution optimale : $\boldsymbol\beta^T = (1001,-1000)$
- $(1,2)$ projetée sur $-999$
- Solution approchée préférable : $\boldsymbol\beta^T = (0.5,0.5)$
- $(1,2)$ projetée sur $1.5$
# Régularisation de Tikhonov ou régression ridge
- Préférence pour les petits coefficients
- Mesurée par $\|\boldsymbol\beta\|_1 = |\beta_1|+|\beta_2|+\dots$
- Ou par $\|\boldsymbol\beta\|_2^2 = \beta_1^2+\beta_2^2+\dots$
$$
\begin{aligned}
& \min_{\boldsymbol\beta} \|\mathbf{X}\boldsymbol\beta-\mathbf{y}\|^2_2 + \lambda \|\boldsymbol\beta\|^2_2 \\
& avec \quad 0 \leq \lambda
\end{aligned}
$$
- Résolution par annulation de la dérivée
$$
\begin{aligned}
& \mathbf{0} = 2\mathbf{X}^T\mathbf{X}\boldsymbol\beta - 2\mathbf{X}^T\mathbf{y} + 2\lambda\boldsymbol\beta \\
=& \\
& \left( \mathbf{X}^T\mathbf{X} + \lambda \mathbf{I}_{n\times n} \right) \boldsymbol\beta = \mathbf{X}^T \mathbf{y}
\end{aligned}
$$
# Standardisation
- Contraction vers $0$ de $\beta_i^2$
- Intensité de la régularisation dépend de l'échelle de la variable
- Mise à l'échelle des variables par standardisation
- $z_{ij} = \frac{x_{ij}-\bar{x_j}}{\sigma_j}$
- Possible de seulement centrer $\mathbf{y}$
$$
\begin{aligned}
& \mathbf{\hat{y_i}} - \bar{\mathbf{y}} = \sum_{j=1}^{p} \hat{\beta_j} \left( \frac{x_{ij}-\bar{x_j}}{\sigma_j} \right) \\
= \{& \text{arithmétique} \} \\
& \mathbf{\hat{y_i}} = \left( \bar{\mathbf{y}} - \sum_{j=1}^{p} \hat{\beta_j} \frac{\bar{x_j}}{\sigma_j} \right) +
\sum_{j=1}^{p} \frac{\hat{\beta_j}}{\sigma_j} x_{ij} \\
\end{aligned}
$$
# Exemple
```{r, echo=FALSE}
set.seed(1123)
# Image par f d'un échantillon uniforme sur l'intervalle [0,1], avec ajout d'un
# bruit gaussien de moyenne nulle et d'écart type 0.2
data = gendat(10,0.2)
par(mfrow=c(1,3))
coef <- ridge(0, data, 7)
plt(data,f,main=expression(paste(plain("Degré = "), 7, plain(", "), lambda,
plain(" = 0"))))
pltpoly(coef)
coef <- ridge(1E-4, data, 7)
plt(data,f,main=expression(paste(plain("Degré = "), 7, plain(", "), lambda,
plain(" = 1E-4"))))
pltpoly(coef)
coef <- ridge(1, data, 7)
plt(data,f,main=expression(paste(plain("Degré = "), 7, plain(", "), lambda,
plain(" = 1"))))
pltpoly(coef)
```
# Convergence des coefficients vers $0$
```{r, echo=FALSE}
lambdas <- c(1E-5, 1E-4, 1E-3, 1E-2, 1E-1, 1)
lcoef <- sapply(lambdas, ridge, data, 7)
matplot(lambdas, t(lcoef), type=c("b"), pch=1, col=1:8, log="x")
legend("topright", legend = 0:7, col=1:8, pch=1)
```
# Régularisation et complexité
- $F(\mathbf{X}) = \mathbf{W}^T\mathbf{X} + b$
- $\mathbf{X^*} = \mathbf{X} + \mathbf{\epsilon}$
- Régularité : $\mathbf{X}$ et $\mathbf{X^*}$ proches $\leadsto$ $F(\mathbf{X})$ et $F(\mathbf{X^*})$ proches
$$
\begin{aligned}
& |F(\mathbf{X}) - F(\mathbf{X^*})| \\
= \{& F(\mathbf{X}) = \mathbf{W}^T\mathbf{X} + b \} \\
& |\mathbf{W}^T\mathbf{X} - \mathbf{W}^T\mathbf{X^*}| \\
= \{& \text{Algèbre linéaire} \} \\
& |\mathbf{W}^T(\mathbf{X}-\mathbf{X^*})| \\
= \{& \mathbf{X^*} = \mathbf{X} + \mathbf{\epsilon} \} \\
& |\mathbf{W}^T \mathbf{\epsilon}| \\
\leq \{& \text{Inégalité de Cauchy-Schwarz} \} \\
& \|\mathbf{W}\|_2 \|\mathbf{\epsilon}\|_2 \\
\end{aligned}
$$

View File

@ -0,0 +1,68 @@
---
title: "04 Validation croisée"
author: Pierre-Edouard Portier
date: mars 2022
output: beamer_presentation
---
```{r, include=FALSE}
source("01_intro.R", local = knitr::knit_global())
source("04_validation_croisee.R", local = knitr::knit_global())
```
# Principe de la validation croisée
- Quelle valeur pour l'__hyperparamètre__ $\lambda$ ?
$$\hat{\boldsymbol\beta} = \left( \mathbf{X}^T\mathbf{X} + \lambda \mathbf{I}_{n\times n} \right)^{-1} \mathbf{X}^T \mathbf{y}$$
- __Validation croisée__ par partition du jeu de données
- __entraînement__ : estimer $\hat{\boldsymbol\beta}$ de chaque modèle
- __validation__ : choisir le modèle qui commet la plus faible erreur
- __test__ : estimer l'erreur du modèle retenu
# Validation croisée à K plis
- Partitionner le jeu de données en entraînement et test
- Partitionner le jeu d'entraînement en K plis disjoints
- Pour chaque valeur $\lambda_i$ de l'hyperparamètre $\lambda$
- Apprendre K modèles $M[\lambda_i,k]$ avec $k \in [1\dots K]$
- Entraîner $M[\lambda_i,k]$ sur les plis $1,2,\dots,k-1,k+1,\dots,K]$
- Tester $M[\lambda_i,k]$ sur le pli $k$
- Faire la moyenne des erreurs commises par les $K$ modèles $M[\lambda_i,\cdot]$ sur les jeux de test
- Choisir la valeur $\lambda_{best}$ qui correspond à la plus faible erreur
- Apprendre, sur tout le jeu d'entrâinement, un modèle avec l'hyperparamètre $\lambda_{best}$
- Tester ce modèle sur le jeu de test
# Moyenne des erreurs absolues pour différentes valeurs de $\lambda$
```{r, echo=FALSE}
set.seed(1123)
N <- 100
deg1 <- 8
data = gendat(N,0.2)
splitres <- splitdata(data,0.8)
entr <- splitres$entr
test <- splitres$test
lambdas <- c(1E-8, 1E-7, 1E-6, 1E-5, 1E-4, 1E-3, 1E-2, 1E-1, 1)
reskfold <- kfoldridge(K = 10, lambdas = lambdas, data = entr, degre = deg1)
boxplot(reskfold$maes, xlab="lambda", ylab="MAE")
```
# Meilleur poly de degré `r deg1` pour $\lambda=$ `r reskfold$lambda` sur l'entraînement
```{r, echo=FALSE}
plt(entr,f)
pltpoly(reskfold$coef)
```
# Meilleur poly de degré `r deg1` pour $\lambda=$ `r reskfold$lambda` sur le test
```{r, echo=FALSE}
testpred <- polyeval(reskfold$coef, test$X)
testmae <- mean(abs(testpred - test$Y))
plt(test,f)
pltpoly(reskfold$coef)
```
- Erreur absolue moyenne de `r testmae` sur le jeu de test

View File

@ -0,0 +1,92 @@
---
title: "05 Présentation de la décomposition en valeurs singulières (SVD)"
author: Pierre-Edouard Portier
date: mars 2022
output: beamer_presentation
---
```{r, include=FALSE}
source("05_presentation_svd.R", local = knitr::knit_global())
```
# Géométrie d'un tableau de données
- $\mathbf{X} \in \mathcal{R}^{n \times p}$ ; $x_{ij} \in \mathcal{R}$
- Nuage de $n$ points $\mathbf{x_i}$ dans $\mathcal{R}^p$
- Nuage de $p$ points $\mathbf{x_j}$ dans $\mathcal{R}^n$
- Projeter le nuage des points $\mathbf{x_i}$ sur $\mathcal{H} \subset \mathbb{R}^P$
- Minimiser les déformations
# Meilleur sous-espace de dimension $1$
```{r, out.width = "250px", echo=FALSE}
knitr::include_graphics("images/svd1.jpeg")
```
- $\mathcal{H}$ une droite de vecteur unitaire $\mathbf{v}$ ($\mathbf{v}^T\mathbf{v}=1$)
- $M_i$ le point de $\mathbb{R}^p$ tel que $\mathbf{x_i} = \mathbf{OM_i}$
- $H_i$ la projection de $M_i$ sur $\mathcal{H}$ : $OH_i = \mathbf{x_i}^T\mathbf{v}$
- $\mathcal{H}$ minimise $\sum_i M_i H_i^2 \quad = \quad \sum_i OM_i^2 - \sum_i OH_i^2$
- $\mathcal{H}$ maximise $\sum_i OH_i^2 = (\mathbf{X}\mathbf{v})^T(\mathbf{X}\mathbf{v}) = \mathbf{v}^T \mathbf{X}^T \mathbf{X} \mathbf{v}$
# Meilleur sous-espace de dimension $k$
- $\mathcal{H}_1$ contient $\mathbf{v_1}$ qui maximise $\mathbf{v_1}^T \mathbf{X}^T \mathbf{X} \mathbf{v_1}$
- $\mathcal{H}_2$ contient $\mathbf{v_1}$ et $\mathbf{v_2}$
- $\mathbf{v_2}$ orthogonal à $\mathbf{v_1}$ et maximise $\mathbf{v_2}^T \mathbf{X}^T \mathbf{X} \mathbf{v_2}$
- $\mathbf{X}^T \mathbf{X} \mathbf{v_1} = \lambda_1 \mathbf{v_1}$ avec $\lambda_1$ la plus grande valeur propre
- $\mathbf{X}^T \mathbf{X} \mathbf{v_2} = \lambda_2 \mathbf{v_2}$ avec $\lambda_2$ la seconde plus grande valeur propre
- $\mathbf{v_1}, \mathbf{v_2},\dots \mathbf{v_k}$ forment une base de $\mathcal{H}_k$
- La projection du nuage des $n$ points $\mathbf{x_i}$ sur $\mathcal{H}_k$ minimise les carrés des écarts à $\mathcal{R}^p$
- Symétriquement pour les vecteurs $\mathbf{u_1}, \mathbf{u_2},\dots \mathbf{u_k}$, etc.
# Décomposition en valeurs singulières
\begin{equation*}
\begin{cases}
\mathbf{X}^T\mathbf{X}\mathbf{v_\alpha} = \lambda_\alpha \mathbf{v_\alpha} \\
\mathbf{X}\mathbf{X}^T\mathbf{u_\alpha} = \mu_\alpha \mathbf{u_\alpha}
\end{cases}
\label{eq:svd-meme-valeurs-propres}
\end{equation*}
- $\left(\mathbf{X}\mathbf{X}^T\right)\mathbf{X}\mathbf{v_\alpha} = \lambda_\alpha \left(\mathbf{X}\mathbf{v_\alpha}\right) \Rightarrow \lambda_\alpha \leq \mu_\alpha$
- $\left(\mathbf{X}^T\mathbf{X}\right)\mathbf{X}^T\mathbf{u_\alpha} = \mu_\alpha \left(\mathbf{X}^T\mathbf{u_\alpha}\right) \Rightarrow \mu_\alpha \leq \lambda_\alpha$
- $\lambda_\alpha = \mu_\alpha$
- $\|\mathbf{X}\mathbf{v_\alpha}\|_2 = \sqrt{\lambda_\alpha}$
- $\mathbf{u_\alpha} = \frac{1}{\sqrt{\lambda_\alpha}} \mathbf{Xv_\alpha}$ et $\mathbf{v_\alpha} = \frac{1}{\sqrt{\lambda_\alpha}} \mathbf{X}^T\mathbf{u_\alpha}$
- $\mathbf{X}\mathbf{v_\alpha}=\mathbf{u_\alpha}\sqrt{\lambda_\alpha} \Rightarrow \mathbf{X}\left(\sum_{\alpha=1}^{P}\mathbf{v_\alpha}\mathbf{v_\alpha}^T\right) = \sum_{\alpha=1}^{P}\sqrt{\lambda_\alpha}\mathbf{u_\alpha}\mathbf{v_\alpha}^T$
$$\mathbf{X} = \sum_{\alpha=1}^{P}\sqrt{\lambda_\alpha}\mathbf{u_\alpha}\mathbf{v_\alpha}^T \quad \equiv \quad \mathbf{X} = \mathbf{U} \mathbf{D} \mathbf{V}^T$$
# Réduction dimensionnelle / Compression
```{r, out.width = "200px", echo=FALSE}
knitr::include_graphics("images/hortensia.jpg")
```
---
```{r, warning=FALSE, echo=FALSE}
X <- read_grey_img("images/hortensia.pgm")
par(mfrow=c(1,2), oma=c(1,1,0,0)+0.1, mar=c(0,0,1,1)+0.1);
print_grey_img(X, main="image originale d=256", asp=1);
print_grey_img(compress_SVD(X,16), main="d=16", asp=1);
```
---
```{r, warning=FALSE, echo=FALSE}
par(mfrow=c(1,2), oma=c(1,1,0,0)+0.1, mar=c(0,0,1,1)+0.1);
print_grey_img(compress_SVD(X,32), main="d=32", asp=1);
print_grey_img(compress_SVD(X,64), main="d=64", asp=1);
```
---
```{r, warning=FALSE, echo=FALSE}
par(mfrow=c(1,2), oma=c(1,1,0,0)+0.1, mar=c(0,0,1,1)+0.1);
print_grey_img(compress_SVD(X,128), main="d=128", asp=1);
print_grey_img(compress_SVD(X,256), main="d=256", asp=1);
```

51
11_projecteurs_slides.Rmd Normal file
View File

@ -0,0 +1,51 @@
---
title: "11 Projecteurs orthogonaux et SVD"
author: Pierre-Edouard Portier
date: mars 2022
output: beamer_presentation
---
# Projecteur
- $\mathbf{P}^2=\mathbf{P}$
- $\mathbf{P}^2=\mathbf{P}$ et $\mathbf{P}=\mathbf{P}^T$, alors $Im(\mathbf{P}) \perp Ker(\mathbf{P})$ (proj. orthogonal)
```{r, out.width = "300px", echo=FALSE}
knitr::include_graphics("images/projecteur1.jpg")
```
# SVD Réduit
```{r, out.width = "300px", echo=FALSE}
knitr::include_graphics("images/SVD_reduit.jpg")
```
# SVD réduit et projecteurs orthogonaux
\begin{align*}
Im(\mathbf{A}) &= Im(\mathbf{U_r}) & \mathbf{U_r}\mathbf{U_r}^T &: \text{ est un projecteur orthogonal sur } Im(\mathbf{A}) \\
Ker(\mathbf{A}) &= Im(\tilde{\mathbf{V_r}}) & \tilde{\mathbf{V_r}}\tilde{\mathbf{V_r}}^T &: \text{ est un projecteur orthogonal sur } Ker(\mathbf{A}) \\
Im(\mathbf{A}^T) &= Im(\mathbf{V_r}) & \mathbf{V_r}\mathbf{V_r}^T &: \text{ est un projecteur orthogonal sur } Im(\mathbf{A}^T) \\
Ker(\mathbf{A}^T) &= Im(\tilde{\mathbf{U_r}}) & \tilde{\mathbf{U_r}}\tilde{\mathbf{U_r}}^T &: \text{ est un projecteur orthogonal sur } Ker(\mathbf{A}^T) \\
\end{align*}
# Projection sur une base quelconque
- $\mathbf{y}$ projection orthogonale de $\mathbf{v}$ sur $Im(\mathbf{A})$
$$
\begin{aligned}
& \mathbf{a_j}^T (\mathbf{y}-\mathbf{v}) = 0, \forall j \\
= \{& \text{Posons $\mathbf{y}=\mathbf{A}\mathbf{x}$.} \} \\
& \mathbf{a_j}^T (\mathbf{A}\mathbf{x}-\mathbf{v}) = 0, \forall j \\
= \{& \text{Linéarité de la multiplication matricielle.} \} \\
& \mathbf{A}^T (\mathbf{A}\mathbf{x}-\mathbf{v}) = \mathbf{0} \\
= \phantom{\{}& \\
& \mathbf{A}^T\mathbf{A}\mathbf{x} = \mathbf{A}^T\mathbf{v} \\
= \{& \text{Si $\mathbf{A}^T\mathbf{A}$ est inversible.} \} \\
& \mathbf{x} = (\mathbf{A}^T\mathbf{A})^{-1}\mathbf{A}^T\mathbf{v} \\
\end{aligned}
$$
- $\mathbf{y} = \mathbf{A}\mathbf{x} = \mathbf{A}(\mathbf{A}^T\mathbf{A})^{-1}\mathbf{A}^T\mathbf{v}$
- $\mathbf{A}(\mathbf{A}^T\mathbf{A})^{-1}\mathbf{A}^T = \mathbf{U_r}\mathbf{U_r}^T$

View File

@ -0,0 +1,18 @@
---
title: "14 Géométrie de la régression ridge et SVD"
author: Pierre-Edouard Portier
date: mars 2022
output: beamer_presentation
---
# Coefficients de la régression ridge en fonction du SVD
$$\mathbf{\hat{\beta}_\lambda} = \sum_{d_j>0} \mathbf{v_j} \frac{d_j}{d_j^2 + \lambda} \mathbf{u_j}^T\mathbf{y}$$
- Le SVD donne $\mathbf{\hat{\beta}_\lambda}$ pour toutes les valeurs souhaitées de $\lambda$
# Régression ridge et géométrie
$$\mathbf{\hat{y}_\lambda} = \mathbf{U}\mathbf{D}\left(\mathbf{D}^T\mathbf{D} + \lambda\mathbf{I}\right)^{-1}\mathbf{D}^T\mathbf{U}^T \mathbf{y} = \sum_{d_j>0} \mathbf{u_j} \frac{d_j^2}{d_j^2 + \lambda} \mathbf{u_j}^T\mathbf{y}$$
- En présence de régularisation, $\lambda > 0$, les coordonnées, sur les axes principaux, de l'estimation $\mathbf{\hat{y}_\lambda}$ sont de plus en plus contractées lorsqu'on progresse vers les axes qui expliquent de moins en moins la variabilités des données.