Skip to content

Track native student_t_lcdf / student_t_lccdf NaN gradient at the location and tail loss in BridgeStan 2.9.0 #63

Open

Description

@nsiccha

Stan Math's native student_t_lcdf, student_t_lccdf and student_t_cdf return a NaN gradient exactly where the bound equals the location, while the value log(1/2) is right. A model emitted by StanBlocks that calls them directly, or a censored Student-t likelihood built before StanBlocks v0.2.11, therefore has a NaN gradient whenever a censoring limit equals the location.

Cause. The kernels work with q = nu / t^2, t = (y - mu) / sigma. At t = 0, q = inf, r = 1 / (1 + q) = 0, and the Jacobian J = 2 r^2 q / t is 0 * inf / 0 = NaN, so every partial (in y, nu, mu, sigma) is NaN. The same kernels also form 1 - x and 1 - inc_beta(...) by subtraction. In BridgeStan 2.9.0 that puts their value off by up to 4e-5, and their gradient by up to 9e-5, at |t| = 1e6 (nu from 0.5 to 4), and makes them return -inf with a NaN gradient once the tail mass underflows. At nu = 2000 that already happens at t = -10, where the true log-CDF is -52.0.

Environment, reproduced on 2026-10-31: Julia 1.10.12, BridgeStan 2.9.0 with bundled Stan 2.39.0 / Stan Math 5.3.0. The code is unchanged in Stan Math 5.4.0 (student_t_lcdf.hpp). Open stan-dev/math#3433 (which fixes #2923) removes the tail subtractions, but it keeps J = 2 r^2 q / t, so it does not repair t = 0.

Probe (synthetic). It compares a raw Stan program calling the native kernel, the same law through StanBlocks' censored(student_t, ...) route, and the closed-form nu = 4 CDF:

using StanBlocks, BridgeStan
import StanBlocks.stan: instantiate, stan_model
setprecision(BigFloat, 256)
# nu = 4: F(t) = 1/2 + (3/8) t/sqrt(1 + t^2/4) (1 - t^2 / (12 (1 + t^2/4))), density 3/8 (1 + t^2/4)^(-5/2).
F(t) = (w = 1 + t^2 / 4; big(1) / 2 + 3 * t / (8 * sqrt(w)) * (1 - t^2 / (12w)))
f(t) = 3 / (8 * (1 + t^2 / 4)^(big(5) / 2))
lo, s = -0.5, 0.5
native = stan_library("""
parameters { real mu; }
model { target += student_t_lcdf(-0.5 | 4, mu, 0.5); }
"""; stanc_args = String[])
nm = BridgeStan.StanModel(native, "{}")
model = @slic (; y = lo, lo) begin
    mu ~ flat()
    y ~ censored(student_t(4.0, mu, 0.5); lower = lo)
end
sm = instantiate(stan_model(model)).model
for mu in (-0.500001, -0.5, -0.499999, 0.0, 0.7)
    row = Any[mu]
    for m in (nm, sm)
        v, g = BridgeStan.log_density_gradient(m, [mu]; propto = false, jacobian = true)
        push!(row, v, only(g))
    end
    t = (big(lo) - big(mu)) / big(s)
    push!(row, Float64(log(F(t))), Float64(-f(t) / (big(s) * F(t))))
    println(join(row, " | "))
end

Output:

mu native value native gradient StanBlocks value StanBlocks gradient closed-form value closed-form gradient
-0.500001 -0.6931456805610703 -1.4999977499996247 -0.6931456805610703 -1.4999977499996249 -0.6931456805610703 -1.4999977499996249
-0.5 -0.6931471805599453 NaN -0.6931471805599453 -1.5 -0.6931471805599453 -1.5
-0.499999 -0.6931486805610703 -1.5000022499996248 -0.6931486805610703 -1.500002249999625 -0.6931486805610703 -1.5000022499996248
0.0 -1.6769114931813458 -2.296463985789342 -1.676911493181346 -2.296463985789341 -1.6769114931813458 -2.296463985789341
0.7 -3.2920336921730833 -2.1691958470136505 -3.2920336921730833 -2.1691958470136505 -3.2920336921730824 -2.1691958470136488

StanBlocks status. Since v0.2.11, censored, truncated and interval_censored(student_t, ...) take their tail masses from the scalar builtins student_t_lcdf_stable(y, nu, mu, sigma) / student_t_lccdf_stable(y, nu, mu, sigma), and truncated_student_t uses them for its limits. They are analytic at t = 0 and avoid the tail subtractions. A direct student_t_lcdf / student_t_lccdf / student_t_cdf call still emits the native kernel. This issue tracks an upstream repair of the t = 0 Jacobian reaching a BridgeStan release; once that ships, the native kernels can be re-measured and the _stable route kept or retired.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions