[Uwgarp-commits] r55 - in pkg/GARPFRM: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Jan 25 23:30:05 CET 2014
Author: rossbennett34
Date: 2014-01-25 23:30:05 +0100 (Sat, 25 Jan 2014)
New Revision: 55
Added:
pkg/GARPFRM/R/monte_carlo.R
pkg/GARPFRM/man/hypTest.Rd
pkg/GARPFRM/man/monteCarlo.Rd
Modified:
pkg/GARPFRM/DESCRIPTION
pkg/GARPFRM/NAMESPACE
Log:
Adding monte carlo
Modified: pkg/GARPFRM/DESCRIPTION
===================================================================
--- pkg/GARPFRM/DESCRIPTION 2014-01-25 21:23:53 UTC (rev 54)
+++ pkg/GARPFRM/DESCRIPTION 2014-01-25 22:30:05 UTC (rev 55)
@@ -15,3 +15,4 @@
License: GPL
Collate:
'capm.R'
+ 'monte_carlo.R'
Modified: pkg/GARPFRM/NAMESPACE
===================================================================
--- pkg/GARPFRM/NAMESPACE 2014-01-25 21:23:53 UTC (rev 54)
+++ pkg/GARPFRM/NAMESPACE 2014-01-25 22:30:05 UTC (rev 55)
@@ -1,3 +1,5 @@
+S3method(hypTest,capm_mv)
+S3method(hypTest,capm_uv)
export(CAPM)
export(chartSML)
export(getAlphas)
@@ -4,7 +6,7 @@
export(getBetas)
export(getStatistics)
export(hypTest)
-export(plot.capm_mlm)
+export(plot.capm_mv)
export(plot.capm_uv)
S3method(getAlphas,capm_mlm)
S3method(getAlphas,capm_uv)
Added: pkg/GARPFRM/R/monte_carlo.R
===================================================================
--- pkg/GARPFRM/R/monte_carlo.R (rev 0)
+++ pkg/GARPFRM/R/monte_carlo.R 2014-01-25 22:30:05 UTC (rev 55)
@@ -0,0 +1,72 @@
+# Monte Carlo Function
+generateMC <- function(mu, sigma, Time=1, steps=52, starting_value=100){
+ dt <- Time / steps
+ S <- vector("numeric", steps)
+ S[1] <- starting_value
+ for(i in 2:length(S)){
+ dS <- mu * dt + sigma * rnorm(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
+ S <- vector("numeric", steps)
+ S[1] <- starting_value
+ for(i in 2:length(S)){
+ S[i] <- S[i-1] * exp((mu - (sigma^2 / 2)) * dt + sigma * rnorm(1) * sqrt(dt))
+ }
+ return(S)
+}
+
+#' Monte Carlo Price Path Simulation
+#'
+#' Description for Monte Carlo. Geometric brownian motion. mu and sigma are assumed constant
+#'
+#' @param mu annualized expected return
+#' @param sigma annualized standard deviation
+#' @param N number of simulations
+#' @param Time length of simulation (in years)
+#' @param steps number of time steps
+#' @param starting_value price to start at
+#' @param log TRUE/FALSE (default = TRUE) simulate ln(P) rather than S; where S
+#' is the price of the asset.
+#' @return matrix of Monte Carlo simulated price paths
+monteCarlo <- function(mu, sigma, N=100, Time=1, steps=52, starting_value=100, log=TRUE){
+ mc_mat <- matrix(0, N, steps)
+ 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)
+ }
+ }
+ class(mc_mat) <- "monte_carlo"
+ return(mc_mat)
+}
+
+plot.monte_carlo <-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:nrow(x)){
+ lines(x[i,])
+ }
+}
+
+#' Ending Prices of Monte Carlo Simulation
+#'
+endingPrices <- function(mc){
+ mc[, ncol(mc)]
+}
+
+plotEndingPrices <- function(mc){
+ ending_prices <- endingPrices(mc)
+ dens_ep <- density(ending_prices)
+ hist(ending_prices, freq=FALSE)
+ lines(dens_ep)
+ invisible(list(ending_prices=ending_prices, density=dens_ep))
+}
Added: pkg/GARPFRM/man/hypTest.Rd
===================================================================
--- pkg/GARPFRM/man/hypTest.Rd (rev 0)
+++ pkg/GARPFRM/man/hypTest.Rd 2014-01-25 22:30:05 UTC (rev 55)
@@ -0,0 +1,14 @@
+\name{hypTest}
+\alias{hypTest}
+\title{CAPM hypthTest}
+\usage{
+ hypTest(object, CI)
+}
+\arguments{
+ \item{object}{a capm object created by
+ \code{\link{CAPM}}}
+}
+\description{
+ Description of CAPM beta/alpha test
+}
+
Added: pkg/GARPFRM/man/monteCarlo.Rd
===================================================================
--- pkg/GARPFRM/man/monteCarlo.Rd (rev 0)
+++ pkg/GARPFRM/man/monteCarlo.Rd 2014-01-25 22:30:05 UTC (rev 55)
@@ -0,0 +1,31 @@
+\name{monteCarlo}
+\alias{monteCarlo}
+\title{Monte Carlo Price Path Simulation}
+\usage{
+ monteCarlo(mu, sigma, N = 100, Time = 1, steps = 52,
+ starting_value = 100, log = TRUE)
+}
+\arguments{
+ \item{mu}{annualized expected return}
+
+ \item{sigma}{annualized standard deviation}
+
+ \item{N}{number of simulations}
+
+ \item{Time}{length of simulation (in years)}
+
+ \item{steps}{number of time steps}
+
+ \item{starting_value}{price to start at}
+
+ \item{log}{TRUE/FALSE (default = TRUE) simulate ln(P)
+ rather than S; where S is the price of the asset.}
+}
+\value{
+ matrix of Monte Carlo simulated price paths
+}
+\description{
+ Description for Monte Carlo. Geometric brownian motion.
+ mu and sigma are assumed constant
+}
+
More information about the Uwgarp-commits
mailing list