Chapter 3 · Section 5 practice
Point Predictions and Two Kinds of Interval
Work from question 1 to 10. Edit your Python box, click Run Code, review the result, then click Submit attempt. Reference answers unlock after all ten attempts are submitted. This records completion, not correctness. Each box runs independently. Progress is saved in this browser.
1. Create a prediction row
Warm-up. Print a one-row design table for Ads=4.
Write your answer, then run it.
2. Calculate a point prediction
Warm-up. Print predicted revenue at Ads=4.
Write your answer, then run it.
3. Read the mean interval
Warm-up. Print only the 95% mean CI at Ads=4.
Write your answer, then run it.
4. Read the individual interval
Build your skills. Print only the 95% individual PI at Ads=4.
Write your answer, then run it.
5. Compare widths
Build your skills. Print the widths of both intervals at Ads=4.
Write your answer, then run it.
6. Change the confidence level
Build your skills. Print the 99% individual PI at Ads=4.
Write your answer, then run it.
7. Predict several inputs
Build your skills. Print mean predictions for Ads=2,4,6 together.
Write your answer, then run it.
8. Detect extrapolation
Challenge. Print whether Ads=15 is inside the observed predictor range.
Write your answer, then run it.
9. Compare centre and edge uncertainty
Challenge. Compare the mean standard errors at Ads=4.5 and Ads=8.
Write your answer, then run it.
10. Reconstruct a mean standard error
Challenge. Manually calculate the mean SE at Ads=4 and compare it with summary_frame.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Create a prediction row
import pandas as pd
sales = pd.DataFrame({
"Ads": [1, 2, 3, 4, 5, 6, 7, 8],
"Revenue": [12, 15, 14, 20, 19, 24, 25, 27],
"Price": [9, 8, 10, 7, 9, 6, 7, 6]
})
import statsmodels.api as sm
X = sm.add_constant(sales[["Ads"]])
model = sm.OLS(sales["Revenue"], X).fit()
new = pd.DataFrame({"const": [1], "Ads": [4]})
print(new)Expected output
const Ads
0 1 4The constant and predictor columns match the fitted design.
2. Calculate a point prediction
import pandas as pd
sales = pd.DataFrame({
"Ads": [1, 2, 3, 4, 5, 6, 7, 8],
"Revenue": [12, 15, 14, 20, 19, 24, 25, 27],
"Price": [9, 8, 10, 7, 9, 6, 7, 6]
})
import statsmodels.api as sm
X = sm.add_constant(sales[["Ads"]])
model = sm.OLS(sales["Revenue"], X).fit()
new = pd.DataFrame({"const": [1], "Ads": [4]})
print(round(model.predict(new).iloc[0], 3))Expected output
18.405This is a fitted conditional mean.
3. Read the mean interval
import pandas as pd
sales = pd.DataFrame({
"Ads": [1, 2, 3, 4, 5, 6, 7, 8],
"Revenue": [12, 15, 14, 20, 19, 24, 25, 27],
"Price": [9, 8, 10, 7, 9, 6, 7, 6]
})
import statsmodels.api as sm
X = sm.add_constant(sales[["Ads"]])
model = sm.OLS(sales["Revenue"], X).fit()
new = pd.DataFrame({"const": [1], "Ads": [4]})
p = model.get_prediction(new).summary_frame()
print(p[["mean_ci_lower", "mean_ci_upper"]].round(3))Expected output
mean_ci_lower mean_ci_upper
0 17.128 19.682The interval concerns the mean for comparable weeks.
4. Read the individual interval
import pandas as pd
sales = pd.DataFrame({
"Ads": [1, 2, 3, 4, 5, 6, 7, 8],
"Revenue": [12, 15, 14, 20, 19, 24, 25, 27],
"Price": [9, 8, 10, 7, 9, 6, 7, 6]
})
import statsmodels.api as sm
X = sm.add_constant(sales[["Ads"]])
model = sm.OLS(sales["Revenue"], X).fit()
new = pd.DataFrame({"const": [1], "Ads": [4]})
p = model.get_prediction(new).summary_frame()
print(p[["obs_ci_lower", "obs_ci_upper"]].round(3))Expected output
obs_ci_lower obs_ci_upper
0 14.652 22.157The interval includes variation of one new outcome.
5. Compare widths
import pandas as pd
sales = pd.DataFrame({
"Ads": [1, 2, 3, 4, 5, 6, 7, 8],
"Revenue": [12, 15, 14, 20, 19, 24, 25, 27],
"Price": [9, 8, 10, 7, 9, 6, 7, 6]
})
import statsmodels.api as sm
X = sm.add_constant(sales[["Ads"]])
model = sm.OLS(sales["Revenue"], X).fit()
new = pd.DataFrame({"const": [1], "Ads": [4]})
p = model.get_prediction(new).summary_frame().iloc[0]
print(round(p["mean_ci_upper"] - p["mean_ci_lower"], 3))
print(round(p["obs_ci_upper"] - p["obs_ci_lower"], 3))Expected output
2.554
7.505Individual variation makes the PI wider.
6. Change the confidence level
import pandas as pd
sales = pd.DataFrame({
"Ads": [1, 2, 3, 4, 5, 6, 7, 8],
"Revenue": [12, 15, 14, 20, 19, 24, 25, 27],
"Price": [9, 8, 10, 7, 9, 6, 7, 6]
})
import statsmodels.api as sm
X = sm.add_constant(sales[["Ads"]])
model = sm.OLS(sales["Revenue"], X).fit()
new = pd.DataFrame({"const": [1], "Ads": [4]})
p = model.get_prediction(new).summary_frame(alpha=0.01)
print(p[["obs_ci_lower", "obs_ci_upper"]].round(3))Expected output
obs_ci_lower obs_ci_upper
0 12.719 24.09alpha=0.01 requests 99% rather than 95% coverage.
7. Predict several inputs
import pandas as pd
sales = pd.DataFrame({
"Ads": [1, 2, 3, 4, 5, 6, 7, 8],
"Revenue": [12, 15, 14, 20, 19, 24, 25, 27],
"Price": [9, 8, 10, 7, 9, 6, 7, 6]
})
import statsmodels.api as sm
X = sm.add_constant(sales[["Ads"]])
model = sm.OLS(sales["Revenue"], X).fit()
new = pd.DataFrame({"const": [1, 1, 1], "Ads": [2, 4, 6]})
print(model.predict(new).round(3).to_list())Expected output
[14.024, 18.405, 22.786]Each row supplies one scenario.
8. Detect extrapolation
import pandas as pd
sales = pd.DataFrame({
"Ads": [1, 2, 3, 4, 5, 6, 7, 8],
"Revenue": [12, 15, 14, 20, 19, 24, 25, 27],
"Price": [9, 8, 10, 7, 9, 6, 7, 6]
})
import statsmodels.api as sm
X = sm.add_constant(sales[["Ads"]])
model = sm.OLS(sales["Revenue"], X).fit()
print(bool(sales["Ads"].min() <= 15 <= sales["Ads"].max()))Expected output
FalseA computable prediction can still be extrapolation.
9. Compare centre and edge uncertainty
import pandas as pd
sales = pd.DataFrame({
"Ads": [1, 2, 3, 4, 5, 6, 7, 8],
"Revenue": [12, 15, 14, 20, 19, 24, 25, 27],
"Price": [9, 8, 10, 7, 9, 6, 7, 6]
})
import statsmodels.api as sm
X = sm.add_constant(sales[["Ads"]])
model = sm.OLS(sales["Revenue"], X).fit()
new = pd.DataFrame({"const": [1, 1], "Ads": [4.5, 8]})
p = model.get_prediction(new).summary_frame()
print(p["mean_se"].round(3).to_list())Expected output
[0.51, 0.931]With this simple model, mean uncertainty is smaller near the sample X mean.
10. Reconstruct a mean standard error
import pandas as pd
sales = pd.DataFrame({
"Ads": [1, 2, 3, 4, 5, 6, 7, 8],
"Revenue": [12, 15, 14, 20, 19, 24, 25, 27],
"Price": [9, 8, 10, 7, 9, 6, 7, 6]
})
import statsmodels.api as sm
X = sm.add_constant(sales[["Ads"]])
model = sm.OLS(sales["Revenue"], X).fit()
import numpy as np
x = sales["Ads"]
sxx = ((x - x.mean()) ** 2).sum()
se = np.sqrt(model.mse_resid * (1 / len(x) + (4 - x.mean()) ** 2 / sxx))
p = model.get_prediction(pd.DataFrame({"const": [1], "Ads": [4]})).summary_frame()
print(round(se, 4), round(p["mean_se"].iloc[0], 4))Expected output
0.5218 0.5218The manual calculation matches the software under the same fitted model.