Skip to content

Commit

Permalink
Updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonfreitas committed Jul 15, 2022
1 parent 2275109 commit f578d08
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 86 deletions.
205 changes: 119 additions & 86 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->

# fixedincome
Expand Down Expand Up @@ -25,13 +26,17 @@ with interest rates using `fixedincome`.

You can install from CRAN with:

install.packages("fixedincome")
``` r
install.packages("fixedincome")
```

You can install the development version of fixedincome from
[GitHub](https://github.com/) with:

# install.packages("devtools")
devtools::install_github("wilsonfreitas/R-fixedincome")
``` r
# install.packages("devtools")
devtools::install_github("wilsonfreitas/R-fixedincome")
```

## Examples

Expand All @@ -57,148 +62,176 @@ 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.

library(fixedincome)
sr <- spotrate(0.06, "simple", "actual/360", "actual")
sr
#> [1] "0.06 simple actual/360 actual"
``` r
library(fixedincome)
sr <- spotrate(0.06, "simple", "actual/360", "actual")
sr
#> [1] "0.06 simple actual/360 actual"
```

Compound the spot rate for 7 months.

compound(sr, 7, "months")
#> [1] 1.035
``` r
compound(sr, 7, "months")
#> [1] 1.035
```

Also compound using dates.

compound(sr, as.Date("2022-02-23"), as.Date("2022-12-28"))
#> [1] 1.051333
``` r
compound(sr, as.Date("2022-02-23"), as.Date("2022-12-28"))
#> [1] 1.051333
```

Spot rates can be put inside data.frames.

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
#> # A tibble: 5 x 2
#> rate terms
#> <SpotRate> <Term>
#> 1 0.1056 discrete business/252 Brazil/ANBIMA 1 year
#> 2 0.1056 discrete business/252 Brazil/ANBIMA 2 years
#> 3 0.1056 discrete business/252 Brazil/ANBIMA 3 years
#> 4 0.1056 discrete business/252 Brazil/ANBIMA 4 years
#> 5 0.1056 discrete business/252 Brazil/ANBIMA 5 years
``` 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
#> # A tibble: 5 x 2
#> rate terms
#> <SpotRate> <Term>
#> 1 0.1056 discrete business/252 Brazil/ANBIMA 1 year
#> 2 0.1056 discrete business/252 Brazil/ANBIMA 2 years
#> 3 0.1056 discrete business/252 Brazil/ANBIMA 3 years
#> 4 0.1056 discrete business/252 Brazil/ANBIMA 4 years
#> 5 0.1056 discrete business/252 Brazil/ANBIMA 5 years
```

The tidyverse verbs can be easily used with `SpotRate` and `Term`
classes.

df |> mutate(fact = compound(rate, terms))
#> # A tibble: 5 x 3
#> rate terms fact
#> <SpotRate> <Term> <dbl>
#> 1 0.1056 discrete business/252 Brazil/ANBIMA 1 year 1.11
#> 2 0.1056 discrete business/252 Brazil/ANBIMA 2 years 1.22
#> 3 0.1056 discrete business/252 Brazil/ANBIMA 3 years 1.35
#> 4 0.1056 discrete business/252 Brazil/ANBIMA 4 years 1.49
#> 5 0.1056 discrete business/252 Brazil/ANBIMA 5 years 1.65
``` r
df |> mutate(fact = compound(rate, terms))
#> # A tibble: 5 x 3
#> rate terms fact
#> <SpotRate> <Term> <dbl>
#> 1 0.1056 discrete business/252 Brazil/ANBIMA 1 year 1.11
#> 2 0.1056 discrete business/252 Brazil/ANBIMA 2 years 1.22
#> 3 0.1056 discrete business/252 Brazil/ANBIMA 3 years 1.35
#> 4 0.1056 discrete business/252 Brazil/ANBIMA 4 years 1.49
#> 5 0.1056 discrete business/252 Brazil/ANBIMA 5 years 1.65
```

### Spot rate curves

Let’s create a spot rate curve using web scraping (from B3 website)

source("examples/utils-functions.R")
curve <- get_curve_from_web("2022-02-23")
curve
#> SpotRateCurve
#> 1 day 0.1065
#> 3 days 0.1064
#> 25 days 0.1111
#> 44 days 0.1138
#> 66 days 0.1168
#> 87 days 0.1189
#> 108 days 0.1207
#> 131 days 0.1220
#> 152 days 0.1227
#> 172 days 0.1235
#> # ... with 29 more rows
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-02-23
``` r
source("examples/utils-functions.R")
curve <- get_curve_from_web("2022-02-23")
curve
#> SpotRateCurve
#> 1 day 0.1065
#> 3 days 0.1064
#> 25 days 0.1111
#> 44 days 0.1138
#> 66 days 0.1168
#> 87 days 0.1189
#> 108 days 0.1207
#> 131 days 0.1220
#> 152 days 0.1227
#> 172 days 0.1235
#> # ... with 29 more rows
#> discrete business/252 Brazil/ANBIMA
#> Reference date: 2022-02-23
```

`SpotRateCurve` plots can be easily done by calling `plot`.

plot(curve)
``` r
plot(curve)
```

<img src="man/figures/README-unnamed-chunk-10-1.png" width="100%" />

For another date.

curve <- get_curve_from_web("2011-02-23")
plot(curve)
``` r
curve <- get_curve_from_web("2011-02-23")
plot(curve)
```

<img src="man/figures/README-unnamed-chunk-11-1.png" width="100%" />

It can show the forward rates for the short term by selecting the first
two years.

curve <- get_curve_from_web("2022-02-23")
plot(fixedincome::first(curve, "2 years"), show_forward = TRUE)
``` r
curve <- get_curve_from_web("2022-02-23")
plot(fixedincome::first(curve, "2 years"), show_forward = TRUE)
```

<img src="man/figures/README-unnamed-chunk-12-1.png" width="100%" />

Once interpolation is set, it can be used in the plot.

interpolation(curve) <- interp_flatforward()
plot(
fixedincome::first(curve, "2 years"),
use_interpolation = TRUE, legend_location = "bottomright"
)
``` r
interpolation(curve) <- interp_flatforward()
plot(
fixedincome::first(curve, "2 years"),
use_interpolation = TRUE, legend_location = "bottomright"
)
```

<img src="man/figures/README-unnamed-chunk-13-1.png" width="100%" />

Parametric models like the Nelson-Siegel-Svensson model can be fitted to
the curve.

interpolation(curve) <- fit_interpolation(
interp_nelsonsiegelsvensson(0.01, 0.01, 0.01, 0.01, 0.01, 0.01), curve
)
``` r
interpolation(curve) <- fit_interpolation(
interp_nelsonsiegelsvensson(0.01, 0.01, 0.01, 0.01, 0.01, 0.01), curve
)

interpolation(curve)
#> <Interpolation: nelsonsiegelsvensson >
#> Parameters:
#> beta1 beta2 beta3 beta4 lambda1 lambda2
#> 0.09283 0.01325 0.04613 0.05007 0.00022 0.01092
interpolation(curve)
#> <Interpolation: nelsonsiegelsvensson >
#> Parameters:
#> beta1 beta2 beta3 beta4 lambda1 lambda2
#> 0.300 -0.181 -0.858 0.611 0.053 0.054
```

Once set to the curve it is used in the plot to show daily forward
rates.

plot(curve, use_interpolation = TRUE, show_forward = TRUE)
``` r
plot(curve, use_interpolation = TRUE, show_forward = TRUE)
```

<img src="man/figures/README-unnamed-chunk-15-1.png" width="100%" />

The interpolation can be changed in order to compare different
interpolations and the effects in forward rates.

interpolation(curve) <- interp_flatforward()
plot(
curve,
use_interpolation = TRUE, show_forward = TRUE,
legend_location = "bottomright"
)
``` r
interpolation(curve) <- interp_flatforward()
plot(
curve,
use_interpolation = TRUE, show_forward = TRUE,
legend_location = "bottomright"
)
```

<img src="man/figures/README-unnamed-chunk-16-1.png" width="100%" />

Interpolation enables the creation of standardized curves, commonly used
in risk management to build risk factors.

risk_terms <- c(1, c(3, 6, 9) * 21, c(1, 5, 10) * 252)
risk_curve <- curve[[risk_terms]]
plot(risk_curve, use_interpolation = TRUE)
``` r
risk_terms <- c(1, c(3, 6, 9) * 21, c(1, 5, 10) * 252)
risk_curve <- curve[[risk_terms]]
plot(risk_curve, use_interpolation = TRUE)
```

<img src="man/figures/README-unnamed-chunk-17-1.png" width="100%" />
Binary file modified man/figures/README-unnamed-chunk-13-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-15-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-17-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f578d08

Please sign in to comment.