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
BetaGeoBetaBinomFitter.fit() not converging #259
Comments
Hey @cereusperuv, thanks for the report. Is is possible for you to send the dataset over (email is fine for me), or can you answer the following:
|
Hi @CamDavidsonPilon and thanks for your prompt reply. I will send you my dataset shortly, just have to edit it a tad first. Meanwhile, I can tell you that
|
Thanks. Try grouping values together, ex:
|
Grouping leads to a never ending stream of new autograd warning messages of the type
And here's the
|
Yea, I can reproduce the error locally. I'm playing with it now. If you are stuck, try downgrading to lifetimes 0.10.1 |
Grouping and penalizing (0.001) makes it work without any warnings, but the results are non-sensical. It also takes forever to fit (didn't time it, but something like 20 minutes on my laptop). BG-NBD fits in less than a second. Thanks, I'll try downgrading. Edit: Changed "Pareto-NBD" above to "BG-NBD" |
@CamDavidsonPilon Sorry for taking so long to follow up on this, I have been busy with other work. There seems to be stability issues with the BG-BB autograd optimizer. For some of my data it does not converge, for other data it converges but to a nonsensical solution ( First of all, I was able to make some progress by changing periodicity from Some patterns that I have observed:
I will now move on and build a simpler version of the BG-BB without the autograd optimizer, and also look into how to suitably parallelize the computations in our Hadoop system. I will follow this thread though and would of course be delighted if there is a resolution. On another note, |
A friend and I were working through the BG/BB model as well. We wrote a separate version of the log-likelihood which fixes some of the stability issues; I think the
The calculation of B at the very end can cause instability (in our case, it was that having super large values of the parameters allowed for arbitrarily large (more) negative log likelihood (i.e., large values of the parameters gave better and better log likelihood). We found that the following fixed this, where we moved the subtraction of betaln_ab and betaln_gd into the for loop and took and exponential.
In addition, I'm not 100% sure but I think doing the |
@pallenmar Sorry for my inactivity, have been working on other projects. Today I tried moving the ab/gd-log-betas inside the for loop like you suggested but I saw no change in convergence with my data. The result is still totally dependent on the amount of regularization. Thanks for your help though! |
What's the current status of this issue? |
@Trollgeir Still unresolved. Not working actively on it anymore. |
Beta-Geometric/Beta-Bernoulli model is a special variant of the beta-binomial model without the binomial coefficient. It is particularly efficient for discrete-time analyses. It is considered "experimental" as the associated "Fitter" in the lifetimes python package is buggy (see CamDavidsonPilon/lifetimes#259). An override is provided in lifetimes_ext which utilizes exponentials and natural logarithms to reduce the observed convergence instability. See custom_beta_geo_beta_binom_fitter.py for details. BG/BB literature: https://brucehardie.com/papers/020/fader_et_al_mksc_10.pdf Change-Id: Icc2701b42bc720635e5d96c9c809c48c0f93e945 GitOrigin-RevId: 9765a1b45087a8b0c05b7eb6070b32d64a81b017
Try using summary_data_from_transaction_data function from lifetimes.datasets import load_transaction_data transaction_data = load_transaction_data() summary = summary_data_from_transaction_data(transaction_data, 'id', 'date', observation_period_end='2014-12-31') print(summary.head()) bgf.fit(summary['frequency'], summary['recency'], summary['T']) <lifetimes.BetaGeoFitter: fitted with 5000 subjects, a: 1.85, alpha: 1.86, b: 3.18, r: 0.16> |
Have you ever tested with a non-zero recency dataframe? |
I have resolved using this solution |
Hi, |
Have you tried using |
Thank you for the reply. No, I haven't used that function. I create the data frame manually. Because the code you sent, it looks same as the summary function given by the library. Maybe I couldn't notice the differences :D |
you can refer to this link If you see code in the method at this link |
I stumbled upon this same issue some time ago. Downgrading to earlier versions (those that do not use autograd) seem to solve the issue. Also, a search on Google brought me to this stack overflow page, where it is suggested to use the Nelder-Mead method in the minimize function for another problem. I run a quick test with lifetimes and indeed the error is gone, although I have not tested the consequences of such change. Is anyone aware of the implications that changing the minimize method could have? |
As previously pointed out, this is not recommended. Those zero-value customers may still be alive and it will throw off the statistical assumptions of the model if they are removed. In fact, it isn't recommended to filter on any of the RFM values except T (the length of the observation period).
These are large T values. In this paper it goes into detail the causes of these same numerical errors for the BG-NBD model, and suggests an alternative time unit for T. I've encountered situations where a model trains just fine over shorter time periods but falls apart with the same data when trained on longer time horizons. Run your data back through https://numpy.org/devdocs/reference/arrays.datetime.html#datetime-units That same paper also provides suggestions on how to reformulate the log-likelihood for the BG-NBD and ParetoNBD models to fix these issues. Lifetimes is no longer being actively maintained, but I've forked this library and have been rebuilding it on a new modeling backend, incorporating the suggestions in that paper. Currently the library in the Beta; if anyone is interested in contributing let me know. |
Any ideas what might be going on? Or more likely, what I've done wrong... Main parts of the error below. Thanks!
Raised by
BaseFitter._fit()
:ConvergenceError: The model did not converge. Try adding a larger penalizer to see if that helps convergence.
print(output)
inBaseFitter._fit()
:Autograd warnings:
Numpy warning:
The text was updated successfully, but these errors were encountered: