From 3ea9baa55d75b97649b6873e7d966b6d2032bd45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Pelletier?= Date: Sat, 22 Feb 2014 13:28:27 -0500 Subject: [PATCH] Ajout des fonctions dampedcfcallCarrMadan, dchangetypeGAL, dmGAL, dscaleGAL --- .Rbuildignore | 6 +++ DESCRIPTION | 2 + R/cfLM.R | 4 +- R/dampedcfcallCarrMadan.R | 44 ++++++++++++++++++ R/dchangetypeGAL.R | 47 +++++++++++++++++++ R/dmGAL.R | 87 ++++++++++++++++++++++++++++++++++++ R/dscaleGAL.R | 34 ++++++++++++++ R/scaleGAL.R | 21 ++++++--- man/cfLM.Rd | 6 +-- man/dampedcfcallCarrMadan.Rd | 37 +++++++++++++++ man/dchangetypeGAL.Rd | 27 +++++++++++ man/dmGAL.Rd | 27 +++++++++++ man/dscaleGAL.Rd | 29 ++++++++++++ man/scaleGAL.Rd | 4 +- 14 files changed, 362 insertions(+), 13 deletions(-) create mode 100644 .Rbuildignore create mode 100644 R/dampedcfcallCarrMadan.R create mode 100644 R/dchangetypeGAL.R create mode 100644 R/dmGAL.R create mode 100644 R/dscaleGAL.R create mode 100644 man/dampedcfcallCarrMadan.Rd create mode 100644 man/dchangetypeGAL.Rd create mode 100644 man/dmGAL.Rd create mode 100644 man/dscaleGAL.Rd diff --git a/.Rbuildignore b/.Rbuildignore new file mode 100644 index 0000000..04a8cf7 --- /dev/null +++ b/.Rbuildignore @@ -0,0 +1,6 @@ +^.*\.Rproj$ +^\.Rproj\.user$ +.gitignore +.project +.git +.settings diff --git a/DESCRIPTION b/DESCRIPTION index fe3cf2b..ee2582c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -8,4 +8,6 @@ Description: This is a package gathering different functions to work with the Generalized Asymmetric Laplace distribution Suggests: MASS +Depends: + moments License: LGPL-3 diff --git a/R/cfLM.R b/R/cfLM.R index f0a2455..617daf6 100644 --- a/R/cfLM.R +++ b/R/cfLM.R @@ -7,15 +7,15 @@ #' Characteristic function of Laplace motion #' @param u Transform variate -#' @param param Parameter vector #' @param time Time of the process +#' @param param Parameter vector #' @param type Choose between "mu" or "kappa" parametrization #' @param log Logical for log-parameters #' @param start Starting value of the process #' @return Characteristic function value at point u for given parameter vector #' #' @author Francois Pelletier -cfLM <- function(u,param,time=1,type="mu",log=FALSE,start=0) +cfLM <- function(u,time,param,type="mu",log=FALSE,start=0) { testparGAL(param,type,log) if(log) diff --git a/R/dampedcfcallCarrMadan.R b/R/dampedcfcallCarrMadan.R new file mode 100644 index 0000000..8a29c74 --- /dev/null +++ b/R/dampedcfcallCarrMadan.R @@ -0,0 +1,44 @@ +# Damped characteristic function of the call option log-price +# +# Author: Francois Pelletier +# +# LGPL 3.0 +############################################################################### + + +#' Damped characteristic function of the call option log-price +#' @param u Transform variate +#' @param char.fn Characteristic function of the log-price process +#' @param eval.time Evaluation time +#' @param expiry.time Expiry time +#' @param rate Continuously compounded interest rate (force of interest) +#' @param alpha Damping parameter +#' @param ... Parameters of the characteristic function +#' @param moneyness Boolean for moneyness of call option +#' (TRUE if strike price is lower than stock price) +#' @return Characteristic function value +#' +#' @author Francois Pelletier +dampedcfcallCarrMadan <- function(u,char.fn,eval.time,expiry.time,rate,alpha,...,moneyness=TRUE) +{ + if(moneyness) + { + exp(-rate*(expiry.time-eval.time))* + char.fn(u-1i*(alpha+1),expiry.time-eval.time,...) / + (alpha^2+alpha-u^2+1i*u*(2*alpha+1)) + } + else + { + auxiliairyf <- function(u,char.fn,eval.time,expiry.time,rate,alpha,...) + { + exp(-rate*(expiry.time-eval.time))* + (1/(1+1i*u)-exp(rate*(expiry.time-eval.time))/ + (1i*u)-char.fn(u-1i,expiry.time-eval.time,...)/(u^2-1i*u)) + } + (auxiliairyf(u-1i*alpha,char.fn,eval.time,expiry.time,rate,alpha,...)- + auxiliairyf(u+1i*alpha,char.fn,eval.time,expiry.time,rate,alpha,...))/2 + } + +} + + diff --git a/R/dchangetypeGAL.R b/R/dchangetypeGAL.R new file mode 100644 index 0000000..7115fd6 --- /dev/null +++ b/R/dchangetypeGAL.R @@ -0,0 +1,47 @@ +# Derivative vector of the type change function +# +# Author: Francois Pelletier +# +# LGPL 3.0 +############################################################################### + +#' Derivative vector of the type change function +#' @param param Parameter vector +#' @param type Choose between "mu" or "kappa" parametrization +#' @param target Choose between "mu" or "kappa" parametrization +#' @param log Logical for log-parameters +#' @return The derivative matrix of the type change function +#' +#' @author Francois Pelletier +dchangetypeGAL <- function(param,type="mu",target="kappa",log=FALSE) +{ + testparGAL(param,type,log) + if(log) + { + if(type=="mu" && target=="kappa") + { + + } + else if(type=="kappa" && target=="mu") + { + + } + } + else + { + if(type=="mu" && target=="kappa") + { + matrix(c(1,0,0,0, + 0,1,0,0, + 0,(param[3]*sqrt(4*param[2]^2+param[3]^2)-param[3]^2)/(2*param[2]^2*sqrt(4*param[2]^2+param[3]^2)),-(sqrt(4*param[2]^2+param[3]^2)-param[3])/(2*param[2]*sqrt(4*param[2]^2+param[3]^2)),0, + 0,0,0,1),4,4) + } + else if(type=="kappa" && target=="mu") + { + matrix(c(1,0,0,0, + 0,1,0,0, + 0,-(param[3]^2-1)/(sqrt(2)*param[3]),-((param[3]^2+1)*param[2])/(sqrt(2)*param[3]^2),0, + 0,0,0,1),4,4) + } + } +} \ No newline at end of file diff --git a/R/dmGAL.R b/R/dmGAL.R new file mode 100644 index 0000000..66cc40e --- /dev/null +++ b/R/dmGAL.R @@ -0,0 +1,87 @@ +# Derivative vector of the mean and standard deviation of GAL distribution +# +# Author: Francois Pelletier +# +# LGPL 3.0 +############################################################################### + + +#' Derivative vector of the mean and standard deviation of GAL distribution +#' @param param Parameter vector +#' @param order 1 for mean, 2 for standard deviation +#' @param type Choose between "mu" or "kappa" parametrization +#' @param log Logical for log-parameters +#' @return A vector of the derivative of the analytical moment +#' +#' @author Francois Pelletier +dmGAL <- function(param,order,type="mu",log=FALSE) +{ if(log) + { + eparam <- exp(param) + if(order==1) + { + if(type=="mu") + { + c(1,0,eparam[4],eparam[3]) + } + else if(type=="kappa") + { + c(1, + -(1/2)*sqrt(2)*eparam[4]*(-1+eparam[3]^2)/eparam[3], + -(1/2)*eparam[4]*eparam[2]*sqrt(2)*(eparam[3]^2+1)/eparam[3]^2, + -(1/2)*sqrt(2)*eparam[2]*(-1+eparam[3]^2)/eparam[3]) + } + } + if(order==2) + { + if(type=="mu") + { + c(0, + eparam[4]*eparam[2]/sqrt(eparam[4]*eparam[2]^2+eparam[4]*eparam[3]^2), + eparam[4]*eparam[3]/sqrt(eparam[4]*eparam[2]^2+eparam[4]*eparam[3]^2), + (1/2)*(eparam[2]^2+eparam[3]^2)/sqrt(eparam[4]*eparam[2]^2+eparam[4]*eparam[3]^2)) + } + else if(type=="kappa") + { + c(0, + (1/2)*sqrt(2)*eparam[4]*eparam[2]*(eparam[3]^4+1)/(sqrt(eparam[4]*eparam[2]^2*(eparam[3]^4+1)/eparam[3]^2)*eparam[3]^2), + (1/2)*sqrt(2)*eparam[4]*eparam[2]^2*(eparam[3]^4-1)/(sqrt(eparam[4]*eparam[2]^2*(eparam[3]^4+1)/eparam[3]^2)*eparam[3]^3), + (1/4)*sqrt(2)*eparam[2]^2*(eparam[3]^4+1)/(sqrt(eparam[4]*eparam[2]^2*(eparam[3]^4+1)/eparam[3]^2)*eparam[3]^2)) + } + } + } + else + { + if(order==1) + { + if(type=="mu") + { + c(1,0,param[4],param[3]) + } + else if(type=="kappa") + { + c(1, + -(1/2)*sqrt(2)*param[4]*(-1+param[3]^2)/param[3], + -(1/2)*param[4]*param[2]*sqrt(2)*(param[3]^2+1)/param[3]^2, + -(1/2)*sqrt(2)*param[2]*(-1+param[3]^2)/param[3]) + } + } + if(order==2) + { + if(type=="mu") + { + c(0, + param[4]*param[2]/sqrt(param[4]*param[2]^2+param[4]*param[3]^2), + param[4]*param[3]/sqrt(param[4]*param[2]^2+param[4]*param[3]^2), + (1/2)*(param[2]^2+param[3]^2)/sqrt(param[4]*param[2]^2+param[4]*param[3]^2)) + } + else if(type=="kappa") + { + c(0, + (1/2)*sqrt(2)*param[4]*param[2]*(param[3]^4+1)/(sqrt(param[4]*param[2]^2*(param[3]^4+1)/param[3]^2)*param[3]^2), + (1/2)*sqrt(2)*param[4]*param[2]^2*(param[3]^4-1)/(sqrt(param[4]*param[2]^2*(param[3]^4+1)/param[3]^2)*param[3]^3), + (1/4)*sqrt(2)*param[2]^2*(param[3]^4+1)/(sqrt(param[4]*param[2]^2*(param[3]^4+1)/param[3]^2)*param[3]^2)) + } + } + } +} diff --git a/R/dscaleGAL.R b/R/dscaleGAL.R new file mode 100644 index 0000000..5e12bc9 --- /dev/null +++ b/R/dscaleGAL.R @@ -0,0 +1,34 @@ +# Derivative of scale and location transform to the GAL distribution +# +# Author: Francois Pelletier +# +# LGPL 3.0 +############################################################################### + +#' Derivative of scale and location transform to the GAL distribution +#' @param param Parameter vector +#' @param type Choose between "mu" or "kappa" parametrization +#' @param location Location shift (unitary) +#' @param scale Scale shift (in standard deviations) +#' @param log Logical for log-parameters +#' @return The matrix derivative of the transformed parameter vector +#' +#' @author Francois Pelletier +dscaleGAL <- function(param,type="kappa",location,scale,log=FALSE) +{ + if(log) + { + + } + else + { + if(type=="kappa") + { + diag(c(scale,scale,1,1)) + } + else if (type=="mu") + { + + } + } +} diff --git a/R/scaleGAL.R b/R/scaleGAL.R index ffd1961..fbf908f 100644 --- a/R/scaleGAL.R +++ b/R/scaleGAL.R @@ -5,25 +5,32 @@ # LGPL 3.0 ############################################################################### - #' Apply scale and location transform to the GAL distribution #' @param param Parameter vector #' @param type Choose between "mu" or "kappa" parametrization #' @param location Location shift (unitary) #' @param scale Scale shift (in standard deviations) +#' @param log Logical for log-parameters #' @return The transformed parameter vector #' #' @author Francois Pelletier -scaleGAL <- function(param,type="kappa",location,scale) +scaleGAL <- function(param,type="kappa",location,scale,log=FALSE) { - if(type=="kappa") + if(log) { - param * c(scale,scale,1,1) + c(location,0,0,0) + } else { - changetypeGAL( - scaleGAL(changetypeGAL(param,type="mu",target="kappa"),type="kappa",location,scale), - type="kappa",target="mu") + if(type=="kappa") + { + param * c(scale,scale,1,1) + c(location,0,0,0) + } + else if (type=="mu") + { + changetypeGAL( + scaleGAL(changetypeGAL(param,type="mu",target="kappa"),type="kappa",location,scale), + type="kappa",target="mu") + } } } diff --git a/man/cfLM.Rd b/man/cfLM.Rd index da84b05..e108541 100644 --- a/man/cfLM.Rd +++ b/man/cfLM.Rd @@ -2,15 +2,15 @@ \alias{cfLM} \title{Characteristic function of Laplace motion} \usage{ -cfLM(u, param, time = 1, type = "mu", log = FALSE, start = 0) +cfLM(u, time, param, type = "mu", log = FALSE, start = 0) } \arguments{ \item{u}{Transform variate} - \item{param}{Parameter vector} - \item{time}{Time of the process} + \item{param}{Parameter vector} + \item{type}{Choose between "mu" or "kappa" parametrization} diff --git a/man/dampedcfcallCarrMadan.Rd b/man/dampedcfcallCarrMadan.Rd new file mode 100644 index 0000000..49450de --- /dev/null +++ b/man/dampedcfcallCarrMadan.Rd @@ -0,0 +1,37 @@ +\name{dampedcfcallCarrMadan} +\alias{dampedcfcallCarrMadan} +\title{Damped characteristic function of the call option log-price} +\usage{ +dampedcfcallCarrMadan(u, char.fn, eval.time, expiry.time, rate, alpha, ..., + moneyness = TRUE) +} +\arguments{ + \item{u}{Transform variate} + + \item{char.fn}{Characteristic function of the log-price + process} + + \item{eval.time}{Evaluation time} + + \item{expiry.time}{Expiry time} + + \item{rate}{Continuously compounded interest rate (force + of interest)} + + \item{alpha}{Damping parameter} + + \item{...}{Parameters of the characteristic function} + + \item{moneyness}{Boolean for moneyness of call option + (TRUE if strike price is lower than stock price)} +} +\value{ +Characteristic function value +} +\description{ +Damped characteristic function of the call option log-price +} +\author{ +Francois Pelletier +} + diff --git a/man/dchangetypeGAL.Rd b/man/dchangetypeGAL.Rd new file mode 100644 index 0000000..369606e --- /dev/null +++ b/man/dchangetypeGAL.Rd @@ -0,0 +1,27 @@ +\name{dchangetypeGAL} +\alias{dchangetypeGAL} +\title{Derivative vector of the type change function} +\usage{ +dchangetypeGAL(param, type = "mu", target = "kappa", log = FALSE) +} +\arguments{ + \item{param}{Parameter vector} + + \item{type}{Choose between "mu" or "kappa" + parametrization} + + \item{target}{Choose between "mu" or "kappa" + parametrization} + + \item{log}{Logical for log-parameters} +} +\value{ +The derivative matrix of the type change function +} +\description{ +Derivative vector of the type change function +} +\author{ +Francois Pelletier +} + diff --git a/man/dmGAL.Rd b/man/dmGAL.Rd new file mode 100644 index 0000000..ea1f6b5 --- /dev/null +++ b/man/dmGAL.Rd @@ -0,0 +1,27 @@ +\name{dmGAL} +\alias{dmGAL} +\title{Derivative vector of the mean and standard deviation of GAL distribution} +\usage{ +dmGAL(param, order, type = "mu", log = FALSE) +} +\arguments{ + \item{param}{Parameter vector} + + \item{order}{1 for mean, 2 for standard deviation} + + \item{type}{Choose between "mu" or "kappa" + parametrization} + + \item{log}{Logical for log-parameters} +} +\value{ +A vector of the derivative of the analytical moment +} +\description{ +Derivative vector of the mean and standard deviation of GAL +distribution +} +\author{ +Francois Pelletier +} + diff --git a/man/dscaleGAL.Rd b/man/dscaleGAL.Rd new file mode 100644 index 0000000..6824472 --- /dev/null +++ b/man/dscaleGAL.Rd @@ -0,0 +1,29 @@ +\name{dscaleGAL} +\alias{dscaleGAL} +\title{Derivative of scale and location transform to the GAL distribution} +\usage{ +dscaleGAL(param, type = "kappa", location, scale, log = FALSE) +} +\arguments{ + \item{param}{Parameter vector} + + \item{type}{Choose between "mu" or "kappa" + parametrization} + + \item{location}{Location shift (unitary)} + + \item{scale}{Scale shift (in standard deviations)} + + \item{log}{Logical for log-parameters} +} +\value{ +The matrix derivative of the transformed parameter vector +} +\description{ +Derivative of scale and location transform to the GAL +distribution +} +\author{ +Francois Pelletier +} + diff --git a/man/scaleGAL.Rd b/man/scaleGAL.Rd index b540a87..08ab72c 100644 --- a/man/scaleGAL.Rd +++ b/man/scaleGAL.Rd @@ -2,7 +2,7 @@ \alias{scaleGAL} \title{Apply scale and location transform to the GAL distribution} \usage{ -scaleGAL(param, type = "kappa", location, scale) +scaleGAL(param, type = "kappa", location, scale, log = FALSE) } \arguments{ \item{param}{Parameter vector} @@ -13,6 +13,8 @@ scaleGAL(param, type = "kappa", location, scale) \item{location}{Location shift (unitary)} \item{scale}{Scale shift (in standard deviations)} + + \item{log}{Logical for log-parameters} } \value{ The transformed parameter vector