-
Notifications
You must be signed in to change notification settings - Fork 0
/
fin-functions-python.txt
141 lines (110 loc) · 4.78 KB
/
fin-functions-python.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
Financial Modeling With Python
------------------------------------------------------
The code of the following programs will introduce
several financial modeling concepts ranged from very simple to
complex (recursion) and their solutions.
Future Value
-------------
If one makes a series of regular deposits for a certain interest
rate for a certain number of periods, the amount of money
accrued at the end of the periods is the future value.
The FV formula is: FV = PMT * ((1+i)^N-1)/i where PMT is the periodic
deposit, i is the interest rate and N is the number of periods.
The program should prompt for the monthly deposit, the annual
interest rate (e.g. 8 for 8%) and then divide that number by 12
and by 100 for the true monthly interest rate. The number of
periods is the number of years multiplied by 12. The result is based
on monthly interest compounding.
# ******fv.py
pmt=float(input("Monthly deposit : "))
interest=float(input("Annual Interest (e.g. 8 for 8%) : "))
interest=interest/1200
periods=float(input("Number of years : "))
periods=periods*12
fv=pmt*((pow(1+interest,periods)-1)/interest)
print ('{:>33}'.format('Future value of investment:'),'{:0,.2f}'.format(fv))
#*****
Present Value
------------------
The present value is the amount of the loan that can be obtained
for the i interest rate, PMT monthly payment and N number of
periods. The formula is PV=PMT * (1-(1+i)^-N)/i.
#*******pv.py
pmt=float(input("Monthly deposit : "))
interest=float(input("Annual Interest (e.g. 8 for 8%) : "))
interest=interest/1200
periods=float(input("Number of years : "))
periods=periods*12
pv=pmt*((1-pow(1+interest,-periods))/interest);
print ('{:>33}'.format('Present value of investment:'),'{:0,.2f}'.format(pv))
#*******
Interest rate
-------------
The interest rate calculation is given by:
ln(1+i)=(ln(FV)-ln(PV))/N. After applying exp to both sides the
equation becomes 1+i=exp((ln(FV)-ln(PV))/N) or
i=exp((ln(FV)-ln(PV))/N)-1.
The program that calculates i through the FV PV link is:
--------------------------
#**********int.py
import math
fv=float(input("Future Value : "))
pv=float(input("Present Value : "))
periods=float(input("Number of years : "))
periods=periods*12
interest=(math.exp((math.log(fv)-math.log(pv))/periods)-1)*1200
print ('{:>33}'.format('Interest Rate of investment:'),'{:0,.2f}'.format(interest),'%')
#***********
Net Present Value
------------------
It's difficult to imagine an investment with nothing but
regular and equal deposits (i.e positive cash flows) and no
withdrawals (i.e. negative cash flows).
<br>Net present value addresses the worth of a series of unequal and different sign
cash flows. The NPV formula is:
CF[1]/(1+i)^1+CF[2]/(1+i)^2+..CF[N]/(1+i)^N where CF are the
cash flows in the respective periods, N is the number of periods
and i is the considered interest rate.
If the interest rate is 0 then the denominator is 1. Therefore,
if i=0 the net present value is the sum of cash flows. As i
becomes larger NPV gets smaller. Consider the following example:
Here's an example: You buy a mountain cabin for $25,000. During the first four
years you are able to rent it out and you collect $3,500, $100,
$4000 and $100 respectively. The fifth year you have a negative
cash flow of $300 due to repairs.
The cash flows for years 6 through 11 are : 4000 2300 -150 5600 321 -100.
In the twelfth year you sell the cabin for $24,900.
You ask yourself, would you
have been better off depositing your $25,000 at 6% or is the
series of cash flows generated by your investment greater in
value? Solving the NPV equation for 6% gives a net present value
of $1,675.49 more than your initial investment.
This the NPV progran. It will ask for the annual interest rate and
it will extract the cash flows from a file called cashflows.txt
#***************************
def read_floats(file_name,cash_flows):
try:
with open(file_name, 'r') as file:
for line in file:
try:
float_value = float(line.strip())
cash_flows.append(float_value)
except ValueError:
print(f"Error in line: {line.strip()}")
except FileNotFoundError:
print(f"File not found: {file_name}")
except Exception as e:
print(f"An error occurred: {e}")
#
def calculate_npv(cash_flows, int_rate):
npv = sum(cf / (1 + int_rate)**t for t, cf in enumerate(cash_flows))
return npv
cash_flows=[]
int_rate=float(input("Annual Interest Rate: "))
int_rate=int_rate/100
file_name = 'cashflows.txt'
read_floats(file_name,cash_flows)
npv = calculate_npv(cash_flows, int_rate)
print ('{:>33}'.format('Net Present value of investments:'),
'{:0,.2f}'.format(npv))
#***************************