4.2
Python
(added on 2015-04-11.)
code
# 4.2.py
# 17:49 2015-04-11
#
# s, current spot rate curve
# s2, next year's spot rate curve
def sd(i): # next year's spot rate curve
j = i + 1
return ((1 + s[j])**j / (1 + s[1]))**(1 / (j - 1)) - 1
print("current spot rate curve")
s = [0, 5.0, 5.3, 5.6, 5.8, 6.0, 6.1]
print("s=", [s[i] for i in range(1, len(s))])
s = list(map(lambda x: x / 100, s))
print("calculating without function")
fmt = "{:10.3f}"
print(fmt.format(((1 + s[2])**2 / (1 + s[1]))**(1 / (2-1)) - 1))
print(fmt.format(((1 + s[3])**3 / (1 + s[1]))**(1 / (3-1)) - 1))
print(fmt.format(((1 + s[4])**4 / (1 + s[1]))**(1 / (4-1)) - 1))
print(fmt.format(((1 + s[5])**5 / (1 + s[1]))**(1 / (5-1)) - 1))
print(fmt.format(((1 + s[6])**6 / (1 + s[1]))**(1 / (6-1)) - 1))
s2 = [0]
for i in range(1, len(s) - 1):
s2.append(round(sd(i), 3))
print("calculating with function")
for i in range(1, len(s) - 1):
print(fmt.format(s2[i]))
print("next year's spot rate curve")
print("s'=", [round(s2[i] * 100, 1) for i in range(1, len(s) - 1)])
# eof
output
current spot rate curve s= [5.0, 5.3, 5.6, 5.8, 6.0, 6.1] calculating without function 0.056 0.059 0.061 0.063 0.063 calculating with function 0.056 0.059 0.061 0.063 0.063 next year's spot rate curve s'= [5.6, 5.9, 6.1, 6.3, 6.3]
JavaScript
コードを次のリンクに示す。node.jsで動作確認済。
Gauche
コードを次のリンクに示す。
Fortran
コードを次のリンクに示す。
appendix
以下の計算は、間違っています。(2015-04-11)
p.103の式(4.1)を用いてs'j-1を求める。
s'1 = [(1 + s2)2 / (1 + s1)]1/1 - 1 = 0.047 = 4.7%
s'2 = [(1 + s3)3 / (1 + s1)]1/2 - 1 = 0.059 = 5.9%
s'3 = [(1 + s4)4 / (1 + s1)]1/3 - 1 = 0.061 = 6.1%
s'4 = [(1 + s5)5 / (1 + s1)]1/4 - 1 = 0.063 = 6.3%
s'5 = [(1 + s6)6 / (1 + s1)]1/5 - 1 = 0.063 = 6.3%
∴s'=(4.7, 5.9, 6.1, 6.3, 6.3)
history
2004-05-23 create, 2015-04-11 revise, add Python code.
2021-02-17 change XHTML to html5, change shift-jis to utf-8, add viewport.
2024-10-26 add a JavaScript code, a Gauche code and a Fortran code.