Function that calculates NPV from a list of cash flows - python

trying to write a function that will calculate present value of list of cash flows. I know that numpy can do this very easily but for an assignment I have to write my own function for this :/.
Here are the three cash flows in a list as well as discount rate.
cfList = [20, 50, 90]
r = 0.05
Here's the function i've written so far. f = 0 because I want to start with the first cash flow (in this case 20). i = 1 because for the first flow its raised to the 1st power and the second flow (50) will be squared and so on.
def npv(cfList, r):
f = 0
i = 1
pv = cfList[f] / ((1 + r) ** i)
while i < len(cfList):
f += 1
i += 1
return pv
print(npv(cfList, r))
However, this output only gives me the PV of the first cashflow, and not the sum of all three from the list. If you can help i appreciate it so much thanks !

You need to sum the individual cashflows within your function and return that. At the moment you are returning the value of pv of the first cashflow as you have a return statement in your for loop.
Also, I think the way you check your while loop against i will mean that you'll miss the last payment value. Usually you don't need to instantiate counter variables yourself (see my examples below):
def npv(cfList, r):
f = 0
i = 1
pv = cfList[f] / ((1 + r) ** i) # <-- this needs to be in the loop
while i < len(cfList): # <-- i will break loop before last payment is calculated.
f += 1
i += 1
return pv # <-- this return here is the issue
print(npv(cfList, r))
NPV being the sum of PV of all future cashflows, that is what you need to calculate. E.g.:
def npv(cfList, r):
sum_pv = 0 # <-- variable used to sum result
for i, pmt in enumerate(cfList, start=1): # <-- use of enumerate allows you to do away with the counter variables.
sum_pv += pmt / ((1 + r) ** i) # <-- add pv of one of the cash flows to the sum variable
return sum_pv # <-- only return the sum after your loop has completed.
Always remember that a return statement in a for-loop will break out of the loop the first time the return is encountered.
An alternate implementation would be to yield individual PVs from a PV generator and sum the results:
def pv_gen(cfList, r):
for i, pmt in enumerate(cfList, start=1):
yield pmt / ((1 + r) ** i)
print(sum(pv_gen(cfList, r)))

Returning the NPV of a list of cash flows would look like:
def npv(cfList, r):
return sum(f / ((1 + r) ** i) for i, f in enumerate(cfList, 1))
In []:
cfList = [20, 50, 90]
r = 0.05
npv(cfList, r)
Out[]:
142.14447683835436

