bayesplain.compare_means

bayesplain.compare_means(x, y=None, prior='conventional', labels=None, equal_var=False, paired=False, log=False, unit='', n_draws=None, seed='unset', by=None, data=None)[source]

Compare the average of two groups.

With equal_var=False (the default) this is the Behrens-Fisher problem: each group’s mean has an exact Student-t posterior, and their difference is obtained by drawing from both and subtracting. Welch’s test already makes the same unequal-variance assumption on the frequentist side, so the two halves of the output are answering questions about the same model.

With equal_var=True the difference itself has a closed-form Student-t posterior and no sampling is needed at all.

With paired=True the two samples are two measurements on the same units, and the analysis is of the within-pair differences: an exact Student-t posterior for their average, paired with the paired t-test.

Parameters:
x : array_like or str

The two samples, or the names of two columns in data. The difference is oriented as y - x. Leave y out and pass by= instead for data in long format.

y : array_like or str

The two samples, or the names of two columns in data. The difference is oriented as y - x. Leave y out and pass by= instead for data in long format.

prior : str, float, or EffectSizePrior, default 'conventional'

Cauchy prior on standardised effect size. Affects the Bayes factor only.

labels : sequence of str, optional

Group names. Defaults to the column names or group labels when there are any, otherwise ('group 1', 'group 2').

equal_var : bool, default False

Pool the variances. Off by default, because assuming two groups have identical spread is an assumption people make out of habit rather than belief. Has no meaning for paired data.

paired : bool, default False

Treat x[i] and y[i] as two measurements on the same unit – the same intersection before and after, the same tract in two years. Pairs with either value missing are dropped. Pairing removes the variation between units that both measurements share, and the summary says how much narrower that makes the interval.

log : bool, default False

Compare on the log scale and report the ratio of typical (geometric-mean) values, y over x. For skewed, strictly positive data such as prices or incomes, where “B is 1.3 times A” is both the better-behaved analysis and the better sentence.

unit : str, optional

Display unit for the difference. Ignored with log=True, where the answer is a ratio.

n_draws : int, optional

Number of draws. Defaults to the package setting.

seed : int or None, optional

Seed for the draws.

by : array_like or str, optional

Group labels splitting x into exactly two groups, for data in long format, e.g. compare_means("value", by="district", data=df). The groups are taken in sorted order of their labels, so the difference is second minus first.

data : data frame, optional

Where to look up x, y and by when they are column names.

Returns:

Call .summary() for the full report.

Return type:

Result

Examples

>>> import numpy as np, bayesplain as bp
>>> rng = np.random.default_rng(0)
>>> a, b = rng.normal(30, 8, 60), rng.normal(34, 11, 55)
>>> res = bp.compare_means(a, b, labels=["Route A", "Route B"], unit="minutes")
>>> res.probability(">", 0) > 0.9
True

The same twelve intersections, measured before and after a signal retiming. Every one improved a little, but the intersections differ from each other far more than that, so only the paired analysis can see it:

>>> before = np.array([41, 55, 38, 62, 47, 51, 36, 58, 44, 49, 53, 40.0])
>>> after = before - np.array([3, 2, 4, 1, 3, 2, 5, 2, 3, 4, 2, 3.0])
>>> paired = bp.compare_means(before, after, paired=True)
>>> round(paired.probability("<", 0), 3)
1.0
>>> round(bp.compare_means(before, after).probability("<", 0), 2)
0.77