Skip to content

MemoryError with IV2SLS #607

Open

Description

@ckarren

I'm trying to run a 2SLS to estimate price elasticity with IV2SLS. This is what my data looks like:
| ln_q | ln_p | .... weather variables ... | ... instruments ... |... user id dummies ...|

all data is np.float32. My data array is approx. (200000, 20000) which is about 16GB.

Using linearmodels IV2SLS I set up my model like:

dependent = ln_q
endog = weather variables + user id dummies
exog = ln_p
instruments = instruments
results = IV2SLS(dependent, endog, exog, instruments).fit()

When running with the full dataset I consistently get the error:
Unable to allocate 27.8GiB of memory to an array with shape (202507, 18450) and data type float64 and it looks like this line is the culprit:
self._wz = self._z * w
which is where weights are assigned.
I'm running 64-bit python on a machine with 128 GB of RAM. I've tried to circumvent this issue by passing my own weights:
results = IV2SLS(dependent, endog, exog, instruments, weights=np.ones(dependent.shape, dtype=np.float32)).fit()
but still get the same MemoryError even when I explicitly pass my own weights of data type float32.
32 GB of RAM usage just to create an array of 1s when weights = None seems like an awful lot of memory usage to essentially keep the input values unchanged. Further, why is it getting recast to float64, when all my other data is of data type float32 and I explicitly pass weights of datatype float32?
Why is an array of ~16GB using >100GB of RAM in this process? What can I do to get this regression to run?

Activity

  1. cpita-work commented on Nov 19, 2025

    @cpita-work

    I'm having a perhaps related issue with PanelOLS. As soon as I pass a weights vector fit takes a lot of time and ends up crashing the kernel. Without weights it's a matter of a few seconds to fit the same model.

  2. bashtage commented on Nov 19, 2025

    @bashtage
    Owner

    Can you post a demo example to help me understand the issue?

  3. bashtage commented on Nov 19, 2025

    @bashtage
    Owner

    As for @ckarren 's issue, I believe that things are upcast to float64 at some point. That will be 32GB already.

  4. bashtage commented on Nov 6, 2026

    @bashtage
    Owner

    Reopening: #862 closed this automatically, but it only fixes part of it.

    #862 stops the IV models from storing weighted copies of the data when all of the weights are 1, which is the case without weights or with constant weights. That cuts memory by about a third. With 80 MiB of float32 data, the memory after constructing an IV2SLS model goes from 12.0x the data to 8.1x, and the peak of fit goes from 1604 MiB to 1290 MiB.

    What remains for the float32 data in this issue:

    • IVData stores float32 input as float64, so the data doubles in size before the model sees it.
    • The model makes further full copies when it builds x and z (the np.c_[...] calls).

    So 16 GB of float32 data would still need roughly 8x its size. Keeping float32, or avoiding those copies, is what is left to do here.

  5. markjayson13 commented on Nov 11, 2026

    @markjayson13

    A memory-side fix will help, but the design is also doing a lot of unnecessary work. IV2SLS is being asked to carry roughly 20,000 user-id dummies as explicit columns (about 200,000 × 18,450 float64), which is why copies of Z and the weights blow up even on a 128 GB machine.

    If those user dummies are fixed effects rather than coefficients you need to report, you can absorb them instead of materializing them. AbsorbingLS will absorb high-dimensional effects, but it is OLS only and has no instruments. For this 2SLS price-elasticity model, residualize ln_q, the weather controls, ln_p, and the instruments with respect to user id, then call IV2SLS on those residualized series without the dummies. The second-stage design is then only a handful of columns.

    Alternatively, pyfixest demeans the fixed effects before 2SLS (FE-IV as in Wooldridge 2010, Ch. 11). In the 0.70 bracketed syntax:

    import pyfixest as pf
    fit = pf.feols(
        "ln_q ~ weather1 + weather2 + [ln_p ~ inst1 + inst2] | user_id",
        data=df,
    )

    (Replace the weather and instrument names with your columns. The older Y ~ X | fe | endog ~ inst form still works in 0.70 but is deprecated.) Either path avoids the 200,000 × 18,450 copies, independently of the remaining float32/copy work in #862.

  6. DresdenGman commented on Nov 25, 2026

    @DresdenGman
    Contributor

    Checked main after #862: the weight copies are gone, but with float32 inputs there are still two memory hits. IVData unconditionally upcasts to float64 (iv/data.py), and iv/model.py builds full copies for self._x and self._z via np.c_. On a 50,000x9 float32 input (~1.7 MiB), the model ends up holding ~7.6 MiB (~4.4x) all in float64.

    Happy to work on the remainder if nobody's on it, just wanted to clarify scope:

    • Given the float64 contract, would you actually want IVData to preserve float32, or just avoid the _x/_z copies?

    • Are @markjayson13's fixed-effects absorption idea and @cpita-work's PanelOLS-with-weights crash in scope here, or separate issues?

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