import polars as pl
import scipy as sp
import numpy as npSingle Sample t-test
# dataset on penguins
# copy from lecture notes: ci example
url = "https://roualdes.sfo3.digitaloceanspaces.com/data/penguins.csv"
df = pl.read_csv(url, null_values = ["NA"]) # this is the trickbill_length = df.select("bill_length_mm").drop_nulls().to_numpy()m = np.mean(bill_length)
s = np.std(bill_length)
N = np.size(bill_length)
t = (m - 43) / (s / np.sqrt(N)) # t^*, test-statistic2 * (1 - sp.stats.t(df = N - 1).cdf(np.abs(t))) # p-valuenp.float64(0.0019152230811219084)
Because the p-value < alpha = 0.05, reject H_0. There is evidence that the mean bill length of penguins is not 43mm.
Paired t-test
ndf = (df
.select("bill_length_mm", "bill_depth_mm")
.drop_nulls()
.with_columns((pl.col("bill_length_mm") - pl.col("bill_depth_mm")).alias("d")) # create new cols based on old cols
)d = ndf["d"].to_numpy()
m = np.mean(d)
s = np.std(d)
N = np.size(d)
t = (m - 0) / (s / np.sqrt(N))
2 * (1 - sp.stats.t(df = N - 1).cdf(np.abs(t))) # p-valuenp.float64(0.0)
Based on the data, because the p-value < level of significance, the mean difference (bill_length - bill_depth) is not equal to 0.
t = sp.stats.t(df = N - 1).ppf([0.025, 0.975])
m + t * s / np.sqrt(N)array([26.10942588, 27.43209458])
Two Sample t-test
sdf = (df
.select("species", "bill_length_mm")
.drop_nulls()
)
da = sdf.filter(pl.col("species") == "Adelie")["bill_length_mm"].to_numpy()
dc = sdf.filter(pl.col("species") == "Chinstrap")["bill_length_mm"].to_numpy()sp.stats.ttest_ind(da, dc)TtestResult(statistic=np.float64(-23.801939237440887), pvalue=np.float64(2.011759018655462e-62), df=np.float64(217.0))
Because p-value < 0.0001 < alpha = 0.05, we reject H0. There is sufficient evidence to claim that the difference of mean bill length between Adelie and Chinstrap penguins is not equal to zero. On average, Chinstrap and Adelie penguins have differen bill lengths.