Skip to content

SUMM/ENH: sparse matrix support in models #2710

Open

Description

@josef-pkt

see mailing list
https://groups.google.com/d/msg/pystatsmodels/fOQtn5RKxzI/SX6UjBYaCwAJ "OLS with sparse exog"
https://groups.google.com/d/msg/pystatsmodels/A2ZBopoCynY/ikoR-X3vCgAJ "Robust standard errors and memory usage"

  • Data handling: need a dataclass for sparse exog, pandas integration, param_names
  • categoricals, patsy's formulas are dense only, new pandas has sparse get_dummies
  • core calculations, see below
  • Results post estimation, robust covariance matrices, see SparseOLS as example (HC0 only)

core calculations
(copied from mailing list)

  • MixedLM uses it for the covariance calculations and penalization (I guess)
  • For linear models like OLS, the optimizer itself needs to be for sparse, Etienne used sparse.linalg.lsqr.
  • For discrete and GLM, I think only predict needs to be changed to calculate the linear prediction with sparse exog if we use a dense Hessian and standard optimization. (Hessian could be sparse but I don't think there is a sparse non-linear optimizer.)
  • There is also an old PR for sysreg which uses sparse block matrices or sparse Kronecker products to handle balanced panel data or models with multivariate endog.

One design question that is not clear to me yet is whether we can retrofit the existing models for sparse or add new models. I think that right now there are too many gaps in the supporting infrastructure that it's better or easier to write new models.

I will clean up SparseOLS (currently in a Gist) or SparseWLS to get a basic model with sparse support. GLM with scipy optimizers would be another good candidate, score_obs and Hessian can be calculated from the 1-D factors.

Activity

  1. josef-pkt commented on Nov 21, 2015

    @josef-pkt
    MemberAuthor

    just an idea: add NotSparseWarning for internal checks in code that we really have sparse matrices.
    Right now I used a plain assert. A warning is more flexible because we can set it to raise an exception for debugging.

    Main motivation: It is difficult to check whether we don't have a big in some intermediate calculations. We can have unit tests but those only work for attributes and returns, not for intermediate objects.

  2. josef-pkt commented on Dec 26, 2015

    @josef-pkt
    MemberAuthor

    related: #2568 absorbing fixed effects, should use sparse if more than one random coefficient to remove.

  3. josef-pkt commented on Jan 13, 2016

    @josef-pkt
    MemberAuthor

    tool: helper function to create sparse dummy matrix
    check MixedLM, and I have a recipe in my original sparse cluster cov_type script
    use case also for absorbing fixed effects in panel data

  4. hanfang commented on Nov 23, 2016

    @hanfang

    Hi @josef-pkt, just wondering if there is any updates related to this issue? Many of our data could really use the sparse matrix with GLM. Thanks.

  5. josef-pkt commented on Nov 23, 2016

    @josef-pkt
    MemberAuthor

    I didn't think much about the GLM case which I guess will depend mainly on having sparse WLS for IRLS.
    (I think I was thinking about the scipy optimizers which only need predict.)
    (For IRLS and WLS I haven't checked yet how to multiply the exog with the weights without switching to dense matrices. I think that wasn't possible with older scipy. Hint: if weights is a sparse diagonal matrix, then it's dot product between two sparse matrices instead of broadcasted element wise calculation between sparse exog and a column array weights.)
    maybe useful: pysal had two (IIRC) GSOC 2016 projects with possible "recipes" #2870

    If we focus on scipy optimizers and overwriting predict, then we might be trading off memory for speed of optimization.

    The main part that I was working on is for OLS which is much simpler, either for panel data or for large sparse categorical variables.

    @hanfang what would be the size of your design matrix, full matrix and sparse part? Which family are do you need?

  6. josef-pkt commented on Nov 23, 2016

    @josef-pkt
    MemberAuthor

    I remembered another option I was thinking about:

    If the sparse part is nuisance parameters, so we don't need them directly, then we can also handle them in the offset. We need to update during the iteration, but we only need exog_sparse dot params, which I think would be a bit similar to using something like backfitting. maybe

    @hanfang another question for a specification of the requirements. Are you interested in all parameters for summary and inference, or can those be separated/partialled out for inference and display. (This might have the advantage of reducing the final hessian with partitioned matrix inversion, similar to OLSAbsorbed.)

  7. hanfang commented on Dec 13, 2016

    @hanfang

    Hi @josef-pkt, Thanks for the information. The size of the design matrix is 2 million by 7 thousand. It is very sparse. In each row, there are only three non-zero values. The family that we need is negative binomial. The sparse part is not nuisance parameters so we can not use offset for them. We are interested in all parameters for summary and inference. Thanks.

  8. josef-pkt commented on Dec 13, 2016

    @josef-pkt
    MemberAuthor

    7000 parameter for inference is pretty heavy. That 49 million elements in the cov_params, more than my computer would be able to handle.
    Do you have some orthogonality in the design matrix, e.g. is the cross-product X'X partially sparse or block-diagonal? (or for the demeaned X)

    printing summary would be quite a long list.

    (I will not be able to go back to this for some time. I don't think it would be very difficult to wire in a sparse WLS for the parameter estimation, but I'm currently in a completely different neighborhood in statistics.)

    related: GLMNet penalization works with coordinate descend for the optimization, which wouldn't have a restriction on the number of explanatory variables, but would still use dense cov_params.
    (I would expect that a sparse linalg version would be faster.)

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