RAM spike on GLM Out-Of-Sample Predictions notebook

Hi everyone,

I was working on running and restoring the GLM-out-of-sample-predictions notebook from pymc-examples on my machine, but for some reason my RAM consumption completely spikes and crashes the kernel (shutting down my VSCode) when it executes the grid prediction cell (code cell 18):

grid_df = pd.DataFrame(x_grid, columns=["x1", "x2"])
grid_df["p"] = ppc_grid.posterior_predictive.p.mean(dim=["chain", "draw"])
p_grid = grid_df.pivot(index="x2", columns="x1", values="p").to_numpy()

Could you tell me if it’s only my setup, or is this actually an issue with this notebook? Has anyone else run into this when running this example?

The grid is large by design. It’s 300x300 sampled for 4 chains x 1000 draws in float64, so it’s about 3GB. xarray’s .mean function also makes a copy when skipna=True (the default), so the 2nd line takes you to about 6gb. So it’s not a bug, just big.

You can shrink the size of the prediction grid to say 100x100, or pass skipna=False to .mean, or both, to save memory.

Nevertheless this example was pretty badly out of date, I opened a PR to make it a bit nicer. It should help with the RAM issues as well.

Thanks for the reply @jessegrabowski, I ran the PR locally and the notebook executes nicely without any issue. One question though, i don’t understand what was the reason behind updating the decision boundary from using the posterior mean coefficients to evaluating (p_grid > 0.5).mean()?

Due to Jensen’s Inequality. In general, you always want to compute a summary statistic as the very last thing you do. Computing (p_grid > 0.5) gives you the distribution of samples over x_grid who meet the decision boundary, and its the mean of that distribution that you want.

Taking the mean of the parameters first then computing the decision boundary at the mean will be wrong by an amount proportional to the curvature of the decision function. Probably small in this case but I think its better to show people the right way.

1 Like

I did some reading, the error is approximately half times the curvature of the decision function times Variance of the parameters.

\text{Error} \approx \frac{1}{2} f''(\mathbb{E}[X]) \, \mathrm{Var}(X)

Thanks for the clarification

2 Likes