Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def fitted_bg(example_summary_data):
bg = BetaGeoFitter()
bg.fit(example_summary_data['frequency'], example_summary_data['recency'], example_summary_data['T'], iterative_fitting=2, tol=1e-6)
return bg
def bgf_transactions(cdnow_transactions):
transactions_summary = utils.summary_data_from_transaction_data(
cdnow_transactions, 'id_sample', 'date', datetime_format='%Y%m%d',
observation_period_end='19970930', freq='W')
bgf = BetaGeoFitter(penalizer_coef=0.01)
bgf.fit(transactions_summary['frequency'],
transactions_summary['recency'], transactions_summary['T'])
return bgf
customer_id_col = 'id_sample'
t = 14
datetime_format = '%Y%m%d'
freq = 'D'
observation_period_end = '19970930'
freq_multiplier = 1
transactions_summary = utils.summary_data_from_transaction_data(
cdnow_transactions, customer_id_col, datetime_col,
datetime_format=datetime_format, freq=freq,
freq_multiplier=freq_multiplier,
observation_period_end=observation_period_end)
transactions_summary = transactions_summary.reset_index()
model = BetaGeoFitter()
model.fit(transactions_summary['frequency'],
transactions_summary['recency'],
transactions_summary['T'])
df_cum = utils.expected_cumulative_transactions(
model, cdnow_transactions, datetime_col, customer_id_col, t,
datetime_format, freq, set_index_date=True,
freq_multiplier=freq_multiplier)
dates = ['1997-01-11', '1997-01-12', '1997-01-13', '1997-01-14']
actual_trans = [11, 12, 15, 19]
expected_trans = [10.67, 12.67, 14.87, 17.24]
date_index = df_cum.iloc[-4:].index.to_timestamp().astype(str)
actual = df_cum['actual'].iloc[-4:].values
predicted = df_cum['predicted'].iloc[-4:].values.round(2)
def bgf(cd_data):
bgf_model = BetaGeoFitter()
bgf_model.fit(cd_data['frequency'], cd_data['recency'], cd_data['T'], iterative_fitting=1)
return bgf_model
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import division
import warnings
import autograd.numpy as np
from autograd.numpy import log, logaddexp
from autograd.scipy.special import gammaln, beta, gamma
from scipy.special import hyp2f1
from lifetimes import BetaGeoFitter
from lifetimes.generate_data import modified_beta_geometric_nbd_model
class ModifiedBetaGeoFitter(BetaGeoFitter):
r"""
Also known as the MBG/NBD model.
Based on [5]_, [6]_, this model has the following assumptions:
1) Each individual, ``i``, has a hidden ``lambda_i`` and ``p_i`` parameter
2) These come from a population wide Gamma and a Beta distribution
respectively.
3) Individuals purchases follow a Poisson process with rate :math:`\lambda_i*t` .
4) At the beginning of their lifetime and after each purchase, an
individual has a p_i probability of dieing (never buying again).
References
----------
.. [5] Batislam, E.P., M. Denizel, A. Filiztekin (2007),
"Empirical validation and comparison of models for customer base
analysis,"