Skip to content

links probit, CDFLink precision loss in Hessian #1878

Open

Description

@josef-pkt

comparing hessian in GLM with hessian of Probit, the agreement is not very high

Are there numerical problems in the numerical second derivatives of the link function?

>>> hess_glm_probit = res_glm_probit_clu.model.hessian(res_probit_clu.params)
>>> print(hess_probit - hess_glm_probit)
[[ 0.03198082 -0.02195936  0.0191278 ]
 [-0.02195936  0.02408385  0.02408385]
 [ 0.0191278   0.02408385 -0.00904406]]
>>> print(hess_probit / hess_glm_probit)
[[ 0.99975476  1.0007156   0.99958227]
 [ 1.0007156   0.99793767  0.99793767]
 [ 0.99958227  0.99793767  1.00047687]]
>>> hess_glm_probit
array([[-130.40437081,  -30.68664568,  -45.79012885],
       [ -30.68664568,  -11.67795402,  -11.67795402],
       [ -45.79012885,  -11.67795402,  -18.96560781]])

>>> print(hess_probit / hess_glm_probit - 1)
[[-0.00024524  0.0007156  -0.00041773]
 [ 0.0007156  -0.00206233 -0.00206233]
 [-0.00041773 -0.00206233  0.00047687]]

in contrast, agreement in score_obs is at floating point precision

>>> sc_probit = res_probit_clu.model.jac(res_probit_clu.params)
>>> sc_glm_probit = res_glm_probit_clu.model.score_obs(res_probit_clu.params)
>>> print(np.max(np.abs(sc_probit - sc_glm_probit)))
4.4408920985e-16

part of cross-checking in #1870 between GLM and other models

Activity

  1. josef-pkt commented on Aug 10, 2014

    @josef-pkt
    MemberAuthor

    example is still burried in a script

  2. josef-pkt commented on Dec 17, 2020

    @josef-pkt
    MemberAuthor

    CDFLink uses numerical derivative for deriv2
    (complaining again links are back to front)

    deriv2 is computed in a very roundabout way,
    inverse_deriv2 would have an explicit form

    using inverse function math
    res_tmp.model.family.link.deriv2(mu) - (linpred * stats.norm.pdf(linpred)) / stats.norm.pdf(linpred)**3
    differs by 8.9e-5

    in an example where I add noise to endog to check whether it works for interval [0, 1] data, the hessian looks pretty unstable as I increase the noise, bse changes quite a bit.
    HC0 for fractional regression with GLM-probit differs from Stata, some of this might be a noisy hessian computation

    background

    SPSS algorithm manual has explicit formula for second derivatives of link function
    res_tmp.model.family.link.deriv(mu)**2 * stats.norm.ppf(mu)

    which agrees closely with the inverse function formula

    np.max(np.abs(res_tmp.model.family.link.deriv(mu)**2 * stats.norm.ppf(mu) - 
                  (linpred * stats.norm.pdf(linpred)) / stats.norm.pdf(linpred)**3))
    6.366462912410498e-12
    

    also
    inverse_deriv is computed through the link deriv function and not directly from pdf
    res_tmp.model.family.link.inverse_deriv(linpred) - stats.norm.pdf(linpred)
    agrees at 2e-16

  3. josef-pkt commented on Dec 17, 2020

    @josef-pkt
    MemberAuthor

    cauchy link has an explicit deriv2

    SPSS algorithm manual has cauchit only as cumulative link, and there they specify first and second inverse derivatives.
    They use gamma instead of mu for expected in cumulative link, gamma = link_inverse(linpred)

  4. josef-pkt commented on Dec 17, 2020

    @josef-pkt
    MemberAuthor

    after adding the inverse definition for deriv2, I get agreement with Stata in fracreg case with bse-HC0
    great, it was only a precision problem

  5. josef-pkt commented on Dec 17, 2020

    @josef-pkt
    MemberAuthor

    base class Link already defines inverse_deriv2 which uses the second derivative of an inverse function formula and deriv2

    if I go generically in cdf link in the other direction, then I will get a circular infinite recursion.

    I could override this by adding NotImplementedError to CDFLink.inverse_deriv2
    assuming that we have cdf, pdf in easier form than ppf and inverse derivative function. i.e. we have derivative/score of log pdf already in models.

  6. josef-pkt commented on Dec 17, 2020

    @josef-pkt
    MemberAuthor

    Based on a quick search it looks like R doesn't define second derivatives in link functions

    a bit more Wolfram

    derivative of t.pdf(x, df), i.e. inverse_deriv2

    -((Sqrt[df]*(1 + df)*x*(df/(df + x^2))^(-1 + (1 + df)/2))/ ((df + x^2)^2*Beta[df/2, 1/2]))

    -(((1 + df)*x*(df/(df + x^2))^((3 + df)/2))/(df^(3/2)*Beta[df/2, 1/2]))

    using n for df
    -((Sqrt[n] (1 + n) x (n/(n + x^2))^(-1 + (1 + n)/2))/((n + x^2)^2 Beta[n/2, 1/2]))
    https://www.wolframalpha.com/input/?i=d%2Fdx+PDF%5BStudentTDistribution%5B0%2C1%2Cn%5D%2C+x%5D

    We should be able to get the TLink also, or StudentT ? to have a more than one letter distribution name

  7. josef-pkt commented on Dec 21, 2020

    @josef-pkt
    MemberAuthor

    another disabled probit test
    test_deriv2 in statsmodels.genmod.families.tests.test_link skips probit
    If I enable it, then tests pass if I increase rtol from 1e-6 to 5e-6, diff in my test run is rtol 1.164e-06

  8. josef-pkt commented on Jun 2, 2021

    @josef-pkt
    MemberAuthor

    more distributions for cdf link

    I found several references that use generalized extreme value distribution for rare events binomial/bernoulli

    Medford, Anthony, and James W. Vaupel. 2019. “An Introduction to Gevistic Regression Mortality Models.” Scandinavian Actuarial Journal 2019 (7): 604–20. https://doi.org/10.1080/03461238.2019.1586756.

    especially also referenced articles by Calabrese

    (I ran into these by chance and didn't read anything.
    "generalised extreme value regression models" in some titles is misleading because it is Binomial/Bernoulli with GEV link and not a GEV distributed (continuous) endog.)

  9. josef-pkt commented on Jun 2, 2021

    @josef-pkt
    MemberAuthor

    related: add more asymmetric link functions for Binomial/Bernoulli (besides loglog and cloglog)

    Wang, Xin, Vivekananda Roy, and Zhengyuan Zhu. "A new algorithm to estimate monotone nonparametric link functions and a comparison with parametric approach." Statistics and Computing 28, no. 5 (2018): 1083-1094.

    many of the references for asymmetric CDFLinks use Bayesian method.
    The above uses MLE, GEV link as a case. The articles also compares with symmetric and nonparametric links.

  10. josef-pkt commented on Jul 22, 2021

    @josef-pkt
    MemberAuthor

    student t link #7287 (comment)

  11. josef-pkt commented on Jul 22, 2021

    @josef-pkt
    MemberAuthor

    generalized logistic, log-F is a cdflink
    #5409 (comment)

    also we could cross-test cdf links for distributions that are equivalent to traditional "named" links, e.g.
    loglog sounds like extreme value distribution.
    logit versus logistic cdflink

    For asymmetric distributions we could add generic sf link based on survival function (1 - F(x))`.
    like loglog versus cloglog?
    We should still be able to maintain increasing monotonicity.

  12. added
    type-possible-bug
    on Sep 28, 2026
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

    comp-discretecomp-genmodtype-possible-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions