Helpful Stan Functions
Skew Generalized T distribution functions

Functions

real variance_adjusted_sgt (real sigma, real lambda, real p, real q)
 
vector mean_centered_sgt (vector x, real sigma, real lambda, real p, real q)
 
real mean_centered_sgt (real x, real sigma, real lambda, real p, real q)
 
real mean_centered_sgt (real x, real sigma, real lambda, real q)
 
real skew_generalized_t_lpdf (vector x, real mu, real sigma, real lambda, real p, real q)
 
real skew_t_lpdf (vector x, real mu, real sigma, real lambda, real q)
 
real generalized_t_lpdf (vector x, real mu, real sigma, real p, real q)
 
real skew_generalized_t_lcdf (real x, real mu, real sigma, real lambda, real p, real q)
 

Detailed Description

From the sgt R package Carter Davis (2015). sgt: Skewed Generalized T Distribution Tree. R package version 2.0. https://CRAN.R-project.org/package=sgt

The Skewed Generalized T Distribution is a univariate 5-parameter distribution introuced by Theodossiou (1998) and known for its extreme flexibility. Special and limiting cases of the SGT distribution include the skewed generalized error distribution, the generalized t distribution introduced by McDonald and Newey (1988), the skewed t proposed by Hansen (1994), the skewed Laplace distribution, the generalized error distribution (also known as the generalized normal distribution), the skewed normal distribution, the student t distribution, the skewed Cauchy distribution, the Laplace distribution, the uniform distribution, the normal distribution, and the Cauchy distribution.

Hansen, B. E., 1994, Autoregressive Conditional Density Estimation, International Economic Review 35, 705-730.

Hansen, C., J. B. McDonald, and W. K. Newey, 2010, Instrumental Variables Estimation with Flexible Distribution sigma Journal of Business and Economic Statistics 28, 13-25.

McDonald, J. B. and W. K. Newey, 1988, Partially Adaptive Estimation of Regression Models via the Generalized t Distribution, Econometric Theory 4, 428-457.

Theodossiou, Panayioti sigma 1998, Financial Data and the Skewed Generalized T Distribution, Management Science 44, 1650-166

real variance_adjusted_sgt(real sigma, real lambda, real p, real q) {
if (p * q <= 2)
reject("p * q must be > 2 found p * q = ", p * q);
if (is_inf(q))
return sigma
* inv_sqrt((pi() * (1 + 3 * lambda ^ 2) * tgamma(3.0 / p)
- 16 ^ (1.0 / p) * lambda ^ 2 * (tgamma(1.0 / 2 + 1.0 / p)) ^ 2
* tgamma(1.0 / p))
/ (pi() * tgamma(1.0 / p)));
return sigma
/ (q ^ (1.0 / p)
* sqrt((3 * lambda ^ 2 + 1) * (beta(3.0 / p, q - 2.0 / p) / beta(1.0 / p, q))
- 4 * lambda ^ 2 * (beta(2.0 / p, q - 1.0 / p) / beta(1.0 / p, q)) ^ 2));
}
vector mean_centered_sgt(vector x, real sigma, real lambda, real p, real q) {
if (p * q <= 1)
reject("p * q must be > 1 found p * q = ", p * q);
if (is_inf(q))
return x + (2 ^ (2.0 / p) * sigma * lambda * tgamma(1.0 / 2 + 1.0 / p)) / sqrt(pi());
return x
+ (2 * sigma * lambda * q ^ (1.0 / p) * beta(2 / p, q - 1.0 / p))
/ beta(1.0 / p, q);
}
real mean_centered_sgt(real x, real sigma, real lambda, real p, real q) {
if (p * q <= 1)
reject("p * q must be > 1 found p * q = ", p * q);
if (is_inf(q))
return x + (2 ^ (2.0 / p) * sigma * lambda * tgamma(1.0 / 2 + 1.0 / p)) / sqrt(pi());
return x
+ (2 * sigma * lambda * q ^ (1.0 / p) * beta(2 / p, q - 1.0 / p))
/ beta(1.0 / p, q);
}
real mean_centered_sgt(real x, real sigma, real lambda, real q) {
if (q <= 1)
reject("q must be > 1 found q = ", q);
if (is_inf(q))
return x + (4 * sigma * lambda * tgamma(1.0 / 2 + 1.0)) / sqrt(pi());
return x + (2 * sigma * lambda * q * beta(2, q - 1.0)) / beta(1.0, q);
}
real skew_generalized_t_lpdf(vector x, real mu, real sigma, real lambda, real p, real q) {
if (sigma <= 0)
reject("sigma must be > 0 found sigma = ", sigma);
if (lambda >= 1 || lambda <= -1)
reject("lambda must be between (-1, 1) found lambda = ", lambda);
if (p <= 0)
reject("p must be > 0 found p = ", p);
if (q <= 0)
reject("q must be > 0 found q = ", q);
int N = num_elements(x);
real out = 0;
real sigma_adj = variance_adjusted_sgt(sigma, lambda, p, q);
if (is_inf(q) && is_inf(p))
return uniform_lpdf(x | mu - sigma_adj, mu + sigma_adj);
vector[N] r = mean_centered_sgt(x, sigma_adj, lambda, p, q) - mu;
vector[N] s;
for (n in 1 : N)
s[n] = r[n] < 0 ? -1 : 1;
if (is_inf(q) && !is_inf(p)) {
out = sum((abs(r) ./ (sigma_adj * (1 + lambda * s))) ^ p);
return log(p) - log(2) - log(sigma_adj) - lgamma(1.0 / p) - out;
} else {
out = sum(log1p(abs(r) ^ p ./ (q * sigma_adj ^ p * pow(1 + lambda * s, p))));
}
return N * (log(p) - log2() - log(sigma_adj) - log(q) / p - lbeta(1.0 / p, q))
- (1.0 / p + q) * out;
}
real skew_t_lpdf(vector x, real mu, real sigma, real lambda, real q) {
return skew_generalized_t_lpdf(x | mu, sigma, lambda, 2, q);
}
real generalized_t_lpdf(vector x, real mu, real sigma, real p, real q) {
return skew_generalized_t_lpdf(x | mu, sigma, 0, p, q);
}
real skew_generalized_t_lcdf(real x, real mu, real sigma, real lambda, real p, real q) {
if (sigma <= 0)
reject("sigma must be > 0 found sigma = ", sigma);
if (lambda >= 1 || lambda <= -1)
reject("lambda must be between (-1, 1) found lambda = ", sigma);
if (p <= 0)
reject("p must be > 0 found p = ", p);
if (q <= 0)
reject("q must be > 0 found q = ", q);
if (is_inf(x) && x < 0)
return 0;
if (is_inf(x) && x > 0)
return 1;
real sigma_adj = variance_adjusted_sgt(sigma, lambda, p, q);
real x_cent = mean_centered_sgt(x, sigma_adj, lambda, p, q);
real r = x_cent - mu;
real lambda_new;
real r_new;
if (r > 0) {
lambda_new = -lambda;
r_new = -r;
} else {
lambda_new = lambda;
r_new = r;
}
if (!is_inf(p) && is_inf(q) && !is_inf(x))
return log_sum_exp([log1m(lambda_new) + log2(),
log(lambda_new - 1) + log2()
+ beta_lcdf((r_new / (sigma * (1 + lambda_new))) ^ p | p, 1)]);
if (is_inf(p) && !is_inf(x))
return uniform_lcdf(x | mu, sigma);
if (is_inf(x) && x < 0)
return 0;
if (is_inf(x) && x > 0)
return 1;
return log_sum_exp([log1m(lambda_new) + log2(),
log(lambda_new - 1) + log2()
+ beta_lcdf(1.0
/ (1 + q * (sigma * (1 - lambda_new) / (-r_new)) ^ p) | 1.0
/ p, q)]);
}
real variance_adjusted_sgt(real sigma, real lambda, real p, real q)
Definition: skew_generalized_t.stanfunctions:44
real generalized_t_lpdf(vector x, real mu, real sigma, real p, real q)
Definition: skew_generalized_t.stanfunctions:206
real skew_generalized_t_lcdf(real x, real mu, real sigma, real lambda, real p, real q)
Definition: skew_generalized_t.stanfunctions:222
real skew_generalized_t_lpdf(vector x, real mu, real sigma, real lambda, real p, real q)
Definition: skew_generalized_t.stanfunctions:142
vector mean_centered_sgt(vector x, real sigma, real lambda, real p, real q)
Definition: skew_generalized_t.stanfunctions:74
real skew_t_lpdf(vector x, real mu, real sigma, real lambda, real q)
Definition: skew_generalized_t.stanfunctions:191

Function Documentation

◆ generalized_t_lpdf()

real generalized_t_lpdf ( vector  x,
real  mu,
real  sigma,
real  p,
real  q 
)

The Generalized T distribution

Author
Sean Pinkney
Parameters
xVector
muReal
sigmaReal \(\in (0, \infty)\) scale parameter
qReal \(\in (0, \infty)\) kurtosis parameter
Returns
log probability

