-
Notifications
You must be signed in to change notification settings - Fork 25
/
README.Rmd
189 lines (142 loc) · 5.16 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# fixedincome <img src="man/figures/logo.png" align="right" width="120" />
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/fixedincome)](https://CRAN.R-project.org/package=fixedincome)
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![R-CMD-check](https://github.com/wilsonfreitas/R-fixedincome/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/wilsonfreitas/R-fixedincome/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
Calculations involving interest rates are usually very easy and straightforward,
but sometimes it involves specific issues that makes the task of writing
structured and reproducible code for it chalenging and annoying.
The `fixedincome` package brings many functions to strucutre and create
facilities to handle with interest rates, term structure of interest rates and
specific issues regarding compounding rates and day count rules, for example.
Below there are a few examples on how to create and make calculations with
interest rates using `fixedincome`.
## Installation
You can install from CRAN with:
```{r eval=FALSE}
install.packages("fixedincome")
```
You can install the development version of fixedincome from [GitHub](https://github.com/) with:
```{r eval=FALSE}
# install.packages("devtools")
devtools::install_github("wilsonfreitas/R-fixedincome")
```
## Examples
To create an interest rate we need to specify 4 elements:
- the value of the interest rate itself, a decimal number
- the compounding regime of interest rate, that can be `simple`, `discrete` or
`continuous`.
- the day count rule which defines how interest is accrued over time, we have a
few options, for example, `actual/360` where the days between two dates
are calculated as the difference and the year is assumed to be 360 days.
- the calendar used to count the number of days between two dates,
we have `actual` calendar that compute the difference between two dates.
There is another important topic that wasn't declared here that is the
*frequency* of interest.
To start with the things simple `fixedincome` handles only with *annual* rates
since this represents the great majority of rates used in financial market
contracts, but this restriction can be reviewed in the future.
Given that let's declare an annual spot rate with a `simple` compounding, an
`actual/360` and the `actual` calendar.
```{r}
library(fixedincome)
sr <- spotrate(0.06, "simple", "actual/360", "actual")
sr
```
Compound the spot rate for 7 months.
```{r}
compound(sr, 7, "months")
```
Also compound using dates.
```{r}
compound(sr, as.Date("2022-02-23"), as.Date("2022-12-28"))
```
Spot rates can be put inside data.frames.
```{r}
library(dplyr)
library(fixedincome)
df <- tibble(
rate = spotrate(rep(10.56 / 100, 5),
compounding = "discrete",
daycount = "business/252",
calendar = "Brazil/ANBIMA"
),
terms = term(1:5, "years")
)
df
```
The tidyverse verbs can be easily used with `SpotRate` and `Term` classes.
```{r}
df |> mutate(fact = compound(rate, terms))
```
### Spot rate curves
Let's create a spot rate curve using web scraping (from B3 website)
```{r}
source("examples/utils-functions.R")
curve <- get_curve_from_web("2022-02-23")
curve
```
`SpotRateCurve` plots can be easily done by calling `plot`.
```{r}
plot(curve)
```
For another date.
```{r}
curve <- get_curve_from_web("2011-02-23")
plot(curve)
```
It can show the forward rates for the short term by selecting the first two years.
```{r}
curve <- get_curve_from_web("2022-02-23")
plot(fixedincome::first(curve, "2 years"), show_forward = TRUE)
```
Once interpolation is set, it can be used in the plot.
```{r}
curve_2y <- fixedincome::first(curve, "2 years")
interpolation(curve_2y) <- interp_flatforward()
plot(curve_2y, use_interpolation = TRUE, legend_location = "bottomright")
```
Parametric models like the Nelson-Siegel-Svensson model can be fitted to the curve.
```{r}
beta1 <- as.numeric(fixedincome::last(curve, "1 day"))
beta2 <- as.numeric(curve[1]) - beta1
interpolation(curve) <- fit_interpolation(
interp_nelsonsiegelsvensson(beta1, beta2, 0.01, 0.01, 2, 1), curve
)
interpolation(curve)
```
Once set to the curve it is used in the plot to show daily forward rates.
```{r}
plot(curve, use_interpolation = TRUE, show_forward = TRUE, legend_location = "bottom")
```
The interpolation can be changed in order to compare different interpolations
and the effects in forward rates.
```{r}
interpolation(curve) <- interp_flatforward()
plot(
curve,
use_interpolation = TRUE, show_forward = TRUE,
legend_location = "bottomright"
)
```
Interpolation enables the creation of standardized curves, commonly used in
risk management to build risk factors.
```{r}
risk_terms <- c(1, c(3, 6, 9) * 21, c(1, 5, 10) * 252)
risk_curve <- curve[[risk_terms]]
interpolation(risk_curve) <- interp_flatforward()
plot(risk_curve, use_interpolation = TRUE)
```