def probability_of_n_purchases_up_to_time(self, t, n): r""" Compute the probability of n purchases up to time t. .. math:: P( N(t) = n | \text{model} ) where N(t) is the number of repeat purchases a customer makes in t units of time. Parameters ---------- t: float number units of time n: int number of purchases Returns ------- float: Probability to have n purchases up to t units of time """ r, alpha, a, b = self._unload_params("r", "alpha", "a", "b") _j = np.arange(0, n) first_term = (beta(a, b + n + 1) / beta(a, b) * gamma(r + n) / gamma(r) / gamma(n + 1) * (alpha / (alpha + t))**r * (t / (alpha + t))**n) finite_sum = (gamma(r + _j) / gamma(r) / gamma(_j + 1) * (t / (alpha + t))**_j).sum() second_term = beta(a + 1, b + n) / beta( a, b) * (1 - (alpha / (alpha + t))**r * finite_sum) return first_term + second_term
def probability_of_n_purchases_up_to_time( self, t, n ): r""" Compute the probability of n purchases. .. math:: P( N(t) = n | \text{model} ) where N(t) is the number of repeat purchases a customer makes in t units of time. Comes from equation (8) of [2]_. Parameters ---------- t: float number units of time n: int number of purchases Returns ------- float: Probability to have n purchases up to t units of time References ---------- .. [2] Fader, Peter S., Bruce G.S. Hardie, and Ka Lok Lee (2005a), "Counting Your Customers the Easy Way: An Alternative to the Pareto/NBD Model," Marketing Science, 24 (2), 275-84. """ r, alpha, a, b = self._unload_params("r", "alpha", "a", "b") first_term = ( beta(a, b + n) / beta(a, b) * gamma(r + n) / gamma(r) / gamma(n + 1) * (alpha / (alpha + t)) ** r * (t / (alpha + t)) ** n ) if n > 0: j = np.arange(0, n) finite_sum = (gamma(r + j) / gamma(r) / gamma(j + 1) * (t / (alpha + t)) ** j).sum() second_term = beta(a + 1, b + n - 1) / beta(a, b) * (1 - (alpha / (alpha + t)) ** r * finite_sum) else: second_term = 0 return first_term + second_term
defvjp( gammaincln, central_difference_of_(gammaincln), lambda ans, a, x: unbroadcast_f( x, lambda g: g * np.exp(-x + np.log(x) * (a - 1) - gammaln(a) - gammaincln(a, x)), ), ) defvjp( betainc, central_difference_of_(betainc, argnum=0), central_difference_of_(betainc, argnum=1), lambda ans, a, b, x: unbroadcast_f( x, lambda g: g * np.power(x, a - 1) * np.power(1 - x, b - 1) / beta( a, b)), ) defvjp( betaincln, central_difference_of_(betaincln, argnum=0), central_difference_of_(betaincln, argnum=1), lambda ans, a, b, x: unbroadcast_f( x, lambda g: g * np.power(x, a - 1) * np.power(1 - x, b - 1) / beta(a, b) / betainc(a, b, x), ), )
defvjp( gammaincln, central_difference_of_log(gammainc), lambda ans, a, x: unbroadcast_f( x, lambda g: g * np.exp(-x + np.log(x) * (a - 1) - gammaln(a) - gammaincln(a, x)), ), ) defvjp( betainc, central_difference_of_(betainc, argnum=0), central_difference_of_(betainc, argnum=1), lambda ans, a, b, x: unbroadcast_f( x, lambda g: g * np.power(x, a - 1) * np.power(1 - x, b - 1) / beta( a, b)), ) defvjp( betaincinv, central_difference_of_(betaincinv, argnum=0), central_difference_of_(betaincinv, argnum=1), lambda ans, a, b, y: unbroadcast_f( y, lambda g: g * 1 / (np.power(betaincinv(a, b, y), a - 1) * np.power( 1 - betaincinv(a, b, y), b - 1) / beta(a, b))), ) defvjp( betaincln, central_difference_of_log(betainc, argnum=0), central_difference_of_log(betainc, argnum=1),