From 473c634d7218351292f775452265f53456297c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Pelletier?= Date: Sat, 4 Jan 2014 17:32:36 -0500 Subject: [PATCH] Ajout de plusieurs fonctions --- R/cmGAL.R | 103 +++++++++++++++++++++++++++++++++++++++ R/fgmGAL.R | 36 ++++++++++++++ R/mGAL.R | 91 ++++++++++++++++++++++++++++++++++ R/riskneutralparGAL.R | 36 ++++++++++++++ R/testparGAL.R | 2 +- man/cgfGAL.Rd | 27 ++++++++++ man/cmGAL.Rd | 26 ++++++++++ man/kurtosisGAL.Rd | 26 ++++++++++ man/mGAL.Rd | 26 ++++++++++ man/riskneutralparGAL.Rd | 26 ++++++++++ man/skewnessGAL.Rd | 24 +++++++++ 11 files changed, 422 insertions(+), 1 deletion(-) create mode 100644 R/cmGAL.R create mode 100644 R/mGAL.R create mode 100644 R/riskneutralparGAL.R create mode 100644 man/cgfGAL.Rd create mode 100644 man/cmGAL.Rd create mode 100644 man/kurtosisGAL.Rd create mode 100644 man/mGAL.Rd create mode 100644 man/riskneutralparGAL.Rd create mode 100644 man/skewnessGAL.Rd diff --git a/R/cmGAL.R b/R/cmGAL.R new file mode 100644 index 0000000..e4833ff --- /dev/null +++ b/R/cmGAL.R @@ -0,0 +1,103 @@ +# Centered moments of the GAL distribution +# Skewness and (adjusted) kurtosis of the GAL distribution +# +# Author: Francois Pelletier +# +# LGPL 3.0 +############################################################################### + +#' Centered moments of the GAL distribution +#' @param order Order of raw moment +#' @param param Parameter vector +#' @param type Choose between "mu" or "kappa" parametrization +#' @param log Logical for log-parameters +#' @return A numeric value of the centered moment +#' +#' @author Francois Pelletier +cmGAL <- function(order,param,type="mu",log=FALSE) +{ + testparGAL(param,type,log) + if(log) + { + cmGAL(order,exp(param),type,log=FALSE) + } + else + { + if(type=="mu") + { + if(order==2) + { + param[4]*param[2]^2+param[4]*param[3]^2 + } + if(order==3) + { + 3*param[3]*param[4]*param[2]^2+2*param[3]^3*param[4] + } + + if(order==4) + { + (3*param[4]^2+3*param[4])*param[2]^4+(6*param[3]^2*param[4]^2+12*param[3]^2*param[4])*param[2]^2+3*param[3]^4*param[4]^2+6*param[3]^4*param[4] + } + else + stop("order must be 2,3 or 4") + } + if(type=="kappa") + { + if(order==2) + { + (1/2)*param[4]*param[2]^2*(param[3]^4+1)/param[3]^2 + } + if(order==3) + { + (1/2)*param[4]*param[2]^3*sqrt(2)*(1-param[3]^6)/param[3]^3 + } + if(order==4) + { + (3/4)*param[4]*((param[4]+2)*param[3]^8+2*param[4]*param[3]^4+param[4]+2)*param[2]^4/param[3]^4 + } + else + stop("order must be 2,3 or 4") + } + } +} + +#' Skewness of the GAL distribution +#' @param param Parameter vector +#' @param type Choose between "mu" or "kappa" parametrization +#' @param log Logical for log-parameters +#' @return A numeric value of the skewness +#' +#' @author Francois Pelletier +skewnessGAL <- function(param,type="mu",log=FALSE) +{ + testparGAL(param,type,log) + if(log) + { + skewnessGAL(exp(param),type,log=FALSE) + } + else + { + cmGAL(3,param,type) / (cmGAL(2,param,type)^(3/2)) + } +} + +#' (Adjusted) kurtosis of the GAL distribution +#' @param param Parameter vector +#' @param type Choose between "mu" or "kappa" parametrization +#' @param log Logical for log-parameters +#' @param adjust Logical to use the adjusted kurtosis +#' @return A numeric value of the kurtosis +#' +#' @author Francois Pelletier +kurtosisGAL <- function(param,type="mu",log=FALSE,adjust=TRUE) +{ + testparGAL(param,type,log) + if(log) + { + kurtosisGAL(exp(param),type,log=FALSE) + } + else + { + cmGAL(4,param,type) / (cmGAL(2,param,type)^2) - 3*adjust + } +} \ No newline at end of file diff --git a/R/fgmGAL.R b/R/fgmGAL.R index df1522a..93ea5d9 100644 --- a/R/fgmGAL.R +++ b/R/fgmGAL.R @@ -1,4 +1,5 @@ # Moment generating function of GAL distribution +# Cumulant generating function of GAL distribution # # Author: Francois Pelletier # @@ -38,4 +39,39 @@ mgfGAL <- function(u,param,type="mu",log=FALSE) exp(param[1]*u)*((param[2]^2*u^2)/2+(param[3]*param[2]*u)/sqrt(2)-(param[2]*u)/(sqrt(2)*param[3])+1)^(-param[4]) } } +} + +#' Cumulant generating function of GAL distribution +#' @param u Transform variate +#' @param param Parameter vector +#' @param type Choose between "mu" or "kappa" parametrization +#' @param log Logical for log-parameters +#' @return Cumulant generating function value at point u for given parameter vector +#' +#' @author Francois Pelletier +cgfGAL <- function(u,param,type="mu",log=FALSE) +{ + testparGAL(param,type,log) + if(log) + { + if(type=="mu") + { + log(exp(exp(param[1])*u)*(1-(1/2)*exp(param[2])^2*u^2-exp(param[3])*u)^(-exp(param[4]))) + } + if(type=="kappa") + { + log(exp(exp(param[1])*u)*((exp(param[2])^2*u^2)/2+(exp(param[3])*exp(param[2])*u)/sqrt(2)-(exp(param[2])*u)/(sqrt(2)*exp(param[3]))+1)^(-exp(param[4]))) + } + } + else + { + if(type=="mu") + { + log(exp(param[1]*u)*(1-(1/2)*param[2]^2*u^2-param[3]*u)^(-param[4])) + } + if(type=="kappa") + { + log(exp(param[1]*u)*((param[2]^2*u^2)/2+(param[3]*param[2]*u)/sqrt(2)-(param[2]*u)/(sqrt(2)*param[3])+1)^(-param[4])) + } + } } \ No newline at end of file diff --git a/R/mGAL.R b/R/mGAL.R new file mode 100644 index 0000000..a96d77c --- /dev/null +++ b/R/mGAL.R @@ -0,0 +1,91 @@ +# Raw moments of the GAL distribution +# +# Author: Francois Pelletier +# +# LGPL 3.0 +############################################################################### + + +#' Raw moments of the GAL distribution +#' @param order Order of raw moment +#' @param param Parameter vector +#' @param type Choose between "mu" or "kappa" parametrization +#' @param log Logical for log-parameters +#' @return A numeric value of the raw moment +#' +#' @author Francois Pelletier +mGAL <- function(order,param,type="mu",log=FALSE) +{ + testparGAL(param,type,log) + if(log) + { + mGAL(order,exp(param),type,log=FALSE) + } + else + { + if(type=="mu") + { + if(order==1) + { + param[1]+param[4]*param[3] + } + if(order==2) + { + param[1]^2+2*param[1]*param[4]*param[3]+param[4]^2*param[3]^2+ + param[4]*param[2]^2+param[4]*param[3]^2 + } + if(order==3) + { + 3*param[1]*param[4]*param[2]^2+3*param[1]^2*param[4]*param[3]+ + 3*param[1]*param[4]^2*param[3]^2+param[1]^3+ + 3*param[1]*param[4]*param[3]^2+param[4]^3*param[3]^3+ + 3*param[4]^2*param[3]*param[2]^2+3*param[4]^2*param[3]^3+ + 3*param[4]*param[2]^2*param[3]+2*param[4]*param[3]^3 + } + + if(order==4) + { + 6*param[1]^2*param[4]*param[3]^2+18*param[4]^2*param[3]^2*param[2]^2+ + 12*param[4]*param[2]^2*param[3]^2+ + 12*param[1]*param[4]*param[2]^2*param[3]+4*param[1]^3*param[4]*param[3]+ + 8*param[1]*param[4]*param[3]^3+12*param[1]*param[4]^2*param[3]*param[2]^2+ + 6*param[1]^2*param[4]^2*param[3]^2+param[1]^4+ + 6*param[1]^2*param[4]*param[2]^2+param[4]^4*param[3]^4+ + 6*param[4]^3*param[3]^2*param[2]^2+6*param[4]^3*param[3]^4+ + 3*param[4]^2*param[2]^4+11*param[4]^2*param[3]^4+ + 3*param[4]*param[2]^4+6*param[4]*param[3]^4+ + 12*param[1]*param[4]^2*param[3]^3+4*param[1]*param[4]^3*param[3]^3 + } + else + stop("order must be 1,2,3 or 4") + } + if(type=="kappa") + { + if(order==1) + { + (1/2)*(param[4]*param[2]*sqrt(2)-param[4]*param[2]*sqrt(2)*param[2]^2+2*param[1]*param[2])/param[2] + } + if(order==2) + { + (1/2)*(-2*param[1]*param[4]*param[2]*(param[2]+1)*(param[2]-1)*param[2]*sqrt(2)+ + param[4]*param[2]^2*(param[4]+1)*param[2]^4+(-2*param[4]^2*param[2]^2+2*param[1]^2)*param[2]^2+ + param[4]*param[2]^2*(param[4]+1))/param[2]^2 + } + if(order==3) + { + (1/4)*(-(param[2]-1)*param[4]*param[2]*(param[2]+1)*(((-2*param[2]^2+1+param[2]^4)*param[4]+ + 2*param[2]^2+2*param[2]^4+2)*(param[4]+1)*param[2]^2+ + 6*param[1]^2*param[2]^2)*sqrt(2)+(6*(param[4]*((-2*param[2]^2+1+param[2]^4)*param[4]+ + param[2]^4+1)*param[2]^2+(2/3)*param[1]^2*param[2]^2))* + param[2]*param[1])/param[2]^3 + } + + if(order==4) + { + 1/4*(4*param[1]^4*param[2]^4+param[4]^4*param[2]^4+6*param[4]^3*param[2]^4+11*param[4]^2*param[2]^4+6*param[4]*param[2]^4-24*param[4]^2*param[2]^4*param[1]^2*param[2]^2+12*param[4]^2*param[2]^2*param[1]^2*param[2]^2+12*param[4]^2*param[2]^6*param[1]^2*param[2]^2+12*param[4]*param[2]^6*param[1]^2*param[2]^2-4*param[4]^4*param[2]^4*param[2]^2+6*param[4]^4*param[2]^4*param[2]^4-4*param[4]^4*param[2]^4*param[2]^6+param[4]^4*param[2]^4*param[2]^8-12*param[4]^3*param[2]^4*param[2]^2+12*param[4]^3*param[2]^4*param[2]^4-12*param[4]^3*param[2]^4*param[2]^6+6*param[4]^3*param[2]^4*param[2]^8+11*param[4]^2*param[2]^4*param[2]^8-8*param[4]^2*param[2]^4*param[2]^2+6*param[4]^2*param[2]^4*param[2]^4-8*param[4]^2*param[2]^4*param[2]^6+6*param[4]*param[2]^4*param[2]^8+12*param[4]*param[2]^2*param[1]^2*param[2]^2-12*param[4]^3*param[2]^3*param[2]^3*param[1]*2^(1/2)+4*param[4]^3*param[2]^3*param[2]*param[1]*2^(1/2)+12*param[4]^2*param[2]^3*param[2]*param[1]*2^(1/2)+12*param[4]^3*param[2]^3*param[2]^5*param[1]*2^(1/2)+12*param[4]^2*param[2]^3*param[2]^5*param[1]*2^(1/2)+8*param[4]*param[2]^3*param[2]*param[1]*2^(1/2)+8*param[4]*param[2]*param[2]^3*param[1]^3*2^(1/2)-12*param[4]^2*param[2]^3*param[2]^3*param[1]*2^(1/2)-4*param[4]^3*param[2]^3*param[2]^7*param[1]*2^(1/2)-12*param[4]^2*param[2]^3*param[2]^7*param[1]*2^(1/2)-8*param[4]*param[2]^3*param[2]^7*param[1]*2^(1/2)-8*param[4]*param[2]*param[2]^5*param[1]^3*2^(1/2))/param[2]^4 + } + else + stop("order must be 1,2,3 or 4") + } + } +} diff --git a/R/riskneutralparGAL.R b/R/riskneutralparGAL.R new file mode 100644 index 0000000..bdad4b9 --- /dev/null +++ b/R/riskneutralparGAL.R @@ -0,0 +1,36 @@ +# Risk neutral conversion of parameters of GAL distirbution +# +# Author: Francois Pelletier +# +# LGPL 3.0 +############################################################################### + +#' Risk neutral conversion of parameters of GAL distirbution +#' @param param Parameter vector +#' @param riskfree Risk-free rate +#' @param type Choose between "mu" or "kappa" parametrization +#' @param log Logical for log-parameters +#' @return Risk neutral parameter vector +#' +#' @author Francois Pelletier +riskneutralparGAL <- function(param,riskfree,type="mu",log=FALSE) +{ + testparGAL(param,type,log) + if(type=="kappa") + { + riskneutralparGAL(changetypeGAL(param,type="kappa",target="mu"),riskfree,type="mu",log) + } + if(type=="mu") + { + if(log) + { + c(log(riskfree+log(1-exp(param[3])-exp(param[2])^2/2)*param[4]),param[2],param[3],param[4]) + } + else + { + c(riskfree+log(1-param[3]-param[2]^2/2)*param[4],param[2],param[3],param[4]) + } + } +} + + diff --git a/R/testparGAL.R b/R/testparGAL.R index 3065434..7759faa 100644 --- a/R/testparGAL.R +++ b/R/testparGAL.R @@ -9,7 +9,7 @@ #' Check for the validity of a parameter vector. Stop at error. #' @param param Parameter vector #' @param type Choose between "mu" or "kappa" parametrization -#' @param log Logical for log-parameters +#' @param log Logical for log-parameters #' @return logical #' #' @author Francois Pelletier diff --git a/man/cgfGAL.Rd b/man/cgfGAL.Rd new file mode 100644 index 0000000..0128340 --- /dev/null +++ b/man/cgfGAL.Rd @@ -0,0 +1,27 @@ +\name{cgfGAL} +\alias{cgfGAL} +\title{Cumulant generating function of GAL distribution} +\usage{ +cgfGAL(u, param, type = "mu", log = FALSE) +} +\arguments{ + \item{u}{Transform variate} + + \item{param}{Parameter vector} + + \item{type}{Choose between "mu" or "kappa" + parametrization} + + \item{log}{Logical for log-parameters} +} +\value{ +Cumulant generating function value at point u for given +parameter vector +} +\description{ +Cumulant generating function of GAL distribution +} +\author{ +Francois Pelletier +} + diff --git a/man/cmGAL.Rd b/man/cmGAL.Rd new file mode 100644 index 0000000..eb93c64 --- /dev/null +++ b/man/cmGAL.Rd @@ -0,0 +1,26 @@ +\name{cmGAL} +\alias{cmGAL} +\title{Centered moments of the GAL distribution} +\usage{ +cmGAL(order, param, type = "mu", log = FALSE) +} +\arguments{ + \item{order}{Order of raw moment} + + \item{param}{Parameter vector} + + \item{type}{Choose between "mu" or "kappa" + parametrization} + + \item{log}{Logical for log-parameters} +} +\value{ +A numeric value of the centered moment +} +\description{ +Centered moments of the GAL distribution +} +\author{ +Francois Pelletier +} + diff --git a/man/kurtosisGAL.Rd b/man/kurtosisGAL.Rd new file mode 100644 index 0000000..d0b0eb2 --- /dev/null +++ b/man/kurtosisGAL.Rd @@ -0,0 +1,26 @@ +\name{kurtosisGAL} +\alias{kurtosisGAL} +\title{(Adjusted) kurtosis of the GAL distribution} +\usage{ +kurtosisGAL(param, type = "mu", log = FALSE, adjust = TRUE) +} +\arguments{ + \item{param}{Parameter vector} + + \item{type}{Choose between "mu" or "kappa" + parametrization} + + \item{log}{Logical for log-parameters} + + \item{adjust}{Logical to use the adjusted kurtosis} +} +\value{ +A numeric value of the kurtosis +} +\description{ +(Adjusted) kurtosis of the GAL distribution +} +\author{ +Francois Pelletier +} + diff --git a/man/mGAL.Rd b/man/mGAL.Rd new file mode 100644 index 0000000..f0cb99b --- /dev/null +++ b/man/mGAL.Rd @@ -0,0 +1,26 @@ +\name{mGAL} +\alias{mGAL} +\title{Raw moments of the GAL distribution} +\usage{ +mGAL(order, param, type = "mu", log = FALSE) +} +\arguments{ + \item{order}{Order of raw moment} + + \item{param}{Parameter vector} + + \item{type}{Choose between "mu" or "kappa" + parametrization} + + \item{log}{Logical for log-parameters} +} +\value{ +A numeric value of the raw moment +} +\description{ +Raw moments of the GAL distribution +} +\author{ +Francois Pelletier +} + diff --git a/man/riskneutralparGAL.Rd b/man/riskneutralparGAL.Rd new file mode 100644 index 0000000..58372e0 --- /dev/null +++ b/man/riskneutralparGAL.Rd @@ -0,0 +1,26 @@ +\name{riskneutralparGAL} +\alias{riskneutralparGAL} +\title{Risk neutral conversion of parameters of GAL distirbution} +\usage{ +riskneutralparGAL(param, riskfree, type = "mu", log = FALSE) +} +\arguments{ + \item{param}{Parameter vector} + + \item{riskfree}{Risk-free rate} + + \item{type}{Choose between "mu" or "kappa" + parametrization} + + \item{log}{Logical for log-parameters} +} +\value{ +Risk neutral parameter vector +} +\description{ +Risk neutral conversion of parameters of GAL distirbution +} +\author{ +Francois Pelletier +} + diff --git a/man/skewnessGAL.Rd b/man/skewnessGAL.Rd new file mode 100644 index 0000000..feab8ad --- /dev/null +++ b/man/skewnessGAL.Rd @@ -0,0 +1,24 @@ +\name{skewnessGAL} +\alias{skewnessGAL} +\title{Skewness of the GAL distribution} +\usage{ +skewnessGAL(param, type = "mu", log = FALSE) +} +\arguments{ + \item{param}{Parameter vector} + + \item{type}{Choose between "mu" or "kappa" + parametrization} + + \item{log}{Logical for log-parameters} +} +\value{ +A numeric value of the skewness +} +\description{ +Skewness of the GAL distribution +} +\author{ +Francois Pelletier +} +