Basic Financial Library for Python
Asked Answered
L

3

9

I am looking for a financial library for Python that will enable me to do a discounted cash flow analysis. I have looked around and found the QuantLib, which is overkill for what I want to do. I just need a small library that I can use to input a series of cash flows and have it output a net present value and internal rate of return. Anyone have something like this or know where I can find it?

Linwoodlinz answered 13/2, 2010 at 22:40 Comment(0)
S
12

Just for completeness, since I'm late: numpy has some functions for (very) basic financial calculations. numpy, scipy could also be used, to do the calculations from the basic formulas as in R.

net present value of cashflow

>>> cashflow = 2*np.ones(6)
>>> cashflow[-1] +=100
>>> cashflow
array([   2.,    2.,    2.,    2.,    2.,  102.])
>>> np.npv(0.01, cashflow)
105.79547647457932

get internal rate or return

>>> n = np.npv(0.01, cashflow)
>>> np.irr(np.r_[-n, cashflow])
0.010000000000000231

just the basics:

>>> [f for f in dir(np.lib.financial) if not f[0] == '_']
['fv', 'ipmt', 'irr', 'mirr', 'np', 'nper', 'npv', 'pmt', 'ppmt', 'pv', 'rate']

and it's necessary to watch out what the timing is.

Steck answered 25/8, 2010 at 2:57 Comment(1)
it's so fun when one can python the irr method of np during the finance classStaphyloplasty
W
3

If you really only want to compute net present value (== an inner product of vectors for cashflows and discount factors) and internal rate of return (== a simple iterative root search for one variable) then you can just code it up.

I use R much more than Python so here is an R solution:

R> data <- data.frame(CF=c(rep(2,5), 102), df=1.01^(-(1:6)))
R> data
   CF     df
1   2 0.9901
2   2 0.9803
3   2 0.9706
4   2 0.9610
5   2 0.9515
6 102 0.9420
R> NPV <- sum(data[,1] * data[,2])
R> print(NPV)
[1] 105.8
R> 

This sets up two-column data structure of cash flows and discount factors and computes NPV as the sum of the products. So a (simplistic) six-year bond with a 2% coupon in 1% flat yield curve would be worth 105.80.

For IRR, we do just about the same but make the NPV a function of the rate:

R> irrSearch <- function(rate) { data <- data.frame(CF=c(rep(2,5), 102), 
                                 df=(1+rate/100)^(-(1:6))); 
                                 100 - sum(data[,1] * data[,2]) }
R> uniroot( irrSearch, c(0.01,5) )
R> irr <- uniroot( irrSearch, c(0.01,5) )
R> irr$root
[1] 2
R> 

So the 'root' to the search for the internal rate of return of 2% bond in a flat-curve world is ... unsurprisingly 2%.

Whitsuntide answered 13/2, 2010 at 23:2 Comment(2)
See this library: rpy.sourceforge.net/index.html That will allow you to call R from Python without much extra work.Kunstlied
Yup, but that is in maintenance mode. It may be wiser to use the newer RPy2 instead.Whitsuntide
O
0

Probably too late to answer your question. I learned about valuation at graduate school and work, but mainly from the dean of valuation, Dr. Damodran. Post learning how to value companies and building models in Excel, I tried many Python libraries to do DCF valuations, and every single one of them had some shortcomings. So I ended up coding a DCF Model in Python that is constructed the way Dr. Damodran builds his DCF model in spreadsheets. Furthermore, I created a DCF Monte Carlo simulation model in Python. To complete making a stab at this project, for those who might be interested in doing intrinsic valuation in Python, I created a tutorial video on how to utilize the DCF model.

It took me +2 years to build this and I thought it could save someone who is looking to do intrinsic business / stock valuation in Python a considerable amount of time. I hope you find them useful.

Tutorial on YouTube

DCF Model In Python Colabratory File

GitHub

Oilla answered 22/9, 2023 at 6:14 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewLianne

© 2022 - 2024 — McMap. All rights reserved.