Repository navigation
pnbd dyncov LL: accumulate F2 on the log scale (customers with many transactions) - #304
bachmannpatrick wants to merge 1 commit into
Conversation
The hypergeometric terms of F2 are of the order alpha^-(r+s+x). For customers
with many transactions (roughly x > 160 with weekly data) they are below the
smallest double. On the linear scale they lose precision in the subnormal
range and then become 0, so that F2 = 0 and the customer is treated as if she
could not have churned. Consequences:
- the log-likelihood of customers with many transactions who stopped buying
was too low (up to 30 units per customer in simulated data),
- the log-likelihood had small jumps in the parameters where terms entered or
left the subnormal range, which stops gradient-based optimizers ("false
convergence") and breaks finite-difference Hessians.
Changes:
- F2 and its hypergeometric terms are kept as sign and log of the absolute
value (struct SignedLog) and combined with log-sum-exp.
- If the GSL value of 2F1 is not finite (2F1 larger than the largest double)
or GSL stops after 30000 terms, the same positive series is summed with
rescaling instead of falling back to the transformation z -> 1-z.
- pnbd_dyncov_LL_i_hyp_alpha_ge_beta() and pnbd_dyncov_LL_i_hyp_beta_g_alpha()
keep their signature (linear scale); new *_log() variants are used by the LL.
- The intermediate results F2, F2.1, F2.2, F2.3 stay on the linear scale.
How results change: customers whose terms are representable are unchanged up
to rounding (largest difference 1e-13 in the LL of a customer). With zero
covariate effects the LL now agrees with the Pareto/NBD likelihood computed
with 60 digits to 1.4e-12 for all customers, including customers with more
than 400 transactions (before: differences of up to 34).
Tests: C++ unit tests against reference values computed with 120 digits.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UTYLyr3kUoxePkWEp1bmB4
|
Check in the built package, on an application with real data The branch was installed on a MacBook ( Scan of the log-likelihood. With the code of #302, every fit on these data ended with "false convergence". A scan around one of the saved estimates (21 equally spaced points per parameter, residuals from a cubic polynomial) showed a log-likelihood that is smooth except for a jump of about 0.09 units at the point where the optimizer had stopped. The same scan at the same point with this branch:
The central-difference gradient for log alpha at this point was 16, -445 and -30 at steps of 1e-5, 1e-4 and 1e-3. With this branch it is 15.5 at all three. Fits.
The covariate coefficients are equal to three digits before and after. The shape parameter s, which had stopped between 5.2 and 5.6, now runs to its upper bound: the log-likelihood is flat in s beyond about 5, and the jumps had stopped the optimizer on the way. Checks of this pull request R CMD check passes on Ubuntu (release, oldrel-1, devel), Windows (release, devel) and macOS (release); the test job on macOS and the coverage job pass. R CMD check on macOS (devel) fails, as on #302; I have not opened its log here. Not shown
Generated by Claude Code |
Stacked on #302 (it uses the helper introduced there). The diff against
bugfix-dyncov-hyp2f1-close-to-oneis this change only; the base can be switched todevelopmentonce #302 is merged.The issue
For customers with many transactions, the part of the dyncov Pareto/NBD likelihood in which the customer has churned (F2) is lost.
LL = log(F0) + log(F1 * F2 + F3).log(F0),log(F1)andlog(F3)are computed on the log scale, but F2 is accumulated on the linear scale. Its hypergeometric terms are of the orderalpha^-(r+s+x). With weekly data they fall below the smallest double (about 1e-308) from roughly x = 160 on, although F2 relative to F3 is not small.F2 = 0andLL = log(F0) + log(F3), the likelihood of a customer who cannot have churned.Consequences:
P(alive) = exp(F1 - LL)is exactly 1 for these customers, so predictions are too high for customers with many transactions who have left.nlminbwith numerical gradients stops there with "false convergence", and finite-difference Hessians are not positive definite.A second, smaller defect: for large
b = r + xand moderate z, 2F1 itself is larger than the largest double. GSL then returnsInfwith status success, and the code of #302 falls back to the transformation z -> 1-z.The change
struct SignedLog) and combined with log-sum-exp. The final step useslog|F2|directly.pnbd_dyncov_LL_i_hyp_series_log()).pnbd_dyncov_LL_i_hyp_alpha_ge_beta()andpnbd_dyncov_LL_i_hyp_beta_g_alpha()keep their signature and return the linear value; the LL uses new*_log()variants.F2,F2.1,F2.2,F2.3stay on the linear scale (0 if too small).Validation
Simulated customers with 86 weeks of data, six covariates in the purchase process, 3,000 customers of whom 521 have 170 or more transactions (maximum 428).
Among the customers with more than 185 transactions, the six who had not bought for more than four weeks change by 20 units on average; the 375 who bought in the last half week change by 1e-4 on average.
src/test-cpp.cppwith reference values computed with 120 digits for terms between 1e-317 and 1e-1393.test-cpp,test_correctness_pnbd_dyncov,test_runability_pnbd_dynamiccovandtest_correctness_pnbd_nocovstaticcovpass locally (Linux, R 4.3.3, GSL 2.7.1).Note for review
Results are no longer bit-identical for customers whose terms are representable: they change in the 13th digit because the terms are now combined on the log scale.
Generated by Claude Code