Skip to content

ENH: numerical derivatives numdiff for outlier-influence MLEInfluence, NegativeBinomial #7891

Open

Description

@josef-pkt

We currently don't have any numdiff fallback if some analytical derivatives are missing in the model

I'm trying out MLEInlfuence(res) where res is a NegativeBinomialP results instance.
but I get
AttributeError: 'NegativeBinomialP' object has no attribute '_deriv_score_obs_dendog'

Currently only Logit and Poisson have _deriv_score_obs_dendog, in both cases it's just exog. (I guess because of canonical link)

aside there are also other parts like resid_pearson missing in NegBin for MLEInlfuence to work
we need predict(which="var") for that.

(I'm not even sure some of the definitions in MLEInfluence apply if we have extra params or multiple link functions)

quick search, very new article (I didn't look at it)
Luisa Rivas & Manuel Galea (2021) On estimation and influence measures for the Negative Binomial regression model based on Q-function, Communications in Statistics - Theory and Methods, DOI: 10.1080/03610926.2021.1942493

Activity

  1. added this to the 0.14 milestone on Nov 18, 2021
  2. josef-pkt commented on Nov 26, 2021

    @josef-pkt
    MemberAuthor

    I think getting a appropriately vectorized complex step first derivative is worth a try.

    eg. derivative of score_factor (2d, tuple) (nobs, k_args) with respect to endog (nobs, 1)
    i.e. f: R -> R^k

    while _approx_fprime_cs_scalar is for f: R^k -> R

    general f : R^(n, k) -> R^(n, g) row-wise
    and deriv : R^(n, k, g) i.e 3_dim or flattened, or tuple on second dim.

    (I think our standard approx_fprime, approx_fprime_cs are already doing something like this but k is in first dimension (k = len(x) not x.shape[-1])

  3. josef-pkt commented on Nov 26, 2021

    @josef-pkt
    MemberAuthor

    this seems to do it

    y = f(x)

    x should be (nobs, k)
    f should return (nobs, g)
    derivatives are vectorized by rows
    (I think the scalar version corresponds to g=1, but return in scalar version is 1-dim instead of 2-dim column vector)
    I'm not sure yet whether epsilon should depend on np.max(abs(x), axis=0)), it could even depend on abs(x).

    def approx_fprime_cs2(x, f, epsilon=None, args=(), kwargs={}):
        n = x.shape[-1]
    
        epsilon = 1e-10 #_get_epsilon(x, 1, epsilon, 1)
        partials = []
        
        for i in range(n):
            ih = np.zeros(n, dtype=np.complex128)
            ih[i] = 1j * epsilon
            partials.append(f(x+ih, *args, **kwargs).imag / epsilon)
    
        return np.array(partials).T
    

    Also possible: use rollaxis to get shape, dim-order for different use cases.

  4. josef-pkt commented on Nov 28, 2021

    @josef-pkt
    MemberAuthor

    I'm still getting confused about the vectorized cases.

    The scalar fprime already works for this _deriv_score_obs_dendog. We are taking derivatives w.r.t vectorized scalar argument y=endog, confusing one letter names.

    eg. derivative of score_factors of BetaModel w.r.t. endog (added as keyword arg)

    en = res_income.model.endog
    def f(y):
        if y.ndim == 2 and y.shape[1] == 1:
            y = y[:, 0]
        sf = res_income.model.score_factor(res_income.params, endog=y)
        return np.column_stack(sf)
    
    
    deriv = _approx_fprime_cs_scalar(en[:,None], f)
    deriv.shape
    

    compared to new version

    df = approx_fprime_cs2(en[:, None], f)
    
    np.max(np.abs(df[...,0].T - deriv))
    5.3290705182007514e-14
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions