Skip to content

pnbd dyncov LL: accumulate F2 on the log scale (customers with many transactions) - #304

Open
bachmannpatrick wants to merge 1 commit into
bugfix-dyncov-hyp2f1-close-to-onefrom
bugfix-dyncov-ll-many-transactions
Open

bachmannpatrick wants to merge 1 commit into
bugfix-dyncov-hyp2f1-close-to-onefrom
bugfix-dyncov-ll-many-transactions

Conversation

@bachmannpatrick

@bachmannpatrick bachmannpatrick commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #302 (it uses the helper introduced there). The diff against bugfix-dyncov-hyp2f1-close-to-one is this change only; the base can be switched to development once #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) and log(F3) are computed on the log scale, but F2 is accumulated on the linear scale. Its hypergeometric terms are of the order alpha^-(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.

  • Between 1e-308 and 5e-324 (subnormal range) the terms lose precision: relative errors of 1e-5 and more.
  • Below that they are 0. Then F2 = 0 and LL = log(F0) + log(F3), the likelihood of a customer who cannot have churned.

Consequences:

  • The log-likelihood of customers with many transactions who stopped buying is too low (up to 30 units per customer in simulated data, see below).
  • P(alive) = exp(F1 - LL) is exactly 1 for these customers, so predictions are too high for customers with many transactions who have left.
  • The log-likelihood has small jumps in the parameters where terms enter or leave the subnormal range. nlminb with numerical gradients stops there with "false convergence", and finite-difference Hessians are not positive definite.

A second, smaller defect: for large b = r + x and moderate z, 2F1 itself is larger than the largest double. GSL then returns Inf with status success, and the code of #302 falls back to the transformation z -> 1-z.

The change

  • F2 and its hypergeometric terms are kept as sign and log of the absolute value (struct SignedLog) and combined with log-sum-exp. The final step uses log|F2| directly.
  • If the GSL value is not finite or GSL stops after 30000 terms, the same series (all terms positive) is summed with rescaling (pnbd_dyncov_LL_i_hyp_series_log()).
  • pnbd_dyncov_LL_i_hyp_alpha_ge_beta() and pnbd_dyncov_LL_i_hyp_beta_g_alpha() keep their signature and return the linear value; the LL uses new *_log() variants.
  • The intermediate results F2, F2.1, F2.2, F2.3 stay 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).

Transactions Customers Largest change of the customer LL Sum of the changes
up to 160 2,415 1.1e-13 0.000
161 to 185 172 0.019 0.036
186 to 250 247 30.4 108.8
more than 250 166 11.2 11.3

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.

  • Against the exact likelihood. With all covariate effects set to 0, the dyncov LL must equal the Pareto/NBD LL. Compared with the closed form evaluated with 60 digits (mpmath) for 171 customer and parameter combinations (s = 0.3, 1.2, 5.5; both branches): largest error 1.4e-12 with this change, also for customers with more than 400 transactions. Before: up to 34.
  • Continuity. Scanning the customer LLs along alpha, r, beta and one covariate parameter: 4 to 6 customers with a jump before, none with this change.
  • P(alive). Customers with more than 185 transactions and no purchase for more than four weeks: 1 before, 0.0005 on average with this change.
  • Speed. One evaluation for the 3,000 customers: 0.12 s (0.20 s before).
  • Tests. New context in src/test-cpp.cpp with reference values computed with 120 digits for terms between 1e-317 and 1e-1393. test-cpp, test_correctness_pnbd_dyncov, test_runability_pnbd_dynamiccov and test_correctness_pnbd_nocovstaticcov pass 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

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

Copy link
Copy Markdown
Owner Author

Check in the built package, on an application with real data

The branch was installed on a MacBook (renv::install("bachmannpatrick/CLVTools@bugfix-dyncov-ll-many-transactions")) and used on the application in which the problem was found: 37,370 customers, 86 weeks, Pareto/NBD with six dynamic covariates in the purchase process.

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:

Parameter (step 1e-4) Roughness with #302 Roughness with this branch
log r 0.018 7e-9
log alpha 0.018 7e-9
log s 0.019 7e-9
log mean attrition rate 0.018 6e-9
covariate shared by all customers 0.018 7e-9
covariates of a subgroup of the customers 3e-9 3e-9

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. nlminb with central-difference gradients and a numDeriv Hessian, 8 to 10 parameters:

With #302 With this branch
Fits ending with relative convergence 0 of 10 6 of 6
Hessian positive definite 6 of 10 6 of 6
Time per fit 2.5 to 8 hours (some stopped early) 34 to 65 minutes

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

  • The comparison with the exact Pareto/NBD likelihood in the description has all covariate effects set to 0. With covariates only the inputs of the same terms change, but there is no independent reference for that case.
  • The new fits end with s at its bound, where it is held fixed. The scan at the old stopping point is the clean before/after comparison; the convergence is supporting evidence.
  • How many customers of the application are above the threshold has not been counted.

Generated by Claude Code

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants