How to compute a confidence interval for a single population variance (in Python, using SciPy)
Task
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
Solution
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.
Contributed by Elizabeth Czarniak (CZARNIA_ELIZ@bentley.edu)