-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New distibution - Asymmetric Laplace. Crazy things are comming, which…
… are relevant to issue #13 !
- Loading branch information
Ivan Svetunkov
committed
Oct 2, 2018
1 parent
4088a9d
commit e06ffc2
Showing
13 changed files
with
268 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
Package: greybox | ||
Type: Package | ||
Title: Toolbox for Model Building and Forecasting | ||
Version: 0.3.2.41007 | ||
Date: 2018-09-30 | ||
Version: 0.3.2.41008 | ||
Date: 2018-10-02 | ||
Authors@R: person("Ivan", "Svetunkov", email = "[email protected]", role = c("aut", "cre"), | ||
comment="Lecturer at Centre for Marketing Analytics and Forecasting, Lancaster University, UK") | ||
URL: https://github.com/config-i1/greybox | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#' Asymmetric Laplace Distribution | ||
#' | ||
#' Density, cumulative distribution, quantile functions and random | ||
#' generation for the Asymmetric Laplace distribution with the | ||
#' location parameter mu, Mean Absolute Error (or Mean Absolute | ||
#' Deviation) equal to 2 b and asymmetry parameter alpha. | ||
#' | ||
#' When mu=0 and b=1, the Laplace distribution becomes standardized. | ||
#' The distribution has the following density function: | ||
#' | ||
#' f(x) = alpha (1-alpha) / b exp(-(x-mu)/b (alpha - I(x<=mu))), | ||
#' | ||
#' where I(.) is the indicator function (equal to 1 if the condition is | ||
#' satisfied and zero otherwise). | ||
#' | ||
#' When alpha=0.5, then the distribution becomes Symmetric Laplace, where | ||
#' b = 1/2 MAE. | ||
#' | ||
#' This distribution function aligns with the quantile estimates of | ||
#' parameters (Geraci & Bottai, 2007). | ||
#' | ||
#' Finally, both \code{palaplace} and \code{qalaplace} are returned for | ||
#' the lower tail of the distribution. | ||
#' | ||
#' @template author | ||
#' @keywords distribution | ||
#' | ||
#' @param q vector of quantiles. | ||
#' @param p vector of probabilities. | ||
#' @param n number of observations. Should be a single number. | ||
#' @param mu vector of location parameters (means). | ||
#' @param b vector of scale parameters. | ||
#' @param alpha value of asymmetry parameter. Varies from 0 to 1. | ||
#' @param log if \code{TRUE}, then probabilities are returned in | ||
#' logarithms. | ||
#' | ||
#' @return Depending on the function, various things are returned | ||
#' (usually either vector or scalar): | ||
#' \itemize{ | ||
#' \item \code{dalaplace} returns the density function value for the | ||
#' provided parameters. | ||
#' \item \code{palaplace} returns the value of the cumulative function | ||
#' for the provided parameters. | ||
#' \item \code{qalaplace} returns quantiles of the distribution. Depending | ||
#' on what was provided in \code{p}, \code{mu} and \code{b}, this | ||
#' can be either a vector or a matrix, or an array. | ||
#' \item \code{ralaplace} returns a vector of random variables | ||
#' generated from the Laplace distribution. Depending on what was | ||
#' provided in \code{mu} and \code{b}, this can be either a vector | ||
#' or a matrix or an array. | ||
#' } | ||
#' | ||
#' @examples | ||
#' x <- dalaplace(c(-100:100)/10, 0, 1, 0.2) | ||
#' plot(x, type="l") | ||
#' | ||
#' x <- palaplace(c(-100:100)/10, 0, 1, 0.2) | ||
#' plot(x, type="l") | ||
#' | ||
#' qalaplace(c(0.025,0.975), 0, c(1,2), c(0.2,0.3)) | ||
#' | ||
#' x <- ralaplace(1000, 0, 1, 0.2) | ||
#' hist(x) | ||
#' | ||
#' @references \itemize{ | ||
#' \item Geraci Marco, Bottai Matteo (2007). Quantile regression for | ||
#' longitudinal data using the asymmetric Laplace distribution. | ||
#' Biostatistics (2007), 8, 1, pp. 140-154 | ||
#' \url{https://doi.org/10.1093/biostatistics/kxj039} | ||
#' \item Yu, K., & Zhang, J. (2005). A three-parameter asymmetric | ||
#' laplace distribution and its extension. Communications in Statistics | ||
#' - Theory and Methods, 34, 1867-1879. | ||
#' \url{https://doi.org/10.1080/03610920500199018} | ||
#' } | ||
#' | ||
#' @rdname alaplace-distribution | ||
|
||
#' @rdname alaplace-distribution | ||
#' @export dalaplace | ||
#' @aliases dalaplace | ||
dalaplace <- function(q, mu=0, b=1, alpha=0.5, log=FALSE){ | ||
e <- q-mu | ||
alaplaceReturn <- alpha * (1-alpha) / b * exp(-(e)/b * (alpha - ((e) <= 0)*1)); | ||
if(log){ | ||
alaplaceReturn <- log(alaplaceReturn); | ||
} | ||
return(alaplaceReturn); | ||
} | ||
|
||
#' @rdname alaplace-distribution | ||
#' @export palaplace | ||
#' @aliases palaplace | ||
palaplace <- function(q, mu=0, b=1, alpha=0.5){ | ||
Ie <- (q<=mu)*1; | ||
alaplaceReturn <- 1 - Ie - (1 - Ie - alpha) * exp((Ie - alpha) / b * (q-mu)); | ||
|
||
return(alaplaceReturn); | ||
} | ||
|
||
#' @rdname alaplace-distribution | ||
#' @export qalaplace | ||
#' @aliases qalaplace | ||
qalaplace <- function(p, mu=0, b=1, alpha=0.5){ | ||
Ie <- (p<=alpha)*1; | ||
alaplaceReturn <- mu + b / (Ie - alpha) * log((1-Ie-p)/(1-Ie-alpha)); | ||
if(any(p==0)){ | ||
alaplaceReturn[p==0] <- -Inf; | ||
} | ||
if(any(p==1)){ | ||
alaplaceReturn[p==1] <- Inf; | ||
} | ||
|
||
return(alaplaceReturn); | ||
} | ||
|
||
#' @rdname alaplace-distribution | ||
#' @export ralaplace | ||
#' @aliases ralaplace | ||
ralaplace <- function(n=1, mu=0, b=1, alpha=0.5){ | ||
alaplaceReturn <- qalaplace(runif(n,0,1),mu,b,alpha); | ||
return(alaplaceReturn); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.