Skip to content

Commit

Permalink
Merge pull request #1 from Techtonique/vine-sim
Browse files Browse the repository at this point in the history
Vine sim
  • Loading branch information
thierrymoudiki authored Oct 7, 2020
2 parents f6b1563 + d92d98f commit d59bbfc
Show file tree
Hide file tree
Showing 47 changed files with 504 additions and 4,215 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
^ESGtoolkit\.Rcheck$
^ESGtoolkit.*\.tar\.gz$
^ESGtoolkit.*\.tgz$
^_pkgdown\.yml$
^docs$
^pkgdown$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ src/*.dll
ESGtoolkit.Rcheck/
ESGtoolkit*.tar.gz
ESGtoolkit*.tgz
docs
10 changes: 0 additions & 10 deletions CHANGES.md

This file was deleted.

23 changes: 13 additions & 10 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
Package: ESGtoolkit
Type: Package
Title: Toolkit for Monte Carlo Simulations
Version: 0.2.0
Date: 2020-01-12
Author: Thierry Moudiki
Maintainer: Thierry Moudiki <thierry.moudiki@gmail.com>
Version: 0.3.0
Date: 2020-10-07
Authors@R: c(
person("T.", "Moudiki", , "thierry.moudiki@gmail.com", role = c("aut", "cre")
)
)
URL: https://techtonique.github.io/ESGtoolkit/
BugReports: https://github.com/Techtonique/ESGtoolkit/issues
Maintainer: T. Moudiki <thierry.moudiki@gmail.com>
Description: A toolkit for Monte Carlo Simulations in Finance, Economics, Insurance, Physics. Multiple simulation models can be created by combining building blocks provided in the package.
License: BSD_3_clause Clear + file LICENSE
Depends: CDVine, ggplot2, gridExtra, reshape2, ycinterextra
Depends: ggplot2, gridExtra, reshape2, ycinterextra, VineCopula, randtoolbox
Imports: Rcpp(>= 0.11.0)
Suggests:
knitr,
fOptions,
devtools,
testthat
LinkingTo: Rcpp
VignetteBuilder: knitr
Collate: 'RcppExports.R' 'fwdrates.R' 'plots.R' 'simulations.R' 'tests.R' 'tools.R'
Collate: 'RcppExports.R' 'fwdrates.R' 'plots.R' 'simulations_risks.R' 'simulations_shocks.R' 'tests.R' 'tools.R' 'zzz.R'
Packaged: 2014-06-12 23:32:30 UTC; Thierry
NeedsCompilation: yes
Repository: CRAN
Date/Publication: 2014-06-13 06:17:14
RoxygenNote: 7.0.2
RoxygenNote: 7.1.1
1 change: 0 additions & 1 deletion ESGtoolkit.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ LaTeX: pdfLaTeX
BuildType: Package
PackageInstallArgs: --no-multiarch --with-keep.source
PackageCheckArgs: --as-cran
PackageRoxygenize: rd,collate,namespace
6 changes: 5 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ export(simshocks)
importFrom(ggplot2, ggplot)
importFrom(ggplot2, qplot)
importFrom(gridExtra, grid.arrange)
importFrom(CDVine, CDVineSim)
importFrom(randtoolbox, sobol)
importFrom(randtoolbox, halton)
importFrom(reshape2, melt)
importFrom(VineCopula, C2RVine)
importFrom(VineCopula, D2RVine)
importFrom(VineCopula, RVineSim)
importFrom(ycinterextra, ycinter)
importFrom(ycinterextra, ycextra)
importFrom(ycinterextra, fitted)
Expand Down
17 changes: 17 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# version 0.3.0

- Use [VineCopula](http://tnagler.github.io/VineCopula/) package instead of CDVine (archived) for dependency simulation (means there's also now VineCopula's R-Vine Copulas simulation)
- Remove roxygen2 comments
- Create website with pkgdown, including docs --> https://techtonique.github.io/ESGtoolkit/


# version 0.2.0

- refactor files
- add reproductibility seeds
- update vignette
- update LICENSE

# version 0.1.0

- Initial version
67 changes: 2 additions & 65 deletions R/fwdrates.R
Original file line number Diff line number Diff line change
@@ -1,69 +1,7 @@
# Instantaneous forward rates ---------------------------------------------


#'@title
#'
#'Instantaneous forward rates
#'
#'@description
#'
#'This function provides instantaneous forward rates. They can be used
#'in no-arbitrage short rate models, to fit the yield curve exactly.
#'
#'@param in.maturities input maturities
#'
#'@param in.zerorates input zero rates
#'
#'@param n number of independent observations
#'
#'@param horizon horizon of projection
#'
#'@param out.frequency either "annual", "semi-annual", "quarterly", "monthly",
#'"weekly", or "daily" (1, 1/2, 1/4, 1/12, 1/52, 1/252)
#'
#'@param ... additional parameters provided to \code{\link{ycinter}}
#'
#'@author
#'
#'Thierry Moudiki
#'
#'@references
#'
#' Thierry Moudiki (2013). ycinterextra: Yield curve or zero-coupon prices interpolation and extrapolation. R package version 0.1. URL
#'\url{https://CRAN.R-project.org/package=ycinterextra}
#'
#'@examples
#'
#'# Yield to maturities
#'txZC <- c(0.01422,0.01309,0.01380,0.01549,0.01747,0.01940,0.02104,0.02236,0.02348,
#' 0.02446,0.02535,0.02614,0.02679,0.02727,0.02760,0.02779,0.02787,0.02786,0.02776
#' ,0.02762,0.02745,0.02727,0.02707,0.02686,0.02663,0.02640,0.02618,0.02597,0.02578,0.02563)
#'
#'# Observed maturities
#'u <- 1:30
#'
#'\dontrun{
#'par(mfrow=c(2,2))
#'fwdNS <- esgfwdrates(in.maturities = u, in.zerorates = txZC,
#' n = 10, horizon = 20,
#' out.frequency = "semi-annual", method = "NS")
#'matplot(time(fwdNS), fwdNS, type = 'l')
#'
#'fwdSV <- esgfwdrates(in.maturities = u, in.zerorates = txZC,
#' n = 10, horizon = 20,
#' out.frequency = "semi-annual", method = "SV")
#'matplot(time(fwdSV), fwdSV, type = 'l')
#'
#'fwdSW <- esgfwdrates(in.maturities = u, in.zerorates = txZC,
#' n = 10, horizon = 20,
#' out.frequency = "semi-annual", method = "SW")
#'matplot(time(fwdSW), fwdSW, type = 'l')
#'
#'fwdHCSPL <- esgfwdrates(in.maturities = u, in.zerorates = txZC,
#' n = 10, horizon = 20,
#' out.frequency = "semi-annual", method = "HCSPL")
#'matplot(time(fwdHCSPL), fwdHCSPL, type = 'l')
#'}
# Instantaneous forward rates
esgfwdrates <- function(in.maturities, in.zerorates,
n, horizon,
out.frequency = c("annual", "semi-annual",
Expand Down Expand Up @@ -124,5 +62,4 @@ esgfwdrates <- function(in.maturities, in.zerorates,

return(ts(replicate(n, fwdrates), start = 0,
deltat = delta))
}
esgfwdrates <- cmpfun(esgfwdrates)
}
183 changes: 5 additions & 178 deletions R/plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,7 @@
# Misc plots --------------------------------------------------------------


#'@title Plot time series percentiles and confidence intervals
#'
#'@description
#'
#'This function plots colored bands for time series percentiles and confidence
#'intervals. You can use it for outputs from \code{link{simdiff}},
#'\code{link{esgmartingaletest}}, \code{link{esgcortest}}.
#'
#'@param x a times series object
#'
#'@param ... additionnal (optional) parameters provided to \code{plot}
#'
#'@export
#'
#'@author Thierry Moudiki
#'
#'@seealso \code{\link{esgplotts}}
#'
#'@examples
#'
#'# Times series
#'
#'kappa <- 1.5
#'V0 <- theta <- 0.04
#'sigma <- 0.2
#'theta1 <- kappa*theta
#'theta2 <- kappa
#'theta3 <- sigma
#'x <- simdiff(n = 100, horizon = 5,
#'frequency = "quart",
#'model = "OU",
#'x0 = V0, theta1 = theta1, theta2 = theta2, theta3 = theta3)
#'
#'par(mfrow=c(2,1))
#'esgplotbands(x, xlab = "time", ylab = "values")
#'matplot(time(x), x, type = 'l', xlab = "time", ylab = "series values")
#'
#'# Martingale test
#'
#'r0 <- 0.03
#'S0 <- 100
#'sigma0 <- 0.1
#'nbScenarios <- 100
#'horizon0 <- 10
#'eps0 <- simshocks(n = nbScenarios, horizon = horizon0, frequency = "quart",
#' method = "anti")
#'sim.GBM <- simdiff(n = nbScenarios, horizon = horizon0, frequency = "quart",
#' model = "GBM",
#' x0 = S0, theta1 = r0, theta2 = sigma0,
#' eps = eps0)
#'
#'mc.test <- esgmartingaletest(r = r0, X = sim.GBM, p0 = S0, alpha = 0.05)
#'esgplotbands(mc.test)
#'
#'# Correlation test
#'
#'nb <- 500
#'
#'s0.par1 <- simshocks(n = nb, horizon = 3, frequency = "semi",
#'family = 1, par = 0.2)
#'
#'s0.par2 <- simshocks(n = nb, horizon = 3, frequency = "semi",
#'family = 1, par = 0.8)
#'
#'(test1 <- esgcortest(s0.par1))
#'(test2 <- esgcortest(s0.par2))
#'par(mfrow=c(2, 1))
#'esgplotbands(test1)
#'esgplotbands(test2)
# Plot time series percentiles and confidence intervals
esgplotbands <- function(x, ...)
{
if (is.ts(x))
Expand Down Expand Up @@ -143,7 +75,7 @@ esgplotbands <- function(x, ...)
abs <- as.numeric(time(x$conf.int))
y.mean <- rep(0, length(abs))

par(mfrow = c(2, 1))
par(mfrow = c(1, 2))
bands.plot(abs, y.mean, ci.upper = x$conf.int[, 2], ci.lower = x$conf.int[, 1],
col = "gray80", ylim = c(min(x$conf.int[, 1]), max(x$conf.int[, 2])),
xlab = "time", ylab = "conf. int.",
Expand All @@ -162,42 +94,7 @@ esgplotbands <- function(x, ...)
}



#'@title Plot time series objects
#'
#'@description This function plots outputs from \code{\link{simdiff}}.
#'
#'@details For a large number of simulations, it's preferable to use
#'\code{\link{esgplotbands}} for a synthetic view by percentiles.
#'
#'@param x a time series object, an output from \code{\link{simdiff}}.
#'
#'@seealso \code{\link{simdiff}}, \code{\link{esgplotbands}}
#'
#'@author
#'
#'Thierry Moudiki
#'
#'@export
#'
#'@references
#'
#'H. Wickham (2009), ggplot2: elegant graphics for data analysis. Springer
#'New York.
#'
#'@examples
#'kappa <- 1.5
#'V0 <- theta <- 0.04
#'sigma <- 0.2
#'theta1 <- kappa*theta
#'theta2 <- kappa
#'theta3 <- sigma
#'x <- simdiff(n = 10, horizon = 5, frequency = "quart",
#'model = "OU",
#'x0 = V0, theta1 = theta1, theta2 = theta2, theta3 = theta3)
#'
#'esgplotts(x)
#'
# Plot time series objects
esgplotts <- function(x)
{
x0 <- start(x)[1]
Expand All @@ -209,77 +106,9 @@ esgplotts <- function(x)
xlab("Maturity") + ylab("Values") +
ggplot2::theme(legend.position = "none")
}
esgplotts <- cmpfun(esgplotts)



#'@title Visualize the dependence between 2 gaussian shocks
#'
#'@description
#'
#'This function helps you in visualizing the dependence between 2 gaussian
#' shocks.
#'
#'@param x an output from \code{\link{simshocks}}, a list with 2 components.
#'
#'@param y an output from \code{\link{simshocks}}, a list with 2 components
#'(Optional).
#'
#'@export
#'
#'@seealso \code{\link{simshocks}}
#'
#'@author Thierry Moudiki + some nice blogs :)
#'
#'@references
#'
#'H. Wickham (2009), ggplot2: elegant graphics for data analysis. Springer
#'New York.
#'
#'@examples
#'
#'# Number of risk factors
#'d <- 2
#'
#'# Number of possible combinations of the risk factors
#'dd <- d*(d-1)/2
#'
#'# Family : Gaussian copula
#'fam1 <- rep(1,dd)
#'# Correlation coefficients between the risk factors (d*(d-1)/2)
#'par0.1 <- 0.1
#'par0.2 <- -0.9
#'
#'# Family : Rotated Clayton (180 degrees)
#'fam2 <- 13
#'par0.3 <- 2
#'
#'# Family : Rotated Clayton (90 degrees)
#'fam3 <- 23
#'par0.4 <- -2
#'
#'# number of simulations
#'nb <- 500
#'
#'# Simulation of shocks for the d risk factors
#'s0.par1 <- simshocks(n = nb, horizon = 4,
#'family = fam1, par = par0.1)
#'
#'s0.par2 <- simshocks(n = nb, horizon = 4,
#'family = fam1, par = par0.2)
#'
#'s0.par3 <- simshocks(n = nb, horizon = 4,
#'family = fam2, par = par0.3)
#'
#'s0.par4 <- simshocks(n = nb, horizon = 4,
#'family = fam3, par = par0.4)
#'
#'\dontrun{
#'esgplotshocks(s0.par1, s0.par2)
#'esgplotshocks(s0.par2, s0.par3)
#'esgplotshocks(s0.par2, s0.par4)
#'esgplotshocks(s0.par1, s0.par4)}
#'
# Visualize the dependence between 2 gaussian shocks
esgplotshocks <- function(x, y = NULL)
{
x <- matrix(unlist(x), ncol = 2)
Expand Down Expand Up @@ -338,6 +167,4 @@ esgplotshocks <- function(x, y = NULL)

#arrange the plots together, with appropriate height and width for each row and column
grid.arrange(plot_top, empty, scatter, plot_right, ncol=2, nrow=2, widths=c(4, 1), heights=c(1, 4))
}
esgplotshocks <- cmpfun(esgplotshocks)

}
Loading

0 comments on commit d59bbfc

Please sign in to comment.