diff --git a/09_puissance_iteree_valeur_propre.Rmd b/09_puissance_iteree_valeur_propre.Rmd index 4a52648..acc1498 100644 --- a/09_puissance_iteree_valeur_propre.Rmd +++ b/09_puissance_iteree_valeur_propre.Rmd @@ -36,10 +36,10 @@ Considérons la séquence $\mathbf{u^{(0)}}, \mathbf{A}\mathbf{u^{(0)}}, \mathbf \begin{equation} \mathbf{A}^m\mathbf{u^{(0)}} = \lambda_1^m \mathbf{u_1} + \lambda_2^m \mathbf{u_2} + \dots + \lambda_r^m \mathbf{u_r} -\label{eq:1} +\label{eq:09_1} \end{equation} -Supposons par ailleurs que les valeurs propres soient ordonnées et que la première valeur propre soit strictement la plus grande en valeur absolue : $|\lambda_1|>|\lambda_2|\geq\dots\geq|\lambda_r|$. En divisant les deux membres de l'égalité (\ref{eq:1}) par $\lambda_1^m$, nous avons : +Supposons par ailleurs que les valeurs propres soient ordonnées et que la première valeur propre soit strictement la plus grande en valeur absolue : $|\lambda_1|>|\lambda_2|\geq\dots\geq|\lambda_r|$. En divisant les deux membres de l'égalité (\ref{eq:09_1}) par $\lambda_1^m$, nous avons : \[ (\lambda_1^{-1}\mathbf{A})^m\mathbf{u^{(0)}}=\mathbf{u_1} + \sum_{i=2}^{r}\left(\frac{\lambda_i}{\lambda_1}\right)^m\mathbf{u_i} \] diff --git a/18_kernel_ridge_regression.Rmd b/18_kernel_ridge_regression.Rmd index 88b8f97..fa079b6 100644 --- a/18_kernel_ridge_regression.Rmd +++ b/18_kernel_ridge_regression.Rmd @@ -33,7 +33,7 @@ plot(x=d, y=k(1,d), type="l") Nous proposons ensuite de représenter la relation entre les observations et la cible par une combinaison linéaire des similarités d'une nouvelle observation $\mathbf{x}$ avec chaque observation du jeu d'entraînement : \begin{equation} f(\mathbf{x}) = \sum_{i=1}^{n} \alpha_i k(\mathbf{x},\mathbf{x_i}) -\label{eq:1} +\label{eq:18_1} \end{equation} Plus $\mathbf{x}$ est proche de $\mathbf{x_i}$, plus $\mathbf{x_i}$ pèse dans le calcul de la valeur prédite pour $\mathbf{x}$. Chaque $k(\cdot,\mathbf{x_i})$ est une fonction gaussienne et $f$ est une superposition de fonctions gaussiennes. @@ -108,7 +108,7 @@ $$ & \hat{y} = \sum_{i=1}^{n} \alpha_i k(\mathbf{x},\mathbf{x_i}) \\ \end{aligned} $$ -Nous retrouvons l'équation (\ref{eq:1}). Nous pouvons écrire ce résultat sous forme matricielle en introduisant la matrice noyau $\mathbf{K}$. +Nous retrouvons l'équation (\ref{eq:18_1}). Nous pouvons écrire ce résultat sous forme matricielle en introduisant la matrice noyau $\mathbf{K}$. $$ \begin{aligned} & \hat{y} = \sum_{i=1}^{n} \alpha_i k(\mathbf{x},\mathbf{x_i}) \\ diff --git a/19_nystroem_approximation.Rmd b/19_nystroem_approximation.Rmd index 50496c5..710b1fd 100644 --- a/19_nystroem_approximation.Rmd +++ b/19_nystroem_approximation.Rmd @@ -162,7 +162,7 @@ $$ \end{aligned} $$ -## Calcul de l'approximation de Nyström dans le cadre d'une régression ridge à noyau +## Régression ridge à noyau et approximation de Nyström Dans le cadre de la régression ridge à noyau (voir un précédent module), nous notons : $\mathbf{G} = \mathbf{K} + \lambda\mathbf{I_n}$. Les coefficients du modèle ridge sont alors donnés par : $\boldsymbol\alpha_\lambda = \mathbf{G}^{-1} \mathbf{y}$. Nous cherchons à calculer efficacement $\mathbf{G}^{-1}$ à partir d'une approximation Nyström de rang $m$ de $\mathbf{K} \approx \mathbf{L}\mathbf{L}^T$. Pour ce faire, nous utilisons une forme de l'identité de Woodbury : $$ diff --git a/23_exercices.R b/23_exercices.R new file mode 100644 index 0000000..5adb796 --- /dev/null +++ b/23_exercices.R @@ -0,0 +1,85 @@ +rm(list=ls()) +set.seed(1123) + +source('15_loocv.R') + +multdiag <- +function(X,d) +{ + R <- matrix(NA, nrow=dim(X)[1], ncol=dim(X)[2]) + for (i in 1:dim(X)[2]) { R[,i]=X[,i]*d[i] } + return(R) +} + +n <- 700 +p <- 55 + +sd <- 6 # standard deviation for zero-mean gaussian noise +X <- matrix(runif(n*p),nrow=n,ncol=p) +X <- scale(X) +beta <- runif(p, min=-10, max=10) +y <- X%*%beta + rnorm(n, mean=0, sd=sd) + +lambdas <- 10^seq(-1,3,by=0.2) + +var <- +function(lambda) +{ + d <- (Xs$d^2)/(Xs$d^2 + lambda)^2 + var <- multdiag(Xs$v,d) + var <- sd^2 * tcrossprod(var,Xs$v) +} + +bias <- +function(lambda) +{ + d <- lambda/(Xs$d^2+lambda) + bias <- multdiag(Xs$v,d) + bias <- bias %*% crossprod(Xs$v,beta) +} + +epeVar <- +function(lambda) +{ + var <- var(lambda) + return( mean(rowSums(X*(X%*%var))) ) +} + +epeBias <- +function(lambda) +{ + bias <- bias(lambda) + return( mean((X%*%bias)^2) ) +} + +epe <- +function(lambda) +{ + return( epeVar(lambda) + epeBias(lambda) + sd^2 ) +} + +#rm <- ridge(X, y, lambdas) +#Xs <- svd(X) +#epes <- sapply(lambdas, epe) + +X.init <- X +beta.init <- beta + +ps <- seq(1,p) +lps <- length(ps) +epes <- numeric(lps) +biass <- numeric(lps) +vars <- numeric(lps) +maes <- numeric(lps) + +for (k in ps) +{ + X <- X.init[,1:k] + beta <- beta.init[1:k] + rm <- ridge(X, y, lambdas) + Xs <- svd(X) + epes[k] <- epe(rm$lambda) + biass[k] <- epeBias(rm$lambda) + vars[k] <- epeVar(rm$lambda) + maes[k] <- rm$mae +} \ No newline at end of file diff --git a/make_chapter b/make_chapter new file mode 100644 index 0000000..7cba288 --- /dev/null +++ b/make_chapter @@ -0,0 +1,4 @@ +#!/bin/bash + +arg="library(bookdown);bookdown::preview_chapter(\"$1\")" +Rscript -e $arg \ No newline at end of file diff --git a/pad.R b/pad.R index 02a77a1..c5dc576 100644 --- a/pad.R +++ b/pad.R @@ -1,4 +1,6 @@ # Font /mnt/font/InputMonoNarrow-Regular/20a/font # rm(list=ls()) # bookdown::render_book() -# :/^\# \ No newline at end of file +# :/^\# + +# bash make_chapter 19_nystroem_approximation.Rmd \ No newline at end of file