# R/ALaD.R In QBAsyDist: Asymmetric Distributions and Quantile Estimation

#' @title Quantile-based asymmetric Laplace distribution
#' @description Density, cumulative distribution function, quantile function and random sample generation
#' for the quantile-based asymmetric Laplace distribution (ALaD) discussed in Yu and Zhang (2005) and Gijbels et al. (2019a).
#' @param y,q These are each a vector of quantiles.
#' @param mu This is the location parameter \eqn{\mu}.
#' @param phi This is the scale parameter  \eqn{\phi}.
#' @param alpha This is the index parameter  \eqn{\alpha}.
#' @param n This is the number of observations, which must be a positive integer that has length 1.
#' @param beta This is a vector of probabilities.
#'
#' The length of the result is determined by \eqn{n} for \code{\link{rALaD}}, and is the maximum of the lengths of the numerical arguments for the other functions.
#'
#' @references{
#'  Gijbels, I., Karim, R. and Verhasselt, A. (2019a). On quantile-based asymmetric family of distributions: properties and inference. \emph{International Statistical Review}, \url{https://doi.org/10.1111/insr.12324}.
#'
#'
#'  Yu., K, and Zhang, J. (2005). A three-parameter asymmetric Laplace distribution and its extension. \emph{Communications in Statistics–Theory and Methods}, \bold{34}(9-10), 1867–1879.
#' }
#'
#'
#'
NULL
#'
#' @export

#' @export

#' @examples
#' # Density
#' rnum<-rnorm(100)
#'
#' # Distribution function
#'
#' # Quantile function
#' beta<-c(0.25,0.5,0.75)
#'
#' # random sample generation
#'
#'
#' @import stats
#' @export

#' @import stats
#' @export
u=runif(n, min = 0, max = 1)
y=ifelse(u>alpha,mu-(phi/alpha)*log((1-u)/(1-alpha)),mu+phi/(1-alpha)*log(u/alpha));
return(y)
}

#' @title Moments estimation for the quantile-based asymmetric Laplace distribution.
#' @description Mean, variance, skewness, kurtosis and moments about the location parameter (i.e., \eqn{\alpha}th quantile) of the quantile-based asymmetric Laplace distribution studied in Gijbels et al. (2019a) useful for quantile regression with location parameter equal to \eqn{\mu}, scale parameter \eqn{\phi} and index parameter \eqn{\alpha}.
#' @param mu This is the location parameter \eqn{\mu}.
#' @param phi This is the scale parameter  \eqn{\phi}.
#' @param alpha This is the index parameter  \eqn{\alpha}.
#'
#'
#' @references{
#'  Gijbels, I., Karim, R. and Verhasselt, A. (2019a). On quantile-based asymmetric family of distributions: properties and inference. \emph{International Statistical Review}, \url{https://doi.org/10.1111/insr.12324}.
#' }
#'
NULL
#' @export

#' @export

#' @export
nu<-(1-2*alpha)*(1-2*alpha)^2*2
deno<-((1-2*alpha)^2+alpha*(1-alpha)*2)^(3/2)
skew<-(nu/deno)
return(skew)
}

#'
#' @export
nu<-((1-alpha)^5+alpha^5)*24-(1-2*alpha)^2*(4*(1-2*alpha+2*alpha^2)*6-6*(1-3*alpha+3*alpha^2)*2+3*(1-2*alpha)^2)
deno<-((1-2*alpha)^2+alpha*(1-alpha)*2)^(2)
kurto<-(nu/deno)
return(kurto)
}

