pythonbayesianpymc3mcmc

Only save transformed parameters in PyMC3


I want to shift my variables in PyMC3. I'm currently just using deterministic transforms, but when I perform the inference, it saves both the original samples and the shifted samples (which is expected behavior).

Example code:

import pymc3 as pm

x_lower = -3

with pm.Model():
    x = pm.Gamma('x', alpha=2., beta=1.5)
    x_shift = pm.Deterministic("x_shift", x + x_lower)
    trace = pm.sample(1000, tune=1000)
trace.remove_values("x")  # my current solution

tp = pm.traceplot(trace)
# other analysis...

Now trace stores all x samples and all x_shift samples, which is clearly a waste when the numbers of variables and samples increase. I can do trace.remove_values("x") before continuing with analysis, but I would prefer to simply not savex at all.

Another option is to not save x_shift at all, but I can't find how to add x_lower on to the samples after inference. So this isn't really a solution if I want to use the in-built analysis tools.

Can I save only the x_shift samples, and not the x samples, when I sample?


Solution

  • You can specify exactly what you want to save by setting the trace argument in the pm.sample() function, e.g.,

    trace = pm.sample(1000, tune=1000, trace=[x_shift])