typestar

When normality is doubtful in Python

Rank-based tests make fewer assumptions than a t-test.

import numpy as np
from scipy import stats

rng = np.random.default_rng(2)
skewed = rng.exponential(scale=20, size=30)
other = rng.exponential(scale=30, size=30)

print(round(stats.shapiro(skewed).pvalue, 5))
print(round(stats.mannwhitneyu(skewed, other).pvalue, 5))
print(round(stats.wilcoxon(skewed, other).pvalue, 5))
print(round(stats.ks_2samp(skewed, other).pvalue, 5))
print(round(stats.kruskal(skewed, other).pvalue, 5))

How it works

  1. mannwhitneyu is the rank alternative to ttest_ind.
  2. wilcoxon is the paired version.
  3. shapiro and ks_2samp test the assumptions themselves.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
427
Tokens
134
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 77 seconds.

Type this snippet

Step 2 of 4 in Hypothesis tests, step 5 of 23 in Scientific computing with SciPy.

← Previous Next →