`duration(cf_p, m_p, y)`

cf_p

cashflows matrix including the current dirty prices of the bonds.

m_p

maturity matrix, the first row is filled with zeros.

y

yields of the bonds.

- The function returns a matrix with three columns, i.e,. duration, modified duration and duration based weights.

$$\bm{d}= \frac{\bm{\iota}'(\bm{C}\cdot\bm{M}\cdot\bm{D})}{\bm{\iota}'(\bm{C}\cdot\bm{D})},$$ whereas $\bm{C}$ is the cashflow matrix and $\bm{M}$ is the maturity matrix. $\bm{\iota}$ is a column vector filled with ones. $(\cdot)$ denotes a elementwise matrix mulitplication and $" ' "$ the transpose of a vector (matrix).

The weight $\omega_j$ for one bond $j$ is defined as $$\omega_j=\frac{\frac{1}{d_j}}{\sum_{i=1}^m\frac{1}{d_i}},$$ where $d_j$ is the duration of the j-th bond.

data(govbonds) cf_p <- create_cashflows_matrix(govbonds[[1]],include_price=TRUE) m_p <- create_maturities_matrix(govbonds[[1]],include_price=TRUE) y <- bond_yields(cf_p,m_p) duration(cf_p,m_p,y[,2])