ANALYSIS TOOL BOX

Statistics

CalculateConfidenceIntervalOfMean

Calculate and visualize the confidence interval for a sample mean.

statisticsdescriptive-statssummaryinference

Introduction

A sample mean is a point estimate, not the truth — the real question is how far off it could plausibly be from the population mean it's standing in for. CalculateConfidenceIntervalOfMean answers that from summary statistics alone: it computes a confidence interval for the population mean, automatically choosing the t-distribution for small samples (n < 30) or the z-distribution for larger ones, and optionally plots the inferred sampling distribution so the width of the uncertainty is visible, not just a printed range.

Teaching Note

Calculating confidence intervals is essential for:

  • Estimating population parameters from sample data with quantified uncertainty.
  • Epidemiology: Determining the mean incubation period of a pathogen in a clinical study.
  • Healthcare: Assessing the average reduction in blood pressure across a patient cohort.
  • Intelligence Analysis: Estimating the mean frequency of communication events from a monitored source.
  • Data Science: Evaluating the average processing time of a distributed system.
  • Quality Control: Monitoring the mean weight or dimensions of manufactured components.
  • Finance: Estimating the average daily return of an asset over a specific period.
  • Social Science: Analyzing the mean response score from a public opinion survey.

Parameters

ParameterTypeDefaultDescription
sample_meanrequiredfloatThe arithmetic mean observed in the sample data.
sample_sdrequiredfloatThe standard deviation of the sample, used to estimate the population standard deviation.
sample_sizerequiredintThe number of observations in the sample. This determines whether a t-stat or z-stat is used.
confidence_intervalfloat0.95The desired confidence level, expressed as a decimal between 0 and 1. Defaults to 0.95.
plot_sample_distributionboolTrueWhether to generate and display a histogram showing the inferred sampling distribution of the mean. Defaults to True.
value_namestr'Possible Mean'A descriptive name for the variable being analyzed, used as the x-axis label in the plot. Defaults to 'Possible Mean'.
fill_colorstr'#999999'The hex color code for the bars in the distribution plot. Defaults to '#999999'.
fill_transparencyfloat0.6The alpha transparency level for the plot's histogram bars (0 to 1). Defaults to 0.6.
title_for_plotstr'Confidence Interval of Mean'The primary title text displayed at the top of the plot. Defaults to 'Confidence Interval of Mean'.
subtitle_for_plotstr'Shows the distribution of the sample mean.'Descriptive subtitle text displayed below the main title.
caption_for_plotstrNoneAn optional descriptive caption or note displayed at the bottom of the plot. Defaults to None.
data_source_for_plotstrNoneText indicating the source of the data, appended to the caption. Defaults to None.
show_y_axisboolFalseWhether to display the frequency/density scale on the y-axis. Defaults to False.
title_y_indentfloat1.1Vertical position offset for the plot title. Defaults to 1.10.
subtitle_y_indentfloat1.05Vertical position offset for the plot subtitle. Defaults to 1.05.
caption_y_indentfloat-0.15Vertical position offset for the plot caption. Defaults to -0.15.
figure_sizetuple(8, 6)The dimensions of the output figure as a (width, height) tuple in inches. Defaults to (8, 6).

Returns

None — the function prints the calculated confidence interval to the console and displays a plot if plot_sample_distribution is True.

Example

python
from analysistoolbox.statistics import CalculateConfidenceIntervalOfMean

# Epidemiology: estimating mean recovery time for a viral infection
CalculateConfidenceIntervalOfMean(
    sample_mean=14.2,
    sample_sd=3.5,
    sample_size=45,
    confidence_interval=0.99,
    value_name="Days to Recovery",
    title_for_plot="99% CI for Mean Recovery Time"
)