ANALYSIS TOOL BOX

Statistics

CalculateConfidenceIntervalOfProportion

Calculate and visualize the confidence interval for a sample proportion.

statisticsdescriptive-statssummaryinference

Introduction

A conversion rate of 8% from 1,200 visitors and a vaccination rate of 72% from 500 respondents both sound precise, but neither is exact — the real population proportion could plausibly sit somewhere in a range around that observed value. CalculateConfidenceIntervalOfProportion computes that range directly from summary statistics, using the t-distribution for small samples (n < 30) and the z-distribution for larger ones, and can plot the sampling distribution so the margin of error is visible rather than just a printed number.

Teaching Note

Calculating confidence intervals for proportions is essential for:

  • Estimating the prevalence of a characteristic within a larger population.
  • Epidemiology: Estimating the proportion of a population infected during an outbreak.
  • Healthcare: Determining the success rate of a new clinical treatment or medication.
  • Intelligence Analysis: Assessing the probability of a specific event based on historical frequency.
  • Data Science: Evaluating click-through rates (CTR) or conversion rates in A/B testing.
  • Quality Control: Monitoring the defect rate in a manufacturing production line.
  • Marketing: Estimating the market share of a product based on survey responses.
  • Public Opinion: Calculating the margin of error for political polling results.

Parameters

ParameterTypeDefaultDescription
sample_proportionrequiredfloatThe observed proportion in the sample (e.g., 0.45 for 45%). Must be between 0 and 1.
sample_sizerequiredintThe total number of observations in the sample. Influences the selection of t-stat vs. z-stat.
confidence_intervalfloat0.95The desired confidence level as a decimal (e.g., 0.95 for 95%). Defaults to 0.95.
plot_sample_distributionboolTrueWhether to generate and display a plot of the sampling distribution. Defaults to True.
value_namestr'Possible Proportion'The label for the x-axis in the generated plot. Defaults to 'Possible Proportion'.
fill_colorstr'#999999'The hex color code for the histogram bars in the distribution plot. Defaults to '#999999'.
fill_transparencyfloat0.6Transparency level (alpha) for the plot bars, ranging from 0 to 1. Defaults to 0.6.
title_for_plotstr'Confidence Interval of Proportion'The primary title displayed at the top of the plot. Defaults to 'Confidence Interval of Proportion'.
subtitle_for_plotstr'Shows the distribution of the sample proportion.'Text displayed below the main title.
caption_for_plotstrNoneOptional descriptive text or notes at the bottom of the plot. Defaults to None.
data_source_for_plotstrNoneText identifying the source of the data, appended to the caption. Defaults to None.
show_y_axisboolFalseWhether to show the y-axis (density/frequency) in the plot. Defaults to False.
title_y_indentfloat1.1Vertical offset for the plot title position. Defaults to 1.10.
subtitle_y_indentfloat1.05Vertical offset for the plot subtitle position. Defaults to 1.05.
caption_y_indentfloat-0.15Vertical offset for the plot caption position. Defaults to -0.15.
figure_sizetuple(8, 6)The width and height of the figure in inches as a (width, height) tuple. Defaults to (8, 6).

Returns

None — the function prints the calculated interval to the console and displays a plot if requested.

Example

python
from analysistoolbox.statistics import CalculateConfidenceIntervalOfProportion

# Epidemiology: estimating the vaccination rate in a specific region
CalculateConfidenceIntervalOfProportion(
    sample_proportion=0.72,
    sample_size=500,
    confidence_interval=0.95,
    value_name="Vaccination Rate",
    title_for_plot="95% CI for Community Vaccination Coverage"
)