12.5.3. Homework#
Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.
Sign in from the account menu before you start working on the assignment.
Write and run your code directly in the coding cells.
Multiple submissions are allowed. The highest score is kept.
1. Correlation and Causation
Across 50 stores, the correlation between a product’s price and units sold is -0.85. This shows that raising the price causes demand to fall.
2. Changing Units
Converting both variables to standard units, or measuring sales in dollars instead of thousands of dollars, does not change the correlation coefficient r.
3. The Regression Effect
The correlation between customers’ monthly visits and their monthly spending is 0.6. A customer whose visits are 2 SDs above average is predicted to spend 1.2 SDs above average.
4. Least Squares
Among all straight lines that could be drawn through a scatter plot, the regression line has the smallest root mean squared error.
5. Fan-Shaped Residuals
In a residual plot, the residuals spread out more and more as the fitted values increase. The regression line is equally precise for small and large predictions.
### 6. Price and Units Sold
# A store varied the price of a product over six weeks.
# 1. Compute r between price and units, using standard units (np.mean, np.std).
# 2. Print r rounded to 3 decimals.
# Print exactly:
# r: -0.995
import numpy as np
import pandas as pd
demand = pd.DataFrame({
"price": [10, 12, 14, 16, 18, 20],
"units": [48, 45, 39, 36, 30, 27],
})
### Your code starts here.
### Your code ends here.
r: -0.995
### 7. Predicting Demand
# 1. Compute the slope and intercept of the regression line of units on price.
# 2. Predict units sold at a price of 15, rounded to 1 decimal
# (use the unrounded slope and intercept).
# Print exactly:
# Slope: -2.19
# Intercept: 70.29
# Predicted units at 15: 37.5
import numpy as np
import pandas as pd
demand = pd.DataFrame({
"price": [10, 12, 14, 16, 18, 20],
"units": [48, 45, 39, 36, 30, 27],
})
### Your code starts here.
### Your code ends here.
Slope: -2.19
Intercept: 70.29
Predicted units at 15: 37.5
### 8. Predicting in Standard Units
# Monthly spending has mean $80 and SD $25; r between visits and spending is 0.6.
# A customer's visits are 2.0 SDs above average.
# 1. Predict the customer's spending in standard units: r * (visits in standard units).
# 2. Convert the prediction to dollars: mean + (standard units) * SD.
# Print exactly:
# Predicted spend (standard units): 1.2
# Predicted spend: 110.0
import numpy as np
import pandas as pd
r = 0.6
visits_su = 2.0
spend_mean = 80
spend_sd = 25
### Your code starts here.
### Your code ends here.
Predicted spend (standard units): 1.2
Predicted spend: 110.0
### 9. How Far Off Are the Predictions?
# 1. Compute the fitted values of the regression line of units on price.
# 2. Compute the RMSE: sqrt(mean of (actual - fitted) ** 2), rounded to 2 decimals.
# Print exactly:
# RMSE: 0.72
import numpy as np
import pandas as pd
demand = pd.DataFrame({
"price": [10, 12, 14, 16, 18, 20],
"units": [48, 45, 39, 36, 30, 27],
})
### Your code starts here.
### Your code ends here.
RMSE: 0.72
### 10. Does the Spread Grow?
# Residuals from a regression of delivery cost on distance, sorted by fitted value.
# 1. Compute the SD (np.std) of the residuals for the 5 smallest fitted values
# and for the 5 largest, each rounded to 2 decimals.
# 2. Print whether the SD for the largest fitted values is more than twice the other.
# Print exactly:
# SD (small fitted): 1.47
# SD (large fitted): 7.4
# Spread grows: True
import numpy as np
import pandas as pd
diag = pd.DataFrame({
"fitted": [20, 25, 30, 35, 40, 45, 50, 55, 60, 65],
"residual": [1, -1, 2, -2, 1, -6, 7, -8, 9, -7],
})
### Your code starts here.
### Your code ends here.
SD (small fitted): 1.47
SD (large fitted): 7.4
Spread grows: True