0
0
SciPydata~20 mins

Confidence intervals on parameters in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
šŸŽ–ļø
Confidence Interval Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā“ Predict Output
intermediate
2:00remaining
Calculate 95% confidence interval for mean with scipy

Given the sample data below, what is the 95% confidence interval for the mean using scipy.stats.t.interval?

SciPy
import numpy as np
from scipy import stats

data = np.array([5, 7, 8, 6, 9, 10, 7, 6])

mean = np.mean(data)
sem = stats.sem(data)
conf_int = stats.t.interval(0.95, len(data)-1, loc=mean, scale=sem)
print(conf_int)
A(5.35, 8.90)
B(5.50, 8.75)
C(5.60, 8.60)
D(5.10, 9.20)
Attempts:
2 left
šŸ’” Hint

Use stats.sem to get the standard error of the mean, then use stats.t.interval with degrees of freedom = sample size - 1.

ā“ data_output
intermediate
1:30remaining
Interpret confidence interval output for regression coefficient

After fitting a linear regression, you get the 95% confidence interval for the slope as (1.2, 3.4). What does this interval mean?

AThe true slope is between 1.2 and 3.4 with 95% confidence.
B95% of the data points fall between 1.2 and 3.4.
CThe slope will be exactly 2.3 in 95% of future samples.
DThe slope is either 1.2 or 3.4 with 95% probability.
Attempts:
2 left
šŸ’” Hint

Confidence intervals estimate where the true parameter lies with a certain confidence level.

šŸ”§ Debug
advanced
1:30remaining
Identify error in confidence interval calculation code

What error will this code raise?

import numpy as np
from scipy import stats

data = np.array([2, 4, 6, 8, 10])
mean = np.mean(data)
sem = stats.sem(data)
conf_int = stats.t.interval(0.95, len(data), loc=mean, scale=sem)
print(conf_int)
SciPy
import numpy as np
from scipy import stats

data = np.array([2, 4, 6, 8, 10])
mean = np.mean(data)
sem = stats.sem(data)
conf_int = stats.t.interval(0.95, len(data), loc=mean, scale=sem)
print(conf_int)
AIndexError: index out of range
BValueError: degrees of freedom <= 0 for slice
CNo error, prints confidence interval
DTypeError: unsupported operand type(s)
Attempts:
2 left
šŸ’” Hint

Check the degrees of freedom parameter for stats.t.interval.

šŸš€ Application
advanced
2:30remaining
Calculate confidence interval for variance using chi-square distribution

You have a sample variance of 4.0 from 10 observations. Using the chi-square distribution, what is the 95% confidence interval for the population variance?

SciPy
import scipy.stats as stats

n = 10
sample_var = 4.0
alpha = 0.05

lower = (n - 1) * sample_var / stats.chi2.ppf(1 - alpha/2, n - 1)
upper = (n - 1) * sample_var / stats.chi2.ppf(alpha/2, n - 1)
print((lower, upper))
A(2.10, 10.50)
B(1.20, 13.00)
C(1.80, 11.20)
D(1.56, 12.03)
Attempts:
2 left
šŸ’” Hint

Use chi-square percent point function (ppf) with degrees of freedom = n - 1.

🧠 Conceptual
expert
1:30remaining
Why use t-distribution for confidence intervals on small samples?

Why is the t-distribution preferred over the normal distribution when calculating confidence intervals for the mean with small sample sizes?

ABecause the normal distribution cannot be used for any sample size below 30.
BBecause the t-distribution has heavier tails to model outliers better.
CBecause the sample variance is unknown and estimated, increasing uncertainty.
DBecause the t-distribution is symmetric while the normal distribution is not.
Attempts:
2 left
šŸ’” Hint

Think about what changes when the sample size is small and variance is estimated.