MyNixOS website logo
Description

Vertical Weighted Strips.

A reference implementation of the Vertical Weighted Strips method explored by Raim, Livsey, and Irimata (2025) <doi:10.48550/arXiv.2401.09696> for rejection sampling.

Introduction

vws is an R package to support rejection sampling using vertical weighted strips (arxiv:2401.09696). Construction of the proposal distribution and rejection sampling are carried out in C++; sampling functions may be exposed in R via Rcpp for use in applications. Programming in C++ is facilitated using the fntl R package.

See the included vignette for a more in-depth discussion of the package and an API guide.

Installation

The vws package may be installed directly from Github using a standard R command like the following.

devtools::install_github("andrewraim/vws", ref = "v0.3.0")

Here, v0.3.0 represents a tagged release; replace it with a later version if one exists.

Getting Started

The following example from the vignette generates variates from the density

$$ f(y \mid z, \mu, \sigma^2) % \propto \underbrace{\frac{1}{\lambda \sqrt{2\pi}} \exp\left[ -\frac{1}{2\lambda^2} (z - y)^2 \right]}{g(y)} \cdot \underbrace{\frac{1}{y} \exp\left[ -\frac{1}{2\sigma^2} (\log y - \mu)^2 \right] \mathrm{I}(y > 0)}{w(y)}. $$

Create the file example.cpp with the following contents.

// [[Rcpp::depends(vws, fntl)]]
#include "vws.h"

// [[Rcpp::export]]
Rcpp::List example(unsigned int n, double z, double mu,
	double sigma, double lambda, unsigned int N, double tol = 0,
	unsigned int max_rejects = 10000, unsigned int report = 10000)
{
	vws::rejection_args args;
	args.max_rejects = max_rejects;
	args.report = report;
	args.action = fntl::error_action::STOP;

	const vws::dfdb& w =
	[&](double x, bool log = true) {
		double out = R_NegInf;
		if (x > 0) {
			out = -std::log(x) - std::pow(std::log(x) - mu, 2) / (2*sigma*sigma);
		}
		return log ? out : std::exp(out);
	};

	fntl::density df = [&](double x, bool log = false) {
		return R::dnorm(x, z, lambda, log);
	};
	fntl::cdf pf = [&](double q, bool lower = true, bool log = false) {
		return R::pnorm(q, z, lambda, lower, log);
	};
	fntl::quantile qf = [&](double p, bool lower = true, bool log = false) {
		return R::qnorm(p, z, lambda, lower, log);
	};

	vws::UnivariateHelper helper(df, pf, qf);
	vws::RealConstRegion supp(0, R_PosInf, w, helper);
	vws::FMMProposal<double, vws::RealConstRegion> h(supp);

	auto lbdd = h.refine(N - 1, tol);
	const vws::rejection_result<double>& out = vws::rejection(h, n, args);

	return Rcpp::List::create(
		Rcpp::Named("draws") = out.draws,
		Rcpp::Named("rejects") = out.rejects,
		Rcpp::Named("lbdd") = lbdd
	);
}

The example function may be called through R as follows.

R> Rcpp::sourceCpp("example.cpp")
R> mu = 5; sigma = sqrt(0.5); lambda = 10; y_true = 58; z = 63
R> out = example(n = 1000, z, mu, sigma, lambda, N = 50, tol = 0.10)
R> head(out$draws)
Metadata

Version

0.3.0

License

Unknown

Platforms (78)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    uefi
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-uefi
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-uefi
  • x86_64-windows