Measures Part 2
Solution
class Cashflow:
def __init__(self, val,t,r):
self.val = val
self.t = t
self.r = r
self.PV = self.valAt(0)
def valAt(self,time):
return self.val*(1+self.r)**(time-self.t)
def NPV(arr):
return sum([x.PV for x in arr])
def pIndex(flows):
pos = [x for x in flows if x.val > 0]
neg = [x for x in flows if x.val < 0]
return NPV(pos)/-NPV(neg)
The first two lines are list comprehension like we have seen except we now have added on an if condition. Basically we get x in our array but only if its value is positive/negative depending on the array. If it does not satisfy the condition it is not added. So we in essence create a filter for both of our arrays. The last line gets the present value of both the positive and negative cash flows, we put a negative sign before the second term so that we can get a positive profitability index measure.
An example:
flows = [Cashflow(-1500,0,.05),Cashflow(1000,1,.05),Cashflow(1000,2,.05)]
print(pIndex(flows))
The rule for taking a project with this measure is that if it is >1 we want to take it. The higher the value, the more appealing that project is because we get more bang for the buck.