tests/testthat/test_HistogramBinOrder.R

# Regression test for the histogram bin-order bug fixed in 0.1.1:
# .DensityFromHist()/.LogDensityFromHist() indexed the padded weight vector
# by the number of breaks >= x, which applied the bin weights in reversed
# order: a histogram with all mass in the lowest bins produced a step
# density with all mass in the highest bins (mirrored in x).

test_that("histogram weights map to the correct bins", {
  w <- c(4, 3, 2, 1)
  f <- .DensityFromHist(w, breaks = 0:4)
  expect_equal(f(c(0.5, 1.5, 2.5, 3.5)), c(4, 3, 2, 1))
  expect_equal(f(c(-1, 5)), c(0, 0))
  lf <- .LogDensityFromHist(w, breaks = 0:4, background = 0)
  expect_equal(lf(c(0.5, 3.5)), log(c(4, 1)))
})

test_that("BDataDensityHistograms places mass at the correct domain end", {
  rm  <- ReferenceMeasureUniform(0, 100, sample=runif(2000,0,100), seed = 5)
  bas <- BayesSpaceBasis(BayesSpace(rm), nbasis = 7)
  brk <- c(seq(0, 90, by = 5), 100)
  w <- rep(1e-6, 19); w[1:2] <- 0.5; w <- w / sum(w)
  h <- BDataDensityHistograms(matrix(w, nrow = 1), breaks = brk, basis = bas)
  fd <- unclass(as.BDataDensities(h))[[1]]
  # mass sits in [0, 10), not mirrored to [90, 100]
  expect_gt(fd(4), 100 * fd(96))
  # and survives the spline projection at the correct end
  s <- as.BDataSplines(as.BDataDensities(h))
  v <- exp(basisEval(bas, c(4, 96)) %*% t(unclass(s)))
  expect_gt(v[1], v[2])
})

Try the DistributionalDDA package in your browser

Any scripts or data that you put into this service are public.

DistributionalDDA documentation built on Oct. 20, 2026, 5:06 p.m.