Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
FinYang committed Apr 7, 2024
1 parent 934ed0e commit d9ee2f7
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 61 deletions.
118 changes: 59 additions & 59 deletions README.Rmd
Expand Up @@ -26,7 +26,7 @@ The goal of `ycevo` is to provide means for the non-parametric estimation of the
If you use any data or code from the `ycevo` package CRAN release in a publication, please use the following citation:

> Bonsoo Koo, and Yangzhuoran Fin Yang (2022). ycevo: Non-Parametric Estimation
of the Yield Curve Evolution. R package version 0.1.2. https://CRAN.R-project.org/package=ycevo.
of the Yield Curve Evolution. R package version 0.2.0. https://CRAN.R-project.org/package=ycevo.

The package provides code used in Koo, La Vecchia, & Linton (2021). Please use the following citation if you use any result from the paper.

Expand Down Expand Up @@ -57,69 +57,69 @@ You can install the __development__ version from [GitHub](https://github.com/) w
# install.packages("devtools")
devtools::install_github("bonsook/ycevo")
```
<!-- ## Usage -->
## Usage

```{r}
#| eval: false
#| echo: false
library(ycevo)
library(dplyr)
library(tidyr)
library(ggplot2)
library(lubridate)
# Simulate 4 bonds issued at 2020-01-01
# with maturity 180, 360, 540, 720 days
# Apart from the first one,
# each has coupon 2,
# of which half is paid every 180 days.
# The yield curve is sumulated fron `get_yield_at_vec`
# Quotation date is also at 2020-01-01
exp_data <- tibble(
qdate = ymd("2020-01-01"),
id = rep(1:4, 1:4),
pdint = c(100, 1, 101, 1, 1, 101, 1, 1, 1, 101),
tupq = unlist(sapply(1:4, seq_len)) * 180,
accint = 0
) %>%
mutate(discount = exp(-tupq/365 * get_yield_at_vec(0, tupq/365))) %>%
group_by(id) %>%
mutate(price = sum(pdint * discount)) %>%
ungroup()
# Only one quotation so time grid is set to 1
xgrid <- 1
# Discount function is evaluated at time to maturity of each payment in the data
tau <- unique(exp_data$tupq/365)
# Estimated yield and discount
yield <- ycevo(
exp_data,
xgrid = xgrid,
tau = tau
)
yield
# True yield
get_yield_at_vec(0, tau)
# Plot of yield curve
yield %>%
mutate(true_yield = get_yield_at_vec(0, tau)) %>%
pivot_longer(c(yield, true_yield)) %>%
mutate(xgrid = round(xgrid, 2)) %>%
ggplot() +
geom_line(aes(x = tau, y = value)) +
facet_wrap(name~xgrid, labeller = label_both)
# Plot of discount function
yield %>%
mutate(true_discount = exp(-tau * get_yield_at_vec(0, tau))) %>%
pivot_longer(c(discount, true_discount)) %>%
mutate(xgrid = round(xgrid, 2)) %>%
ggplot() +
geom_line(aes(x = tau, y = value)) +
facet_wrap(name~xgrid, labeller = label_both)
## Simulate
set.seed(1)
bonds <- ycevo_data()
bonds
```


```{r}
#| eval: false
## Estimate at specified time points
x <- seq(ymd("2023-03-01"), ymd("2023-08-01"), by = "1 month")
res <- ycevo(bonds, x)
## Supports parallel processing and progress bar
# future::plan(future::multisession)
# res <- progressr::with_progress(ycevo(bonds, x))
```
```{r}
#| echo: false
x <- seq(ymd("2023-03-01"), ymd("2023-08-01"), by = "1 month")
future::plan(future::multisession)
res <- ycevo(bonds, x)
```
```{r}
res
```


```{r}
## Predict
augment(res)
## Plot
autoplot(res)
```


```{r}
#| eval: false
## Compare to the true yield curve
curve_true <- list()
for(i in seq_along(x)) {
curve_true[[i]] <- tibble(
tau=1:20,
qdate = x[[i]],
.yield = get_yield_at((x[[i]] - ymd("2023-01-01"))/365, tau),
.discount = exp(-tau * .yield))
}
curve_true <- bind_rows(curve_true)
curve_true <- pivot_longer(
curve_true,
c(.discount, .yield),
names_to = ".est",
values_to = ".value")
autoplot(res) +
geom_line(data = curve_true, linetype = "dashed")
```


Expand Down
100 changes: 98 additions & 2 deletions README.md
Expand Up @@ -19,7 +19,7 @@ If you use any data or code from the `ycevo` package CRAN release in a
publication, please use the following citation:

> Bonsoo Koo, and Yangzhuoran Fin Yang (2022). ycevo: Non-Parametric
> Estimation of the Yield Curve Evolution. R package version 0.1.2.
> Estimation of the Yield Curve Evolution. R package version 0.2.0.
> <https://CRAN.R-project.org/package=ycevo>.
The package provides code used in Koo, La Vecchia, & Linton (2021).
Expand Down Expand Up @@ -59,7 +59,103 @@ You can install the **development** version from
devtools::install_github("bonsook/ycevo")
```

<!-- ## Usage -->
## Usage

``` r
library(ycevo)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union

## Simulate
set.seed(1)
bonds <- ycevo_data()
bonds
#> # A tibble: 388,795 × 5
#> qdate id price tupq pdint
#> <date> <fct> <dbl> <dbl> <dbl>
#> 1 2023-01-02 20230106.106643 103. 4 103.
#> 2 2023-01-02 20230721.107768 106. 19 3.88
#> 3 2023-01-02 20230721.107768 106. 200 104.
#> 4 2023-01-02 20240203.106386 106. 32 3.19
#> 5 2023-01-02 20240203.106386 106. 213 3.19
#> 6 2023-01-02 20240203.106386 106. 397 103.
#> 7 2023-01-02 20240817.109392 113. 46 4.70
#> 8 2023-01-02 20240817.109392 113. 227 4.70
#> 9 2023-01-02 20240817.109392 113. 411 4.70
#> 10 2023-01-02 20240817.109392 113. 593 105.
#> # ℹ 388,785 more rows
```

``` r
## Estimate at specified time points
x <- seq(ymd("2023-03-01"), ymd("2023-08-01"), by = "1 month")
res <- ycevo(bonds, x)
## Supports parallel processing and progress bar
# future::plan(future::multisession)
# res <- progressr::with_progress(ycevo(bonds, x))
```

``` r
res
#> # A tibble: 6 × 2
#> qdate .est
#> * <date> <list>
#> 1 2023-03-01 <tibble [73 × 3]>
#> 2 2023-04-01 <tibble [73 × 3]>
#> 3 2023-05-01 <tibble [73 × 3]>
#> 4 2023-06-01 <tibble [73 × 3]>
#> 5 2023-07-01 <tibble [73 × 3]>
#> 6 2023-08-01 <tibble [73 × 3]>
```

``` r
## Predict
augment(res)
#> # A tibble: 438 × 4
#> qdate tau .discount .yield
#> <date> <dbl> <dbl> <dbl>
#> 1 2023-03-01 0.0822 0.986 0.169
#> 2 2023-03-01 0.164 0.985 0.0912
#> 3 2023-03-01 0.247 0.984 0.0654
#> 4 2023-03-01 0.329 0.983 0.0528
#> 5 2023-03-01 0.411 0.982 0.0454
#> 6 2023-03-01 0.493 0.980 0.0407
#> 7 2023-03-01 0.658 0.977 0.0351
#> 8 2023-03-01 0.822 0.974 0.0321
#> 9 2023-03-01 0.986 0.970 0.0305
#> 10 2023-03-01 1.15 0.967 0.0296
#> # ℹ 428 more rows

## Plot
autoplot(res)
```

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

``` r
## Compare to the true yield curve
curve_true <- list()
for(i in seq_along(x)) {
curve_true[[i]] <- tibble(
tau=1:20,
qdate = x[[i]],
.yield = get_yield_at((x[[i]] - ymd("2023-01-01"))/365, tau),
.discount = exp(-tau * .yield))
}
curve_true <- bind_rows(curve_true)
curve_true <- pivot_longer(
curve_true,
c(.discount, .yield),
names_to = ".est",
values_to = ".value")

autoplot(res) +
geom_line(data = curve_true, linetype = "dashed")
```

## License

Expand Down
Binary file added man/figures/README-unnamed-chunk-6-1.png
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 d9ee2f7

Please sign in to comment.