Chapter 3 · Section 4 practice
Coefficient Uncertainty and Statistical Inference
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. Read a standard error
Warm-up. Print the Ads coefficient standard error.
Write your answer, then run it.
2. Read degrees of freedom
Warm-up. Print residual degrees of freedom.
Write your answer, then run it.
3. Reconstruct the default t statistic
Warm-up. Calculate slope/SE for a zero null.
Write your answer, then run it.
4. Read a two-sided p-value
Build your skills. Print the stored two-sided p-value for Ads.
Write your answer, then run it.
5. Read a 95% interval
Build your skills. Print the coefficient interval for Ads.
Write your answer, then run it.
6. Calculate a critical value
Build your skills. Calculate the two-sided 5% critical t value using df_resid.
Write your answer, then run it.
7. Use a nonzero null
Build your skills. Test Ads slope=2; print the t statistic and two-sided p-value.
Write your answer, then run it.
8. Test a different null
Challenge. For H0: Ads slope=3 against a two-sided alternative, print the p-value.
Write your answer, then run it.
9. Compare confidence levels
Challenge. Print widths of 95% and 99% Ads intervals.
Write your answer, then run it.
10. Check agreement of test and interval
Challenge. Print whether the 95% Ads interval excludes zero, and whether the classical two-sided p-value is below 0.05.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Read a 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()
print(round(model.bse["Ads"], 4))Expected output
0.2225This is coefficient uncertainty, not outcome spread.
2. Read degrees of freedom
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(int(model.df_resid))Expected output
6Eight observations minus the intercept and one slope gives six.
3. Reconstruct the default t statistic
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(round(model.params["Ads"] / model.bse["Ads"], 4))Expected output
9.8446The null is zero in the default coefficient table.
4. Read a two-sided p-value
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(round(model.pvalues["Ads"], 6))Expected output
6.3e-05This applies to slope=0 under the classical fitted model.
5. Read a 95% 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()
print(model.conf_int().loc["Ads"].round(4).to_list())Expected output
[1.646, 2.7349]These are lower and upper coefficient limits, not sales predictions.
6. Calculate a critical value
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()
from scipy import stats
print(round(stats.t.ppf(0.975, model.df_resid), 4))Expected output
2.4469The upper critical value is the 97.5th percentile.
7. Use a nonzero null
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()
from scipy import stats
t_stat = (model.params["Ads"] - 2) / model.bse["Ads"]
print(round(t_stat, 4))
print(round(2 * stats.t.sf(abs(t_stat), model.df_resid), 6))Expected output
0.8561
0.424831Subtract 2 rather than reading the stored zero-null p-value.
8. Test a different null
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()
from scipy import stats
t_stat = (model.params["Ads"] - 3) / model.bse["Ads"]
print(round(2 * stats.t.sf(abs(t_stat), model.df_resid), 6))Expected output
0.010858Count equally extreme departures on both sides of the null value 3.
9. Compare confidence levels
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()
ci95 = model.conf_int(alpha=0.05).loc["Ads"]
ci99 = model.conf_int(alpha=0.01).loc["Ads"]
print(round(ci95.iloc[1] - ci95.iloc[0], 4))
print(round(ci99.iloc[1] - ci99.iloc[0], 4))Expected output
1.0889
1.6498Higher confidence requires a wider interval.
10. Check agreement of test and 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()
ci = model.conf_int().loc["Ads"]
print(bool((ci.iloc[0] > 0) or (ci.iloc[1] < 0)))
print(bool(model.pvalues["Ads"] < 0.05))Expected output
True
TrueFor the same classical procedure, these decisions agree.