Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ParetoNBD with covariates #545

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pymc_marketing/clv/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@
dropout_time = rng.exponential(scale=1 / mu)
wait = rng.exponential(scale=1 / lam)

while t + wait < min(dropout_time, T):
final_t = min(dropout_time, T)
while (t + wait) < final_t:

Check warning on line 324 in pymc_marketing/clv/distributions.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/clv/distributions.py#L323-L324

Added lines #L323 - L324 were not covered by tests
t += wait
n += 1
wait = rng.exponential(scale=1 / lam)
Expand Down
17 changes: 9 additions & 8 deletions pymc_marketing/clv/models/beta_geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,17 +352,18 @@
alpha = pm.HalfFlat("alpha")
r = pm.HalfFlat("r")

# This is the shape with fit_method="map"
if self.fit_result.dims == {"chain": 1, "draw": 1}:
shape_kwargs = {"shape": 1000}
else:
shape_kwargs = {}
fit_result = self.fit_result
if fit_result.sizes["chain"] == 1 and fit_result.sizes["draw"] == 1:

Check warning on line 356 in pymc_marketing/clv/models/beta_geo.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/clv/models/beta_geo.py#L355-L356

Added lines #L355 - L356 were not covered by tests
# For map fit add a dummy draw dimension
fit_result = self.fit_result.squeeze("draw").expand_dims(

Check warning on line 358 in pymc_marketing/clv/models/beta_geo.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/clv/models/beta_geo.py#L358

Added line #L358 was not covered by tests
draw=range(1000)
)

pm.Beta("population_dropout", alpha=a, beta=b, **shape_kwargs)
pm.Gamma("population_purchase_rate", alpha=r, beta=alpha, **shape_kwargs)
pm.Beta("population_dropout", alpha=a, beta=b)
pm.Gamma("population_purchase_rate", alpha=r, beta=alpha)

Check warning on line 363 in pymc_marketing/clv/models/beta_geo.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/clv/models/beta_geo.py#L362-L363

Added lines #L362 - L363 were not covered by tests

return pm.sample_posterior_predictive(
self.fit_result,
fit_result,
var_names=var_names,
random_seed=random_seed,
).posterior_predictive
Expand Down
710 changes: 487 additions & 223 deletions pymc_marketing/clv/models/pareto_nbd.py

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions tests/clv/models/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from arviz import InferenceData, from_dict

from pymc_marketing.clv.models.basic import CLVModel
from tests.clv.utils import set_model_fit


class CLVModelTest(CLVModel):
Expand Down Expand Up @@ -54,10 +55,10 @@ def test_create_distribution_from_wrong_prior(self):
model = CLVModelTest()
with pytest.raises(
ValueError,
match="Distribution definately_not_PyMC_dist does not exist in PyMC",
match="Distribution definitely_not_PyMC_dist does not exist in PyMC",
):
model._create_distribution(
{"dist": "definately_not_PyMC_dist", "kwargs": {"alpha": 1, "beta": 1}}
{"dist": "definitely_not_PyMC_dist", "kwargs": {"alpha": 1, "beta": 1}}
)

def test_fit_mcmc(self):
Expand Down Expand Up @@ -165,9 +166,7 @@ def test_thin_fit_result(self):
model = CLVModelTest(data=data)
model.build_model()
fake_idata = from_dict(dict(x=np.random.normal(size=(4, 1000))))
fake_idata.add_groups(dict(fit_data=data.to_xarray()))
model.set_idata_attrs(fake_idata)
model.idata = fake_idata
set_model_fit(model, fake_idata)

thin_model = model.thin_fit_result(keep_every=20)
assert thin_model is not model
Expand Down
4 changes: 2 additions & 2 deletions tests/clv/models/test_gamma_gamma.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
GammaGammaModel,
GammaGammaModelIndividual,
)
from tests.clv.utils import set_model_fit


class BaseTestGammaGammaModel:
Expand Down Expand Up @@ -232,8 +233,7 @@ def test_new_customer_spend(self, distribution):
fake_fit = pm.sample_prior_predictive(
samples=1000, model=model.model, random_seed=self.rng
)
fake_fit.add_groups(dict(posterior=fake_fit.prior))
model.idata = fake_fit
set_model_fit(model, fake_fit.prior)
# Closed formula solution for the mean and var of the population spend (eqs 3, 4 from [1]) # noqa: E501
expected_preds_mean = p_mean * v_mean / (q_mean - 1)
expected_preds_std = np.sqrt(
Expand Down
Loading
Loading