#' Compute conditional expectations and optimally stopped conditional expectations via PDEs, PIDEs, and variational inequalities
#'
#' @param K the parameter for the payoff functions
#' @param payoff string either "cdf", "put", "call" or "straddle" (the payoff functions with one parameter)
#' @param setup output from either \code{pde_setup} or \code{pide_setup}
#' @param variational boolean TRUE for variational inequalities or FALSE for plain PDEs or PIDEs
#' @param exponential boolean to exponentiate the input to the payoff function or not
#'
#' @description {Vectorized solver for PDE, PIDE and variational inequality problems over the parameter for the terminal payoff function.}
#' @return vector
#' @export feynman_kac
feynman_kac <- function(K, payoff, setup, variational = FALSE, exponential = FALSE)
{
payoff_name_call <- paste("payoff_", payoff, sep = "")
nk <- length(K)
problem_type <- setup$type
output <- matrix(0, nrow = nk)
for(i in 1:nk)
{
if(exponential)
{
payoff <- function(y) do.call(what = payoff_name_call, args = list(x = setup$spot*exp(y), K = K[i]))
} else
{
payoff <- function(y) do.call(what = payoff_name_call, args = list(x = y, K = K[i]))
}
payoff <- grid_function(payoff, x = setup$grid.x)
setup$payoff <- payoff
if(problem_type == "pde")
{
output[i] <- pde_solve(setup, variational, output = "price")
} else if(problem_type == "pide")
{
output[i] <- pide_solve(setup, variational, output = "price")
}
}
return(data.frame(K = K, u = output))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.