def sample_wishart(sigma, nu):
n = sigma.shape[0]
chol = np.linalg.cholesky(sigma)
# use matlab's heuristic for choosing between the two different sampling schemes
if (nu <= 81+n) and (nu == round(nu)):
# direct
X = np.dot(chol,np.random.normal(size=(n,nu)))
else:
A = np.diag(np.sqrt(np.random.chisquare(nu - np.arange(n))))
A[np.tri(n,k=-1,dtype=bool)] = np.random.normal(size=(n*(n-1)/2.))
X = np.dot(chol,A)
return np.dot(X,X.T)
评论列表
文章目录