! 10.11.f90 ! 2024-10-09 ! $Id: 10.11.f90 1.1 2024/11/03 07:23:57 s Exp $ ! ! a : amount ! d : discount factor, d[k] = d(0,k) ! f : forward rate, f[k] = f(k-1,k) ! g(r) : pv2 - pv1 ! pv1 : present value in variable rate ! pv2 : present value in fixed rate ! r : fixed rate ! s : spot rate, s[k] = s(k) ! x : cash flow in variable rate, x[0] = x(k) ! y : cash flow in fixed rate, y = a * r program b implicit none integer :: i,n real(8) a, d(6), f(6),pv1, r,root,s(6), x(6), y ! input s = [0.070d0, 0.073d0, 0.077d0, 0.081d0, 0.084d0, 0.088d0] f = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] a = 1000d0 n = 6 ! calculation do i = 1, n if(i == 1) then f(1) = s(1) else f(i) = (1.0 + s(i)) ** i / (1.0 + s(i - 1)) ** (i - 1) - 1d0 end if end do x = a * f(1:n) do i = 1, n d(i) = 1d0 / (1d0 + s(i)) ** i end do pv1 = sum(x(1:n) * d(1:n)) ! solve g(r)=0 do i = 8700, 8900 r = i / 1d5 if( g(a,r,pv1) > 0) then root=r exit end if end do ! output print *, '10.10.py' print '(a7,f8.2)', 'a', a print '(a7,7f8.3)', 's', (s(i), i = 1, n) print '(a7,7f8.3)', 'f', (f(i), i = 1, n) print '(a7,7f8.3)', 'x', (x(i), i = 1, n) print '(a7,7f8.3)', 'd', (d(i), i = 1, n) print *, 'pv1 in variable rates ', pv1 print *, 'r, g(r)', root, g(a,root,pv1) stop contains ! function real(8) function g(a,r,pv1) ! g=pv2-pv1 implicit none real(8) a,d1,d2,d3,d4,d5,d6,pv1,pv2,r d1 = 1 / (1d0 + r) ** 1 d2 = 1 / (1d0 + r) ** 2 d3 = 1 / (1d0 + r) ** 3 d4 = 1 / (1d0 + r) ** 4 d5 = 1 / (1d0 + r) ** 5 d6 = 1 / (1d0 + r) ** 6 y = a * r pv2 = y * (d1 + d2 + d3 + d4 + d5 + d6) g = pv2 - pv1 end function g end program b