I want to fit a MVN to my data in 4 dimensions. I will give N=10
samples to my program. The MVN is modelled using 4-parameter Normal-Wishart Distribution. read Bayesian Inference for the Multivariate Normal
: https://www.fil.ion.ucl.ac.uk/~wpenny/publications/bmn.pdf.
The Normal-Wishart distribution has four parameters - mu_0
, Sigma
, nu
, beta_0
.
data {
int<lower=0> N;
vector[4] x[N];
}
parameters {
vector[4] mu_0;
real beta_0;
real nu;
vector[4] mu;
cov_matrix[4] Sigma;
cov_matrix[4] Omega;
}
model {
Omega ~ wishart(nu, Sigma);
mu ~ multi_normal(mu_0, beta_0 * Omega);
for (n in 1:N)
x[n] ~ multi_normal_prec(mu, Omega);
}
I run this via PyStan with:
mu = 10 * np.array([1, 2, 3, 4])
Omega = 3 * np.eye(4)
x = np.random.multivariate_normal(mu, Omega, size = 10)
# Put our data in a dictionary
data = {'N': len(x), 'x': x}
# Compile the model
sm = pystan.StanModel(model_code=model)
# Train the model and generate samples
fit = sm.sampling(data=data, iter=100, chains=2, warmup=5, thin=1, seed=101)
print(fit)
which constantly throws
INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_8bc181663e727aba88358a43cb3cb853 NOW.
---------------------------------------------------------------------------
RemoteTraceback Traceback (most recent call last)
RemoteTraceback:
"""
Traceback (most recent call last):
File "/home/myntra/anaconda3/envs/pytorch_p36/lib/python3.6/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/home/myntra/anaconda3/envs/pytorch_p36/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
File "stanfit4anon_model_8bc181663e727aba88358a43cb3cb853_7548108693669776004.pyx", line 371, in stanfit4anon_model_8bc181663e727aba88358a43cb3cb853_7548108693669776004._call_sampler_star
File "stanfit4anon_model_8bc181663e727aba88358a43cb3cb853_7548108693669776004.pyx", line 404, in stanfit4anon_model_8bc181663e727aba88358a43cb3cb853_7548108693669776004._call_sampler
RuntimeError: Initialization failed.
"""
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
<ipython-input-19-bc8d52142265> in <module>()
11
12 # Train the model and generate samples
---> 13 fit = sm.sampling(data=data, iter=100, chains=2, warmup=5, thin=1, seed=101)
14
15 print(fit)
~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/pystan/model.py in sampling(self, data, pars, chains, iter, warmup, thin, seed, init, sample_file, diagnostic_file, verbose, algorithm, control, n_jobs, **kwargs)
776 call_sampler_args = izip(itertools.repeat(data), args_list, itertools.repeat(pars))
777 call_sampler_star = self.module._call_sampler_star
--> 778 ret_and_samples = _map_parallel(call_sampler_star, call_sampler_args, n_jobs)
779 samples = [smpl for _, smpl in ret_and_samples]
780
~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/pystan/model.py in _map_parallel(function, args, n_jobs)
83 try:
84 pool = multiprocessing.Pool(processes=n_jobs)
---> 85 map_result = pool.map(function, args)
86 finally:
87 pool.close()
~/anaconda3/envs/pytorch_p36/lib/python3.6/multiprocessing/pool.py in map(self, func, iterable, chunksize)
286 in a list that is returned.
287 '''
--> 288 return self._map_async(func, iterable, mapstar, chunksize).get()
289
290 def starmap(self, func, iterable, chunksize=None):
~/anaconda3/envs/pytorch_p36/lib/python3.6/multiprocessing/pool.py in get(self, timeout)
668 return self._value
669 else:
--> 670 raise self._value
671
672 def _set(self, i, obj):
RuntimeError: Initialization failed.