◆ mean_centered_sgt() [1/3]

real mean_centered_sgt ( real  x,
real  sigma,
real  lambda,
real  p,
real  q 
)

◆ mean_centered_sgt() [2/3]

real mean_centered_sgt ( real  x,
real  sigma,
real  lambda,
real  q 
)

◆ mean_centered_sgt() [3/3]

vector mean_centered_sgt ( vector  x,
real  sigma,
real  lambda,
real  p,
real  q 
)

Skew Generalized T Center Mean

Centers the mean around mu, otherwise mu is the mode.

Author
Sean Pinkney
Parameters
sigmaReal \(\in (0, \infty)\) scale parameter
lambdaReal \(-1 < \lambda < 1\)
pReal \(\in (0, \infty)\) kurtosis parameter
qReal \(\in (0, \infty)\) kurtosis parameter
Exceptions
rejectif \( pq \leq 1 \)
Returns
rescaled x

◆ skew_generalized_t_lcdf()

real skew_generalized_t_lcdf ( real  x,
real  mu,
real  sigma,
real  lambda,
real  p,
real  q 
)

Skew Generalized T log cumulative density function

Author
Sean Pinkney
Parameters
xReal
muReal
sigmaReal \(\in (0, \infty)\) scale parameter
lambdaReal \(-1 < \lambda < 1\)
pReal \(\in (0, \infty)\) kurtosis parameter
qReal \(\in (0, \infty)\) kurtosis parameter
Returns
log probability

◆ skew_generalized_t_lpdf()

real skew_generalized_t_lpdf ( vector  x,
real  mu,
real  sigma,
real  lambda,
real  p,
real  q 
)

The Skewed Generalized T distribution is defined as

\[ f_{SGT}(x; \mu, \sigma, \lambda, p, q) = \frac{p}{2 v \sigma q^{1/p} B(\frac{1}{p},q) \left(\frac{| x-\mu + m |^p}{q (v \sigma)^p (\lambda sign(x-\mu + m)+1)^p}+1\right)^{\frac{1}{p}+q}} \]

where \(B\) is the beta function, \( \mu \) is the location parameter, \(\sigma > 0\) is the scale parameter, \(-1 < \lambda < 1\) is the skewness parameter, and \(p > 0\) and \(q > 0\) are the parameters that control the kurtosis. \(m\) and \(v\) are not parameter sigma
but functions of the other parameters that are used here to scale or shift the distribution appropriately to match the various parameterizations of this distribution.

In the original parameterization Theodossiou of the skewed generalized t distribution,

\[ m = \frac{2 v \sigma \lambda q^{\frac{1}{p}} B(\frac{2}{p},q-\frac{1}{p})}{B(\frac{1}{p},q)} \]

and

\[ v = \frac{q^{-\frac{1}{p}}}{\sqrt{ (3 \lambda^2 + 1) \frac{ B ( \frac{3}{p}, q - \frac{2}{p} )}{B (\frac{1}{p}, q )} -4 \lambda^2 \frac{B ( \frac{2}{p}, q - \frac{1}{p} )^2}{ B (\frac{1}{p}, q )^2}}}. \]

Author
Sean Pinkney
Parameters
xVector
muReal
sigmaReal \(\in (0, \infty)\) scale parameter
lambdaReal \(-1 < \lambda < 1\)
pReal \(\in (0, \infty)\) kurtosis parameter
qReal \(\in (0, \infty)\) kurtosis parameter
Returns
log probability

◆ skew_t_lpdf()

real skew_t_lpdf ( vector  x,
real  mu,
real  sigma,
real  lambda,
real  q 
)

The Skewed T distribution

Author
Sean Pinkney
Parameters
xVector
muReal
sigmaReal \(\in (0, \infty)\) scale parameter
lambdaReal \(-1 < \lambda < 1\)
qReal \(\in (0, \infty)\) kurtosis parameter
Returns
log probability

◆ variance_adjusted_sgt()

real variance_adjusted_sgt ( real  sigma,
real  lambda,
real  p,
real  q 
)

Skew Generalized T Rescale Sigma to be Variance

Author
Sean Pinkney
Parameters
sigmaReal \(\in (0, \infty)\) scale parameter
lambdaReal \(-1 < \lambda < 1\)
pReal \(\in (0, \infty)\) kurtosis parameter
qReal \(\in (0, \infty)\) kurtosis parameter
Exceptions
rejectif \( pq \leq 2 \)
Returns
rescaled sigma