If we are given a set of yields for various maturities, the simplest way to construct a term structure is by straightforward linear interpolation between the observations we have to find an intermediate time. For many purposes this is ``good enough.''

This interpolation can be on both yields and forward rates.

// file lin_intp.cc // author: Bernt A Oedegaard. // given a set of yields, produce a term structure by linear interpolation. #include "fin_algoritms.h" double term_structure_yield_linearly_interpolated(double time, const vector<double>& obs_times, const vector<double>& obs_yields) // assume the yields are in increasing time to maturity order. { int no_obs = obs_times.size(); if (no_obs<1) return 0; double t_min = obs_times[0]; if (time <= t_min) return obs_yields[0]; // earlier than lowest obs. double t_max = obs_times[no_obs-1]; if (time >= t_max) return obs_yields[no_obs-1]; // later than latest obs int t=1; // find which two observations we are between while ( (t<no_obs) && (time>obs_times[t])) { ++t; }; double lambda = (obs_times[t]-time)/(obs_times[t]-obs_times[t-1]); // by ordering assumption, time is between t-1,t double r = obs_yields[t-1] * lambda + obs_yields[t] * (1.0-lambda); return r; };

As an alternative to

// file lin_intp.cc // author: Bernt A Oedegaard. // given a set of forward yields, produce a term structure by linear interpolation. #include "fin_algoritms.h" double term_structure_forward_linearly_interpolated(double time, const vector<double>& obs_times, const vector<double>& obs_forwards) { // assume observations in increasing time to maturity order. int no_obs = obs_times.size(); if (no_obs<1) return 0; double t_min = obs_times[0]; if (time <= t_min) return obs_forwards[0]; // earlier than lowest obs. double t_max = obs_times[no_obs-1]; if (time >= t_max) return obs_forwards[no_obs-1]; // later than latest obs int t=1; // find which two observations we are between while ( (t<no_obs) && (time>obs_times[t])) { ++t; }; double lambda = (obs_times[t]-time)/(obs_times[t]-obs_times[t-1]); // by ordering assumption, time is between t-1,t double r = obs_forwards[t-1] * lambda + obs_forwards[t] * (1.0-lambda); return r; };