forked from plexagon/lucius-ltv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
executable file
·71 lines (57 loc) · 2.23 KB
/
sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import matplotlib.pyplot as plt
import numpy as np
import arviz as az
from utils import generate_synthetic_cohort_matrix
from model import fit_sbg_model, compute_empirical_ltv
from plots import plot_ltv, plot_cohort_matrix_retention, plot_conversion_rate
def main():
cohorts = 1
max_obs_period = 10
true_alpha = 0.7
true_beta = 1.8
true_conversion_rate = 1.3
cohort_sizes = 100
cohort_sizes = np.random.normal(cohort_sizes, 0., size=cohorts).astype(int)
alphas = np.random.normal(true_alpha, 0., size=cohorts)
betas = np.random.normal(true_beta, 0., size=cohorts)
conversion_rate = np.random.normal(true_conversion_rate, 0.1, size=cohorts) # Conversion to Free Trial
cohort_matrix = generate_synthetic_cohort_matrix(
cohort_sizes=list(cohort_sizes),
alphas=list(alphas),
betas=list(betas),
max_obs_period=max_obs_period
)
inference_data, model = fit_sbg_model(
cohort_matrix,
# all_users=list((cohort_sizes / conversion_rate).astype(int)), # Optional, if you have free trials
progressbar=True,
periods=52,
true_alpha=true_alpha,
true_beta=true_beta,
target_accept=0.9
)
empirical_ltv = compute_empirical_ltv(inference_data, cohort_matrix=cohort_matrix, price=1)
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()
plot_cohort_matrix_retention(cohort_matrix, 'Cohort Retention')
fig, ax = plt.subplots(figsize=(20, 10))
plot_conversion_rate(inference_data, ax=ax)
fig.suptitle(f'Conversion Rate')
ax.grid()
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=true_conversion_rate,
ax=ax3)
fig.suptitle(f'True v Recovered values')
plt.show()
if __name__ == '__main__':
main()