#' @param r This is a value which is used to calculate \eqn{r}th moment about \eqn{\mu}.
#' @import stats
#' @examples
#' # Example
mu.r<-2*stats::integrate(integrand, lower = 0, upper = Inf)$value return(phi^r*((1-alpha)^(r+1)+(-1)^r*alpha^(r+1))*mu.r/(alpha^r*(1-alpha)^r)) } #' @title Method of moments (MoM) estimation for the quantile-based asymmetric Laplace distribution. #' @description Parameter estimation in the quantile-based asymmetric Laplace distribution by using method of moments is studied in Gijbels et al. (2019a). #' @param y This is a vector of quantiles. #' @param alpha This is the index parameter \eqn{\alpha}. If \eqn{\alpha} is unknown, the it should be NULL which is default option. In this case, the sample skewness will be used to estimate \eqn{\alpha}. If \eqn{\alpha} is known, then the value of \eqn{\alpha} has to be specified in the function. #' #' #' #' @import stats #' @return \code{\link{momALaD}} provides the method of moments estimates of the unknown parameters of the distribution. #' #' #' @references{ #' Gijbels, I., Karim, R. and Verhasselt, A. (2019a). On quantile-based asymmetric family of distributions: properties and inference. \emph{International Statistical Review}, \url{https://doi.org/10.1111/insr.12324}. #' } #' #' @name momALaD NULL #' @rdname momALaD #' @examples #' # Example #' y=rnorm(100) #' momALaD(y=y,alpha=0.5) # If alpha is known with alpha=0.5 #' momALaD(y=y) # If alpha is unknown #' #' @export momALaD<-function(y,alpha=NULL){ muMoM<-function(y,f,alpha=NULL){ if (is.null(alpha)){ m1<-mean(y) m2<-sum(y^2)/length(y) cm2<-sum((y-m1)^2)/length(y) cm3<-sum((y-m1)^3)/length(y) mu1<-1 mu2<-2 mu3<-6 diffskew<-matrix(NA,99,2) for (al.i in 1:99){ al=al.i/100 nu<-(1-2*al)*((1-2*al)^2*(mu3-3*mu1*mu2+2*mu1^3)+al*(1-al)*(2*mu3-3*mu1*mu2)) deno<-((1-2*al)^2*(mu2-mu1^2)+al*(1-al)*mu2)^(3/2) skewPop<-(nu/deno) diffskew[al.i,1]<-abs(skewPop- cm3/cm2^(3/2)) diffskew[al.i,2]<-al} alpha<-diffskew[diffskew[,1]== min( diffskew[,1]), ][2] k1<-(1-2*alpha)*mu1/(alpha*(1-alpha)) k2<-((1-alpha)^3+alpha^3)*mu2/(alpha^2*(1-alpha)^2) mu<-m1-(k1/sqrt(k2-k1^2))*sqrt(m2-m1^2) } else { m1<-mean(y) m2<-sum(y^2)/length(y) mu1<-1 mu2<-2 k1<-(1-2*alpha)*mu1/(alpha*(1-alpha)) k2<-((1-alpha)^3+alpha^3)*mu2/(alpha^2*(1-alpha)^2) mu<-m1-(k1/sqrt(k2-k1^2))*sqrt(m2-m1^2) } return(mu) } phiMoM<-function(y,alpha=NULL){ if (is.null(alpha)){ m1<-mean(y) m2<-sum(y^2)/length(y) cm2<-sum((y-m1)^2)/length(y) cm3<-sum((y-m1)^3)/length(y) mu1<-1 mu2<-2 mu3<-6 diffskew<-matrix(NA,99,2) for (al.i in 1:99){ al=al.i/100 nu<-(1-2*al)*((1-2*al)^2*(mu3-3*mu1*mu2+2*mu1^3)+al*(1-al)*(2*mu3-3*mu1*mu2)) deno<-((1-2*al)^2*(mu2-mu1^2)+al*(1-al)*mu2)^(3/2) skewPop<-(nu/deno) diffskew[al.i,1]<-abs(skewPop- cm3/cm2^(3/2)) diffskew[al.i,2]<-al} alpha<-diffskew[diffskew[,1]== min( diffskew[,1]), ][2] k1<-(1-2*alpha)*mu1/(alpha*(1-alpha)) k2<-((1-alpha)^3+alpha^3)*mu2/(alpha^2*(1-alpha)^2) phi<-(1/sqrt(k2-k1^2))*sqrt(m2-m1^2) } else { m1<-mean(y) m2<-sum(y^2)/length(y) mu1<-1 mu2<-2 k1<-(1-2*alpha)*mu1/(alpha*(1-alpha)) k2<-((1-alpha)^3+alpha^3)*mu2/(alpha^2*(1-alpha)^2) phi<-(1/sqrt(k2-k1^2))*sqrt(m2-m1^2) } return(phi) } alphaMoM<-function(y){ m1<-mean(y) cm2<-sum((y-m1)^2)/length(y) cm3<-sum((y-m1)^3)/length(y) mu1<-1 mu2<-2 mu3<-6 diffskew<-matrix(NA,99,2) for (al.i in 1:99){ al=al.i/100 nu<-(1-2*al)*((1-2*al)^2*(mu3-3*mu1*mu2+2*mu1^3)+al*(1-al)*(2*mu3-3*mu1*mu2)) deno<-((1-2*al)^2*(mu2-mu1^2)+al*(1-al)*mu2)^(3/2) skewPop<-(nu/deno) diffskew[al.i,1]<-abs(skewPop- cm3/cm2^(3/2)) diffskew[al.i,2]<-al } return(diffskew[diffskew[,1]== min( diffskew[,1]), ][2]) } if (is.null(alpha)){ list(mu.MoM=muMoM(y),phi.MoM=phiMoM(y),alpha.MoM=alphaMoM(y)) } else { list(mu.MoM=muMoM(y),phi.MoM=phiMoM(y))} } #' @title Log-likelihood function for the quantile-based asymmetric Laplace distribution. #' @description Log-Likelihood function \eqn{\ell_n(\mu,\phi,\alpha)=\ln[L_n(\mu,\phi,\alpha)]} #' of the quantile-based asymmetric Laplace distribution discussed in Gijbels et al. (2019a). #' @param y This is a vector of quantiles. #' @param mu This is the location parameter \eqn{\mu}. #' @param phi This is the scale parameter \eqn{\phi}. #' @param alpha This is the index parameter \eqn{\alpha}. #' @return \code{\link{LogLikALaD}} provides the value of the Log-likelihood function of the quantile-based asymmetric Laplace distribution. #' @references{ #' Gijbels, I., Karim, R. and Verhasselt, A. (2019a). On quantile-based asymmetric family of distributions: properties and inference. \emph{International Statistical Review}, \url{https://doi.org/10.1111/insr.12324}. #' } #' #' @examples #' # Example #' y<-rnorm(100) #' LogLikALaD(y,mu=0,phi=1,alpha=0.5) #' #' @export LogLikALaD<- function(y,mu,phi,alpha){ LL<-log(dALaD(y,mu,phi,alpha)) return(sum(LL[!is.infinite(LL)])) } #' @title Maximum likelihood estimation (MLE) for the quantile-based asymmetric Laplace distribution. #' @description The log-likelihood function \eqn{\ell_n(\mu,\phi,\alpha)=\ln[L_n(\mu,\phi,\alpha)]} #' and parameter estimation of \eqn{ \theta=(\mu,\phi,\alpha)} in the quantile-based asymmetric Laplace distribution #' by using the maximum likelihood estimation are discussed in Gijbels et al. (2019a). #' See also in Yu and Zhang (2005). The linear programing (LP) algorithm is used to obtain a solution to the maximization problem. #' The LP algorithm can be found in Koenker (2005). See also \code{\link[ald:mleALD]{mleALD}} in the Package \pkg{ald}. #' @param y This is a vector of quantiles. #' @return The maximum likelihood estimate of parameter \eqn{ \theta=(\mu,\phi,\alpha)} of the quantile-based asymmetric Laplace distribution. #' @import zipfR #' @import ald #' #' @references{ #' Gijbels, I., Karim, R. and Verhasselt, A. (2019a). On quantile-based asymmetric family of distributions: properties and inference. \emph{International Statistical Review}, \url{https://doi.org/10.1111/insr.12324}. #' #' #' Koenker, R. (2005). \emph{Quantile Regression}. Cambridge University Press. #' #' #' Yu., K, and Zhang, J. (2005). A three-parameter asymmetric Laplace distribution and its extension. \emph{Communications in Statistics–Theory and Methods}, \bold{34}(9-10), 1867–1879. #' #' } #' #' @name mleALaD NULL #' @rdname mleALaD #' @examples #' ## Example: #' y=rnorm(100) #' mleALaD(y) #' #' @export mleALaD<-function(y){ y<-sort(y) fit.ALD<-ald::mleALD(y, initial = NA) LogLik.ALaD<-sum(log(dALaD(y, mu = fit.ALD$par[1], phi = fit.ALD$par[2], alpha =fit.ALD$par[3])))
return(list(LogLikelihood.ALaD=round(LogLik.ALaD,4),mu.ALaD=round(fit.ALD$par[1],4),phi.ALaD=round(fit.ALD$par[2],4),alpha.ALaD=round(fit.ALD\$par[3],4)))