# Matmul on deterministic variable TypeError

**URL:** <https://discourse.pymc.io/t/matmul-on-deterministic-variable-typeerror/9422>\
**Category:** v3\
**Created:** [May 13, 2022, 8:47pm UTC](https://discourse.pymc.io/t/matmul-on-deterministic-variable-typeerror/9422 "2022-05-13T20:47:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davipatti](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/davipatti/32/5146_2.png) [@davipatti](https://discourse.pymc.io/u/davipatti)\
**Post date:** [May 13, 2022, 8:47pm UTC](https://discourse.pymc.io/t/matmul-on-deterministic-variable-typeerror/9422/1 "2022-05-13T20:47:35Z")

</div>

Hi there

I’m trying to run a [non-centered](https://twiecki.io/blog/2017/02/08/bayesian-hierchical-non-centered/) hierarchical model, and running into a type error while trying to do matrix multiplication on a `pm.Deterministic` variable.

I have tried various different things like using `np.matmul` explicitly, using `np.dot` and taking the `beta_unit * beta_sigma + beta_mu` expression out of the `pm.Deterministic` class, but I run into errors with everything I try. (Happy to provide more details here).

This seems like relatively simple functionality; I wonder if anyone else has run into this, or if there’s a known way to achieve this?

Thanks in advance for any advice  
David

```python
import numpy as np
import pymc3 as pm

# random data to illustrate TypeError
X = np.random.randn(104, 216)
Y = np.random.rand(104)

# model
with pm.Model() as non_centered_model:
    
    # hyperpriors
    beta_mu = pm.Normal("beta_mu", mu=1, sigma=1)
    beta_sigma = pm.Exponential("beta_sigma", 1)
    
    # priors
    beta_unit = pm.Normal("beta_unit", mu=0, sigma=1, shape=X.shape[1])
    beta = pm.Deterministic("beta", beta_unit * beta_sigma + beta_mu)
    sigma = pm.Exponential("sigma", 1)
    
    # likelihood
    pm.Normal("lik", mu=X @ beta, sigma=sigma, observed=Y)
    
    idata_non_centered = pm.sample(return_inferencedata=True)

```

```auto
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [150], in <cell line: 9>()
     18 sigma = pm.Exponential("sigma", 1)
     20 # likelihood
---> 21 pm.Normal("lik", mu=X @ beta, sigma=sigma, observed=Y)
     23 idata_non_centered = pm.sample(return_inferencedata=True)

TypeError: unsupported operand type(s) for @: 'numpy.ndarray' and 'DeterministicWrapper'

```

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [May 13, 2022, 8:50pm UTC](https://discourse.pymc.io/t/matmul-on-deterministic-variable-typeerror/9422/2 "2022-05-13T20:50:49Z")

</div>

When performing algebraic operations on parameters, you often have the functions in [`pm.math`](https://docs.pymc.io/en/latest/api/math.html) (many of which mirror their numpy counterparts). Also, see [here](https://discourse.pymc.io/t/matrix-multiplication-in-pymc3/1114).

---

<div class="post-metadata">

**Author:** ![davipatti](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/davipatti/32/5146_2.png) [@davipatti](https://discourse.pymc.io/u/davipatti)\
**Post date:** [May 16, 2022, 5:45pm UTC](https://discourse.pymc.io/t/matmul-on-deterministic-variable-typeerror/9422/3 "2022-05-16T17:45:21Z")

</div>

Thanks for that. Sorry for not properly searching the docs.
