Entropy and Testing

Overview of the entropy-based testing family in np, including equality, symmetry, and dependence tests.
Keywords

npdeneqtest, npunitest, npsymtest, npdeptest, npsdeptest, entropy tests

If you want a minimal downloadable script for the quickest first run on this page, start with np_entropy_quickstart.R.

The entropy-based procedures compare probability laws rather than only moments. They test equality of densities, symmetry, pairwise dependence, or serial dependence and can be more computationally demanding than estimation alone.

The main functions

Function Main use
npdeneqtest equality of multivariate densities
npunitest equality of univariate densities
npsymtest asymmetry in a univariate variable or series
npdeptest nonlinear pairwise dependence
npsdeptest nonlinear serial dependence

Each procedure builds its test statistic from nonparametric probability estimates and obtains inference from the corresponding bootstrap scheme.

The public bootstrap-count argument is consistently named B throughout this family. The former boot.num input is retired; the examples below already use the current spelling.

Default fixed-Gaussian integration uses bounded deterministic corrected-trapezoid quadrature and no longer depends on cubature. Eligible summation and bootstrap work uses bounded native batches rather than retaining an \(n \times n\) pair cache. Long-running phases report completed work, elapsed time, and an ETA once enough progress has been observed, then clear the transient display on completion or failure.

A practical warning

Some of these procedures rely on numerical integration and bootstrap resampling. They can therefore be slow.

Practical advice:

  • begin with small examples,
  • use the faster summation/moment versions only for quick exploration when appropriate,
  • for serious work, prefer the default integral-based versions unless you have a good reason not to.

Equality of multivariate densities: npdeneqtest

Use npdeneqtest when you want to compare two multivariate samples, including settings with a mixture of continuous and categorical variables.

## Compare two multivariate samples with matching mixed-data structure
library(np)
set.seed(1234)

n <- 250
sample_A <- data.frame(a = rnorm(n),
  b = factor(rbinom(n, 2, 0.5)))
sample_B <- data.frame(a = rnorm(n),
  b = factor(rbinom(n, 2, 0.5)))

npdeneqtest(sample_A, sample_B, B = 99)

The two data frames should have matching variable names and compatible structure.

Equality of univariate densities: npunitest

Use npunitest when the problem is one-dimensional and you want to compare two univariate densities or probability functions.

## Compare two one-dimensional samples directly
library(np)
set.seed(1234)

n <- 1000
x <- rnorm(n)
y <- rnorm(n)

npunitest(x, y, B = 99)

It also works with discrete data, not just continuous data.

Asymmetry testing: npsymtest

npsymtest is aimed at the null of symmetry about a center such as the mean. This is useful for univariate data and can also be used for time series.

## Test a simple univariate sample for symmetry
library(np)
set.seed(1234)

n <- 100
y <- rnorm(n)

npsymtest(y, B = 99)

For bootstrap choice, iid is the simple resampling route, while block-based choices are relevant when serial dependence matters.

Pairwise nonlinear dependence: npdeptest

Use npdeptest when you want a nonparametric measure or test of dependence between two variables. One natural use is to compare observed values with fitted or predicted values.

## Compare observed values with fitted values using a dependence test
library(np)
set.seed(123)

n <- 100
x <- rnorm(n)
y <- 1 + x + rnorm(n)
model <- lm(y ~ x)

npdeptest(y, fitted(model), B = 99, method = "summation")

For actual applications, the integration-based route remains the stronger default.

Nonlinear serial dependence: npsdeptest

Use npsdeptest when the question is serial independence in a univariate series.

## Test a short series for nonlinear serial dependence
library(np)
set.seed(123)

ar_series <- function(phi, epsilon) {
  n <- length(epsilon)
  y <- numeric(n)
  y[1] <- epsilon[1] / (1 - phi)
  for (i in 2:n) {
    y[i] <- phi * y[i - 1] + epsilon[i]
  }
  y
}

yt <- ar_series(0.95, rnorm(100))
npsdeptest(yt, lag.num = 2, B = 99, method = "summation")

Again, the fast summation version is useful for illustration, but not the first choice for serious empirical work.

Rolling your own entropy measure

These functions do not have to be treated as a closed box. If you can write down the quantity you want and evaluate the relevant densities, you can build your own entropy-style measure.

Here is a short example comparing a kernel density estimate to a Gaussian benchmark:

## Build a custom entropy-style comparison against a Gaussian benchmark
library(np)

Srho <- function(x, y, ...) {
  integrand <- function(t, x, y) {
    f_x <- fitted(npudens(tdat = x, edat = t, bws = bw.SJ(x), ...))
    f_y <- dnorm(t, mean = mean(y), sd = sd(y))
    0.5 * (sqrt(f_x) - sqrt(f_y))^2
  }

  integrate(integrand, -Inf, Inf, x = x, y = y)$value
}

set.seed(123)
x <- rnorm(1000)
y <- rnorm(1000, sd = 100)
Srho(x, y)

That is not meant to replace the packaged procedures. It is meant to show that the package can also serve as a toolkit for custom work.

Back to top