Académique Documents
Professionnel Documents
Culture Documents
0.8
Statistiques descriptives
I Analyse univariée
I Analyse bivariée : dépendance linéaire
Les données
n couple d’observations:
I xi ∈ IR : la variable explicative
I yi ∈ IR : la variable à expliquer, prédire
Le problème
I Trouver la droite qui représente au mieux la relation linéaire
yb = f (x) ' ax + b
ybi = axi + b + εi
I Modélise le bruit
3 / 28
20
5
18
16 3 4
14
12 2
10
8 1
6 0
1 2 3 4 5 6
I a ' 2, 73
I b ' 2.95
I εi ' [−0.197, −1.840, 0.781, 3.670, −1.339, −1.076]
4 / 28
Le Modèle Linéaire
5 / 28
Résumé du vocabulaire
I Variables explicatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x ∈ IR
I Variable à expliquer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . y ∈ IR
I Erreur ou bruit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ε
I Paramètres scalaires . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . a, b ∈ IR,
I Paramètres (forme vectorielle) . . . . . . . . . . . . . . . . . . . . . . a ∈ IR2
I Modèle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . y = f (x, a) + ε
I Estimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .as tar
I Prédiction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . yb = f (x, as tar)
I Variables aléatoires . . . . . . . . . . . . . . . ε et donc y et donc as tar
y = ax + }b + ε
| {z
f (x,a)
6 / 28
Un exemple : l’étalonnage d’un capteur
Les différentes phases de la régression
7 / 28
Les moindres carrés pour la régression simple
8 / 28
Moindres carrés
Fonction objectif
min J(a, b)
a,b
avec
n
X 2
J(a, b) = yi − axi − b
| {z }
i=1 εi
Interprétation
Ce problème peut s’interpréter comme la recherche de la droite
d’équation ax + b passant “au mieux” (au sens des moindres
carrés) parmi le nuage des observations (xi , yi ) , i = 1, . . . , n.
9 / 28
10 / 28
10 / 28
10 / 28
10 / 28
Le problème des moindres carrés
Les données dont nous disposons peuvent aussi être vue comme un
système de n équations à 2 + n inconnues (a, b et les εi ). Ce
système s’écrit de la manière suivante :
ax1 + b + ε1 = y1
.. ..
. .
axi + b + εi = yi
.. ..
. .
axn + b + εn = yn
11 / 28
Calcul du gradient
n
1X
arg min J(a, b) avec J(a, b) = (axi + b − yi )2
a,b 2 i=1
Méthode du gradient
I (a? , b? ) est solution du problème
? ?
∂J(a , b ) = 0
arg min J(a, b) ⇔ ∂a? , b? )
a,b
∂J(a
=0
∂b
12 / 28
Dérivées partielles de J(a, b)
n n
∂J(a, b) X X
ax2i + bxi − yi xi
= (axi + b − yi ) xi =
∂a
i=1 i=1
n
X n
X n
X
= ax2i + bxi − yi xi
i=1 i=1 i=1
Xn Xn Xn
=a x2i +b xi − yi xi
i=1 i=1 i=1
n n n n
∂J(a, b) X X X X
= (axi + b − yi ) = axi + b− yi
∂b
i=1 i=1 i=1 i=1
Xn n
X
=a xi + bn − yi
i=1 i=1
13 / 28
Calcul de a? et b?
n
X n
X n
X
? ? ?
x2i b?
∂J(a , b ) = 0
a + xi = yi xi (1)
∂a
? , b? ) ⇔ i=1
n
i=1 i=1
n
∂J(a X X
=0 a? + b? n
∂b xi = yi (2)
i=1 i=1
14 / 28
Calcul de a? et b?
Calcul de a?
I (1) ∗ n − (2) ∗ ni=1 xi
P
P n n n n
X 2 X X X
I a? n ni=1 x2i − xi =n yi xi − yi xi
i=1 i=1 i=1 i=1
n
X Xn n
X
n yi x i − yi xi
I a? = i=1 i=1 i=1
n n
X X 2
n x2i − xi
i=1 i=1
Calcul nde b? n
X X
?
yi − a xi
? i=1 i=1
b =
n
15 / 28
Réécrivons la solution
n n n
1X 1X 1X
Soit x = xi , y = yi et Vx = (xi − x)2 .
n n n
i=1 i=1 i=1
n n n n n n
X X X 1X 1X 1X
n yi xi − yi xi yi xi − yi xi
n n n
a? = i=1
n
i=1
n
i=1
= i=1
n
i=1
n
i=1
X X 2 1 X 2 1 X 2
n x2i − xi xi − xi
n n
i=1 i=1 i=1 i=1
n n
1X 1X
yi x i − y x (yi − y) (xi − x)
n n
i=1 i=1
= n = n
1X 2 2 1X
xi −x (xi − x)2
n n
i=1 i=1
cov(x, y)
a? =
Vx
16 / 28
n
X n
X
yi − a? xi
b? = i=1
n
i=1
n n
1X 1X
= yi − a? xi
n n
i=1 i=1
= y − a? x
17 / 28
Le théorème des moindres carrés
Théorème : Théorème des moindres carrées
Modèle prédictif
I Dépend d’une observation x, et des paramètres a? et b?
I Prédire une nouvelle observation x :
yb = f (x) = a? x + b?
19 / 28
Mise en oeuvre
mx = np.mean(x) 3.4
my = np.mean(y) 4
3.2
sxx = np.sum((x-mx)**2) 3.0
x y yp εi
0.1 2.3196 2.0681 0.2514
0.3 2.1000 2.3061 −0.2061
0.7 2.5836 2.7820 −0.1984
0.8 2.7000 2.9010 −0.2010
0.95 3.4335 3.0795 0.3540
20 / 28
Le poids des observations
X X
xi − x̄
wi = Pn 2 mesure le poids de l’observation xi dans
i=1 (xi −x̄)
le calcul de a? .
21 / 28
Remarques
a? et corrélation
a? = covs(x,y)
2 = cov (x,y) sy
sx sx sy =
cov(x,y) sy = cor(x, y) sy
sx sy sx sx
x
Droite de régression
y = a? x + b?
= a? x + ȳ − a? x̄
= a? (x − x̄) + ȳ = covs(x,y)
2 (x − x̄) + ȳ
x
cov(x, y)
f (x̄) = a? x̄ + b? = (x − x̄) +ȳ
s2x | {z }
=0
22 / 28
Coefficient de détermination R2
R2
Le coefficient de détermination R2 ∈ {0, 1} est le rapport de
l’écart expliqué versus l’écart total
Pn
2 (ybi − ȳ)2
R = Pni=1 2
i=1 (yi − ȳ)
23 / 28
Prédiction et erreur de prédiction
24 / 28
Intervalle de prédiction
Théorème : Intervalle de prédicition
y=ax+b
Intervalle
de confiance
Pt suspect
25 / 28
Calculer t α2 avec scipy
26 / 28
Conclusion
La régression simple
I observation = modèle + bruit
I modèlisation linéaire d’une relation
I certains modèles non linéaires peuvent être linéarisés
(projection)
I Moindres carrés : facile à calculer
La suite
I Passer au multi dimensionnel (plusieurs variables explicatives)
I Valider le modèle : diagnostic de la régression
27 / 28
Éléments de démonstration de V ar(yx )
Pn
(y − ȳ) (xi − x̄)
?
V (a ) = V i=1Pn i 2
i=1 (xi − x̄) !
2 X n
= Pn 1 2 V (a(xi − x̄) + ε) (xi − x̄)
i=1 (xi −x̄)
i=1 !
2 X n
1
= Pn 2 V ε (xi − x̄)
i=1 (xi −x̄)
i=1
2
σ
= Pn 2
i=1 (xi − x̄)
D’où
V (yx ) = V (ȳ) + V (a? )(x − x̄)2 + σ 2
σ2 σ2
= + Pn 2
(x − x̄)2 + σ 2
n i=1 (x i − x̄)
1 (x − x̄)2
= σ 2 1 + + Pn 2
n i=1 (xi − x̄)