The Wayback Machine - https://web.archive.org/web/20250405063539/https://www.geeksforgeeks.org/confidence-interval/
Open In App

Confidence Interval

Last Updated : 06 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
News Follow

Confidence Interval (CI) is a range of values that estimates where the true population value is likely to fall. Instead of just saying The average height of students is 165 cm a confidence interval allow us to say We are 95% confident that the true average height is between 160 cm and 170 cm.

Before diving into confidence intervals you should be familiar with:

Interpreting Confidence Intervals

Let’s say we take a sample of 50 students and calculate a 95% confidence interval for their average height which turns out to be 160–170 cm. This means If we repeatedly take similar samples 95% of those intervals would contain the true average height of all students in the population.

2

Confidence Interval

Confidence level tells us how sure we are that the true value is within a calculated range. If we have to repeat the sampling process many times we expect that a certain percentage of those intervals will include the true value.

  • 90% Confidence: 90% of intervals would include the true population value.
  • 95% Confidence: 95% of intervals would include the true value which is commonly used in data science.
  • 99% Confidence: 99% of intervals would include the true value but the intervals would be wider.

Why are Confidence Intervals Important in Data Science?

  • They helps to measure uncertainty in predictions and estimates.
  • Through this data scientists finds the reliable results instead of just giving a single number.
  • They are widely used in A/B testing, machine learning, and survey analysis which we study later to check if results are meaningful.

Steps for Constructing a Confidence Interval

To calculate a confidence interval follow these simple 4 steps:

Step 1: Identify the sample problem.

Define the population parameter you want to estimate e.g., mean height of students. Choose the right statistic such as the sample mean.

Step 2: Select a confidence level.

In this step we select the confidence level some common choices are 90%, 95% or 99%. It represents how sure we are about our estimate.

Step 3: Find the margin of error.

To find the Margin of Error, you use the formula:

[Tex]Margin of Error=Critical Value×Standard Error[/Tex]

The Critical Value is found using Z-tables or T-tables for small samples. First you choose the significance level (α) which is typically 0.05 for a 95% confidence level. Then decide whether you are performing a one-tailed or two-tailed test with two-tailed being the more common choice. After this you look the corresponding value in the Z-table or T-table based on your significance level and test type.

The Standard Error measures the variability of the sample and calculated by dividing the sample’s standard deviation by the square root of the sample size. Combining the Critical Value and Standard Error gives you the Margin of Error which tells you the range within which you expect the true value to fall.

Step 4: Specify the confidence interval.

To find a Confidence Interval, we use this formula:

[Tex]Confidence Interval=Point Estimate±Margin of Error[/Tex]

Now the Point Estimate is usually the average or mean from your sample. It’s the best guess of the true value based on the sample data. The Margin of Error tells you how much the sample data might vary from the true value that we have calculated in previous step.

So when you add or subtract the margin of error from your point estimate you get a range. This range tells you where the true value is likely to fall.

Types of Confidence Intervals

Some of the common types of Confidence Intervals are:

Different-Types-of-Confidence-Intervals_

Types of confidence Interval

1. Confidence Interval for the Mean of Normally Distributed Data

When we want to find the mean of a population based on a sample we use this method.

  • If the sample size is small (less than 30) we use the T-distribution because small samples tend to have more variability.
  • If the sample size is large (more than 30) then we use the Z-distribution because large samples tend to give more accurate estimates.

2. Confidence Interval for Proportions

This type is used when estimating population proportions like the percentage of people who like a product. Here we use the sample proportion, the standard error and the critical Z-value to calculate the interval. It gives us the idea where the real value could fall based on sample data.

3. Confidence Interval for Non-Normally Distributed Data

Sometimes the data you have isn’t normally distributed meaning it doesn’t follow a bell-shaped curve. In such cases traditional confidence intervals is not the best method. Instead we can use bootstrap methods. This involves resampling the data many times to create different samples and then calculating the confidence interval from those resamples.

Calculating Confidence Interval

After understanding t-tests and z-tests now we move towards how to calculate Confidence Intervals. To calculate a confidence interval you need two key statistics: 

  • Mean (μ) — Arithmetic mean is the average of numbers. It is defined as the sum of n numbers divided by the count of numbers till n.
    [Tex]\mu=\frac{1+2+3+\ldots+n}{n} \quad [/Tex]
  • Standard deviation (σ) — It is the measure of how spread out the numbers are. It is defined as the summation of squared of the difference between each number and the mean.
    [Tex]\sigma=\sqrt{\sum \frac{\left(x_{i}-\mu\right)^{2}}{n}} \quad [/Tex]

Once you have these you can calculate the confidence interval either using t-distribution or z-distribution depend on the sample size whether the population standard deviation is known.

A) Using t-distribution 

When your sample size is small (typically n < 30) and you don’t know the population standard deviation we use the t-distribution. This is commonly seen in fields like A/B testing or when working with small datasets.

Consider the following example. A random sample of 10 UFC fighters was taken and their weights were measured. The mean weight was found to be 240 kg. Construct a 95% confidence interval estimate for the mean weight. The sample standard deviation was 25 kg. Find a confidence interval for a sample for the true mean weight of all UFC fighters.

Step-by-Step Process:

  • Degrees of Freedom (df):
    For t-distribution we first calculate the degrees of freedom: [Tex]df=n−1=10−1=9[/Tex]
  • Significance Level (α):
    The confidence level (CL) is 95% so the significance level is: [Tex]\alpha = \frac{1 – \text{CL}}{2} = \frac{1 – 0.95}{2} = 0.025[/Tex]
  • Find t-value from t-distribution table: From the t-table for df = 9 and α = 0.025 the t-value is 2.262 which can be find using the below table.
(df)/(α)0.10.050.025. .

∞

1.2821.6451.960. .

1

3.0786.31412.706. .

2

1.8862.9204.303. .

:

:::. .

8

1.3971.8602.306. .

9

1.3831.8332.262. .
  • Apply t-value in the formula:
    The formula for the confidence interval is: [Tex]\mu \pm t \left( \frac{\sigma}{\sqrt{n}} \right) [/Tex] Using the values: [Tex]240 \pm 2.262 \times \left(\frac{25}{\sqrt{10}}\right)[/Tex]
  • The confidence interval becomes: [Tex](222.117,257.883)[/Tex]

Therefore we are 95% confident that the true mean weight of UFC fighters is between 222.117 kg and 257.883 kg.

This can be calculated using Python’s scipy.stats library to find the t-value and perform the necessary calculations. The stats module provides various statistical functions, probability distributions, and statistical tests.

Python
import scipy.stats as stats
import math

sample_mean = 240
sample_std_dev = 25
sample_size = 10
confidence_level = 0.95

df = sample_size - 1

alpha = (1 - confidence_level) / 2

t_value = stats.t.ppf(1 - alpha, df)

margin_of_error = t_value * (sample_std_dev / math.sqrt(sample_size))

lower_limit = sample_mean - margin_of_error
upper_limit = sample_mean + margin_of_error

print(f"Confidence Interval: ({lower_limit}, {upper_limit})")

Output:

Confidence Interval: (222.1160773511857, 257.8839226488143)

B) Using Z-distribution

When the sample size is large (n > 30) or the population standard deviation is known the z-distribution is used. This is common in large-scale surveys, market research.

Consider the following example. A random sample of 50 adult females was taken and their RBC count is measured. The sample mean is 4.63 and the standard deviation of RBC count is 0.54. Construct a 95% confidence interval estimate for the true mean RBC count in adult females.

Step-by-Step Process:

  1. Find the mean and standard deviation given in the problem.
  2. Find the z-value for the confidence level:
    For a 95% confidence interval the z-value is 1.960.
  3. Apply z-value in the formula: [Tex]\mu \pm z\left(\frac{\sigma}{\sqrt{n}}\right) [/Tex]

Using the values: some common values in the table given below:

Confidence Intervalz-value

90%

1.645

95%

1.960

99%

2.576

The confidence interval becomes: (4.480,4.780)

Therefore we are 95% confident that the true mean RBC count for adult females is between 4.480 and 4.780.

Now let’s do the implementation of it using Python (stats library)

Python
from scipy import stats
import numpy as np

sample_mean = 4.63
std_dev = 0.54
sample_size = 50
confidence_level = 0.95

standard_error = std_dev / np.sqrt(sample_size)

z_value = 1.960  

margin_of_error = z_value * (std_dev / math.sqrt(sample_size))

lower_limit = sample_mean - margin_of_error
upper_limit = sample_mean + margin_of_error

print(f"Confidence Interval: ({lower_limit:.3f}, {upper_limit:.3f})")

Output:

Confidence Interval: (4.480, 4.780)

Some KeyTakeaways from Confidence Interval are:

  • Confidence Intervals are essential in data science to find the uncertainty of estimates and make predictions more reliable.
  • t-distribution is used for small sample sizes (n < 30) while z-distribution is used for large sample sizes (n > 30).
  • Confidence intervals help to make data-driven decisions by providing a range instead of a single point estimate. This is especially important in A/B testing, market research and machine learning.

Frequently Asked Questions (FAQs)

1. What is the 95% confidence interval rule?

The 95% confidence interval rule states that if we repeatedly construct 95% confidence intervals for a population parameter, we can expect 95% of those intervals to contain the true parameter value.

2. What if 95 confidence interval includes 1?

If the 95% confidence interval includes 1, it means that we are not statistically confident in saying that the true parameter value is different from 1. In other words, the data is not strong enough to rule out the possibility that the true parameter value is 1.

3. What is the difference between confidence level and confidence interval?

The confidence level is the probability that the confidence interval contains the true parameter value. The confidence interval is a range of values that is likely to contain the true parameter value.

4. How to find sample size?

The sample size is the number of observations in a sample. The sample size is determined by the desired confidence level, the desired margin of error, and the variability of the data.

5. What is the 5 significance level?

The significance level is the probability of rejecting the null hypothesis when it is actually true. The significance level is typically set at 0.05, which means that we are willing to accept a 5% chance of making a Type I error.




Next Article

Similar Reads

three90RightbarBannerImg