How to compute a confidence interval for a single population variance
Description
Let’s say we want to compute a confidence interval for the variability of a population. We take a sample of data, $x_1, x_2, x_3, \ldots, x_k$ and compute its variance, $s^2$. How do we construct a confidence interval for the population variance $\sigma^2$?
Related tasks:
- How to compute a confidence interval for a mean difference (matched pairs)
- How to compute a confidence interval for a regression coefficient
- How to compute a confidence interval for a population mean
- How to compute a confidence interval for the difference between two means when both population variances are known
- How to compute a confidence interval for the difference between two means when population variances are unknown
- How to compute a confidence interval for the difference between two proportions
- How to compute a confidence interval for the expected value of a response variable
- How to compute a confidence interval for the population proportion
- How to compute a confidence interval for the ratio of two population variances
Using SciPy, in Python
We’ll use R’s dataset EuStockMarkets here. This dataset has information on the daily closing prices of 4 European stock indices. We’re going to look at the variability of Germany’s DAX closing prices. For more information on how this is done, see how to quickly load some sample data. You can replace the example data below with your actual data.
1
2
3
4
5
6
7
# Load in EuStockMarkets data
from rdatasets import data
import pandas as pd
df = data('EuStockMarkets')
# Select the column for Germany's DAX closing prices
sample = df['DAX']
Now that we have our sample data loaded, let’s go ahead and make the confidence interval using SciPy.
1
2
3
4
5
6
7
8
9
10
11
12
from scipy import stats
# Find the critical values from the right and left tails of the Chi-square distribution
alpha = 0.05 # replace with your chosen alpha (here, a 95% confidence level)
n = len( sample )
left_critical_val = stats.chi2.ppf(1-alpha/2, df=n-1)
right_critical_val = stats.chi2.ppf(alpha/2, df=n-1)
# Find the upper and lower bounds of the confidence interval and print them out
lower_bound = ((n - 1)*sample.var())/left_critical_val
upper_bound = ((n - 1)*sample.var())/right_critical_val
lower_bound, upper_bound
1
(1104642.2801539514, 1256248.1273200295)
Our 95% confidence interval for the standard deviation of Germany’s DAX closing prices is $[1104642, 1256248]$.
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Solution, in R
We’ll use R’s dataset EuStockMarkets here. This dataset has information on the daily closing prices of 4 European stock indices. We’re going to look at the variability of Germany’s DAX closing prices. Feel free to replace this sample data with your actual data if you use this code.
1
2
3
4
5
6
# install.packages("datasets") # if you have not done so already
library(datasets)
# Load stock market data, convert to DataFrame, and choose the DAX column.
EuStockMarkets <- data.frame(EuStockMarkets)
sample <- EuStockMarkets$DAX
Now that we have our sample data loaded, let’s go ahead and make the confidence interval.
1
2
3
4
5
6
7
8
9
10
11
# Find the critical values from the right and left tails of the Chi-square distribution
alpha <- 0.05 # replace with your chosen alpha (here, a 95% confidence level)
n <- length(sample)
left_critical_val <- qchisq(p = alpha/2, df = n-1, lower.tail=FALSE)
right_critical_val <- qchisq(p = 1-alpha/2, df = n-1, lower.tail=FALSE)
# Find the upper and lower bounds of the confidence interval and print them out
lower_bound <- ((n - 1)*var(sample))/left_critical_val
upper_bound <- ((n - 1)*var(sample))/right_critical_val
lower_bound
upper_bound
1
2
3
4
5
[1] 1104642
[1] 1256248
Our 95% confidence interval for the standard deviation of Germany’s DAX closing prices is $[1104642, 1256248]$.
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Topics that include this task
Opportunities
This website does not yet contain a solution for this task in any of the following software packages.
- Excel
- Julia
If you can contribute a solution using any of these pieces of software, see our Contributing page for how to help extend this website.