Skip to content

BUG: GLS fit_regularized, different params if sigma is 1-dim or diag 2 dim #7755

Closed

Description

@josef-pkt

I don't see the problem right now. Needs investigating.

Fixing a unit test for GLS.fit_regularized that used incorrect kwarg "weights" #7751

I think the results, specifically params, should be the same if sigma = 1/ wghts and sigma = np.diag(1 / wgths)
The second matches the params from the WLS(... weights=wgths) estimate.
The former looks like an unpenalized estimate

my current version form test_regression
the last two asserts fail if I use `model_gls = GLS(yvec, xmat, sigma=1 / wgt)

def test_regularized_predict():
    n = 100
    p = 5
    np.random.seed(3132)
    xmat = np.random.normal(size=(n, p))
    yvec = xmat.sum(1) + np.random.normal(size=n)
    wgt = np.random.uniform(1, 2, n)
    model_wls = WLS(yvec, xmat, weights=wgt)
    # TODO: params is not the same in GLS if sigma=1 / wgth, i.e 1-dim
    model_gls = GLS(yvec, xmat, sigma=np.diag(1 / wgt))
    res = []
    for model1 in [model_wls, model_gls]:
        result1 = model1.fit_regularized(alpha=20.0, L1_wt=0.5, refit=True)
        res.append(result1)
        params = result1.params
        fittedvalues = np.dot(xmat, params)
        pr = model1.predict(result1.params)
        assert_allclose(fittedvalues, pr)
        assert_allclose(result1.fittedvalues, pr)

        pr = result1.predict()
        assert_allclose(fittedvalues, pr)

    assert_allclose(res[0].model.wendog, res[1].model.wendog, rtol=1e-10)
    assert_allclose(res[0].model.wexog, res[1].model.wexog, rtol=1e-10)
    assert_allclose(res[0].fittedvalues, res[1].fittedvalues, rtol=1e-10)
    assert_allclose(res[0].params, res[1].params, rtol=1e-10)

Activity

  1. added this to the 0.14 milestone on Sep 25, 2021
  2. josef-pkt commented on Sep 25, 2021

    @josef-pkt
    MemberAuthor

    found it
    in GLS.fit_regularized

            if self.sigma is not None:
                alpha = alpha * np.sum(1 / np.diag(self.sigma)) / len(self.endog)
    

    If sigma is 1-dim, then np.diag(self.sigma) creates diagonal matrix, and zero division.
    The np.sum(1 / np.diag(self.sigma)) and alpha are inf

  3. josef-pkt commented on Sep 25, 2021

    @josef-pkt
    MemberAuthor

    fix is

            if self.sigma is not None:
                if self.sigma.ndim == 2:
                    var_obs = np.diag(self.sigma)
                elif self.sigma.ndim == 1:
                    var_obs = self.sigma
                else:
                    raise ValueError("sigma should be 1-dim or 2-dim")
    
                alpha = alpha * np.sum(1 / var_obs) / len(self.endog)
    
  4. modified the milestones: 0.14, 0.13 on Sep 25, 2021
  5. added
    type-bug-wrong
    and removed on Sep 25, 2021
  6. added a commit that references this issue on Sep 25, 2021
    19cae58
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-regressionprio-elevtopic-penalizationtype-bug-wrong

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions