Debug calculation formula to avoid infinity when log(0) is encountered

This commit is contained in:
CIEF ACC1
2023-12-29 09:39:15 +00:00
parent 0b0af14e2a
commit 6c63f37d0c
@@ -107,7 +107,6 @@ compute_censored_subjects AS (
),
compute_probability AS (
SELECT
@@ -115,15 +114,25 @@ compute_probability AS (
-- The survival probability represents the probability of customers that will not churn up to a specific tenure
-- Example: survival_day = 61, survival_prob = 95%. For customers with 61 days of tenure, the customer has a 95% chance of not churning.
EXP(SUM(LN(1 - events / at_risk)) OVER (
-- When events / at_risk = 1, we replace value 1 with a value very near to 1, to prevent log 0 which causes infinity value
EXP(SUM(LN
(
CASE WHEN (1 - events / at_risk) = 0 THEN 0.999999 ELSE (1 - events / at_risk) END
))
OVER (
ORDER BY survival_time_days ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)) AS survival_probability,
100 * (1 - EXP(SUM(LN(1 - events / at_risk)) OVER (
100 * (1 - EXP(SUM(LN
(
CASE WHEN (1 - events / at_risk) = 0 THEN 0.999999 ELSE (1 - events / at_risk) END
))
OVER (
ORDER BY survival_time_days ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
))) AS conversion_percentage,
SUM(events / at_risk) OVER (
SUM(events / at_risk)
OVER (
ORDER BY survival_time_days ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS cumulative_hazard,