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.
gradient_log_likelihood2(params, X, Z, y, n, weights = NULL, lch = NULL)Numeric vector: gradient of the negative log-likelihood with
respect to 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.
A matrix of predictors for the alpha model.
A matrix of predictors for the beta model.
A numeric vector of response values.
The maximum score (number of trials).
A numeric vector of weights for each observation (NULL = equal weights).
Optional precomputed lchoose(n, y). Since this term does
not depend on the parameters, passing it once avoids recomputation in
every optimizer iteration.
Supplying this gradient to optim (L-BFGS-B) replaces
the finite-difference approximation, which required
length(params) + 1 likelihood evaluations per gradient.