I have to write a code in R that calculates the convexity of bond when parameters $T,y$ are given and coupons are paid once every year. Recall that convexity of bond is given by: $$C=\frac{1}{v}\cdot\frac{d^2v}{dy^2},$$where $v$ is equal to:
- $P(1+y)^T$ for zero-coupon bond with componded interest
- $Pe^{-yT}$ for zero-coupon bond with continuously componded interest
- $P(1+y)^T+\sum_{i=1}^nc_iP(1+y)^{t_i}$ for coupon-bearing bond with componded interest
- $Pe^{-yT}+\sum_{i=1}^nc_iPe^{-yt_i}$ for coupon-bearing bond with continuously componded interest
So for calculating the bond using R i need to find these formulas without using the derivate symbol. This is how I did this:
For the first one we have: $$C=\frac{T(T-1)P(1+y)^{T-2}}{P(1+y)^{T}}=\frac{T^2-T}{(1+y)^2}$$
For the second one: $$C=\frac{T^2Pe^{-yT}}{Pe^{-yT}}=T^2$$
Now for the third one we have: $$C=\frac{T(T-1)P(1+y)^{T-2}+\sum_{i=1}^nt_i(t_i-1)c_iP(1+y)^{t_i-2}}{P(1+y)^T+\sum_{i=1}^nc_iP(1+y)^{t_i}}=\frac{T(T-1)(1+y)^{T-2}+\sum_{i=1}^nt_i(t_i-1)c_i(1+y)^{t_i-2}}{(1+y)^T+\sum_{i=1}^nc_i(1+y)^{t_i}}$$
And for the last one: $$C=\frac{T^2Pe^{-yT}+\sum_{i=1}^nt_i^2c_iPe^{-yt_i}}{Pe^{-yT}+\sum_{i=1}^nc_iPe^{-yt_i}}=\frac{T^2e^{-yT}+\sum_{i=1}^nt_i^2c_ie^{-yt_i}}{e^{-yT}+\sum_{i=1}^nc_ie^{-yt_i}}$$
Is that correct? Can I simplify this more? Also I'm not sure if my formula for $v$ is correct in the third case.