make.c.dist: Define a probability distribution object with C log-density

View source: R/00dist-util.R

make.c.distR Documentation

Define a probability distribution object with C log-density


Defines a probability distribution object for use with compare.samplers with log-density implemented in C.


make.c.dist(ndim, name, c.log.density, c.context = NULL,
             name.expression = NULL, mean = NULL, cov = NULL)



The size of the distribution's state space.


A human-readable name for the distribution.


A C function returning the log-density and gradient of the target distribution.


An opaque object passed to c.log.density


A name for the distribution in plotmath notation. Used in preference to name in plot functions when available.


A vector specifying the true mean of the distribution.


A matrix specifying the true covariance of the distribution.


See make.dist for discussion of ndim, name, name.expression, mean, and cov.

c.log.density is a string containing the symbol name of a C function that computes the log density and log density gradient of the target distribution. It has the type log_density_t, defined in SamplerCompare.h as:

typedef double log_density_t(dist_t *ds, double *x,
                             int compute_grad, double *grad);
typedef struct {
  log_density_t *log_dens;
  SEXP context;
  int ndim;
} dist_t;

The ds structure defines the distribution, where the log_dens element is a pointer to the function named by c.log.density, the context element is a SEXP containing the c.context parameter, and ndim is the ndim parameter to make.c.dist.

The x parameter is an ndim-long array of doubles containing the location at which to evaluate the log-density, which the log_density_t should return.

If compute_grad is nonzero, the function should compute the gradient of the log density and store it in the double array pointed to by grad. If for some reason it cannot do this, it should call the R-internal error function to report an error to the user. If the implementor does not plan to sample from the distribution with a method that computes gradients, this can reduce implementation effort.

The details of this interface are described in greater detail in “R/C Glue in SamplerCompare”.


A scdist object.

See Also

compare.samplers, make.dist, “R/C Glue in SamplerCompare” (vignette)

SamplerCompare documentation built on April 24, 2023, 9:09 a.m.