Skip to main content

A simple multicohort LTV calculator for subscription based products

Project description

Lucius LTV

A Python simple multi-cohort LTV calculator library for subscription-based products.

Installation

pip install lucius-ltv

Building a cohort matrix

To use this library, the first step is to build a Cohort retention matrix for your product.

To build a cohort matrix:

  • For each period of interest (week/month/year - depending on your subscription)
  • Retrieve the number of users that started paying for the first time
  • Track those users over time to observe how many where still alive in the following periods
  • Stack them in order to obtain a pd.DataFrame resembling this:
2015 2016 2017 2018 2019 2020 2021 2022
100 95 82 78 75 71 68 63
120 109 99 87 80 67 66
101 90 80 77 73 68
130 122 115 108 99
95 91 85 67
102 90 81
90 80

Alternatively if you are just trying out this library, you can generate a retention cohort matrix by calling generate_synthetic_cohort_matrix. An example of this is contained in the sample file, sample.py shipped with this project.

Fitting the model

Once you have a cohort matrix, you can finally fit the sBG model that will predict LTV. Behind the scenes this library implements "Fader, Peter and Hardie, Bruce, How to Project Customer Retention (May 2006)" (Available at SSRN: https://ssrn.com/abstract=801145 or http://dx.doi.org/10.2139/ssrn.801145) using the powerful Bayesian Inference library pymc3.

To fit the pymc model, simply run:

Basic fit

inference_data, model = fit_sbg_model(
    cohort_matrix,
    periods=10,    # Number of projected periods
)

Fit with "all_users"

If your product offers free trials or initial offers of any kind, you can add a conversion layer to the model, by specifying the starting users:

inference_data, model = fit_sbg_model(
    cohort_matrix,
    all_users=[150, 147, 180, 160, 130, 140, 160], 
    periods=10,    # Number of projected periods
)

Fit with true parameters

If you are testing the model with synthetic data you can specify the true alpha/beta sbg parameter values (those won't be used in fitting, but to generate reference/comparison timeseries)

inference_data, model = fit_sbg_model(
    cohort_matrix,
    periods=10,    # Number of projected periods
    true_a=1.5,
    true_b=2.3,
)

Extra pymc sampling parameters

If you want you can pass extra arguments that will be re-routed to the pymc sampler, like target_accept, steps, tune, etc..

Analyzing results

The returned inference_data is a standard Arviz InferenceData object will contain a posterior estimate of the modelled variables as returned by pymc3. The most interesting variables are:

  • inference_data.posterior['ltv'] the LTV timeseries
  • inference_data.posterior['conversion_rate_by_cohort'] the conversion rates if you did the fit with all_users
  • inference_data.posterior['ltv'] the true LTV timeseries if you provided true values of alpha and beta

Please note that these are trace objects resulting from the sampling process, therefore each variable will have multiple possible values representing the sampling of the posterior distribution. From this sampling you can obtain high-density intervals using np.percentile or az.hdi.

Empirical LTV from cohort matrix

You can also compute the empirical LTV starting from the cohort matrix, allowing you to compare empirical LTV to the projected one.

empirical_ltv = compute_empirical_ltv(cohort_matrix=cohort_matrix)

Plots

The library also includes a few methods for quickly plotting results.

LTV

Plot the user lifetime value

fig, ax = plt.subplots(figsize=(20, 10))
plot_ltv(empirical_ltv, inference_data=inference_data, ax=ax)
fig.suptitle(f'Lifetime value')
ax.legend()
ax.grid()

Conversion Rate

Plot the conversion rate by cohort

fig, ax = plt.subplots(figsize=(20, 10))
plot_conversion_rate(inference_data, ax=ax)
fig.suptitle(f'Conversion Rate')
ax.grid()

Cohort matrix

Plot the cohort matrix retention rates

plot_cohort_matrix_retention(cohort_matrix, 'Cohort Retention')

Posterior distributions vs true values

Plot the posterior distributions vs true values

fig, (ax1, ax2, ax3) = plt.subplots(3, figsize=(12, 12))
az.plot_posterior(inference_data, var_names=("alpha",),
                  ref_val=true_alpha,
                  ax=ax1)
az.plot_posterior(inference_data, var_names=("beta",),
                  ref_val=true_beta,
                  ax=ax2)
az.plot_posterior(inference_data, var_names=("conversion_rate",),
                  ref_val=1/true_conversion_rate,
                  ax=ax3)
fig.suptitle(f'True v Recovered values')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lucius-ltv-0.1.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lucius_ltv-0.1.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file lucius-ltv-0.1.0.tar.gz.

File metadata

  • Download URL: lucius-ltv-0.1.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.9 Darwin/21.1.0

File hashes

Hashes for lucius-ltv-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4d0420008d378cdcfd106b1d547dbcf66e7ab5ee5d9ca830060f5d9bc3fc89b6
MD5 aa1e932eb1755e5cb86703b11ba3d320
BLAKE2b-256 f9edd93355d40eb61739d7e485134a759c1635886f1abb5cacfa1a279ca9dec0

See more details on using hashes here.

File details

Details for the file lucius_ltv-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: lucius_ltv-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.9 Darwin/21.1.0

File hashes

Hashes for lucius_ltv-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2162e4e2f29a892cfcbcff0314dfa2c604e9e975bfa3b69ce19f423e4e57e6a3
MD5 ffbc9a10a57ed87c17e3634fb6a6afbe
BLAKE2b-256 22a663c6b235d93ec7ccb7e16a59247b6bee0424702e0668e2fa3cd0acb5e08b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page