Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save abhijeet-talaulikar/a40ea614ac72bcf5544fa29bf1a1689a to your computer and use it in GitHub Desktop.
Save abhijeet-talaulikar/a40ea614ac72bcf5544fa29bf1a1689a to your computer and use it in GitHub Desktop.
def get_response_curve(channel, start_time, end_time):
def hill_transform(x, alpha, gamma):
return 1 / (1 + (x/gamma)**-alpha)
# parameters
coefficient = model.query("variable == @channel")['coefficient'].iloc[0]
alpha = model.query("variable == @channel")['alpha'].iloc[0]
gamma = model.query("variable == @channel")['gamma'].iloc[0]
# means for inverse scaling
spend_mean = media.query("DATE >= @start_time and DATE <= @end_time")[channel].mean()
revenue_mean = media.query("DATE >= @start_time and DATE <= @end_time")['REVENUE'].mean()
# actual spend and contribution
spend = media.query("DATE >= @start_time and DATE <= @end_time")[channel].sum()
revenue = hill_transform(spend / spend_mean, alpha, gamma) * coefficient * revenue_mean
spend_axis = np.arange(spend/10, spend*2, 1e03)
revenue_axis = hill_transform(spend_axis / spend_mean, alpha, gamma) * coefficient * revenue_mean
return spend_axis, revenue_axis, spend, revenue
spend_axis, revenue_axis, spend, revenue = get_response_curve('PAID_SEARCH', '2022-11-01', '2023-01-01')
plt.plot(spend_axis, revenue_axis)
x0, y0 = spend, revenue
plt.plot(x0, y0, "s");
plt.xlabel('Spend');
plt.ylabel('Revenue');
plt.title('Paid Search Response Curve');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment