steps = pd.DataFrame({
'ad_su': standard_units(stores['ad_spend']),
'sales_su': standard_units(stores['sales']),
})
steps['product'] = steps['ad_su'] * steps['sales_su']
print(round(np.mean(steps['product']), 3))
0.689
np.corrcoef(x, y)[0, 1] gives the same 0.689The sign of r depends on which kind of product outweighs the other, not on a head count.
in_dollars = stores[['ad_spend', 'sales']] * 1000
print(round(correlation(stores, 'ad_spend', 'sales'), 3),
round(correlation(in_dollars, 'ad_spend', 'sales'), 3))
0.689 0.689
Simulated monthly sales at a seaside shop:
r (ice cream, sunscreen): 0.933
r (temperature, ice cream): 0.978
r (temperature, sunscreen): 0.97
r (ice cream, sunscreen), months 68 to 78 F: 0.058
The stores table also has staff and store_age.
staff and salesstore_age and sales12.2 · Think Data Science & Management
Correlation r: 0.6894
A straight line needs only two numbers and is easy to explain to a manager.
new_ad_su = (8 - ad_mean) / ad_sd # 1. to standard units
pred_su = ad_sales_r * new_ad_su # 2. multiply by r
pred_sales = pred_su * sales_sd + sales_mean # 3. back to $1,000s
Ad spend in standard units: 0.94
Predicted sales, standard units: 0.64
Predicted sales ($1,000s): 88.5
Simulated Q1 and Q2 sales for 200 reps (skill plus luck), r = 0.52:
cutoff = np.percentile(reps['q1_sales'], 90)
top = reps[reps['q1_sales'] >= cutoff]
print(len(top), round(np.mean(top['q1_sales']), 1),
round(np.mean(top['q2_sales']), 1))
20 137.9 120.5
def slope(table, x, y):
r = correlation(table, x, y)
return r * np.std(table[y]) / np.std(table[x])
def intercept(table, x, y):
return np.mean(table[y]) - slope(table, x, y) * np.mean(table[x])
Slope: 3.87
Intercept: 57.54
stores['fitted'] = store_slope * stores['ad_spend'] + store_intercept
stores['residual'] = stores['sales'] - stores['fitted']
store ad_spend sales fitted residual
0 S01 6.0 77.3 80.7 -3.4
1 S02 7.2 85.3 85.4 -0.1
2 S03 2.0 50.2 65.3 -15.1
The line will produce a number for any input. It cannot warn you the number is unreliable.
Manager's rule: predicted sales = 50 + 5 × ad spend
def rmse(table, a, b, x='ad_spend', y='sales'):
errors = table[y] - (a + b * table[x])
return np.sqrt(np.mean(errors ** 2))
Average error: 1.38 (errors run from -23.6 to 24.0)
RMSE of the manager's line: 11.61
Manager's rule: 50 + 5x RMSE = 11.61
Flatter line: 60 + 3x RMSE = 11.58
Low base: 40 + 4x RMSE = 20.16
Always the mean RMSE = 15.33
def rmse_of_line(params):
return rmse(stores, params[0], params[1])
best = minimize(rmse_of_line, [50, 5])
Intercept found: 57.535
Slope found: 3.869
Smallest RMSE: 11.106
Predict Q2 sales from Q1 sales (sales_reps.csv):
q1_sales and q2_sales12.3 · Think Data Science & Management
Average residual: 0.0
Smallest residual: -25.37
Largest residual: 24.04
Residual SD: 11.11
Average residual, prices under $6: 47.3 units
Average residual, prices $9 to $12: -27.7 units
Average residual, prices over $14: 18.9 units
Under 2 miles: 2971 trips, residual SD: $ 2.77
2 to 10 miles: 1723 trips, residual SD: $ 3.45
Over 10 miles: 306 trips, residual SD: $ 7.31
All trips: 5000 trips, residual SD: $ 3.52
Slope from all 40 stores: 3.87
Slope from the first 20 stores: 5.04
Slope from the last 20 stores: 3.39
def one_bootstrap_slope():
resample = stores.sample(len(stores), replace=True)
return slope(resample, 'ad_spend', 'sales')
np.random.seed(42)
bstrap_slopes = np.array([one_bootstrap_slope()
for i in range(5000)])
left, right = np.percentile(bstrap_slopes, [2.5, 97.5], method='nearest')
2.79 4.98
To decide, the manager needs an experiment that assigns ad budgets at random.
sales on staff, with ex_stores.sample(len(ex_stores), replace=True, random_state=i) for i = 0, ..., 999np.percentile(..., method='nearest')