dwishart: Wishart distribution

View source: R/wishart.R

dwishart_cppR Documentation

Wishart distribution

Description

The function dwishart() computes the density of a Wishart distribution.

The function rwishart() samples from a Wishart distribution.

The functions with suffix ⁠_cpp⁠ perform no input checks, hence are faster.

Usage

dwishart_cpp(x, df, scale, log = FALSE, inv = FALSE)

rwishart_cpp(df, scale, inv = FALSE)

dwishart(x, df, scale, log = FALSE, inv = FALSE)

rwishart(df, scale, inv = FALSE)

Arguments

x

[matrix()]
A covariance matrix of dimension p.

df

[integer()]
The degrees of freedom greater of equal p.

scale

[matrix()]
The scale covariance matrix of dimension p.

log

[logical(1)]
Return the logarithm of the density value?

inv

[logical(1)]
Use this inverse Wishart distribution?

Value

For dwishart(): The density value.

For rwishart(): A matrix, the random draw.

See Also

Other simulation helpers: Simulator, correlated_regressors(), ddirichlet_cpp(), dmixnorm_cpp(), dmvnorm_cpp(), dtnorm_cpp(), gaussian_tv(), simulate_markov_chain()

Examples

x <- diag(2)
df <- 6
scale <- matrix(c(1, -0.3, -0.3, 0.8), ncol = 2)

# compute density
dwishart(x = x, df = df, scale = scale)
dwishart(x = x, df = df, scale = scale, log = TRUE)
dwishart(x = x, df = df, scale = scale, inv = TRUE)

# sample
rwishart(df = df, scale = scale)
rwishart(df = df, scale = scale, inv = TRUE)

# expectation of Wishart is df * scale
n <- 100
replicate(n, rwishart(df = df, scale = scale), simplify = FALSE) |>
  Reduce(f = "+") / n
df * scale

# expectation of inverse Wishart is scale / (df - p - 1)
n <- 100
replicate(n, rwishart(df = df, scale = scale, TRUE), simplify = FALSE) |>
  Reduce(f = "+") / n
scale / (df - 2 - 1)

oeli documentation built on Aug. 18, 2025, 5:24 p.m.