Chapter 3 · Section 3 practice
Simple Regression with SPY and NVDA Returns
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 paired returns
Warm-up. Print the first three paired SPY and NVDA daily returns.
Write your answer, then run it.
2. Count complete dates
Warm-up. Print the number of complete return dates.
Write your answer, then run it.
3. Check the time split
Warm-up. Print the final training date and first test date.
Write your answer, then run it.
4. Read fitted coefficients
Build your skills. Print the intercept and SPY slope.
Write your answer, then run it.
5. Interpret the slope
Build your skills. Print the fitted change in NVDA return, in percentage points, for a one percentage-point increase in SPY return.
Write your answer, then run it.
6. Interpret the intercept
Build your skills. Print fitted NVDA return, as a percentage, when SPY return is zero.
Write your answer, then run it.
7. Supply a scenario
Build your skills. Predict NVDA return, as a percentage, when SPY return is 1%.
Write your answer, then run it.
8. Inspect a residual
Challenge. Print the first training date’s observed, fitted and residual NVDA returns.
Write your answer, then run it.
9. Read a slope interval
Challenge. Print the 95% confidence interval for the SPY slope.
Write your answer, then run it.
10. State the required input
Challenge. Print the SPY inputs needed for the first three reserved test dates.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Read paired returns
import pandas as pd
prices = pd.read_csv(
"https://busanalytics-book.pages.dev/data/nvda_spy_daily_2023_2024.csv",
index_col="Date", parse_dates=True).sort_index()
data = prices[["SPY", "NVDA"]].pct_change().dropna()
cut = int(len(data) * 0.8)
train = data.iloc[:cut].copy()
test = data.iloc[cut:].copy()
import statsmodels.api as sm
X = sm.add_constant(train[["SPY"]])
Y = train["NVDA"]
model = sm.OLS(Y, X).fit()
print(data[["SPY", "NVDA"]].head(3))Expected output
SPY NVDA
Date
2023-01-04 0.007720 0.030318
2023-01-05 -0.011413 -0.032816
2023-01-06 0.022932 0.041640Both returns in a row belong to the same trading day.
2. Count complete dates
import pandas as pd
prices = pd.read_csv(
"https://busanalytics-book.pages.dev/data/nvda_spy_daily_2023_2024.csv",
index_col="Date", parse_dates=True).sort_index()
data = prices[["SPY", "NVDA"]].pct_change().dropna()
cut = int(len(data) * 0.8)
train = data.iloc[:cut].copy()
test = data.iloc[cut:].copy()
import statsmodels.api as sm
X = sm.add_constant(train[["SPY"]])
Y = train["NVDA"]
model = sm.OLS(Y, X).fit()
print(len(data))Expected output
500The first price row has no previous observation and cannot supply a return.
3. Check the time split
import pandas as pd
prices = pd.read_csv(
"https://busanalytics-book.pages.dev/data/nvda_spy_daily_2023_2024.csv",
index_col="Date", parse_dates=True).sort_index()
data = prices[["SPY", "NVDA"]].pct_change().dropna()
cut = int(len(data) * 0.8)
train = data.iloc[:cut].copy()
test = data.iloc[cut:].copy()
import statsmodels.api as sm
X = sm.add_constant(train[["SPY"]])
Y = train["NVDA"]
model = sm.OLS(Y, X).fit()
print(train.index.max())
print(test.index.min())Expected output
2024-08-07 00:00:00
2024-08-08 00:00:00The reserved dates follow all fitting dates.
4. Read fitted coefficients
import pandas as pd
prices = pd.read_csv(
"https://busanalytics-book.pages.dev/data/nvda_spy_daily_2023_2024.csv",
index_col="Date", parse_dates=True).sort_index()
data = prices[["SPY", "NVDA"]].pct_change().dropna()
cut = int(len(data) * 0.8)
train = data.iloc[:cut].copy()
test = data.iloc[cut:].copy()
import statsmodels.api as sm
X = sm.add_constant(train[["SPY"]])
Y = train["NVDA"]
model = sm.OLS(Y, X).fit()
print(model.params)Expected output
const 0.003357
SPY 2.332029
dtype: float64const is the intercept and SPY labels the slope.
5. Interpret the slope
import pandas as pd
prices = pd.read_csv(
"https://busanalytics-book.pages.dev/data/nvda_spy_daily_2023_2024.csv",
index_col="Date", parse_dates=True).sort_index()
data = prices[["SPY", "NVDA"]].pct_change().dropna()
cut = int(len(data) * 0.8)
train = data.iloc[:cut].copy()
test = data.iloc[cut:].copy()
import statsmodels.api as sm
X = sm.add_constant(train[["SPY"]])
Y = train["NVDA"]
model = sm.OLS(Y, X).fit()
print(model.params["SPY"])Expected output
2.3320290464037203Both returns use the same scale, so the slope also converts changes expressed in percentage points.
6. Interpret the intercept
import pandas as pd
prices = pd.read_csv(
"https://busanalytics-book.pages.dev/data/nvda_spy_daily_2023_2024.csv",
index_col="Date", parse_dates=True).sort_index()
data = prices[["SPY", "NVDA"]].pct_change().dropna()
cut = int(len(data) * 0.8)
train = data.iloc[:cut].copy()
test = data.iloc[cut:].copy()
import statsmodels.api as sm
X = sm.add_constant(train[["SPY"]])
Y = train["NVDA"]
model = sm.OLS(Y, X).fit()
print(model.params["const"] * 100)Expected output
0.3356858253159234Set the predictor to zero in the fitted equation.
7. Supply a scenario
import pandas as pd
prices = pd.read_csv(
"https://busanalytics-book.pages.dev/data/nvda_spy_daily_2023_2024.csv",
index_col="Date", parse_dates=True).sort_index()
data = prices[["SPY", "NVDA"]].pct_change().dropna()
cut = int(len(data) * 0.8)
train = data.iloc[:cut].copy()
test = data.iloc[cut:].copy()
import statsmodels.api as sm
X = sm.add_constant(train[["SPY"]])
Y = train["NVDA"]
model = sm.OLS(Y, X).fit()
new = pd.DataFrame({"const": [1], "SPY": [0.01]})
print(model.predict(new) * 100)Expected output
0 2.667715
dtype: float64This uses a supplied SPY return as the model input.
8. Inspect a residual
import pandas as pd
prices = pd.read_csv(
"https://busanalytics-book.pages.dev/data/nvda_spy_daily_2023_2024.csv",
index_col="Date", parse_dates=True).sort_index()
data = prices[["SPY", "NVDA"]].pct_change().dropna()
cut = int(len(data) * 0.8)
train = data.iloc[:cut].copy()
test = data.iloc[cut:].copy()
import statsmodels.api as sm
X = sm.add_constant(train[["SPY"]])
Y = train["NVDA"]
model = sm.OLS(Y, X).fit()
print(train["NVDA"].iloc[0])
print(model.fittedvalues.iloc[0])
print(model.resid.iloc[0])Expected output
0.030317967052142514
0.021360532392055622
0.008957434660086892Observed return equals fitted return plus residual.
9. Read a slope interval
import pandas as pd
prices = pd.read_csv(
"https://busanalytics-book.pages.dev/data/nvda_spy_daily_2023_2024.csv",
index_col="Date", parse_dates=True).sort_index()
data = prices[["SPY", "NVDA"]].pct_change().dropna()
cut = int(len(data) * 0.8)
train = data.iloc[:cut].copy()
test = data.iloc[cut:].copy()
import statsmodels.api as sm
X = sm.add_constant(train[["SPY"]])
Y = train["NVDA"]
model = sm.OLS(Y, X).fit()
print(model.conf_int().loc["SPY"])Expected output
0 2.005432
1 2.658626
Name: SPY, dtype: float64This interval concerns the fitted slope, not the range of individual future returns.
10. State the required input
import pandas as pd
prices = pd.read_csv(
"https://busanalytics-book.pages.dev/data/nvda_spy_daily_2023_2024.csv",
index_col="Date", parse_dates=True).sort_index()
data = prices[["SPY", "NVDA"]].pct_change().dropna()
cut = int(len(data) * 0.8)
train = data.iloc[:cut].copy()
test = data.iloc[cut:].copy()
import statsmodels.api as sm
X = sm.add_constant(train[["SPY"]])
Y = train["NVDA"]
model = sm.OLS(Y, X).fit()
print(test[["SPY"]].head(3))Expected output
SPY
Date
2024-08-08 0.023117
2024-08-09 0.004410
2024-08-12 0.000526A same-day return regression needs that day's SPY return. It does not forecast an unknown SPY return.