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.
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
| Parameter | Type | Default | Description |
|---|---|---|---|
sample_proportionrequired | float | — | The observed proportion in the sample (e.g., 0.45 for 45%). Must be between 0 and 1. |
sample_sizerequired | int | — | The total number of observations in the sample. Influences the selection of t-stat vs. z-stat. |
confidence_interval | float | 0.95 | The desired confidence level as a decimal (e.g., 0.95 for 95%). Defaults to 0.95. |
plot_sample_distribution | bool | True | Whether to generate and display a plot of the sampling distribution. Defaults to True. |
value_name | str | 'Possible Proportion' | The label for the x-axis in the generated plot. Defaults to 'Possible Proportion'. |
fill_color | str | '#999999' | The hex color code for the histogram bars in the distribution plot. Defaults to '#999999'. |
fill_transparency | float | 0.6 | Transparency level (alpha) for the plot bars, ranging from 0 to 1. Defaults to 0.6. |
title_for_plot | str | 'Confidence Interval of Proportion' | The primary title displayed at the top of the plot. Defaults to 'Confidence Interval of Proportion'. |
subtitle_for_plot | str | 'Shows the distribution of the sample proportion.' | Text displayed below the main title. |
caption_for_plot | str | None | Optional descriptive text or notes at the bottom of the plot. Defaults to None. |
data_source_for_plot | str | None | Text identifying the source of the data, appended to the caption. Defaults to None. |
show_y_axis | bool | False | Whether to show the y-axis (density/frequency) in the plot. Defaults to False. |
title_y_indent | float | 1.1 | Vertical offset for the plot title position. Defaults to 1.10. |
subtitle_y_indent | float | 1.05 | Vertical offset for the plot subtitle position. Defaults to 1.05. |
caption_y_indent | float | -0.15 | Vertical offset for the plot caption position. Defaults to -0.15. |
figure_size | tuple | (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
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"
)