Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Confidence Intervals on Parameters
📖 Scenario: You are a data analyst working with a small dataset of exam scores. You want to estimate the average score and understand how confident you can be about this estimate.
🎯 Goal: Build a simple Python program that calculates the 95% confidence interval for the mean exam score using scipy.
📋 What You'll Learn
Create a list of exam scores called scores with exact values
Create a variable confidence_level set to 0.95
Calculate the mean and the 95% confidence interval for the mean using scipy.stats.t.interval
Print the confidence interval in a clear format
💡 Why This Matters
🌍 Real World
Confidence intervals help us understand how precise our estimates are when working with sample data, like exam scores or survey results.
💼 Career
Data scientists and analysts use confidence intervals to report uncertainty in their findings, which is important for making informed decisions.
Progress0 / 4 steps
1
Create the exam scores data
Create a list called scores with these exact exam scores: 88, 92, 79, 93, 85, 91, 87.
SciPy
Hint
Use square brackets [] to create a list and separate numbers with commas.
2
Set the confidence level
Create a variable called confidence_level and set it to 0.95.
SciPy
Hint
Use a simple assignment statement to create the variable.
3
Calculate the confidence interval
Import scipy.stats. Calculate the mean of scores and then use scipy.stats.t.interval with confidence_level, degrees of freedom, sample mean, and standard error to find the confidence interval. Store the result in conf_interval.
SciPy
Hint
Use np.mean for the average, len(scores) - 1 for degrees of freedom, and scipy.stats.sem for standard error.
4
Print the confidence interval
Print the text "95% confidence interval for the mean score:" followed by the conf_interval variable.
SciPy
Hint
Use print with a string and the variable conf_interval.
Practice
(1/5)
1. What does a confidence interval represent in statistics?
easy
A. A range of values likely containing the true parameter
B. The exact value of the parameter
C. The average of the sample data
D. The maximum value observed in the data
Solution
Step 1: Understand the meaning of confidence interval
A confidence interval gives a range where the true parameter is likely to be found, not a single exact value.
Step 2: Compare options with definition
Only A range of values likely containing the true parameter correctly describes this range; others describe different concepts.
Final Answer:
A range of values likely containing the true parameter -> Option A
Quick Check:
Confidence interval = range of likely parameter values [OK]
Hint: Confidence interval = range, not exact value [OK]
Common Mistakes:
Thinking it gives exact parameter value
Confusing with sample mean
Assuming it shows data maximum
2. Which of the following is the correct way to import the function to calculate confidence intervals from scipy?
easy
A. from scipy.stats import t
B. import scipy.confidence as conf
C. from scipy import confidence_interval
D. import scipy.stats.confidence
Solution
Step 1: Recall scipy.stats module usage
The t-distribution and its interval function are in scipy.stats, imported as 'from scipy.stats import t'.
Step 2: Check other options
Other imports do not exist or are incorrect syntax.
Final Answer:
from scipy.stats import t -> Option A
Quick Check:
Correct import for t interval = from scipy.stats import t [OK]
Hint: Use 'from scipy.stats import t' for confidence intervals [OK]
Common Mistakes:
Trying to import non-existent modules
Using wrong import syntax
Confusing function location
3. What is the output of the following code?
import numpy as np
from scipy.stats import t
data = np.array([5, 7, 8, 6, 9])
mean = np.mean(data)
se = np.std(data, ddof=1) / np.sqrt(len(data))
interval = t.interval(0.95, len(data)-1, loc=mean, scale=se)
print(tuple(round(x, 2) for x in interval))
medium
A. (5.00, 9.00)
B. (4.50, 9.30)
C. (6.00, 7.00)
D. (5.04, 8.96)
Solution
Step 1: Calculate mean and standard error
Mean = (5+7+8+6+9)/5 = 7.0; sample std dev ≈ 1.58; SE = 1.58 / sqrt(5) ≈ 0.71.
Step 2: Calculate 95% confidence interval using t-distribution
Degrees of freedom = 4; t critical ≈ 2.776; interval = mean ± t * SE = 7.0 ± 2.776*0.71 ≈ (5.04, 8.96).
Final Answer:
(5.04, 8.96) -> Option D
Quick Check:
Mean ± t*SE = (5.04, 8.96) [OK]
Hint: Calculate mean, SE, then apply t.interval [OK]
Common Mistakes:
Using population std dev instead of sample
Wrong degrees of freedom
Rounding errors
4. Identify the error in this code snippet for calculating a 90% confidence interval:
from scipy.stats import t
sample_mean = 10
sample_std = 2
n = 25
se = sample_std / n
interval = t.interval(0.90, n-1, loc=sample_mean, scale=se)
print(interval)
medium
A. Degrees of freedom should be n, not n-1
B. Wrong confidence level value
C. Standard error calculation is incorrect
D. t.interval function does not exist
Solution
Step 1: Check standard error calculation
Standard error should be sample_std divided by sqrt(n), not by n.
Step 2: Verify other parts
Confidence level 0.90 and degrees of freedom n-1 are correct; t.interval exists.
Final Answer:
Standard error calculation is incorrect -> Option C
Quick Check:
SE = std / sqrt(n), not std / n [OK]
Hint: SE = std / sqrt(n), not std / n [OK]
Common Mistakes:
Dividing std by n instead of sqrt(n)
Confusing degrees of freedom
Using wrong confidence level format
5. You have a dataset with 100 measurements and want a 99% confidence interval for the mean. Which code correctly computes it using scipy?
hard
A. from scipy.stats import t
import numpy as np
data = np.random.randn(100)
mean = np.mean(data)
se = np.std(data) / 100
interval = t.interval(0.99, 100, loc=mean, scale=se)
print(interval)
B. from scipy.stats import t
import numpy as np
data = np.random.randn(100)
mean = np.mean(data)
se = np.std(data, ddof=1) / np.sqrt(100)
interval = t.interval(0.99, 99, loc=mean, scale=se)
print(interval)
C. from scipy.stats import t
import numpy as np
data = np.random.randn(100)
mean = np.mean(data)
se = np.std(data, ddof=1) / np.sqrt(100)
interval = t.interval(0.95, 99, loc=mean, scale=se)
print(interval)
D. from scipy.stats import t
import numpy as np
data = np.random.randn(100)
mean = np.mean(data)
se = np.std(data, ddof=1) / 100
interval = t.interval(0.99, 99, loc=mean, scale=se)
print(interval)
Solution
Step 1: Check standard error calculation
Standard error must be sample std dev with ddof=1 divided by sqrt(n), which is 100 here.
Step 2: Check confidence level and degrees of freedom
99% confidence means 0.99; degrees of freedom = n-1 = 99.
Step 3: Verify code correctness
from scipy.stats import t
import numpy as np
data = np.random.randn(100)
mean = np.mean(data)
se = np.std(data, ddof=1) / np.sqrt(100)
interval = t.interval(0.99, 99, loc=mean, scale=se)
print(interval) correctly uses ddof=1, sqrt(100), 0.99 confidence, and 99 degrees of freedom.
Final Answer:
The code with ddof=1, /np.sqrt(100), 0.99 confidence, df=99 -> Option B
Quick Check:
Use ddof=1, sqrt(n), 0.99 confidence, df=n-1 [OK]
Hint: Use ddof=1 and sqrt(n) for SE; df = n-1 [OK]