def fit(self, frequency, recency, T, iterative_fitting=1, initial_params=None, verbose=False): """ This methods fits the data to the MBG/NBD model. Parameters: frequency: the frequency vector of customers' purchases (denoted x in literature). recency: the recency vector of customers' purchases (denoted t_x in literature). T: the vector of customers' age (time since first purchase) iterative_fitting: perform `iterative_fitting` additional fits to find the best parameters for the model. Setting to 0 will improve performances but possibly hurt estimates. initial_params: set the initial parameters for the fitter. verbose: set to true to print out convergence diagnostics. Returns: self, with additional properties and methods like params_ and predict """ super(self.__class__, self).fit( frequency, recency, T, iterative_fitting, initial_params, verbose ) #although the partent method is called, this class's _negative_log_likelihood is referenced self.generate_new_data = lambda size=1: modified_beta_geometric_nbd_model( T, *self._unload_params('r', 'alpha', 'a', 'b'), size=size ) #this needs to be reassigned from the parent method return self
def simulate(pareto, mbg): times = [10, 365, 3650, 36500] r_par, alpha_par, s, beta = pareto.params_ r_mbg, alpha_mbg, a, b = mbg.summary['coef'] size = 100 for duration in times: generated_customers_par = pareto_nbd_model(T=duration, r=r_par, alpha=alpha_par, s=s, beta=beta, size=size) generated_customers_mbg = modified_beta_geometric_nbd_model( T=duration, r=r_mbg, alpha=alpha_mbg, a=a, b=b, size=size) print("Duration: {}".format(duration)) print("Pareto/NBD : Modified BG/NBD") print("Number of Purchases: {} : Number of Purchases: {}".format( generated_customers_par['frequency'].sum(), generated_customers_mbg['frequency'].sum())) print("Number of Customers Alive: {} : Number of Customers Alive: {}". format(generated_customers_par['alive'].sum(), generated_customers_mbg['alive'].sum())) print( "-----------------------------------------------------------------" )
def fit( self, frequency, recency, T, weights=None, initial_params=None, verbose=False, tol=1e-7, index=None, **kwargs ): """ Fit the data to the MBG/NBD model. Parameters ---------- frequency: array_like the frequency vector of customers' purchases (denoted x in literature). recency: array_like the recency vector of customers' purchases (denoted t_x in literature). T: array_like customers' age (time units since first purchase) weights: None or array_like Number of customers with given frequency/recency/T, defaults to 1 if not specified. Fader and Hardie condense the individual RFM matrix into all observed combinations of frequency/recency/T. This parameter represents the count of customers with a given purchase pattern. Instead of calculating individual log-likelihood, the log-likelihood is calculated for each pattern and multiplied by the number of customers with that pattern. verbose : bool, optional set to true to print out convergence diagnostics. tol : float, optional tolerance for termination of the function minimization process. index: array_like, optional index for resulted DataFrame which is accessible via self.data kwargs: key word arguments to pass to the scipy.optimize.minimize function as options dict Returns ------- ModifiedBetaGeoFitter: With additional properties and methods like ``params_`` and ``predict`` """ # although the parent method is called, this class's # _negative_log_likelihood is referenced super(ModifiedBetaGeoFitter, self).fit( frequency, recency, T, weights, initial_params, verbose, tol, index=index, **kwargs ) # this needs to be reassigned from the parent method self.generate_new_data = lambda size=1: modified_beta_geometric_nbd_model( T, *self._unload_params("r", "alpha", "a", "b"), size=size ) self.variance_matrix_ = self._compute_variance_matrix() self.standard_errors_ = self._compute_standard_errors() self.confidence_intervals_ = self._compute_confidence_intervals() return self
def fit(self, frequency, recency, T, iterative_fitting=1, initial_params=None, verbose=False, tol=1e-4, index=None): """ This methods fits the data to the MBG/NBD model. Parameters: frequency: the frequency vector of customers' purchases (denoted x in literature). recency: the recency vector of customers' purchases (denoted t_x in literature). T: the vector of customers' age (time since first purchase) iterative_fitting: perform iterative_fitting fits over random/warm-started initial params initial_params: set the initial parameters for the fitter. verbose: set to true to print out convergence diagnostics. index: index for resulted DataFrame which is accessible via self.data Returns: self, with additional properties and methods like params_ and predict """ super(self.__class__, self).fit( frequency, recency, T, iterative_fitting, initial_params, verbose, tol, index=index ) # although the parent method is called, this class's _negative_log_likelihood is referenced self.generate_new_data = lambda size=1: modified_beta_geometric_nbd_model( T, *self._unload_params('r', 'alpha', 'a', 'b'), size=size ) # this needs to be reassigned from the parent method return self