# Generating a random matrix with MvNormal

**URL:** <https://discourse.pymc.io/t/generating-a-random-matrix-with-mvnormal/4175>\
**Category:** Questions\
**Created:** [December 4, 2019, 4:32am UTC](https://discourse.pymc.io/t/generating-a-random-matrix-with-mvnormal/4175 "2019-12-04T04:32:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpananos](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/dpananos/32/925_2.png) [@dpananos](https://discourse.pymc.io/u/dpananos)\
**Post date:** [December 4, 2019, 4:32am UTC](https://discourse.pymc.io/t/generating-a-random-matrix-with-mvnormal/4175/1 "2019-12-04T04:32:38Z")

</div>

Is it possible to generate a matrix comprised of N draws from a multivariate normal from within the `pm.Model()` context?

Here is what I’ve tried

```
import pymc3 as pm

N = 10
Sigma = np.eye(2)

with pm.Model() as model:
    
    X = pm.MvNormal('X', mu=np.zeros(2), cov = Sigma, shape = N)
    betas = pm.Normal('betas', 0, 1, shape = 2)
    y = pm.Deterministic('y', pm.math.dot(X,betas))
    
    prior_pred = pm.sample_prior_predictive(1)

```

However, I receive the following error:

`ValueError: operands could not be broadcast together with shapes (10,) (2,)`

Clearly some sort of shape error, but when I try `X=pm.MvNormal('X',mu=np.zeros(2), cov=Sigma, shape=(N,2))` I get

`ValueError: 1-dimensional argument does not have enough dimensions for all core dimensions ('i_0_0', 'i_0_1')`

What’s the problem here? How can I generate what I want from within the model context?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [December 4, 2019, 6:09am UTC](https://discourse.pymc.io/t/generating-a-random-matrix-with-mvnormal/4175/2 "2019-12-04T06:09:20Z")

</div>

There are some shape weirdness happening here, but you can try:

```python
with pm.Model() as model: 
  X = pm.MvNormal('X', mu=np.zeros(2), cov=Sigma, shape=(N, 2)) 
  betas = pm.Normal('betas', 0, 1, shape=2) 
  y = pm.Deterministic('y', pm.math.dot(X,betas)) 
  prior_pred = pm.sample_prior_predictive(10) # <== using something > 1

```

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [December 4, 2019, 6:10am UTC](https://discourse.pymc.io/t/generating-a-random-matrix-with-mvnormal/4175/3 "2019-12-04T06:10:31Z")

</div>

spoke too fast, still getting wrong answer - definitely a bug

Could you raise an issue?

---

<div class="post-metadata">

**Author:** ![dpananos](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/dpananos/32/925_2.png) [@dpananos](https://discourse.pymc.io/u/dpananos)\
**Post date:** [December 4, 2019, 6:11am UTC](https://discourse.pymc.io/t/generating-a-random-matrix-with-mvnormal/4175/4 "2019-12-04T06:11:29Z")

</div>

Yep, will raise now.

---

<div class="post-metadata">

**Author:** ![dpananos](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/dpananos/32/925_2.png) [@dpananos](https://discourse.pymc.io/u/dpananos)\
**Post date:** [December 4, 2019, 6:17am UTC](https://discourse.pymc.io/t/generating-a-random-matrix-with-mvnormal/4175/5 "2019-12-04T06:17:34Z")

</div>

> <https://github.com/pymc-devs/pymc3/issues/3706>
>
> See here for more.
> When attempting to simulate an Nxp random matrix in which the rows are draws from a multivariate normal,...

Issue here.
