sns.check.logdensity | R Documentation |
Utility function for validating log-density: 1) dimensional consistency of function argument, gradient and Hessian, 2) finiteness of function, gradient and Hessian, 3) closeness of analytical and numerical derivatives, and 4) negative definiteness of Hessian.
sns.check.logdensity(x, fghEval , numderiv.method = c("Richardson", "complex") , numderiv.args = list() , blocks = append(list(1:length(x)), as.list(1:length(x))) , dx = rep(1, length(x)), nevals = 100, negdef.tol = 1e-08, ...) ## S3 method for class 'sns.check.logdensity' print(x, ...)
x |
For |
fghEval |
Log-density to be validated. A valid log-density can have one of 3 forms: 1) return log-density, but no gradient or Hessian, 2) return a list of |
numderiv.method |
Method used for numeric differentiation. This is passed to the |
numderiv.args |
Arguments to the numeric differentiation method chosen in |
blocks |
A list of state space subsets (identified by their positional indexes), for which negative-definiteness of Hessian blocks are to be tested. The default is to test for 1) entire state space, and 2) each dimension individually. |
dx |
A vector of same length as |
nevals |
Number of points in state space, for which validation tests will be performed. |
negdef.tol |
Lower bound for absolute value of (negative) eigenvalues of Hessian, evaluated at each of the |
... |
Other arguments to be passed to |
sns.check.logdensity
returns a list of class sns.check.logdensity
, with the following elements:
check.ld.struct |
Boolean flag, indicating whether log-density |
numderiv |
Integer with values of |
check.length.g |
Boolean flag, indicating whether length of gradient vector (element |
check.dim.h |
Boolean flag, indicating whether number of rows and columns of the Hessian matrix (element |
x.mat |
Collection of state space vectors (one per row), for which validation tests are performed. It has |
t.evals |
Time spent on evaluating |
t.num.evals |
Time spent on evaluating the numeric version of |
f.vec |
Vector of log-density values for state space vectors listed in |
g.mat.num |
Collection of numerically-computed gradient vectors for state space values listed in |
is.g.num.finite |
Boolean flag, indicating whether all numerically-computed gradient vectors have finite values. |
h.array.num |
Collection of numerically-computed Hessian matrices at points listed in |
is.h.num.finite |
Boolean flag, indicating whether all numerically-computed Hessian matrices have finite values. |
g.mat |
Collection of analytically-computed gradient vectors for state space values listed in |
is.g.finite |
Boolean flag (if available), indicating whether all analytically-computed gradient vectors have finite values (if available). |
g.diff.max |
If available, maximum relative difference between analytical and numerical gradient vectors, over all |
h.array |
If available, collection of analytically-computed Hessian matrices at points listed in |
is.h.finite |
Boolean flag (if available), indicating whether all analytically-computed Hessian matrices have finite values. |
h.diff.max |
If available, maximum relative difference between analytical and numerical Hessian matrices, over all |
is.negdef.num |
Boolean flag, indicating whether numerical Hessian is negative-definite at all state space points indicated in |
is.negdef |
Boolean flag, indicating whether analytical Hessian is negative-definite at all state space points indicated in |
1. Validation tests performed in sns.check.logdensity
cannot prove that a log-density is twice-differentiable, or globally concave. However, when e.g. log-density Hessian is seen to be non-negative-definite at one of the points tested, we can definitively say that the Hessian is not globally negative-definite, and therefore sns
should not be used for sampling from this distribution. Users must generally consider this function as a supplement to analytical work.
2. See package vignette for more details on SNS theory, software, examples, and performance.
Alireza S. Mahani, Asad Hasan, Marshall Jiang, Mansour T.A. Sharabiani
Mahani A.S., Hasan A., Jiang M. & Sharabiani M.T.A. (2016). Stochastic Newton Sampler: The R Package sns. Journal of Statistical Software, Code Snippets, 74(2), 1-33. doi:10.18637/jss.v074.c02
## Not run: # using RegressionFactory for generating log-likelihood and its derivatives library(RegressionFactory) loglike.poisson <- function(beta, X, y) { regfac.expand.1par(beta, X = X, y = y, fbase1 = fbase1.poisson.log) } # simulating data K <- 5 N <- 1000 X <- matrix(runif(N * K, -0.5, +0.5), ncol = K) beta <- runif(K, -0.5, +0.5) y <- rpois(N, exp(X %*% beta)) beta.init <- rep(0.0, K) my.check <- sns.check.logdensity(beta.init, loglike.poisson , X = X, y = y, blocks = list(1:K)) my.check # mistake in log-likelihood gradient loglike.poisson.wrong <- function(beta, X, y) { ret <- regfac.expand.1par(beta, X = X, y = y, fbase1 = fbase1.poisson.log) ret$g <- 1.2 * ret$g return (ret) } # maximum relative diff in gradient is now much larger my.check.wrong <- sns.check.logdensity(beta.init , loglike.poisson.wrong, X = X, y = y, blocks = list(1:K)) my.check.wrong # mistake in log-likelihood Hessian loglike.poisson.wrong.2 <- function(beta, X, y) { ret <- regfac.expand.1par(beta, X = X, y = y, fbase1 = fbase1.poisson.log) ret$h <- 1.2 * ret$h return (ret) } # maximum relative diff in Hessian is now much larger my.check.wrong.2 <- sns.check.logdensity(beta.init , loglike.poisson.wrong.2, X = X, y = y, blocks = list(1:K)) my.check.wrong.2 ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.