README.md

pside

This package provides R functions for prediction, fitting, estimation and calibration of models to financial data.

(under development)

Installation

You can install the latest GitHub version with devtools:

devtools::install_github("shill1729/pside")

Comparing exact, empirical, and estimated densities for Merton's jump diffusion model

This example simulates a sample path that follows Merton's jump-diffusion dynamics (geometric Brownian motion + log-normal jumps). It plots a sample path along with three probabilitiy density functions: the exact density with the parameters used to generate the simulation, the empirical density obtained from estimating it with the kernel method from the simulated log-increments, and finally a model-fit density with parameters estimated from an approximate MLE routine using R's optim function.

# For simulating
library(FeynmanKacSolver)
# For exact density
library(OptionPricing)
# For MLE routine and optimal fraction
library(KellyCriterion)
# Maybe someday I'll be organized enough to get these all into one package...

thresh_hold <- 0.08
tn <- 1
n <- 1000
# Specify continuous dynamics
continuous.model <- "gbm"
# Specify jump dynamics
jump.model <- "norm"
# Specify parameters
parameters <- list(mu = 0.15, 
                   volat = 0.5, 
                   lambda = function(x, t) 10, 
                   mean = -0.08,
                   sd = 0.002
                   )
list2env(x = parameters, envir = .GlobalEnv)
# Specify initial values
IC <- list(spot = 25)
# Simulate a sample-path and compute log-returns 
x <- sample_path(t = tn, n, continuous.model, jump.model, parameters, IC)
log_returns <- diff(log(x$X))
# Empirical PDF
epdf <- density(log_returns)
# Exact PDF
eta <- exp(mean+0.5*sd^2)-1
exact_pdf <- djdf(epdf$x, t = tn/n, drift = mu-lambda(1, 0)*eta-0.5*volat^2, volat = volat, lambda = lambda(1, 0), a = mean, b = sd)
par(mfrow = c(2, 1))
# Sample path plot
plot(x, type = "l", main = paste("sample path of", continuous.model, "-", jump.model))
# MLE estimate for parameters from simulation
mle_fit <- mle_merton(log_returns, thresh_hold = thresh_hold, tn = tn, time_step = tn/n, compensate = TRUE, direction = "both")
v <- mle_fit$par
# Model-fit PDF with estimated parameters
eta_est <- exp(v[4]+0.5*v[5]^2)-1
estimated_pdf <- djdf(epdf$x, t = tn/n, drift = v[1]-v[3]*eta_est-0.5*v[2]^2, volat = v[2], lambda = v[3], a = v[4], b = v[5])
# Finish plotting PDFs
# PDF of increments plot
plot(epdf$x, exact_pdf, main = "PDF of log-increment", type = "l", ylim = c(0, max(exact_pdf, epdf$y, estimated_pdf)))
lines(epdf$x, epdf$y, col = "blue", lty = "dashed")
lines(epdf$x, estimated_pdf, col = "green", lty = "dashed")
legend("topleft", legend = c("exact", "empirical", "estimated"), col = c("black", "blue", "green"), lty = 1, cex = 0.8)
exact_par <- unlist(c(parameters[1:2], lambda = parameters$lambda(0, 1), parameters[4:5]))
# Print estimated parameters vs exact
print(rbind(v, exact_par))


shill1729/pside documentation built on June 11, 2020, 12:18 a.m.