def test_customer_lifetime_value_with_bgf(self): from collections import OrderedDict ggf = estimation.GammaGammaFitter() ggf.params_ = OrderedDict({'p': 6.25, 'q': 3.74, 'v': 15.44}) bgf = estimation.BetaGeoFitter() bgf.fit(cdnow_customers_with_monetary_value['frequency'], cdnow_customers_with_monetary_value['recency'], cdnow_customers_with_monetary_value['T'], iterative_fitting=3) ggf_clv = ggf.customer_lifetime_value( bgf, cdnow_customers_with_monetary_value['frequency'], cdnow_customers_with_monetary_value['recency'], cdnow_customers_with_monetary_value['T'], cdnow_customers_with_monetary_value['monetary_value']) utils_clv = utils.customer_lifetime_value( bgf, cdnow_customers_with_monetary_value['frequency'], cdnow_customers_with_monetary_value['recency'], cdnow_customers_with_monetary_value['T'], ggf.conditional_expected_average_profit( cdnow_customers_with_monetary_value['frequency'], cdnow_customers_with_monetary_value['monetary_value'])) npt.assert_equal(ggf_clv.values, utils_clv.values)
def test_params_out_is_close_to_Hardie_paper(self): ggf = estimation.GammaGammaFitter() ggf.fit( cdnow_customers_with_monetary_value['frequency'], cdnow_customers_with_monetary_value['monetary_value'], iterative_fitting=3 ) expected = np.array([6.25, 3.74, 15.44]) npt.assert_array_almost_equal(expected, np.array(ggf._unload_params('p', 'q', 'v')), decimal=2)
def test_conditional_expected_average_profit(self): ggf = estimation.GammaGammaFitter() ggf.params_ = OrderedDict({'p':6.25, 'q':3.74, 'v':15.44}) summary = cdnow_customers_with_monetary_value.head(10) estimates = ggf.conditional_expected_average_profit(summary['frequency'], summary['monetary_value']) expected = np.array([24.65, 18.91, 35.17, 35.17, 35.17, 71.46, 18.91, 35.17, 27.28, 35.17]) # from Hardie spreadsheet http://brucehardie.com/notes/025/ npt.assert_allclose(estimates.values, expected, atol=0.1)
def test_negative_log_likelihood_is_inf_when_q_constraint_true_and_q_lt_one( self): frequency = 25 avg_monetary_value = 100 ggf = estimation.GammaGammaFitter() assert np.isinf( ggf._negative_log_likelihood([6.25, -3.75, 15.44], frequency, avg_monetary_value, q_constraint=True))
def test_fit_with_index(self, cdnow_customers_with_monetary_value): returning_cdnow_customers_with_monetary_value = cdnow_customers_with_monetary_value[ cdnow_customers_with_monetary_value['frequency'] > 0] ggf = estimation.GammaGammaFitter() index = range(len(returning_cdnow_customers_with_monetary_value), 0, -1) ggf.fit( returning_cdnow_customers_with_monetary_value['frequency'], returning_cdnow_customers_with_monetary_value['monetary_value'], iterative_fitting=1, index=index) assert (ggf.data.index == index).all() == True ggf = estimation.GammaGammaFitter() ggf.fit( returning_cdnow_customers_with_monetary_value['frequency'], returning_cdnow_customers_with_monetary_value['monetary_value'], iterative_fitting=1, index=None) assert (ggf.data.index == index).all() == False
def test_params_out_is_close_to_Hardie_paper_with_q_constraint( self, cdnow_customers_with_monetary_value): returning_cdnow_customers_with_monetary_value = cdnow_customers_with_monetary_value[ cdnow_customers_with_monetary_value['frequency'] > 0] ggf = estimation.GammaGammaFitter() ggf.fit( returning_cdnow_customers_with_monetary_value['frequency'], returning_cdnow_customers_with_monetary_value['monetary_value'], iterative_fitting=3, q_constraint=True) expected = np.array([6.25, 3.74, 15.44]) npt.assert_array_almost_equal(expected, np.array( ggf._unload_params('p', 'q', 'v')), decimal=2)