Open ricardoV94 opened 4 weeks ago
Implementation checks may fail until https://github.com/pymc-devs/pytensor/issues/799 is fixed
Check out this pull request on
See visual diffs & provide feedback on Jupyter Notebooks.
Powered by ReviewNB
Benchmark code
import pymc as pm
import numpy as np
import pytensor
import pytensor.tensor as pt
rng = np.random.default_rng(123)
n = 100
L = rng.uniform(low=0.1, high=1.0, size=(n,n))
Sigma = L @ L.T
Q_test = np.linalg.inv(Sigma)
x_test = rng.normal(size=n)
with pm.Model(check_bounds=False) as m:
Q = pm.Data("Q", Q_test)
x = pm.MvNormal("x", mu=pt.zeros(n), tau=Q)
logp_fn = m.compile_logp().f
logp_fn.trust_input=True
print("logp")
pytensor.dprint(logp_fn)
dlogp_fn = m.compile_dlogp().f
dlogp_fn.trust_input=True
print("dlogp")
pytensor.dprint(dlogp_fn)
np.testing.assert_allclose(logp_fn(x_test), np.array(-1789.93662205))
np.testing.assert_allclose(np.sum(dlogp_fn(x_test) ** 2), np.array(18445204.8755109), rtol=1e-6)
# Before: 2.66 ms
# After: 1.31 ms
%timeit -n 1000 logp_fn(x_test)
# Before: 2.45 ms
# After: 72 µs
%timeit -n 1000 dlogp_fn(x_test)
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 92.20%. Comparing base (
b496127
) to head (2b9886e
). Report is 1 commits behind head on main.
Final question is just whether we want / can do a similar thing for the MvStudentT. Otherwise it's ready to merge on my end
CC @elizavetasemenova
Description
This PR is exploring a specialized logp for a MvNormal (and possible MvStudentT) parametrized directly in terms of tau. According to common model implementation looks like:
TODO (some are optional for this PR)
Sparse implementation? May need some ideas like: https://stackoverflow.com/questions/19107617/how-to-compute-scipy-sparse-matrix-determinant-without-turning-it-to-denseInvestigate in a follow up PRRelated Issue
Checklist
Type of change
CC @theorashid @elizavetasemenova
📚 Documentation preview 📚: https://pymc--7345.org.readthedocs.build/en/7345/