Hi,
I am using the 3.10.0 pymc3 version and I know that the random method for the MvNormal was modified. I was using it and I faced a somewhat strange behavior. If I don’t explicitly define the shape argument I get a shape error, despite the fact that both mu and cov have a defined shape that could be inferred to do the sampling. From what I can see this happens because dist_shape
is set to be shapeless when the shape argument is not supplied.
Example,
sigma = 2.0
cov = pm.gp.cov.WhiteNoise(sigma)
X = np.linspace(0, 2, 200)[:, None]
K = cov(X).eval()
plt.plot(MvNormal.dist(mu=np.zeros(200), cov=K).random(size=3).T);
trace
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-829-c9af7d4d5a47> in <module>
5 K = cov(X).eval()
6
----> 7 plt.plot(MvNormal.dist(mu=np.zeros(200), cov=K).random(size=3).T);
<ipython-input-824-191770c5572e> in random(self, point, size)
99
100 mu = broadcast_dist_samples_to(to_shape=output_shape, samples=[mu], size=size)[0]
--> 101 param = np.broadcast_to(param, shape=output_shape + dist_shape[-1:])
102
103 assert mu.shape == output_shape
<__array_function__ internals> in broadcast_to(*args, **kwargs)
~/anaconda3/envs/phd/lib/python3.8/site-packages/numpy/lib/stride_tricks.py in broadcast_to(array, shape, subok)
178 [1, 2, 3]])
179 """
--> 180 return _broadcast_to(array, shape, subok=subok, readonly=True)
181
182
~/anaconda3/envs/phd/lib/python3.8/site-packages/numpy/lib/stride_tricks.py in _broadcast_to(array, shape, subok, readonly)
121 'negative')
122 extras = []
--> 123 it = np.nditer(
124 (array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'] + extras,
125 op_flags=['readonly'], itershape=shape, order='C')
ValueError: input operand has more dimensions than allowed by the axis remapping
To make it work:
sigma = 2.0
cov = pm.gp.cov.WhiteNoise(sigma)
X = np.linspace(0, 2, 200)[:, None]
K = cov(X).eval()
plt.plot(MvNormal.dist(mu=np.zeros(200), cov=K, shape=200).random(size=3).T);
I don’t know if this was intentionally defined this way because of some other reason, but it changes the way to use the random method from MvNormal. It is also certainly breaking a lot of examples that use MvNormal. Happy to submit an issue if this is a bug.
Thanks!
Luis