python
import numpy as np
import matplotlib.pyplot as plt
def payback_calculator(cac, monthly_arpu, gross_margin, monthly_retention_rates, max_months=240, discount_rate=0.0, plot=True):
"""
Payback Period Calculator.
Parameters:
- cac: Customer Acquisition Cost (float)
- monthly_arpu: Average Revenue Per User per month (float)
- gross_margin: Gross margin fraction (0.0 to 1.0)
- monthly_retention_rates: List of monthly retention rates [r1, r2, ..., rn]
where r1 is retention from month 1 to 2, etc. After n, uses last rate.
- max_months: Max months to project (default 240 ~20 years)
- discount_rate: Annual discount rate (default 0.0 for undiscounted)
- plot: Whether to show plot (default True)
Returns:
- payback_months: Payback period in months (nan if not reached)
- ltv: Lifetime Value
- ltv_cac_ratio: LTV / CAC
"""
n_variable = len(monthly_retention_rates)
if n_variable == 0:
raise ValueError("Provide at least one retention rate.")
last_ret_rate = monthly_retention_rates[-1]
# Active customers at start of each month (index 1 = month 1, etc.)
active_start = np.zeros(max_months + 1)
active_start[1] = 1.0 # Full cohort starts month 1
for t in range(2, max_months + 1):
month_idx = t - 2
ret_rate = monthly_retention_rates[month_idx] if month_idx < n_variable else last_ret_rate
active_start[t] = active_start[t - 1] * ret_rate
# Monthly net cash flow (per customer)
months_axis = np.arange(1, max_months + 1)
net_cf = monthly_arpu * gross_margin * active_start[1:]
# Discount factors (monthly)
monthly_discount = (1 + discount_rate / 12) ** (-months_axis)
discounted_cf = net_cf * monthly_discount
# Cumulative
cum_cf_undisc = np.cumsum(net_cf)
cum_cf_disc = np.cumsum(discounted_cf)
# LTV (discounted)
ltv = cum_cf_disc[-1]
# Payback: undiscounted cumulative (standard for payback period)
payback_mask = cum_cf_undisc >= cac
if not np.any(payback_mask):
payback_months = np.nan
else:
payback_months = np.argmax(payback_mask) + 1 # +1 for 1-based month
ltv_cac_ratio = ltv / cac
# Print results
print(f"\n=== Results ===")
print(f"LTV (discounted): ${ltv:.2f}")
print(f"LTV / CAC: {ltv_cac_ratio:.2f}x")
if np.isnan(payback_months):
print("Payback Period: Never recovered within {max_months} months")
else:
print(f"Payback Period (undiscounted): {payback_months} months ({payback_months/12:.1f} years)")
# Plot
if plot:
plt.figure(figsize=(12, 7))
plt.plot(months_axis, cum_cf_undisc, label='Cumulative Cash Flow (Undiscounted)', linewidth=2)
plt.plot(months_axis, cum_cf_disc, label='Cumulative Cash Flow (Discounted)', linewidth=2, linestyle='--')
plt.axhline(y=cac, color='red', linestyle='-', linewidth=2, label=f'CAC = ${cac:.2f}')
if not np.isnan(payback_months):
plt.axvline(x=payback_months, color='green', linestyle='--', linewidth=2,
label=f'Payback: {payback_months} months')
plt.xlabel('Months')
plt.ylabel('Per Customer ($)')
plt.title('CAC Payback Period & LTV Curve')
plt.legend()
plt.grid(True, alpha=0.3)
plt.ylim(0, max(cac * 2, ltv * 1.1))
plt.show()
return payback_months, ltv, ltv_cac_ratio
# Interactive CLI version
def interactive_calculator():
print("=== Sherlock's Payback Period Calculator ===")
print("Inputs for a single customer cohort.")
cac = float(input("\nCAC ($): "))
monthly_arpu = float(input("Monthly ARPU ($): "))
gross_margin = float(input("Gross Margin (e.g., 0.8): "))
print("\nRetention curve: Enter monthly retention rates (0-1), comma-separated.")
print("E.g., '0.92,0.90,0.88,0.86,0.85,0.84' for first 6 months.")
print("Tail uses the last rate provided.")
ret_input = input("Retention rates: ").strip()
monthly_retention_rates = [float(r.strip()) for r in ret_input.split(',') if r.strip()]
discount_rate = float(input("\nAnnual discount rate (0 for none): ") or "0")
payback_months, ltv, ratio = payback_calculator(
cac, monthly_arpu, gross_margin, monthly_retention_rates,
discount_rate=discount_rate, plot=True
)
# Example usage (uncomment to run)
if __name__ == "__main__":
# Example 1: Typical SaaS
# CAC=400, ARPU=50, Margin=0.8, Retention: high initial drop then stable
example_ret = [0.90, 0.87, 0.85, 0.84, 0.83, 0.82] + [0.82] * 18 # First 24 months
payback, ltv, ratio = payback_calculator(400, 50, 0.8, example_ret[:6], discount_rate=0.1)
# Run interactive
# interactive_calculator()
Example output (SaaS-like): LTV ~$1,200, Payback ~9 months, 3x ratio. Adjust inputs for your model!