Skip to content

MixedLinearModel setting params.fe_params inconsistently #3660

Closed

Description

@jbrockmendel

In mixed_linear_model.MixedLM, when profile_fe is True, loglike and score behave slightly differently. TLDR: is this intentional?

From loglike:

        # Move to the profile set
        if profile_fe:
            fe_params = self.get_fe_params(cov_re, vcomp)
        else:
            fe_params = params.fe_params

From score:

        if profile_fe:
            params.fe_params = self.get_fe_params(params.cov_re, params.vcomp)

Is there a reason why params gets altered in one case but not in the other?

It looks like the not profile_fe case in score is not hit in the tests.

Similarly, each of loglike, score, and hessian check for isinstance(params, MixedLMParams), and if not create a new instance. But the kwargs they pass are slightly different. Specifically, hessian sets has_fe=True while the others set has_fe=False.

Activity

  1. jbrockmendel commented on May 23, 2017

    @jbrockmendel
    ContributorAuthor

    An update on this:

    Editing the behavior of loglike to match score by setting params.fe_params does not appear to break any tests.

    However editing hessian to use has_fe=False like the other methods does break test_lme.TestMixedLM.test_regularized and test_lme.TestMixedLM.test_regularized1.

  2. kshedden commented on May 23, 2017

    @kshedden
    Contributor
  3. jbrockmendel commented on May 24, 2017

    @jbrockmendel
    ContributorAuthor

    Excellent, thanks.

    On further tinkering, I found a workaround that lets me get rid of the troublesome asymmetry (though I still don't grok what's going on, hopefully this will be obvious to you). First, I've refactored out from fit everything between if start_params is None through raise ValueError("invalid start_params" into a function creatively called _unpack_start_params.

    Then the if type(params) is not MixedLMParams: blocks in loglike, score, and hessian can use _unpack_start_params instead of MixedLMParams.from_packed. Doing this avoids the need to make a decision about the has_fe kwarg, since it is determined based on dimension matching.

  4. jbrockmendel commented on Mar 31, 2018

    @jbrockmendel
    ContributorAuthor

    @kshedden this appears to be relevant in troubleshooting #4396 downstream.

    The relevant point in the test is here:

                            fe_params = np.random.normal(size=k_fe)
                            cov_re = np.random.normal(size=(k_re, k_re))
                            cov_re = np.dot(cov_re.T, cov_re)
                            vcomp = np.random.normal(size=2) ** 2
                            params = MixedLMParams.from_components(
                                fe_params, cov_re=cov_re, vcomp=vcomp)
                            params_vec = params.get_packed(
                                has_fe=not profile_fe, use_sqrt=use_sqrt)
    
                            # Check scores
                            gr = -model.score(params, profile_fe=profile_fe)
                            ngr = nd.approx_fprime(params_vec, loglike)
                            assert_allclose(gr, ngr, rtol=1e-3)
    

    This last assertion is flaky, can be made to fail more reliably by decreasing the n_grps parameter at the top of this test. The reason I think this flakiness is related to this issue is because the conversion from params to params_vec does not round-trip correctly when params_vec is passed to loglike above.

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