R/monte_carlo.R

Defines functions entropy_monte_carlo kelly_monte_carlo

Documented in entropy_monte_carlo kelly_monte_carlo

#' Monte-Carlo optimization of expected log
#'
#' @param distr string for distribution name of "r, p, d" family for simulating, CDFs, and PDFs
#' @param rate the discounting rate
#' @param lb the lower bound for interval given to the uniroot bisection solver
#' @param ub the upper bound for interval given to the uniroot bisection solver
#' @param n the number of samples to use in the Monte-Carlo approximation
#' @param ... parameters of distribution, specifically the rdistr(n, params...) form
#' @description {A Monte-Carlo implementation of the Kelly criterion for a continuous return RV.}
#' @details {Using Leibniz's rule one can differentiate under the expectation and then set this expectation to zero. Using LLN we approximate this numerically}
#' @return list
#' @export kelly_monte_carlo
#' @import stats
kelly_monte_carlo <- function(distr, rate, lb, ub, n = 50000, ...)
{
  # For normally distributed returns there is no upper and lower bound but we can instead rely on
  # Number of standard deviations away from the mean.
  a <- -(1+rate)/(ub-rate)
  b <- -(1+rate)/(lb-rate)

  gm <- function(x, rate, distr, n = n, ...)
  {
    distr <- paste("r", distr, sep = "")
    U <- get(distr)(n, ...)
    h <- (U-rate)/(1+rate+x*(U-rate))
    mean(h)
  }

  rootIt <- stats::uniroot(gm, interval = c(a, b), rate = rate, distr = distr, n = n, ...)
  return(list(interval = c(a, b), solution = unlist(rootIt)))
}






#' Monte-Carlo computation of expected log
#'
#' @param x the fraction input
#' @param rate the discounting rate
#' @param distr string for distirbution name of "r, p, d" family for simulating, CDFs, and PDFs
#' @param n the number of samples to use in the Monte-Carlo approximation
#' @param ... parameters of distribution, specifically the rdistr(n, params...) form
#' @description {A Monte-Carlo implementation of the expected log growth for a continuous return RV.}
#' @details {Using LLN we approximate the expected log growth numerically via sample means}
#' @return numeric
#' @export entropy_monte_carlo
entropy_monte_carlo <- function(x, rate, distr, n = 50000, ...)
{
  distr <- paste("r", distr, sep = "")
  U <- get(distr)(n, ...)
  h <- log((1+rate+x*(U-rate)))
  mean(h)
}
shill1729/KellyCriterion documentation built on Oct. 12, 2020, 4:21 a.m.