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.
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
| Parameter | Type | Default | Description |
|---|---|---|---|
sample_meanrequired | float | — | The arithmetic mean observed in the sample data. |
sample_sdrequired | float | — | The standard deviation of the sample, used to estimate the population standard deviation. |
sample_sizerequired | int | — | The number of observations in the sample. This determines whether a t-stat or z-stat is used. |
confidence_interval | float | 0.95 | The desired confidence level, expressed as a decimal between 0 and 1. Defaults to 0.95. |
plot_sample_distribution | bool | True | Whether to generate and display a histogram showing the inferred sampling distribution of the mean. Defaults to True. |
value_name | str | 'Possible Mean' | A descriptive name for the variable being analyzed, used as the x-axis label in the plot. Defaults to 'Possible Mean'. |
fill_color | str | '#999999' | The hex color code for the bars in the distribution plot. Defaults to '#999999'. |
fill_transparency | float | 0.6 | The alpha transparency level for the plot's histogram bars (0 to 1). Defaults to 0.6. |
title_for_plot | str | 'Confidence Interval of Mean' | The primary title text displayed at the top of the plot. Defaults to 'Confidence Interval of Mean'. |
subtitle_for_plot | str | 'Shows the distribution of the sample mean.' | Descriptive subtitle text displayed below the main title. |
caption_for_plot | str | None | An optional descriptive caption or note displayed at the bottom of the plot. Defaults to None. |
data_source_for_plot | str | None | Text indicating the source of the data, appended to the caption. Defaults to None. |
show_y_axis | bool | False | Whether to display the frequency/density scale on the y-axis. Defaults to False. |
title_y_indent | float | 1.1 | Vertical position offset for the plot title. Defaults to 1.10. |
subtitle_y_indent | float | 1.05 | Vertical position offset for the plot subtitle. Defaults to 1.05. |
caption_y_indent | float | -0.15 | Vertical position offset for the plot caption. Defaults to -0.15. |
figure_size | tuple | (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
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"
)