[Uwgarp-commits] r104 - in pkg/GARPFRM: . R demo man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Mar 5 18:28:35 CET 2014
Author: rossbennett34
Date: 2014-03-05 18:28:34 +0100 (Wed, 05 Mar 2014)
New Revision: 104
Added:
pkg/GARPFRM/demo/monte_carlo.R
Modified:
pkg/GARPFRM/NAMESPACE
pkg/GARPFRM/R/monte_carlo.R
pkg/GARPFRM/man/EWMA.Rd
pkg/GARPFRM/man/endingPrices.Rd
pkg/GARPFRM/man/getCor.Rd
pkg/GARPFRM/man/getCov.Rd
pkg/GARPFRM/man/monteCarlo.Rd
pkg/GARPFRM/man/plotEndingPrices.Rd
Log:
Updating docs and adding demo for Monte Carlo. Updating docs for EWMA
Modified: pkg/GARPFRM/NAMESPACE
===================================================================
--- pkg/GARPFRM/NAMESPACE 2014-03-05 16:46:28 UTC (rev 103)
+++ pkg/GARPFRM/NAMESPACE 2014-03-05 17:28:34 UTC (rev 104)
@@ -31,5 +31,6 @@
S3method(hypTest,capm_uv)
S3method(plot,corEWMA)
S3method(plot,covEWMA)
+S3method(plot,MonteCarlo)
S3method(plot,varEWMA)
S3method(print,EWMA)
Modified: pkg/GARPFRM/R/monte_carlo.R
===================================================================
--- pkg/GARPFRM/R/monte_carlo.R 2014-03-05 16:46:28 UTC (rev 103)
+++ pkg/GARPFRM/R/monte_carlo.R 2014-03-05 17:28:34 UTC (rev 104)
@@ -1,24 +1,30 @@
+# TODO: We should implement this in C or C++ later assuming we get funding
+# and we use monte carlo for option pricing
+
# Monte Carlo Function
-generateMC <- function(mu, sigma, Time=1, steps=52, starting_value=100){
- dt <- Time / steps
- S <- vector("numeric", steps+1)
- eps <- rnorm(steps)
- S[1] <- starting_value
- for(i in 2:length(S)){
- dS <- mu * dt + sigma * eps(i-1) * sqrt(dt)
- S[i] <- dS + S[i-1]
- }
- return(S)
-}
+# generateMC <- function(mu, sigma, Time=1, steps=52, starting_value=100){
+# dt <- Time / steps
+# S <- vector("numeric", steps+1)
+# eps <- rnorm(steps)
+# S[1] <- starting_value
+# for(i in 2:length(S)){
+# dS <- mu * dt + sigma * eps(i-1) * sqrt(dt)
+# S[i] <- dS + S[i-1]
+# }
+# return(S)
+# }
# Monte Carlo using ln(S) rather than S
# more accurate
generateLogMC <- function(mu, sigma, Time=1, steps=52, starting_value=100){
dt <- Time / steps
+ musigdt <- (mu - 0.5 * sigma^2) * dt
+ sigdt <- sigma * sqrt(dt)
S <- vector("numeric", steps+1)
+ eps <- rnorm(steps)
S[1] <- starting_value
for(i in 2:length(S)){
- S[i] <- S[i-1] * exp((mu - 0.5 * sigma^2) * dt + sigma * eps(i-1) * sqrt(dt))
+ S[i] <- S[i-1] * exp(musigdt + sigdt * eps[i-1])
}
return(S)
}
@@ -43,26 +49,24 @@
#' @param Time length of simulation (in years)
#' @param steps number of time steps
#' @param starting_value asset price starting value
-#' @param log TRUE/FALSE (default = TRUE) simulate ln(S) rather than S; where S
-#' is the price of the asset.
#' @return matrix of simulated price paths where each column represents a price path
+#' @examples
+#' library(GARPFRM)
+#'
+#' mc <- monteCarlo(0.05, 0.25, 500, 1, 52, 10)
#' @export
-monteCarlo <- function(mu, sigma, N=100, Time=1, steps=52, starting_value=100, log=TRUE){
- mc_mat <- matrix(0, steps, N)
- if(log){
- for(i in 1:N){
- mc_mat[,i] <- generateLogMC(mu, sigma, Time, steps, starting_value)
- }
- } else {
- for(i in 1:N){
- mc_mat[,i] <- generateMC(mu, sigma, Time, steps, starting_value)
- }
+monteCarlo <- function(mu, sigma, N=100, time=1, steps=52, starting_value=100){
+ mc_mat <- matrix(0, steps+1, N)
+ for(i in 1:N){
+ mc_mat[,i] <- generateLogMC(mu, sigma, time, steps, starting_value)
}
- class(mc_mat) <- "monte_carlo"
+ class(mc_mat) <- "MonteCarlo"
return(mc_mat)
}
-plot.monte_carlo <-function(x, y, ..., main="Monte Carlo Simulation", xlab="Time Index", ylab="Price"){
+#' @method plot MonteCarlo
+#' @S3method plot MonteCarlo
+plot.MonteCarlo <-function(x, y, ..., main="Monte Carlo Simulation", xlab="Time Index", ylab="Price"){
plot(x[,1], type="n", ylim=range(x), main=main, xlab=xlab, ylab=ylab, ...)
for(i in 1:ncol(x)){
lines(x[,i])
@@ -72,24 +76,35 @@
#' Ending Prices of Monte Carlo Simulation
#'
#' Get the ending prices, i.e. terminal values, of a monte carlo simulation
-#' @param mc monte carlo object created with \link{\code{monteCarlo}}
+#' @param mc monte carlo object created with \code{monteCarlo}
#' @return vector ending prices
+#' library(GARPFRM)
+#'
+#' mc <- monteCarlo(0.05, 0.25, 500, 1, 52, 10)
+#' ep <- endingPrices(mc)
#' @export
endingPrices <- function(mc){
- if(!inherits(mc, "monte_carlo")) stop("mc must be of class 'monte_carlo'")
+ if(!inherits(mc, "MonteCarlo")) stop("mc must be of class 'MonteCarlo'")
return(mc[nrow(mc),])
}
#' Plot Ending Prices
#'
-#' Plot the ending prices from a Monte Carlo simulation
-#' @param mc monte carlo object created with \link{\code{monteCarlo}}
+#' Plot the kernel density estimate and histogram of the ending prices
+#' from a Monte Carlo simulation.
+#' @param mc monte carlo object created with \code{monteCarlo}
+#' @param \dots additional arguments passed to \code{hist}
+#' @examples
+#' library(GARPFRM)
+#'
+#' mc <- monteCarlo(0.05, 0.25, 500, 1, 52, 10)
+#' plotEndingPrices(mc)
#' @export
-plotEndingPrices <- function(mc){
- if(!inherits(mc, "monte_carlo")) stop("mc must be of class 'monte_carlo'")
+plotEndingPrices <- function(mc, ..., main="Ending Prices", xlab="Price", ylab="Density"){
+ if(!inherits(mc, "MonteCarlo")) stop("mc must be of class 'MonteCarlo'")
ending_prices <- endingPrices(mc)
dens_ep <- density(ending_prices)
- hist(ending_prices, freq=FALSE)
+ hist(ending_prices, freq=FALSE, ylim=range(dens_ep$y), ...=..., main=main, xlab=xlab, ylab=ylab)
lines(dens_ep)
invisible(list(ending_prices=ending_prices, density=dens_ep))
}
Added: pkg/GARPFRM/demo/monte_carlo.R
===================================================================
--- pkg/GARPFRM/demo/monte_carlo.R (rev 0)
+++ pkg/GARPFRM/demo/monte_carlo.R 2014-03-05 17:28:34 UTC (rev 104)
@@ -0,0 +1,14 @@
+# Monte Carlo demo
+
+library(GARPFRM)
+
+mc <- monteCarlo(0.05, 0.25, 500, 1, 52, 10)
+
+# plot the simulated asset paths from the monte carlo simulation
+plot(mc)
+
+# get the ending prices
+ending_prices <- endingPrices(mc)
+
+# plot the ending prices
+plotEndingPrices(mc)
Modified: pkg/GARPFRM/man/EWMA.Rd
===================================================================
--- pkg/GARPFRM/man/EWMA.Rd 2014-03-05 16:46:28 UTC (rev 103)
+++ pkg/GARPFRM/man/EWMA.Rd 2014-03-05 17:28:34 UTC (rev 104)
@@ -20,4 +20,11 @@
Use an exponentially weighted moving average to estimate
the covariance or correlation of asset returns.
}
+\examples{
+data(crsp.short)
+# Use the first 5 assets in largecap.ts for the returns data
+R <- largecap.ts[, 1:5]
+# Estimate the covariance matrix via EWMA
+covEst <- EWMA(R, 0.94, 15)
+}
Modified: pkg/GARPFRM/man/endingPrices.Rd
===================================================================
--- pkg/GARPFRM/man/endingPrices.Rd 2014-03-05 16:46:28 UTC (rev 103)
+++ pkg/GARPFRM/man/endingPrices.Rd 2014-03-05 17:28:34 UTC (rev 104)
@@ -6,10 +6,13 @@
}
\arguments{
\item{mc}{monte carlo object created with
- \link{\code{monteCarlo}}}
+ \code{monteCarlo}}
}
\value{
- vector ending prices
+ vector ending prices library(GARPFRM)
+
+ mc <- monteCarlo(0.05, 0.25, 500, 1, 52, 10) ep <-
+ endingPrices(mc)
}
\description{
Get the ending prices, i.e. terminal values, of a monte
Modified: pkg/GARPFRM/man/getCor.Rd
===================================================================
--- pkg/GARPFRM/man/getCor.Rd 2014-03-05 16:46:28 UTC (rev 103)
+++ pkg/GARPFRM/man/getCor.Rd 2014-03-05 17:28:34 UTC (rev 104)
@@ -14,4 +14,15 @@
Extract the correlation of two assets from an \code{EWMA}
object
}
+\examples{
+data(crsp.short)
+# Use the first 5 assets in largecap.ts for the returns data
+R <- largecap.ts[, 1:5]
+# Estimate the correlation matrix via EWMA
+corEst <- EWMA(R, 0.94, 15, TRUE)
+# get the correlation between AMAT and CAT
+corAMATCAT <- getCov(corEst, assets=c("AMAT", "CAT"))
+cor13 <- getCov(corEst, assets=c(1, 3))
+all.equal(corAMATCAT, cor13)
+}
Modified: pkg/GARPFRM/man/getCov.Rd
===================================================================
--- pkg/GARPFRM/man/getCov.Rd 2014-03-05 16:46:28 UTC (rev 103)
+++ pkg/GARPFRM/man/getCov.Rd 2014-03-05 17:28:34 UTC (rev 104)
@@ -15,4 +15,15 @@
Extract the covariance between two assets from an EWMA
object
}
+\examples{
+data(crsp.short)
+# Use the first 5 assets in largecap.ts for the returns data
+R <- largecap.ts[, 1:5]
+# Estimate the covariance matrix via EWMA
+covEst <- EWMA(R, 0.94, 15)
+# get the covariance between AMAT and CAT
+covAMATCAT <- getCov(covEst, assets=c("AMAT", "CAT"))
+cov13 <- getCov(covEst, assets=c(1, 3))
+all.equal(covAMATCAT, cov13)
+}
Modified: pkg/GARPFRM/man/monteCarlo.Rd
===================================================================
--- pkg/GARPFRM/man/monteCarlo.Rd 2014-03-05 16:46:28 UTC (rev 103)
+++ pkg/GARPFRM/man/monteCarlo.Rd 2014-03-05 17:28:34 UTC (rev 104)
@@ -2,8 +2,8 @@
\alias{monteCarlo}
\title{Monte Carlo Price Path Simulation}
\usage{
- monteCarlo(mu, sigma, N = 100, Time = 1, steps = 52,
- starting_value = 100, log = TRUE)
+ monteCarlo(mu, sigma, N = 100, time = 1, steps = 52,
+ starting_value = 100)
}
\arguments{
\item{mu}{annualized expected return}
@@ -17,9 +17,6 @@
\item{steps}{number of time steps}
\item{starting_value}{asset price starting value}
-
- \item{log}{TRUE/FALSE (default = TRUE) simulate ln(S)
- rather than S; where S is the price of the asset.}
}
\value{
matrix of simulated price paths where each column
@@ -42,4 +39,9 @@
terms of speed and memory should be used, for example, to
price options.
}
+\examples{
+library(GARPFRM)
+mc <- monteCarlo(0.05, 0.25, 500, 1, 52, 10)
+}
+
Modified: pkg/GARPFRM/man/plotEndingPrices.Rd
===================================================================
--- pkg/GARPFRM/man/plotEndingPrices.Rd 2014-03-05 16:46:28 UTC (rev 103)
+++ pkg/GARPFRM/man/plotEndingPrices.Rd 2014-03-05 17:28:34 UTC (rev 104)
@@ -2,13 +2,23 @@
\alias{plotEndingPrices}
\title{Plot Ending Prices}
\usage{
- plotEndingPrices(mc)
+ plotEndingPrices(mc, ..., main = "Ending Prices",
+ xlab = "Price", ylab = "Density")
}
\arguments{
\item{mc}{monte carlo object created with
- \link{\code{monteCarlo}}}
+ \code{monteCarlo}}
+
+ \item{\dots}{additional arguments passed to \code{hist}}
}
\description{
- Plot the ending prices from a Monte Carlo simulation
+ Plot the kernel density estimate and histogram of the
+ ending prices from a Monte Carlo simulation.
}
+\examples{
+library(GARPFRM)
+mc <- monteCarlo(0.05, 0.25, 500, 1, 52, 10)
+plotEndingPrices(mc)
+}
+
More information about the Uwgarp-commits
mailing list