Chapter 3 · Section 1 practice
Business Questions and Association
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. Identify the response
Warm-up. Print the Revenue Series. State its units.
Write your answer, then run it.
2. Select paired variables
Warm-up. Print only Ads and Revenue for the first three weeks.
Write your answer, then run it.
3. Count observations
Warm-up. Print the number of paired weekly records.
Write your answer, then run it.
4. Compute correlation
Build your skills. Print the Ads–Revenue correlation rounded to three decimals.
Write your answer, then run it.
5. Change currency units
Build your skills. Convert Revenue into HKD and verify that correlation is unchanged.
Write your answer, then run it.
6. Compare two relationships
Build your skills. Print correlations of Revenue with Ads and Price.
Write your answer, then run it.
7. Inspect an unusual week
Build your skills. Replace the last Revenue by 45 and print the new correlation.
Write your answer, then run it.
8. Calculate a curved example
Challenge. Create a Series of location offsets [-3, -2, -1, 0, 1, 2, 3]. Set cost = 5 + offset ** 2. Print their correlation rounded to six decimals.
Write your answer, then run it.
9. Compare a transformed predictor
Challenge. Using the same offsets and costs, compare correlation with offset against correlation with offset ** 2. Print both to three decimals.
Write your answer, then run it.
10. Support a cautious conclusion
Challenge. Print the Ads–Revenue correlation. Then explain why a near-zero correlation cannot rule out association, and why a large correlation does not prove an advertising effect.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Identify the response
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]
})
print(sales["Revenue"])Expected output
0 12
1 15
2 14
3 20
4 19
5 24
6 25
7 27
Name: Revenue, dtype: int64Revenue is the response, measured in thousands of HKD per week.
2. Select paired variables
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]
})
print(sales[["Ads", "Revenue"]].head(3))Expected output
Ads Revenue
0 1 12
1 2 15
2 3 14The two columns remain aligned by week.
3. Count observations
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]
})
print(len(sales[["Ads", "Revenue"]].dropna()))Expected output
8Each complete row supplies one paired observation.
4. Compute correlation
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]
})
print(round(sales["Ads"].corr(sales["Revenue"]), 3))Expected output
0.97The positive value describes linear association in these eight weeks.
5. Change currency units
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]
})
revenue_hkd = sales["Revenue"] * 1000
print(round(sales["Ads"].corr(revenue_hkd), 3))Expected output
0.97Positive unit conversion preserves correlation.
6. Compare two relationships
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]
})
print(sales[["Ads", "Price", "Revenue"]].corr()["Revenue"].round(3))Expected output
Ads 0.970
Price -0.851
Revenue 1.000
Name: Revenue, dtype: float64Each is a separate pairwise association; neither controls for the other predictor.
7. Inspect an unusual week
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]
})
sales.loc[7, "Revenue"] = 45
print(round(sales["Ads"].corr(sales["Revenue"]), 3))Expected output
0.863The altered observation changes the relationship; inspect its business explanation.
8. Calculate a curved example
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]
})
offset = pd.Series([-3, -2, -1, 0, 1, 2, 3])
cost = 5 + offset ** 2
print(round(offset.corr(cost), 6))Expected output
0.0The symmetric U-shaped relationship has zero Pearson correlation despite following an exact rule.
9. Compare a transformed predictor
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]
})
offset = pd.Series([-3, -2, -1, 0, 1, 2, 3])
cost = 5 + offset ** 2
print(round(offset.corr(cost), 3))
print(round((offset ** 2).corr(cost), 3))Expected output
0.0
1.0Squared offset captures the shape of this constructed rule. Transformations should reflect the problem and then face validation.
10. Support a cautious conclusion
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]
})
print(round(sales["Ads"].corr(sales["Revenue"]), 3))
print(
"Near-zero Pearson correlation can hide curvature. Large correlation alone does not isolate advertising from price or season."
)Expected output
0.97
Near-zero Pearson correlation can hide curvature. Large correlation alone does not isolate advertising from price or season.Check the scatterplot for nonlinear patterns and distinguish observed association from causation.