Convexity is the sensitivity measure of the duration of a bond to yield changes. Think of convexity as the second derivative of the relationship between the price and yield:
Bond traders use convexity as a risk-management tool to measure the amount of market risk in their portfolio. Higher-convexity portfolios are less affected by interest-rate volatilities than lower-convexity portfolios, given the same bond duration and yield. As such, higher-convexity bonds are more expensive than lower-convexity ones, everything else being equal.
The implementation of a bond convexity is given as follows:
In [ ]:
def bond_convexity(price, par, T, coup, freq, dy=0.01):
ytm = bond_ytm(price, par, T, coup, freq)
ytm_minus = ytm - dy
price_minus = bond_price(par, T, ytm_minus, coup, freq)
ytm_plus = ytm + dy
price_plus = bond_price...