given a set of cash flows (either positive of negative) from an investment and the dates at which they are available, the function computes: the duration of the cash flows, the convexity of the cash flows, and the yield-to-maturity "ytm" (both duration and convexity are computed by taking ytm as the discount rate)
[dur,con,ytm]=duration(t,c);
On cash flows available at given dates, duration, convexity and yield to maturity are computed
A bond, whose value on the market is 101.5, pays a cash flow of 2.5 euros any six month (i.e. 1/2 year) and for the next 2 years. At maturity it pays also its face value of 100 euros. Let us compute duration, convexity, and yield-to-maturity.
At first we define the vector of dates and cash flows
-->t=[0 0.5 1 1.5 2]';
-->c=[-101.5 2.5 2.5 2.5 102.5]';
-->[D,C,Y]=duration(t,c)
Y = 0.0425443
C = 3.7981714
D = 1.9286213
Francesco Menoncin - Brescia University - 2010