Learn R Programming

scorematchingad

The goal of scorematchingad is to enable fast implementation of score matching estimators through the use of automatic differentiation in the CppAD library. Estimators for new models can be created quickly via the tape_uld() and the tape_smd() function. If the new models require domain/manifold not yet implemented in scorematchingad, then (for now) the domain/manifold has to be added to the source of scorematchingad.

See the file DESCRIPTION for a slightly longer description, and ./R/scorematchingad-package.R (equivalently help(scorematchingad, scorematchingad) from within R) for an even longer description. The built-in help for R packages is well populated.

Installation

You can install the development version of scorematchingad from GitHub with:

# install.packages("devtools")
devtools::install_github("kasselhingee/scorematchingad")

Example

Some models are already incorporated into scorematchingad. Below is an example of estimating the Polynomially-Tilted Pairwise Interaction model (Scealy and Wood, 2023) for compositional data:

library(scorematchingad)
model <- rppi_egmodel(100)
estalr <- ppi(model$sample,
              paramvec = ppi_paramvec(betap = -0.5, p = ncol(model$sample)),
              trans = "alr")

This is an example of obtaining a tape of the score matching discrepancy of a custom likelihood for compositional data, which most naturally lies on the simplex:

myuld <- tape_uld(
"a1type dirich(const veca1 &u, const veca1 &beta) {
  size_t d  = u.size();
  a1type y(0.);  // initialize summation at 0
  for(size_t i = 0; i < d; i++)
  {   y   += beta[i] * CppAD::log(u[i]);
  }
  return y;
}",
  x = rep(0.2, 5), theta = rep(-0.1, 5))

tapes <- tape_smd("sim", "identity", "sim", 
 myuld$tape, myuld$tape$xtape, usertheta = NA * myuld$tape$dyntape, 
 bdryw="minsq", acut = 0.01)

Using scorematchingad in Other Packages [draft notes]

  • Avoid including the implementations of wrap and as for veca1, mata1 etc except in RcppExports.cpp. This makes sure that the speciailsations definitions are not duplicated for each .cpp file in your ./src directory. In practise you can get the scorematchingad types by including just the _forward.h header file.

Copy Link

Version

Install

install.packages('scorematchingad')

Monthly Downloads

132

Version

0.1.4

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Kassel Liam Hingee

Last Published

September 1st, 2025

Functions in scorematchingad (0.1.4)

fixdynamic

Fix Dynamic Parameters of a Tape
ppi_mmmm

A PPI Score-Matching Marginal Moment Matching Estimator (dimension=3 only)
fixindependent

Fix Independent Arguments of a Tape
ppi_cW

Quickly Generate a Vector of Windham Exponents for the PPI Model
keeprange

Reduce Range Dimension of a Tape
microbiome

16s Microbiome Data for Soil-Transmitted Helminths
ppi_robust

Robustly Estimate Parameters of the PPI Distribution
ppi

Estimation of Polynomially-Tilted Pairwise Interaction (PPI) Model
ppi_param_tools

PPI Parameter Tools
print,Rcpp_ADFun

Print or show a summary of an Rcpp_ADFun
rsymmetricmatrix

Quickly Generate a Symmetric Matrix for Testing and Examples
quadratictape_parts

Evaluate the Hessian and Gradient Offset of a Taped Quadratic Function
scorematchingtheory

Introduction to Score Matching
rppi

Simulate from a PPI Model
tape_bdryw

Generate a tape of a custom boundary weight function
smvalues

Compute Score Matching Discrepancy Value, Gradient, and Hessian
tape_Hessian

Tape the Hessian of a CppAD Tape
tape_gradoffset

Tape the Gradient Offset of a Quadratic CppAD Tape
scorematchingad-package

scorematchingad: Score Matching Estimation by Automatic Differentiation
tape_Jacobian

Tape the Jacobian of CppAD Tape
tape_logJacdet

Tape the log of Jacobian determinant of a CppAD Tape
tape_swap

Switch Dynamic and Independent Values of a Tape
tape_smd

Build CppAD Tapes for Score Matching
vMF

Score Matching Estimator for the von-Mises Fisher Distribution
testquadratic

Test Whether a CppAD Tape is a Quadratic Function
tape_uld

Generate a tape of a custom unnormalised log-density
vMF_robust

Robust Fitting of von Mises Fisher
cppad_closed

Score Matching Estimator for Quadratic-Form Score Matching Discrepancies
FB

Estimate the Fisher-Bingham Distribution
Bingham

Score Matching Estimators for the Bingham Distribution
Windham_populationinverse

Inverse Transform for the Population Parameters Under Windham Weights
Windham

Windham Robustification of Point Estimators for Exponential Family Distributions
evaltape

Evaluate a CppAD Tape Many Times
cppad_search

Iterative Score Matching Estimator Using Conjugate-Gradient Descent
dppi

Improper Log-Density of the PPI Model
Rcpp_ADFun-class

A Class for CppAD Tapes
avgrange

Average Across Range of a Tape