If you're iterating across the list using the while loop, then you should have the action taking line of code within the while loop.
It also looks like your loop will be cutting early as i = 2 = len(cflist) on the second iteration (don't forget that python uses 0 based indexing) and because the return call is within the while loop.
This should work:
def npv(cfList, r):
f = 0
i = 1
pv = 0
while f <= len(cfList):
pv += (cfList[f] / ((1 + r) ** i))
f += 1
i += 1
return pv

Related

Python Maclaurin series ln(x+1)

I have to write a program of Maclaurin series ln(x+1) on Python.
I need to use input function for two values: x, n. Then check if the values are legal and calculates the Maclaurin approximation (of order n) of the expression ln (1 + 𝑥) around the point x.
*Maclaurin series ln(x+1)= sum of ((-1)^n/n)*x^n
I stacked in the end when I calculate to expression, that what I wrote (after all the checks before):
for i in range(n + 1):
if i <= 1:
continue
else:
x = x + (((-1) ** (i + 1)) * (x ** i) / i)
When I input the test I get a number but it's a wrong answer.
Please help me understand what is wrong in this code.
Mathematically, the Maclaurin series is a bit beyond me, but I'll try to help. Two things.
First, you're storing all the successive values in x, as you calculate them; that means that the term for n = 5 (i = 5) is using a value of x which isn't the original value of the parameter x, but which has the successive results of the four previous computations stored in it. What you need to do instead is something like:
total = 0
for each value:
this term = some function of x # the value of x does not change
total = total + this term
Second, why aren't you interested in the term when i (or n) is equal to 1? The condition
if i <= 1:
continue
skips out the case when i equals 1, which evaluates to -x.
That should fix it, as far as I can see.
You are modifying the value of x in each iteration of the loop. Add and then store the partial sums in another variable.
def maclaurin_ln(x, n):
mac_sum = 0
for i in range(1, n + 1):
mac_sum += (((-1) ** (i + 1)) * (x ** i) / i)
return mac_sum
You can test this with the built-in function log1p to see how close they can get.
For ln(2) for different n,
from tabulate import tabulate
res = []
for n in [1, 10, 100, 1000, 10000]:
p = math.log1p(1)
q = maclaurin_ln(1, n)
res.append([1, n, p, q, q-p])
tabulate(res, headers=["x", "n", "log1p", "maclaurin_ln", "maclaurin_ln-log1p"])
x n log1p maclaurin_ln maclaurin_ln-log1p
--- ----- -------- -------------- --------------------
1 1 0.693147 1 0.306853
1 10 0.693147 0.645635 -0.0475123
1 100 0.693147 0.688172 -0.004975
1 1000 0.693147 0.692647 -0.00049975
1 10000 0.693147 0.693097 -4.99975e-05
For different x,
res = []
for x in range(10):
p = math.log1p(x/10)
q = maclaurin_ln(x/10, 100)
res.append([x/10, 1000, p, q, q-p])
tabulate(res, headers=["x", "n", "log1p", "maclaurin_ln", "maclaurin_ln-log1p"])
x n log1p maclaurin_ln maclaurin_ln-log1p
--- ---- --------- -------------- --------------------
0 1000 0 0 0
0.1 1000 0.0953102 0.0953102 1.38778e-17
0.2 1000 0.182322 0.182322 2.77556e-17
0.3 1000 0.262364 0.262364 -1.11022e-16
0.4 1000 0.336472 0.336472 0
0.5 1000 0.405465 0.405465 -1.11022e-16
0.6 1000 0.470004 0.470004 5.55112e-17
0.7 1000 0.530628 0.530628 -4.44089e-16
0.8 1000 0.587787 0.587787 -9.00613e-13
0.9 1000 0.641854 0.641854 -1.25155e-07

How can I implement this recursive formula?

Photo Formula
def prob(i, r, b): #r and b may be np.arrays or lists()
if i == 0:
return 1
else:
return (1 / i) * #And what's next?
Formula denotes unnormalized probability probability P(i) - the probability that the system is in state i, wherein and P(0) = 1, where I() - indicator function, b_k and r_k - const parameters, which fed to the input. I don't know how I can implement this code. I will appreciate any help!
def prob(i, rs, bs): # i: int, rs: [num], bs: [num]:
# I not know if the elems in rs are integers, neither positives
# I apply abs in rs elems and compare with equals and less
if i <= 0:
return 1
return (1 / i) * sum(b * prop(i - abs(r)) for (r, b) in zip(rs, bs))

Why my while loop failed (python)?

I'm a new learner of python programming. Recently I'm trying to write a "tool" program of "dynamic programming" algorithm. However, the last part of my programe -- a while loop, failed to loop. the code is like
import numpy as np
beta, rho, B, M = 0.5, 0.9, 10, 5
S = range(B + M + 1) # State space = 0,...,B + M
Z = range(B + 1) # Shock space = 0,...,B
def U(c):
"Utility function."
return c**beta
def phi(z):
"Probability mass function, uniform distribution."
return 1.0 / len(Z) if 0 <= z <= B else 0
def Gamma(x):
"The correspondence of feasible actions."
return range(min(x, M) + 1)
def T(v):
"""An implementation of the Bellman operator.
Parameters: v is a sequence representing a function on S.
Returns: Tv, a list."""
Tv = []
for x in S:
# Compute the value of the objective function for each
# a in Gamma(x), and store the result in vals (n*m matrix)
vals = []
for a in Gamma(x):
y = U(x - a) + rho * sum(v[a + z]*phi(z) for z in Z)
# the place v comes into play, v is array for each state
vals.append(y)
# Store the maximum reward for this x in the list Tv
Tv.append(max(vals))
return Tv
# create initial value
def v_init():
v = []
for i in S:
val = []
for j in Gamma(i):
# deterministic
y = U(i-j)
val.append(y)
v.append(max(val))
return v
# Create an instance of value function
v = v_init()
# parameters
max_iter = 10000
tol = 0.0001
num_iter = 0
diff = 1.0
N = len(S)
# value iteration
value = np.empty([max_iter,N])
while (diff>=tol and num_iter<max_iter ):
v = T(v)
value[num_iter] = v
diff = np.abs(value[-1] - value[-2]).max()
num_iter = num_iter + 1
As you can see, the while loop at the bottom is used to iterate over "value function" and find the right answer. However, the while fails to loop, and just return num_iter=1. As for I know, the while loop "repeats a sequence of statements until some condition becomes false", clearly, this condition will not be satisfied until the diff converge to near 0
The major part of code works just fine, as far as I use the following for loop
value = np.empty([num_iter,N])
for x in range(num_iter):
v = T(v)
value[x] = v
diff = np.abs(value[-1] - value[-2]).max()
print(diff)
You define value as np.empty(...). That means that it is composed completely of zeros. The difference, therefore, between the last element and the second-to-last element will be zero. 0 is not >= 0.0001, so that expression will be False. Therefore, your loop breaks.

Solving a mathematical equation recursively in Python

I want to solve an equation which I am supposed to solve it recursively, I uploaded the picture of formula (Sorry! I did not know how to write mathematical formulas here!)
I wrote the code in Python as below:
import math
alambda = 1.0
rho = 0.8
c = 1.0
b = rho * c / alambda
P0 = (1 - (alambda*b))
P1 = (1-(alambda*b))*(math.exp(alambda*b) - 1)
def a(n):
a_n = math.exp(-alambda*b) * ((alambda*b)**n) / math.factorial(n)
return a_n
def P(n):
P(n) = (P0+P1)*a(n) + sigma(n)
def sigma(n):
j = 2
result = 0
while j <= n+1:
result = result + P(j)*a(n+1-j)
j += 1
return result
It is obvious that I could not finish P function. So please help me with this.
when n=1 I should extract P2, when n=2 I should extract P3.
By the way, P0 and P1 are as written in line 6 and 7.
When I call P(5) I want to see P(0), P(1), P(2), P(3), P(4), P(5), P(6) at the output.
You need to reorganize the formula so that you don't have to calculate P(3) to calculate P(2). This is pretty easy to do, by bringing the last term of the summation, P(n+1)a(0), to the left side of the equation and dividing through by a(0). Then you have a formula for P(n+1) in terms of P(m) where m <= n, which is solvable by recursion.
As Bruce mentions, it's best to cache your intermediate results for P(n) by keeping them in a dict so that a) you don't have to recalculate P(2) etc everytime you need it, and b) after you get the value of P(n), you can just print the dict to see all the values of P(m) where m <= n.
import math
a_lambda = 1.0
rho = 0.8
c = 1.0
b = rho * c / a_lambda
p0 = (1 - (a_lambda*b))
p1 = (1-(a_lambda*b))*(math.exp(a_lambda*b) - 1)
p_dict = {0: p0, 1: p1}
def a(n):
return math.exp(-a_lambda*b) * ((a_lambda*b)**n) / math.factorial(n)
def get_nth_p(n, p_dict):
# return pre-calculated value if p(n) is already known
if n in p_dict:
return p_dict[n]
# Calculate p(n) using modified formula
p_n = ((get_nth_p(n-1, p_dict)
- (get_nth_p(0, p_dict) + get_nth_p(1, p_dict)) * a(n - 1)
- sum(get_nth_p(j, p_dict) * a(n + 1 - j) for j in xrange(2, n)))
/ a(0))
# Save computed value into the dict
p_dict[n] = p_n
return p_n
get_nth_p(6, p_dict)
print p_dict
Edit 2
Some cosmetic updates to the code - shortening the name and making p_dict a mutable default argument (something I try to use only sparingly) really makes the code much more readable:
import math
# Customary to distinguish variables that are unchanging by making them ALLCAP
A_LAMBDA = 1.0
RHO = 0.8
C = 1.0
B = RHO * C / A_LAMBDA
P0 = (1 - (A_LAMBDA*B))
P1 = (1-(A_LAMBDA*B))*(math.exp(A_LAMBDA*B) - 1)
p_value_cache = {0: P0, 1: P1}
def a(n):
return math.exp(-A_LAMBDA*B) * ((A_LAMBDA*B)**n) / math.factorial(n)
def p(n, p_dict=p_value_cache):
# return pre-calculated value if p(n) is already known
if n in p_dict:
return p_dict[n]
# Calculate p(n) using modified formula
p_n = ((p(n-1)
- (p(0) + p(1)) * a(n - 1)
- sum(p(j) * a(n + 1 - j) for j in xrange(2, n)))
/ a(0))
# Save computed value into the dict
p_dict[n] = p_n
return p_n
p(6)
print p_value_cache
You could fix if that way:
import math
alambda = 1.0
rho = 0.8
c = 1.0
b = rho * c / alambda
def a(n):
# you might want to cache a as well
a_n = math.exp(-alambda*b) * ((alambda*b)**n) / math.factorial(n)
return a_n
PCache={0:(1 - (alambda*b)),1:(1-(alambda*b))*(math.exp(alambda*b) - 1)}
def P(n):
if n in PCache:
return PCache[n]
ret= (P(0)+P(1))*a(n) + sigma(n)
PCache[n]=ret
return ret
def sigma(n):
# caching this seems smart as well
j = 2
result = 0
while j <= n+1:
result = result + P(j)*a(n+1-j)
j += 1
return result
void displayP(n):
P(n) # fill cache :-)
for x in range(n):
print ("%u -> %d\n" % (x,PCache[x]))
Instead of managing the cache manually, you might want to use a memoize decorator (see http://www.python-course.eu/python3_memoization.php )
Notes:
not tested, but you should get the idea behind it
your recurrence won't work P(n) depends on P(n+1) on your equation... This will never end
It looks like I misunderstood P0 and P1 as being Both constants (big numbers) and results (small numbers, indices)... Notation is not the best choice I guess...

Unknown numpy.optimize.fmin error

I am trying to write a program that calculates the optimum amount to bet based on log utility and simultaneous dependent events.
In order to do this I am trying to use the numpy.optimize.fmin function. The function anon that I am passing to it works and produces (hopefully) correct output but when numpy tries to optimise the function I get the following error
s[i].append(f[i][0]*w[i][0] + f[i][1]*w[i][1])
IndexError: invalid index to scalar variable.
Since I have no idea about fmin, I have no idea what is causing this error.
My code is below, hopefully not tl;dr but I wouldn't blame you.
APPENDIX
def main():
p = [[0.1,0.1,0.2, 0.2,0.1,0, 0.1,0.1,0.1]]
w = [[5,4]]
MaxLU(p,w,True)
def MaxLU(p, w, Push = False, maxIter = 10):
#Maximises LU, using Scipy in built function
if Push == True:
anon = lambda f: -PushLogUtility(p, w, f)
else:
anon = lambda f: -LogUtility(p, w, f)
#We use multiple random starts
f = []
LU = []
for i in range(0,maxIter):
start = np.random.rand(len(p))
start = start / 5 * np.sum(start)
f.append(optimize.fmin(anon, start)) #Error occurs in here!
if Push == True:
LU.append(PushLogUtility(p, w, f[-1]))
else:
LU.append(LogUtility(p, w, f[-1]))
#Now find the index of the max LU and return that same index of f
return f[LU.index(np.max(LU))]
def PushLogUtility(p,w,f):
#Outputs log utility incoroporating pushes and dependent totals, money data
#p : 9xk length vector of joint probabilities for each of the k games, p = [[p_(W_T W_M), p_(W_T P_M), p_(W_T L_M), p_(P_T W_M) ... ]]
#w : 2xk matrix of odds where w = [[total odds, money odds] ... ]
#f : 2xk matrix of bankroll percentages to bet, f = [[f_T, f_M] ... ]
utility = 0
k = len(p)
s = k*[[]]
for i in range(0,k):
s[i].append(f[i][0]*w[i][0] + f[i][1]*w[i][1])
s[i].append(f[i][0]*w[i][0])
s[i].append(f[i][0]*w[i][0] - f[i][1])
s[i].append(f[i][1]*w[i][1])
s[i].append(0)
s[i].append(-f[i][1])
s[i].append(-f[i][0] - f[i][1])
s[i].append(-f[i][0] - f[i][1])
s[i].append(-f[i][0] - f[i][1])
for i in range(0,9 ** k):
l = de2ni(i) #Converts number to base 9
if i == 0:
l += int(math.ceil(k - 1 - math.log(i + 1,9))) * [0]
else:
l += int(math.ceil(k - 1 - math.log(i,9))) * [0]
productTerm = np.prod([p[i][l[i]] for i in range(0,k)])
sumTerm = np.sum([s[i][l[i]] for i in range(0,k)])
utility = utility + productTerm * np.log(1 + sumTerm)
return utility
Here where you do:
s[i].append(f[i][0]*w[i][0] + f[i][1]*w[i][1])
if you look at the types, you'll find s[i] is a [], f[i] is 0.104528 and w[i] is [5,4]. You then try to index f[i] a second time - which is not possible and causes the error.

Categories

Resources