pymc-experimental icon indicating copy to clipboard operation
pymc-experimental copied to clipboard

Add `opt_sample` that performs optimizations prior to calling `pymc.sample`

Open ricardoV94 opened this issue 1 year ago • 7 comments

Includes building blocks for https://github.com/pymc-devs/pymc-experimental/issues/358

import pymc as pm
import pymc_experimental as pmx

with pm.Model() as m:
    p = pm.Beta("p", 1, 1, shape=(1000,))
    y = pm.Binomial("y", n=100, p=p, observed=[1, 50, 99, 50]*250)

    idata = pm.sample(verbose=True) 

# Applied optimization: beta_binomial_conjugacy 1x
# ConjugateRVSampler: [p]

CC @wd60622 @theorashid

Hopefully it will make it easy for follow-up PRs to add more cases

ricardoV94 avatar Dec 05 '24 15:12 ricardoV94

Need to try out. This example in description is different than the docstrings. Both work?

import pymc as pm
import pymc_experimental as pmx

with pm.Model() as m:
    p = pm.Beta("p", 1, 1, shape=(1000,))
    y = pm.Binomial("y", n=100, p=p, observed=[1, 50, 99, 50]*250)

    idata = pm.sample(verbose=True) 

# Applied optimization: beta_binomial_conjugacy 1x
# ConjugateRVSampler: [p]

williambdean avatar Dec 07 '24 09:12 williambdean

Need to try out. This example in description is different than the docstrings. Both work?

import pymc as pm
import pymc_experimental as pmx

with pm.Model() as m:
    p = pm.Beta("p", 1, 1, shape=(1000,))
    y = pm.Binomial("y", n=100, p=p, observed=[1, 50, 99, 50]*250)

    idata = pm.sample(verbose=True) 

# Applied optimization: beta_binomial_conjugacy 1x
# ConjugateRVSampler: [p]

Yes, I wanted an example that is faster than NUTS, but for testing it would be overkill

ricardoV94 avatar Dec 07 '24 09:12 ricardoV94

@wd60622 I had forgot to push the updates. I wasn't trying to gaslight you :D

ricardoV94 avatar Dec 08 '24 16:12 ricardoV94

Any objection to cleaning this up and merging it? I think it's nice to have around, and I might have found an actual use case in DPs, see this discussion

jessegrabowski avatar Jul 05 '25 03:07 jessegrabowski

No objections

ricardoV94 avatar Jul 05 '25 03:07 ricardoV94

I've resolved the conflicts

williambdean avatar Oct 25 '25 14:10 williambdean

pre-commit.ci autofix

williambdean avatar Oct 25 '25 14:10 williambdean