gradient_log_likelihood2: Analytic gradient of the negative log-likelihood (mode 2)

View source: R/betaBinomial.R

gradient_log_likelihood2R Documentation

Analytic gradient of the negative log-likelihood (mode 2)

Description

Computes the exact gradient of log_likelihood2 with respect to all regression coefficients. With the per-observation log-probability

\log p = \mathrm{lchoose}(n, y) + \mathrm{lbeta}(y+\alpha, n-y+\beta) - \mathrm{lbeta}(\alpha, \beta),

the partial derivatives on the natural scale are (with digamma function \psi):

\partial \log p/\partial\alpha = \psi(y+\alpha) - \psi(\alpha) + \psi(\alpha+\beta) - \psi(n+\alpha+\beta)

\partial \log p/\partial\beta = \psi(n-y+\beta) - \psi(\beta) + \psi(\alpha+\beta) - \psi(n+\alpha+\beta)

The chain rule through the log link multiplies by \alpha resp. \beta; coefficient gradients follow as -X^\top(w \cdot \partial\ell/\partial\eta) for the negative log-likelihood. Contributions of observations whose linear predictor is clamped (see log_likelihood2) are zeroed, which is the exact subgradient of the clamped objective.

Usage

gradient_log_likelihood2(params, X, Z, y, n, weights = NULL, lch = NULL)

Arguments

params

A numeric vector containing all model parameters. The first n_alpha elements are coefficients for the alpha model, and the remaining elements are coefficients for the beta model.

X

A matrix of predictors for the alpha model.

Z

A matrix of predictors for the beta model.

y

A numeric vector of response values.

n

The maximum score (number of trials).

weights

A numeric vector of weights for each observation (NULL = equal weights).

lch

Optional precomputed lchoose(n, y). Since this term does not depend on the parameters, passing it once avoids recomputation in every optimizer iteration.

Details

Supplying this gradient to optim (L-BFGS-B) replaces the finite-difference approximation, which required length(params) + 1 likelihood evaluations per gradient.

Value

Numeric vector: gradient of the negative log-likelihood with respect to params.


cNORM documentation built on July 20, 2026, 5:07 p.m.