Skip to content

ENH covariance for one-way groups #1646

Open

Description

@josef-pkt

what's the best way to get one-way covariance for example in unbalanced panel?

After reshape and filling with nans we could use masked arrays or pandas.

data: x = 2d data, groups is 1d integer group indicator

similar, possibly different shapes
in the panel hac versions
I also have written an extract all pairs version somewhere (for AR1 ?)

similar use case as in gee and mixed without pattern
trying to figure out random coefficient similar to Stata's xtrc
(GLS instead of MLE or REML in Mixed )
SUR is another case, but there we assume balanced panel

Activity

  1. jseabold commented on Apr 29, 2014

    @jseabold
    Member

    Isn't this what the helper code that @vincentarelbundock wrote is for? I think you can just use the transform_ methods as appropriate and apply arbitrary functions across groups. Balanced/unbalanced shouldn't matter.

    https://github.com/statsmodels/statsmodels/blob/master/statsmodels/tools/grouputils.py#L315
    https://github.com/statsmodels/statsmodels/pull/1133/files#diff-8ab5d9484c849d2418de300970ad5b58R49

    (Used extensively throughout for one or two-way panel balanced and unbalanced AFAIK. Might be worth a look at least.)

    Maybe I'm misunderstanding though. Can you post some code of the result you generally expect given some input?

  2. josef-pkt commented on Apr 29, 2014

    @josef-pkt
    MemberAuthor

    It's very similar to the random effects in panel.
    Kerby loops directly over the list of panel units (groups) in GEE and Mixed.
    I'm still trying to find a way without or with less looping, and might go sparse.
    (my guess is that it will take a while to get the panel/longitudinal/repeated pattern settled.)

    no code yet, I'm playing with balanced panel where I can just reshape and use np.cov.

    I'm digging out some old code, (random intercept as example for Generalized Ridge Regression, but assumes prior restriction and doesn't estimate yet the random effects distribution)
    https://github.com/statsmodels/statsmodels/blob/master/statsmodels/sandbox/regression/penalized.py#L322 (no much in terms of helpful documentation or comments in there.)
    (nice plots after fixing a refactoring bug)

  3. jseabold commented on Apr 29, 2014

    @jseabold
    Member

    It would be great to point all implementations that are doing their own thing at the Grouping code, so we can improve, generalize, and share. I pulled it out of panel and merged it for this reason. I'm also reusing it in duration models.

  4. josef-pkt commented on Apr 29, 2014

    @josef-pkt
    MemberAuthor

    I still think explicit looping as in GEE and Mixed makes more sense, but we don't have any benchmarking yet.

    The group structure is fixed and needs to be put only once into lists of arrays, and there is a lot of work to do for each group. I find it relatively easy to read.

    One advantage is that each group has contiguous arrays, which should be faster to access than going back to the original data (which I guess would have to do a lot of array copying in each loop).

  5. jseabold commented on Apr 29, 2014

    @jseabold
    Member

    I don't care how it's implemented (and the Grouping code is more or less an explicit loop, it's just done in Cython via pandas IIRC), but we should be sharing and writing general code if we can.

    I don't know why/where there would be any copying in the other implementation.

  6. jseabold commented on Apr 29, 2014

    @jseabold
    Member

    I also moved about 100-300 lines of loops in the old survival to a few lines of boiler-plate.

  7. josef-pkt commented on Apr 29, 2014

    @josef-pkt
    MemberAuthor

    I don't know why/where there would be any copying in the other implementation.

    out = dataframe.groupby(level=level).apply(function, **kwargs)
    or transform_slices

    AFAIU, these index into the original dataframe/array each time we apply a function.

    We need to get the right segment of the memory, and if that doesn't make a "nice" memory layout, then numpy has to copy the memory.
    If the function is some heavy calculation especially linear algebra, then we need additionally contiguous memory which would also force an array copy even if the original slice is not a copy but has weird strides.

    If this is additionally inside an optimization loop, then extra time will add up.
    If it's just used a few times, then it doesn't make much difference.

    mostly speculation, only Vincent once did a timing comparison.

    GEE and Mixed are heavy, and I don't think using groupby or similar makes it easier to read, and won't save any lines of code except in the initial setup.
    It might simplify some things in the extra results, but not in the fit.

    All I'm saying is that grouputils might not be the best choice for every model, and that I wouldn't fully commit to a pattern until we have more experience with this.

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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions