---
title: "Rates and Discount Factors"
author: "Imanuel Costigan"
date: "`r Sys.Date()`"
output: 
  rmarkdown::html_document:
    toc: true
    theme: spacelab
vignette: >
  %\VignetteIndexEntry{Rates and Discount Factors}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r, echo = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
```

fmbasics makes it easy to work with interest rates and discount factors. You can convert interest rates to discount factors and vice-versa.

## Interest rates

Let's make an interest rate object:

```{r interest_rate_make}
library("fmdates")
library("fmbasics")
# Quarterly compounding, with Actual/365 day basis
(rate <- InterestRate(value = 0.04, compounding = 4, day_basis = 'act/365'))
```

You can convert this rate to another rate with a different compounding/day basis:

```{r convert}
as_InterestRate(rate, compounding = 2)
as_InterestRate(rate, day_basis = 'act/360')
as_InterestRate(rate, compounding = Inf, day_basis = '30/360us')
```

You can perform arithmetic on interest rates. Rates are implicitly converted to equivalent day basis and compounding before the operation is performed on the rates' values.

```{r rate_ops}
rate1 <- InterestRate(0.04, 2, 'act/365')
rate2 <- InterestRate(0.01, Inf, 'act/360')
rate1 + rate2
```

You can also convert interest rates into discount factors:

```{r to_df, message=FALSE}
library("lubridate")
df <- as_DiscountFactor(rate, ymd(20140101), ymd(20140401))
```

The `InterestRate` class is vectorised.

```{r interest_rate_vectorised}
rates <- InterestRate(seq(0.04, 0.05, 1e-4), 2, 'act/365')
rates[23:26]
rates[23:26] <- InterestRate(0.05, 2, 'act/365')
rates[23:26]
```


## Discount factors

Let's make a discount factor object:

```{r discount_factor_make}
(df <- DiscountFactor(0.9, ymd(20140101), ymd(20150101)))
```

You can convert discount factors to interest rates.

```{r to_rate}
as_InterestRate(df, compounding = 2, day_basis = 'act/365')
```

The `DiscountFactor` class is vectorised.

```{r df_vectorised}
dfs <- DiscountFactor(seq(1, 0.9, -1e-3), ymd(20140101), ymd(20150101) + days(0:100))
dfs[23:26]
```
