1

Can anyone help me to calculate PALIVE(the probability that they are still alive at the end of the calibration period.) in Python?

I know R has a Pareto negative binomial distribution function pnbd.PAlive(params, r, s, apha, beta) but what is similar to it in Python?

Anton Protopopov
  • 30,354
  • 12
  • 88
  • 93
pybeginner
  • 11
  • 1

1 Answers1

1

You could use conditional_probability_alive method from lifetimes package. You need to pass frequency, recency, and T for each customer. For example for BetaGeoFitter (BG/NBD model):

from lifetimes import BetaGeoFitter
from lifetimes.datasets import load_cdnow_summary

# load data
data = load_cdnow_summary(index_col=[0])
print(data.head())

# fit lifetimes model
bgf = BetaGeoFitter(penalizer_coef=0.0)
bgf.fit(data['frequency'], data['recency'], data['T'])
print(bgf)

# predict p_alives for customers
p_alive1 = bgf.conditional_probability_alive(2, 30.43, 38.86)
p_alive2 = bgf.conditional_probability_alive(1, 30, 30)
print(p_alive1, p_alive2)

Output:

    frequency  recency      T
ID                           
1           2    30.43  38.86
2           1     1.71  38.86
3           0     0.00  38.86
4           0     0.00  38.86
5           0     0.00  38.86
<lifetimes.BetaGeoFitter: fitted with 2357 subjects, a: 0.79, alpha: 4.41, b: 2.43, r: 0.24>
0.7266084620654866 0.753658243186767
Anton Protopopov
  • 30,354
  • 12
  • 88
  • 93