Vous êtes sur la page 1sur 31

Régression Linéaire Simple

Benoit Gaüzère, Stéphane Canu


benoit.gauzere@insa-rouen.fr

INSA Rouen Normandie - ITI

April 14, 2022


Observation de deux variables

0.8

Élévation température moyenne


0.6
0.4
0.2
0.0
0.2
0.4
0.6
1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
Concentration en CO2

Statistiques descriptives
I Analyse univariée
I Analyse bivariée : dépendance linéaire

Comment expliciter cette relation ?


2 / 28
Le problème de la régression 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

I Mais les points ne sont pas forcément sur la droite

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

Définition : Modèle Linéaire

Le modèle linéaire pose la relation suivante entre la variable


explicative x et la variable à expliquer y avec les paramètres
inconnus (a, b, ε)

y = ax + b + ε avec a = (a, b) ∈ IR2

I Hypothèse : Observations = modèle + bruit


I ε ∼ N (0, σ 2 )

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

On recherche a et b qui minimisent simultanément tous les εi


n
X 2
J(a, b) = yi − axi − b
| {z }
i=1 εi

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?

Deux équations linéaires à deux inconnues

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

Soit (xi , yi ), i = 1, n un ensemble de couples d’observations.


La solution du problème de minimisation de la somme des
carrées des erreurs
n
X
min (axi + b − yi )2
a,b
i=1

est donnée par a? et b? définis par :


n
X
(yi − y) (xi − x)
? i=1 cov(x, y)
a = n = et b? = y − a? x
X Vx
(xi − x̄)2
i=1
a? et b? sont les estimateurs au sens des moindres carrés.
18 / 28
En Résumé

Apprentissage des paramètres


I Dépend de x et y
cov(x, y)
I a? =
Vx
I b? = y − a? x

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?

I Erreur de prédiction ε = yi − ybi

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

sxy = (x-mx).T @ (y-my) 2.8 3


2
a = sxy/sxx 2.6
2.4
b = my - a*mx
2.2 0 1
yp = a*x+b
2.0
e = y - yp 0.2 0.4 0.6 0.8

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

Influence des observations dans le calcul de a?


n
X n
X
(xi − x̄) (yi − ȳ) (xi − x̄) yi
i=1 P i=1
a? = n 2 = Pn 2
i=1 (xi −x̄) i=1 (xi −x̄)
n
X xi − x̄
= Pn 2 yi
i=1 i=1 (xi − x̄)
n
X n
X
= g(xi ) yi = wi yi
i=1 i=1

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

I La droite de régression passe par le point (x̄, ȳ):

cov(x, y)
f (x̄) = a? x̄ + b? = (x − x̄) +ȳ
s2x | {z }
=0

22 / 28
Coefficient de détermination R2

Quelle quantité de la relation est expliquée ?


I Écart expliqué par le modèle : ni=1 (ybi − ȳ)2
P

I Écart total : ni=1 (yi − ȳ)2


P

I Écart résiduel : ni=1 (yi − ybi )2


P

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

Erreurs et variable aléatoire


I Les εi suivent une loi normale N (0, σ 2 )
c2 = 1 Pn ε2 = 1 Pn (yi − a? xi − b? )2
I σ n−2 i=1 i n−2 i=1

yx est également une v.a.


I yx = a? x + b? + ε = ȳ + a? (x − x̄) + ε
I IE(y
\ ? ? ?
x ) = a x + b = ȳ + a (x − x̄)
 2

I V (yx ) = σ 2 1 + n1 + Pn(x−x̄)
(xi −x̄)2
i=1

24 / 28
Intervalle de prédiction
Théorème : Intervalle de prédicition

Pour un x donné, il est probable, avec une probabilité 1 − α, que


s
n
? 1 (x − x̄)2 o
yx ∈ ȳ + a (x − x̄) ± t α2 σ 1+ + Pn 2
i=1 (xi − x̄)
b
n

où tα vérifie, relativement à la loi de Student à n − 2 degrès


de libertés, P (T ≤ tα ) = α.
Plus on s’éloigne de la moyenne des x, plus l’incertitude grandit
y

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̄)

Vous aimerez peut-être aussi