Yield to maturity.

The yield to maturity is the interest rate that makes the present value of the
future coupon payments equal to the current bondprice, that is, for a known
price , the yield is the solution to the equation

The algoritm that follows is simple bisection, we know that the yield is above zero, and find a maximium yield which the yield is below, and then bisect the interval until we are ``close enough.''

// file bonds_yield.cc // author: Bernt A Oedegaard // calculate Yield to maturity for bond #include <cmath> #include "fin_algoritms.h" double bonds_yield_to_maturity( const vector<double>& cashflow_times, const vector<double>& cashflow_amounts, const double& bondprice) { const float ACCURACY = 1e-5; const int MAX_ITERATIONS = 200; double bot=0, top=1.0; while (bonds_price(cashflow_times, cashflow_amounts, top) > bondprice) { top = top*2; }; double r = 0.5 * (top+bot); for (int i=0;i<MAX_ITERATIONS;i++){ double diff = bonds_price(cashflow_times, cashflow_amounts,r) - bondprice; if (fabs(diff)<ACCURACY) return r; if (diff>0.0) { bot=r;} else { top=r; }; r = 0.5 * (top+bot); }; return r; };