stdt: Standardized Student t Distribution

stdtR Documentation

Standardized Student t Distribution

Description

These functions calculate and differentiate a cumulative distribution function and density function of the standardized (to zero mean and unit variance) Student distribution. Quantile function and random numbers generator are also provided.

Usage

dt0(x, df = 10, log = FALSE, grad_x = FALSE, grad_df = FALSE)

pt0(x, df = 10, log = FALSE, grad_x = FALSE, grad_df = FALSE, n = 10L)

rt0(n = 1L, df = 10)

qt0(x = 1L, df = 10)

Arguments

x

numeric vector of quantiles.

df

positive real value representing the number of degrees of freedom. Since this function deals with standardized Student distribution, argument df should be greater than 2 because otherwise variance is undefined.

log

logical; if TRUE then probabilities (or densities) p are given as log(p) and derivatives will be given respect to log(p).

grad_x

logical; if TRUE then function returns a derivative respect to x.

grad_df

logical; if TRUE then function returns a derivative respect to df.

n

positive integer. If rt0 function is used then this argument represents the number of random draws. Otherwise n states for the number of iterations used to calculate the derivatives associated with pt0 function via pbetaDiff function.

Details

Standardized (to zero mean and unit variance) Student distribution has the following density and cumulative distribution functions:

f(x) = \frac{\Gamma\left(\frac{v + 1}{2}\right)}{ \sqrt{(v - 2)\pi}\Gamma\left(\frac{v}{2}\right)} \left(1 + \frac{x^2}{v - 2}\right)^{-\frac{v+1}{2}},

F(x) = \begin{cases} 1 - \frac{1}{2}I(\frac{v - 2}{x^2 + v - 2}, \frac{v}{2}, \frac{1}{2})\text{, if }x \geq 0\\ \frac{1}{2}I(\frac{v - 2}{x^2 + v - 2}, \frac{v}{2}, \frac{1}{2})\text{, if }x < 0 \end{cases},

where v > 2 is the number of degrees of freedom df and I(.) is a cumulative distribution function of beta distribution which is calculated by pbeta function.

Value

Function rt0 returns a numeric vector of random numbers. Function qt0 returns a numeric vector of quantiles. Functions pt0 and dt0 return a list which may contain the following elements:

  • prob - numeric vector of probabilities calculated for each element of x. Exclusively for pt0 function.

  • den - numeric vector of densities calculated for each each element of x. Exclusively for dt0 function.

  • grad_x - numeric vector of derivatives respect to p for each element of x. This element appears only if input argument grad_x is TRUE.

  • grad_df - numeric vector of derivatives respect to q for each element of x. This element appears only if input argument grad_df is TRUE.

Examples

# Simple examples
pt0(x = 0.3, df = 10, log = FALSE, grad_x = TRUE, grad_df = TRUE)
dt0(x = 0.3, df = 10, log = FALSE, grad_x = TRUE, grad_df = TRUE)
qt0(x = 0.3, df = 10)

# Compare analytical and numeric derivatives
delta <- 1e-6
x <- c(-2, -1, 0, 1, 2)
df <- 5

# For probabilities
out <- pt0(x, df = df, grad_x = TRUE, grad_df = TRUE)
p0 <- out$prob
  # grad_x
p1 <- pt0(x + delta, df = df)$prob
data.frame(numeric = (p1 - p0) / delta, analytical = out$grad_x)
  # grad_df
p1 <- pt0(x, df = df + delta)$prob
data.frame(numeric = (p1 - p0) / delta, analytical = out$grad_df)

# For densities
out <- dt0(x, df = df, grad_x = TRUE, grad_df = TRUE)
p0 <- out$den
  # grad_x
p1 <- dt0(x + delta, df = df)$den
data.frame(numeric = (p1 - p0) / delta, analytical = out$grad_x)
  # grad_df
p1 <- dt0(x, df = df + delta)$den
data.frame(numeric = (p1 - p0) / delta, analytical = out$grad_df)


mnorm documentation built on Aug. 22, 2023, 9:12 a.m.

Related to stdt in mnorm...