Réaliser des tests t

Tests d'hypothèses en Python

James Chapman

Curriculum Manager, DataCamp

Problèmes à deux échantillons

  • Comparer des statistiques d’échantillon entre groupes d’une variable
  • converted_comp est une variable numérique
  • age_first_code_cut est une variable catégorielle avec niveaux ("child" et "adult")
  • Les utilisateurs ayant commencé enfants sont-ils mieux rémunérés que ceux ayant commencé adultes ?
Tests d'hypothèses en Python

Hypothèses

$H_{0}$ : La rémunération moyenne (en USD) est la même pour ceux ayant codé d’abord enfant et ceux ayant codé d’abord adulte.

$H_{0}$ : $\mu_{child} = \mu_{adult}$

$H_{0}$ : $\mu_{child} - \mu_{adult} = 0$

$H_{A}$ : La rémunération moyenne (en USD) est plus élevée pour ceux ayant codé d’abord enfant que pour ceux ayant codé d’abord adulte.

$H_{A}$ : $\mu_{child} > \mu_{adult}$

$H_{A}$ : $\mu_{child} - \mu_{adult} > 0$

Tests d'hypothèses en Python

Calcul des statistiques par groupe

stack_overflow.groupby('age_first_code_cut')['converted_comp'].mean()
age_first_code_cut
adult    111313.311047
child    132419.570621
Name: converted_comp, dtype: float64
Tests d'hypothèses en Python

Statistiques de test

  • La moyenne d’échantillon estime la moyenne de la population
  • $\bar{x}$ : une moyenne d’échantillon
  • $\bar{x}_{child}$ : moyenne d’échantillon de la rémunération pour ceux ayant codé enfants
  • $\bar{x}_{adult}$ : moyenne d’échantillon de la rémunération pour ceux ayant codé adultes
  • $\bar{x}_{child} - \bar{x}_{adult}$ : un statistique de test
  • score z : un statistique de test (standardisé)
Tests d'hypothèses en Python

Standardiser le statistique de test

$z = \dfrac{\text{stat. d’échantillon} - \text{paramètre de population}}{\text{erreur standard}}$

$t = \dfrac{\text{différence des stat. d’échantillon} - \text{différence des paramètres de population}}{\text{erreur standard}}$

$t = \dfrac{(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}}) - (\mu_{\text{child}} - \mu_{\text{adult}})}{SE(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}})}$

Tests d'hypothèses en Python

Erreur standard

$SE(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}}) \approx \sqrt{\dfrac{s_{\text{child}}^2}{n_{\text{child}}} + \dfrac{s_{\text{adult}}^2}{n_{\text{adult}}}}$

$s$ est l’écart type de la variable

$n$ est la taille de l’échantillon (nombre d’observations/lignes)

Tests d'hypothèses en Python

En supposant l’hypothèse nulle vraie

$t = \dfrac{(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}}) - (\mu_{\text{child}} - \mu_{\text{adult}})}{SE(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}})}$

$H_{0}$ : $\mu_{\text{child}} - \mu_{\text{adult}} = 0$    $\rightarrow$     $t = \dfrac{(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}}) }{SE(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}})}$

$t = \dfrac{(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}})}{\sqrt{\dfrac{s_{\text{child}}^2}{n_{\text{child}}} + \dfrac{s_{\text{adult}}^2}{n_{\text{adult}}}}}$

Tests d'hypothèses en Python

Calculs sous l’hypothèse nulle

xbar = stack_overflow.groupby('age_first_code_cut')['converted_comp'].mean()
adult    111313.311047
child    132419.570621
Name: converted_comp, dtype: float64 age_first_code_cut
s = stack_overflow.groupby('age_first_code_cut')['converted_comp'].std()
adult    271546.521729
child    255585.240115
Name: converted_comp, dtype: float64 age_first_code_cut
n = stack_overflow.groupby('age_first_code_cut')['converted_comp'].count()
adult    1376
child     885
Name: converted_comp, dtype: int64
Tests d'hypothèses en Python

Calcul du statistique de test

$t = \dfrac{(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}})}{\sqrt{\dfrac{s_{\text{child}}^2}{n_{\text{child}}} + \dfrac{s_{\text{adult}}^2}{n_{\text{adult}}}}}$

import numpy as np
numerator = xbar_child - xbar_adult
denominator = np.sqrt(s_child ** 2 / n_child + s_adult ** 2 / n_adult)
t_stat = numerator / denominator
1.8699313316221844
Tests d'hypothèses en Python

Passons à la pratique !

Tests d'hypothèses en Python

Preparing Video For Download...