So, according to the Wikipedia article , sampling for multidimensional truncated normal distribution (MTND) is more difficult. I ended up making a relatively simple way out and using the MCMC sampler to weaken the initial assumption regarding MTND as follows.
I used emcee to get the MCMC work done. I find this package phenomenally easy to use. It only requires a function that returns the log probability of the desired distribution. So I defined this function
from numpy.linalg import inv
def lnprob_trunc_norm(x, mean, bounds, C):
if np.any(x < bounds[:,0]) or np.any(x > bounds[:,1]):
return -np.inf
else:
return -0.5*(x-mean).dot(inv(C)).dot(x-mean)
Here Cis the covariance matrix of the multidimensional normal. Then you can run something like
S = emcee.EnsembleSampler(Nwalkers, Ndim, lnprob_trunc_norm, args = (mean, bounds, C))
pos, prob, state = S.run_mcmc(pos, Nsteps)
mean, bounds C. pos, ,
pos = emcee.utils.sample_ball(mean, np.sqrt(np.diag(C)), size=Nwalkers)
,
pos = numpy.random.multivariate_normal(mean, C, size=Nwalkers)
.. , , , MCMC.
.
, emcee , threads=Nthreads EnsembleSampler. , .