Skip to content

Inference from Summary Statistics

Standard error, t- and z-based confidence intervals, margin of error, manual t-tests and sample-size planning computed from summary statistics (mean, standard deviation, n).

compute_sem(std_dev, n)

Compute the Standard Error of the Mean (SEM).

Parameters:

Name Type Description Default
std_dev float

Sample standard deviation.

required
n int

Sample size.

required

Returns:

Type Description
float

SEM = std_dev / sqrt(n).

Source code in utils/inference_utils.py
def compute_sem(std_dev: float, n: int) -> float:
    """
    Compute the Standard Error of the Mean (SEM).

    Parameters
    ----------
    std_dev : float
        Sample standard deviation.
    n : int
        Sample size.

    Returns
    -------
    float
        SEM = std_dev / sqrt(n).
    """
    return std_dev / np.sqrt(n)

confidence_interval(mean, std_dev, n, confidence=0.95)

Compute a t-based confidence interval from summary statistics.

Parameters:

Name Type Description Default
mean float

Sample mean.

required
std_dev float

Sample standard deviation.

required
n int

Sample size.

required
confidence float

Confidence level (e.g., 0.95 for a 95% CI).

0.95

Returns:

Type Description
tuple of (float, float)

(lower, upper) confidence interval bounds.

Source code in utils/inference_utils.py
def confidence_interval(
    mean: float, std_dev: float, n: int, confidence: float = 0.95
) -> tuple[float, float]:
    """
    Compute a t-based confidence interval from summary statistics.

    Parameters
    ----------
    mean : float
        Sample mean.
    std_dev : float
        Sample standard deviation.
    n : int
        Sample size.
    confidence : float, default=0.95
        Confidence level (e.g., 0.95 for a 95% CI).

    Returns
    -------
    tuple of (float, float)
        (lower, upper) confidence interval bounds.
    """
    sem = compute_sem(std_dev, n)
    df = n - 1
    t_crit = stats.t.ppf((1 + confidence) / 2.0, df)
    margin = t_crit * sem
    return (mean - margin, mean + margin)

z_confidence_interval(mean, pop_std, n, confidence=0.95)

Compute a Z-based confidence interval using the known population standard deviation.

Parameters:

Name Type Description Default
mean float

Sample mean.

required
pop_std float

Known population standard deviation.

required
n int

Sample size.

required
confidence float

Confidence level.

0.95

Returns:

Type Description
tuple of (float, float)

(lower, upper) confidence interval bounds.

Notes

Use this when population variance is known (e.g., large samples or known process). For unknown variance, use confidence_interval() which uses the t-distribution.

Source code in utils/inference_utils.py
def z_confidence_interval(
    mean: float, pop_std: float, n: int, confidence: float = 0.95
) -> tuple[float, float]:
    """
    Compute a Z-based confidence interval using the known population standard deviation.

    Parameters
    ----------
    mean : float
        Sample mean.
    pop_std : float
        Known population standard deviation.
    n : int
        Sample size.
    confidence : float, default=0.95
        Confidence level.

    Returns
    -------
    tuple of (float, float)
        (lower, upper) confidence interval bounds.

    Notes
    -----
    Use this when population variance is known (e.g., large samples or known process).
    For unknown variance, use confidence_interval() which uses the t-distribution.
    """
    z_crit = stats.norm.ppf((1 + confidence) / 2.0)
    margin = z_crit * (pop_std / np.sqrt(n))
    return (mean - margin, mean + margin)

compute_t_stat(sample_mean, pop_mean, sample_std, n)

Compute the one-sample t-statistic from summary statistics.

Parameters:

Name Type Description Default
sample_mean float

Observed sample mean.

required
pop_mean float

Hypothesized population mean (null hypothesis).

required
sample_std float

Sample standard deviation.

required
n int

Sample size.

required

Returns:

Type Description
float

t-statistic = (sample_mean - pop_mean) / (sample_std / sqrt(n)).

Source code in utils/inference_utils.py
def compute_t_stat(sample_mean: float, pop_mean: float, sample_std: float, n: int) -> float:
    """
    Compute the one-sample t-statistic from summary statistics.

    Parameters
    ----------
    sample_mean : float
        Observed sample mean.
    pop_mean : float
        Hypothesized population mean (null hypothesis).
    sample_std : float
        Sample standard deviation.
    n : int
        Sample size.

    Returns
    -------
    float
        t-statistic = (sample_mean - pop_mean) / (sample_std / sqrt(n)).
    """
    return (sample_mean - pop_mean) / (sample_std / np.sqrt(n))

manual_t_test(sample_mean, pop_mean, sample_std, n)

Perform a one-sample t-test manually from summary statistics.

Parameters:

Name Type Description Default
sample_mean float

Observed sample mean.

required
pop_mean float

Hypothesized population mean (null hypothesis).

required
sample_std float

Sample standard deviation.

required
n int

Sample size.

required

Returns:

Type Description
tuple of (float, float)

(t_stat, p_value) for a two-tailed test.

Source code in utils/inference_utils.py
def manual_t_test(
    sample_mean: float, pop_mean: float, sample_std: float, n: int
) -> tuple[float, float]:
    """
    Perform a one-sample t-test manually from summary statistics.

    Parameters
    ----------
    sample_mean : float
        Observed sample mean.
    pop_mean : float
        Hypothesized population mean (null hypothesis).
    sample_std : float
        Sample standard deviation.
    n : int
        Sample size.

    Returns
    -------
    tuple of (float, float)
        (t_stat, p_value) for a two-tailed test.
    """
    t_stat = compute_t_stat(sample_mean, pop_mean, sample_std, n)
    df = n - 1
    p_value = 2 * (1 - stats.t.cdf(abs(t_stat), df))
    return t_stat, p_value

margin_of_error(std_dev, n, confidence=0.95)

Compute the margin of error for a t-based confidence interval.

Parameters:

Name Type Description Default
std_dev float

Sample standard deviation.

required
n int

Sample size.

required
confidence float

Confidence level.

0.95

Returns:

Type Description
float

Margin of error (half-width of the confidence interval).

Source code in utils/inference_utils.py
def margin_of_error(std_dev: float, n: int, confidence: float = 0.95) -> float:
    """
    Compute the margin of error for a t-based confidence interval.

    Parameters
    ----------
    std_dev : float
        Sample standard deviation.
    n : int
        Sample size.
    confidence : float, default=0.95
        Confidence level.

    Returns
    -------
    float
        Margin of error (half-width of the confidence interval).
    """
    sem = compute_sem(std_dev, n)
    t_crit = stats.t.ppf((1 + confidence) / 2.0, n - 1)
    return t_crit * sem

compute_sample_size(effect_size, alpha=0.05, power=0.8)

Compute required sample size for a given effect size, alpha, and power. Uses one-sample t-test power analysis.

Source code in utils/inference_utils.py
def compute_sample_size(effect_size, alpha=0.05, power=0.8):
    """
    Compute required sample size for a given effect size, alpha, and power.
    Uses one-sample t-test power analysis.
    """
    analysis = TTestPower()
    return analysis.solve_power(effect_size=effect_size, alpha=alpha, power=power)