DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk5 min

How to Calculate Confidence Intervals in Python with SciPy: 9 Options

SciPy offers different interval methods for different targets. See nine approaches for bootstrap statistics, binomial proportions, and empirical CDF or survival-function estimates, plus related APIs for mean differences and distribution intervals.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In SciPy, the right confidence-interval function depends on what you are estimating: an arbitrary statistic, a binomial success proportion, or an empirical distribution function. This guide covers nine documented approaches across those targets, plus the related APIs for a difference in means and distribution intervals. They are not interchangeable, and SciPy does not define an official set of “nine methods.”

Choose an interval for the quantity you want to estimate

Before writing code, identify the estimand—the population quantity your interval is intended to cover. A mean, a binomial proportion, and an empirical CDF value require different methods and APIs. The nine approaches below are a practical grouping of methods in the SciPy 1.18.0 documentation, not a formal SciPy taxonomy.

As an Amazon Associate I earn from qualifying purchases.

  • Any statistic calculated from sample data: use scipy.stats.bootstrap.
  • Success probability from a binomial count: use binomtest(...).proportion_ci().
  • Empirical CDF or survival-function estimate: use the empirical distribution function’s confidence_interval().

For independent samples and a difference in population means, ttest_ind provides a separate result-based interval. By contrast, binom.interval and t.interval describe intervals in a specified random variable’s distribution, not confidence intervals for unknown parameters.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Three bootstrap methods for an arbitrary statistic

Bootstrap intervals resample the observed data with replacement, calculate a statistic for each resample, then use the resulting bootstrap distribution to construct bounds. They are useful when the target is a statistic that does not have a dedicated SciPy interval API. The method names and behavior below follow the SciPy bootstrap reference.

1. Percentile bootstrap

The percentile method takes quantiles of the bootstrap distribution as the interval endpoints. It is intuitive, although SciPy characterizes it as rarely used in practice. Choose it explicitly with method="percentile" when that construction suits your analysis.

2. Basic (reverse percentile) bootstrap

The basic method is a common alternative to the percentile method. Select it with method="basic". Both methods use bootstrap resamples, but they construct bounds differently; do not treat the method choice as merely a change in label.

3. BCa bootstrap

BCa means bias-corrected and accelerated. SciPy uses it as the default bootstrap method, but a degenerate bootstrap distribution can produce NaN interval endpoints. If that happens, examine whether the data or statistic leaves too little variation for resampling to work, rather than reporting the bounds as if they were valid.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Example: interval for a sample mean

This example estimates the population mean from one sample. It fixes the method, confidence level, resample count, and random-number generator so the run is reproducible for the same data and software environment.

import numpy as np
import scipy
from scipy import stats

x = np.array([4.2, 5.1, 3.8, 6.0, 4.7, 5.5])
rng = np.random.default_rng(2026)

result = stats.bootstrap(
    (x,),
    np.mean,
    confidence_level=0.95,
    n_resamples=9999,
    method="BCa",
    rng=rng,
)

print("SciPy version:", scipy.__version__)
print("95% CI for the population mean:", result.confidence_interval)

The installed version matters: this example follows the SciPy 1.18.0 reference, whose signature documents 9,999 resamples and BCa as defaults. Specifying them makes the choices visible. A fixed RNG seed supports reproducibility, but does not guarantee identical output across different SciPy or NumPy versions.

Paired versus independent samples

For paired observations, pass the samples to the statistic in corresponding order and set paired=True. SciPy then resamples shared indices, preserving each pair. With the default independent resampling, each sample is resampled separately; use that design when the samples are independent, not when observations are matched.

Three methods for a binomial success proportion

When the data are a count of successes k in n binomial trials, the target is the underlying success proportion. SciPy exposes three methods through binomtest(k, n).proportion_ci(); the SciPy 1.18.0 reference documents exact as the default.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

4. Exact Clopper–Pearson

Use method="exact" for the Clopper–Pearson interval. It is the default, but that does not establish it as universally preferable for every analysis.

5. Wilson score

Use method="wilson" for the Wilson score interval.

6. Wilson with continuity correction

Use method="wilsoncc" for the Wilson interval with continuity correction.

Example: compare the available proportion intervals

Here k is the observed number of successes and n is the number of trials. Each result is an interval for the binomial success proportion, not for a sample mean.

from scipy.stats import binomtest

k, n = 42, 60
result = binomtest(k, n)

for method in ("exact", "wilson", "wilsoncc"):
    ci = result.proportion_ci(confidence_level=0.95, method=method)
    print(method, ci)

SciPy’s API documentation identifies the supported constructions and cites their source papers, but does not establish a universal performance ranking among them. Make the selected method explicit when reporting results.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Two intervals for an empirical CDF or survival function

An empirical distribution function estimates a CDF or survival-function value from observed data. Its confidence interval is a specialized choice for that estimate, rather than a general-purpose interval for any statistic. SciPy documents two methods in its empirical distribution function reference.

7. Greenwood linear interval

method="linear" selects the conventional Greenwood interval and is the documented default.

8. Exponential Greenwood (log-log) interval

method="log-log" selects the exponential Greenwood, or log-log, interval. SciPy clips the conventional Greenwood bounds to the unit interval, [0, 1]. Either method can produce NaN endpoints, so inspect the result rather than assuming every dataset yields finite bounds.

Example: request a log-log interval

After constructing an empirical distribution function, specify the point at which to evaluate it and the desired method:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from scipy.stats import ecdf

sample = [1, 2, 2, 3, 4, 5]
res = ecdf(sample)
ci = res.cdf.confidence_interval(
    confidence_level=0.95,
    method="log-log",
)
print(ci)
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When SciPy returns an interval that is not a confidence interval for an unknown parameter

The word “interval” appears in other SciPy APIs, but those results answer a different question. The binomial distribution and Student’s t distribution each provide an interval method. These return equal-area intervals around the median of the specified random variable. They are distribution intervals, not confidence intervals for an unknown parameter estimated from a sample.

For a confidence interval around the difference between independent population means, use the interval attached to the test result instead. The ttest_ind reference documents confidence_interval() on the result object; this capability was added in SciPy 1.11.0.

from scipy import stats

x = [12, 15, 14, 10, 13]
y = [9, 11, 8, 12, 10]

result = stats.ttest_ind(x, y, equal_var=True)
ci = result.confidence_interval(confidence_level=0.95)
print("95% CI for the difference in population means:", ci)

This test-result interval concerns the difference in population means. Set the test’s variance assumption to match the analysis; the example uses the equal-variance setting.

A practical method-selection checklist

  • Write down the target quantity first: mean, difference in means, proportion, arbitrary statistic, or CDF/SF value.
  • Match the sampling design: use paired bootstrap resampling for matched observations and independent resampling for independent samples.
  • For proportions, name the exact, Wilson, or continuity-corrected Wilson construction in your report.
  • For bootstrap intervals, record the method, confidence level, resample count, RNG setup, and SciPy version.
  • Check endpoints for NaNs, especially with BCa bootstrap and empirical distribution function intervals.
  • Do not interpret binom.interval or t.interval as confidence intervals for parameters inferred from observed data.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.