12.4.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.
### 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.
### 8. Is Advertising Associated with Sales?
# The first 12 stores (S01 to S12) of the chapter's simulated store data,
# store_ad_sales.csv: weekly ad spend and weekly sales, both in $1,000s.
# 1. Collect 1,000 bootstrap slopes of sales on ad_spend: for i = 0, ..., 999, resample
# the rows with stores.sample(len(stores), replace=True, random_state=i)
# and compute the slope of the resample.
# 2. Compute the 95% interval with np.percentile(..., [2.5, 97.5], method='nearest')
# and print it rounded to 2 decimals.
# 3. Print whether 0 is inside the interval.
# 4. If each extra $1,000 of ads caused more than $3,330 of sales, ads would pay for
# themselves. Print whether the whole interval is above 3.33.
# Print exactly:
# 95% interval: 2.76 to 7.1
# 0 inside the interval: False
# Whole interval above 3.33: False
import numpy as np
import pandas as pd
stores = pd.DataFrame({
"ad_spend": [6.0, 7.2, 2.0, 3.4, 10.0, 8.3, 6.1, 9.8, 6.9, 5.1, 9.9, 6.6], # $1,000s per week
"sales": [77.3, 85.3, 50.2, 69.8, 91.4, 85.1, 88.5, 97.7, 93.9, 51.9, 94.4, 85.6], # $1,000s per week
})
### Your code starts here.
### Your code ends here.
### 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.
### 10. Does the Spread Grow?
# Illustrative residuals for a regression of delivery cost on distance, sorted by
# fitted value. (They are made up for this exercise, so unlike least squares
# residuals they do not sum to 0.)
# 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.