2.8
Let CFSA、CFSB denote the cash flow streams of option A and option B, and they are the following.
CFSA = (-6000, -8000, -8000, -8000, -8000, 0) CFSB = (-30000, -2000, -2000, -2000, -2000, 10000)
Let CFSA->B denote CFSA - CFSB, and it is the following.
CFSA->B = CFSB - CFSA =(-24000, 6000, 6000, 6000, 6000, 10000)
The internal rate of return is found by solving the equation
f(c) = -24000 + 6000c + 6000c2 + 6000c3 + 6000c4 + 10000c5
(Fig.2.8 shows the above equation.)
The solution can be found to be 0.89,
and thus
∴ IRR = (1 / c) - 1 = (1 / 0.89) - 1 = 0.12
note: gnuplot input file
The file is linked below.
- 2.8.plt for Fig. 2.8
Python with numpy.irr
codeの説明
関数 numpy.irr() を用いて、内部収益率 irr を求める。(added on 2016-12-03)
code
# 2.8.numpy.irr.py
# create on 2016/12/03
import numpy
# input
cfs = [-24, 6, 6, 6, 6, 10]
# calculate
a = numpy.irr(cfs)
# output
print(round(a, 3))
# end
output
0.118
Python with numpy.polynomial.Polynomial
codeの説明
- p = numpy.polynomial.Polynomial()で、多項式を作成し、その多項式を p に代入
- root1 = p.roots() で、多項式 p = 0 の解の配列を求め、その解の配列を root1 に入力 (root1は、複素数の配列である。)
- index1 = numpy.isreal(root1) で、解の配列 root1 の各要素 root1[i] が実数か否か (True/False) の配列を index1 に入力
- root2 = root1[index1] で、解の配列root1から実数の解のみの配列を作成し、 root2に代入(root2は、複素数(虚部を持つ)の配列である。)
- root3 = numpy.real(root2) で、複素数の配列root2を実数の配列に変換し、 その配列をroo3に代入 (root3は、実数の配列である。)
- root4 = root3[0]で、実数解の配列root3の添え字が0の要素をroot4に代入 (root4は、実数である。)
code
# 2.8.poly.py # created on 2016-11-19 import numpy # input p = numpy.polynomial.Polynomial([-24, 6, 6, 6, 6, 10]) # calculate root1 = p.roots() index1 = numpy.isreal(root1) root2 = root1[index1] root3 = numpy.real(root2) root4 = root3[0] # output print('2.8.poly.py') print('p=', p) print('root1=', root1) print() print('real root?', index1) print('{:10}{:20}{:20}'.format('root2=', str(root2), ' (=complex array)')) print('{:10}{:20}{:20}'.format('root3=', str(root3), ' (=real array)' )) print('{:10}{:20}{:20}'.format('root4=', root4, ' (=real number)' )) # end
output
2.8.poly.py p= poly([-24. 6. 6. 6. 6. 10.]) root1= [-1.06272954-0.7431193j -1.06272954+0.7431193j 0.31566636-1.22333315j 0.31566636+1.22333315j 0.89412636+0.j ] real root? [False False False False True] root2= [ 0.89412636+0.j] (=complex array) root3= [ 0.89412636] (=real array) root4= 0.8941263576148393 (=real number)
Python
codeの説明
- x = 0、x = 1 をそれぞれ左端、右端として、2分法で f(x) = 0 の解を求める。 ただし、f(0) < 0, f(1) > 0である。
code
# 2.8.py # 16:35 2015-03-29 def f(x): return -24e3 + 6e3*x + 6e3*x**2 + 6e3*x**3 + 6e3*x**4 + 1e4*x**5 # input i = 1 eps = 1e-5 left = 0 right = 1 mid = (left + right) / 2 while abs(f(mid))>eps: if(f(mid))>0: right = mid else: left = mid mid = (left + right) / 2 i = i + 1 # output print("c= ", round(mid, 2)) print("f(c)= {:.2e}".format(f(mid))) print("i= ", i) # end
output
c= 0.89 f(c)= -8.36e-06 i= 29
Fortran code
An Fortran code to solve f(c) = 0 in bisection method is below.
2.8.f90(2025-02-01 open revised 2.8.f90)
Gauche code
An Gauche code to solve f(c) = 0 in bisection method is below.
JavaScript code
(added on 2016-02-07)
Maxima
Maxima で f(c) = 0 の根を求める。(added on 2016-11-04)
注
下記は、(-24000) + 6000*c + 6000*c^2 + 6000*c^3 + 6000*c^4 + 10000*c^5 = 0
の根を閉区間[0, 1]上で見つける。見つけた根は、0.894...である。
Maxima 5.38.1 http://maxima.sourceforge.net using Lisp SBCL 1.3.4 Distributed under the GNU Public License. See the file COPYING. Dedicated to the memory of William Schelter. The function bug_report() provides bug reporting information. (%i1) find_root((-24000)+6000*c+6000*c^2+6000*c^3+6000*c^4+10000*c^5 = 0,c,0,1); (%o1) 0.8941263576148387 (%i2)
history
2004-4-10, revised on 2004-05-01, 2007-04-21, 2009-11-01,
2011-10-27, 2011-11-02, 2012-06-08, 2014-02-12, 2015-03-29, 2016-02-07,
2016-09-28.
2016-09-28 Fortran, Gauche, Javascript codes moved to link.
2016-11-04 Maxima added.
2016-11-18 Python 3 code with numpy added.
2018-01-02 png changed to svg.
2018-01-04 XHTML1.0 changed to HTML5.
2018-06-03 charset shift-jis to utf-8.
2021-02-14 move history.
2025-02-01 open revised 2.8.f90.