How to use the lifetimes.estimation.GammaGammaFitter function in Lifetimes

To help you get started, we’ve selected a few Lifetimes examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github CamDavidsonPilon / lifetimes / tests / test_estimation.py View on Github external
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)
github CamDavidsonPilon / lifetimes / tests / test_estimation.py View on Github external
def test_customer_lifetime_value_with_bgf(self, cdnow_customers_with_monetary_value):

        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']
        )
github CamDavidsonPilon / lifetimes / tests / test_estimation.py View on Github external
def test_params_out_is_close_to_Hardie_paper(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
        )
        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)
github CamDavidsonPilon / lifetimes / tests / test_estimation.py View on Github external
def test_conditional_expected_average_profit(self, cdnow_customers_with_monetary_value):

        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)
github CamDavidsonPilon / lifetimes / tests / test_estimation.py View on Github external
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
github CamDavidsonPilon / lifetimes / tests / test_estimation.py View on Github external
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))