With this small piece of code you can
- Integrate multivariate normal densities (CDFs)
- Easily obtain partial derivatives of CDFs w.r.t location, mean and covariance (implementation of closed-form formulas, see e.g. Marmin et al. 2019, appendix 6)
- Manipulate quantities within a tensor-based framework (e.g. broadcasting is fully supported)
Torch-MvNorm is a library that consists of the two following components:
- integration -- PyTorch-Fortan bridge for Alan Genz's routine using SciPy.
- multivariate_normal_cdf -- implementation of the formula of the multivariate normal CDF gradient, with respect to location and covariance.
There is no GPU support. I am still looking for the best way to perform integrations of normal densities with GPUs. If you want, please share your opinion on this, e.g. opening an issue.
-
Install joblib python module, e.g.
sudo apt-get install -y python3-joblib
- Install SciPy python module.
git clone --recursive https://github.com/SebastienMarmin/torch-mvnorm
cd torch-mvnormimport torch
from torch.autograd import grad
from mvnorm import multivariate_normal_cdf as Phi
d = 3
x = torch.randn(d)
m = torch.zeros(d)
Croot = torch.randn(d,d)
C = Croot.mm(Croot.t())+torch.diag(torch.ones(d))
x.requires_grad = True
m.requires_grad = True
C.requires_grad = True
P = Phi(x,m,C)
dPdx, dPdm, dPdC = grad(P,(x,m,C)) # Bien sûr, dPdx = -dPdm.
- Run the code on small examples.
- Have a look at the documentation.
I welcome all contributions. Please let me know if you encounter a bug by filing an issue. Feel free to request a feature, make suggestions, share thoughts, etc, using the GitHub plateform or contacting me.
If you came across this work for a publication, please considere citing me for the code or for the mathematical derivation.
Torch-MvNorm is under GNU General Public License. See the LICENSE file.