# How to compute a confidence interval for a single population variance (in R)

## 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. 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.

Contributed by Elizabeth Czarniak (CZARNIA_ELIZ@bentley.edu)