Skip to content

Evaluation, diagnostics, benchmarks, and additional tests · Evaluation, diagnostics, benchmarks, and additional hypothesis tests · EVA020 · Hypothesis test

Kernel Stein discrepancy goodness-of-fit test

Tests a target density using its score function without requiring its normalizing constant.

Runs in CodeSplash (Experimental) Complete calculation CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page. The methods that run it are Experimental.

Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.

Availability
Runs in CodeSplash (Experimental)
Code example
Complete calculation
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through distribution.ksd. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Tests a target density using its score function without requiring its normalizing constant.

Question / hypotheses

H0: observations follow p; H1: they do not, within the test’s regularity and kernel conditions.

Architecture / computational structure

Differentiate the log target density to obtain its score, evaluate kernel first and mixed derivatives, and combine them into the Stein kernel matrix. Its off-diagonal average is a degenerate U-statistic under a correctly specified fixed target; use the corresponding justified bootstrap/null calibration.

Assumptions

The score s_p(x)=gradient log p(x) must be available and correct; integration-by-parts boundary conditions and kernel regularity matter. The null U-statistic is degenerate and needs suitable calibration.

Formula / computational definition

up(x,y)=sp(x)Tk(x,y)sp(y)+sp(x)T∇yk+sp(y)T∇xk+tr⁡(∇x∇yk).u_p(x,y)=s_p(x)^Tk(x,y)s_p(y)+s_p(x)^T\nabla_y k+s_p(y)^T\nabla_x k+\operatorname{tr}(\nabla_x\nabla_y k).

Parameters and notation

KSD estimates the average Stein kernel u_p, often excluding diagonal terms. Bandwidth and bootstrap scheme are hyperparameters; nuisance fitting on the same sample needs extra care.

Fitting, fine-tuning, and adaptation

The target score can be available without the density normalizing constant. If target parameters are estimated from these same observations, the fixed-target reference is generally insufficient: use an applicable composite-null method or independent fitting split. Kernel bandwidth and regularity conditions must be reported; the snippet computes only an RBF discrepancy for a known standard normal target.

How to use and calibrate

Calculate score vectors and kernel derivatives, then use the paper’s wild-bootstrap or other justified null procedure. Evaluate sensitivity to bandwidth and dimension.

Example application

Check whether approximate samples from an unnormalized energy model match its specified target.

Interpretation / pitfalls

Watch out

A raw KSD value is not a p-value. Some kernel choices can fail to detect certain escaping distributions; assumptions matter when claiming convergence.

Python and R code examples

Scope: Mathematical calculation on synthetic inputs; not a complete benchmark pipeline.

Validation: Python — Executed successfully on synthetic inputs. R — reviewed but not executed in this environment. Python dependencies are explicit imports; R uses base R unless a package/bridge is explicitly named.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Complete calculation — Mathematical calculation on synthetic inputs; not a complete benchmark pipeline. · needs numpy

import numpy as np
x=np.array([-1.,0.,1.]); d=x[:,None]-x[None,:]; k=np.exp(-d*d/2)
s=-x; U=(s[:,None]*s[None,:]+s[:,None]*d-s[None,:]*d+1-d*d)*k
print((U.sum()-np.trace(U))/(len(x)*(len(x)-1)))
# One-dimensional standard-normal target, RBF bandwidth 1.
R

Complete calculation — Mathematical calculation on synthetic inputs; not a complete benchmark pipeline.

x <- c(-1,0,1); d <- outer(x,x,"-"); K <- exp(-d*d/2); s <- -x
U <- (outer(s,s)+s*d-t(t(d)*s)+1-d*d)*K
print((sum(U)-sum(diag(U)))/(length(x)*(length(x)-1)))

Sources

From the CodeSplash reference, entry EVA020.