Last active
November 26, 2015 11:18
-
-
Save talegari/b1e1f4575476a6dea9b1 to your computer and use it in GitHub Desktop.
customer lifecycle value calulation (CLV,LCV,LTV)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#---------------------------------------------------------- | |
# customer lifecycle value calulation (CLV,LCV,LTV) | |
# python version : 2.7.10 | |
#---------------------------------------------------------- | |
# facilitate division | |
from __future__ import division | |
# clvBasic ---- | |
# define clv for finite or infinite horizon(from geometric series) | |
# margin : gross margin per customer(or cohort) per time period (typically a year) | |
# retention : retention rate for the time period(typically a year) | |
# discount : discount rate(inflation accounted for) | |
# time : number of time periods(typically number of years) | |
# ,avg lifetime = 1/churn rate, default is zero indicating infinite horizon | |
# firstPay : whether the customer pays for the first time period | |
# prepaid : whether the scheme is prepaid or not | |
# (note that both firstPay and prepaid cannot be False) | |
def clvBasic(margin | |
,retention | |
,discount | |
,time = 0 | |
,firstPay = False | |
,prepaid = True): | |
if time == 0: | |
if firstPay == False and prepaid == True: | |
return margin * (retention/(1 + discount - retention)) | |
elif firstPay == True and prepaid == True: | |
return margin * (1 + d) * (retention/(1 + discount - retention)) | |
elif firstPay == True and prepaid == False: | |
return margin * (1/(1 + discount - retention)) | |
else: | |
raise ValueError('Both firstPay and prepaid cannot be False.') | |
else: | |
if firstPay == False and prepaid == True: | |
firstTerm = 0 | |
begin = 1 | |
end = time | |
return margin * sum([(retention/(1 + discount)) ** i for i in range(begin,end)]) + firstTerm | |
elif firstPay == True and prepaid == True: | |
firstTerm = margin | |
begin = 1 | |
end = time - 1 | |
return margin * sum([(retention/(1 + discount)) ** i for i in range(begin,end)]) + firstTerm | |
elif firstPay == True and prepaid == False: | |
firstTerm = margin/(1 + discount) | |
begin = 1 | |
end = time - 1 | |
return margin * sum([(retention ** i)/((1 + discount) ** (i + 1)) for i in range(begin,end)]) + firstTerm | |
else: | |
raise ValueError('Both firstPay and prepaid cannot be False.') | |
# example (not run) | |
''' | |
clvBasic(margin = 100 \ | |
,retention = 0.8 \ | |
,discount = 0.1 \ | |
,time = 10 \ | |
,firstPay = False \ | |
,prepaid = True) | |
''' | |
# clvCost ---- | |
# clv for finite horizon with contribution and retention costs | |
# contrib : gross margin per customer(or cohort) per time period (typically a year) | |
# cost : retention cost per time period(typically a year) | |
# retention : retention rate for the time period(typically a year)(1 - churn rate) | |
# discount : discount rate(inflation accounted for) | |
# time : number of time periods(typically number of years) | |
# ,avg lifetime = 1/churn rate | |
# firstPay : whether the customer pays for the first time period | |
# prepaid : whether the scheme is prepaid or not | |
# (note that both firstPay and prepaid cannot be False) | |
def clvCost(contrib | |
,cost | |
,retention | |
,discount | |
,time | |
,firstPay = False | |
,prepaid = True): | |
if firstPay == False and prepaid == True: | |
firstTerm = 0 | |
begin = 1 | |
end = time | |
conSum = contrib * sum([(retention/(1 + discount)) ** i for i in range(begin,end)]) + firstTerm | |
retSum = cost * sum([((retention)**(i-1))/((1 + discount)**(i - 0.5)) for i in range(1,time)]) | |
return conSum - retSum | |
elif firstPay == True and prepaid == True: | |
firstTerm = contrib | |
begin = 1 | |
end = time - 1 | |
conSum = contrib * sum([(retention/(1 + discount)) ** i for i in range(begin,end)]) + firstTerm | |
retSum = cost * sum([((retention)**(i-1))/((1 + discount)**(i - 0.5)) for i in range(1,time)]) | |
return conSum - retSum | |
elif firstPay == True and prepaid == False: | |
firstTerm = contrib/(1 + discount) | |
begin = 1 | |
end = time - 1 | |
conSum = contrib * sum([(retention ** i)/((1 + discount) ** (i + 1)) for i in range(begin,end)]) + firstTerm | |
retSum = cost * sum([((retention)**(i-1))/((1 + discount)**(i - 0.5)) for i in range(1,time)]) | |
return conSum - retSum | |
else: | |
raise ValueError('Both firstPay and prepaid cannot be False.') | |
# example (not run) | |
''' | |
clvCost(contrib = 100 | |
,cost = 25 | |
,retention = 0.8 | |
,discount = 0.09 | |
,time = 15 | |
,firstPay = False | |
,prepaid = True) | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment