# How to input variables into some functions in the model

**URL:** <https://discourse.pymc.io/t/how-to-input-variables-into-some-functions-in-the-model/9088>\
**Category:** v3\
**Created:** [March 24, 2022, 1:43pm UTC](https://discourse.pymc.io/t/how-to-input-variables-into-some-functions-in-the-model/9088 "2022-03-24T13:43:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![W\_H](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/w_h/32/4981_2.png) [@W\_H](https://discourse.pymc.io/u/W_H)\
**Post date:** [March 24, 2022, 1:43pm UTC](https://discourse.pymc.io/t/how-to-input-variables-into-some-functions-in-the-model/9088/1 "2022-03-24T13:43:14Z")

</div>

Hi all, I am new to pymc3 and trying to migrate some code from Turing in Julia to pymc3. Here is a toy model of my work.

```auto
import numpy as np
import scipy
import pymc3 as pm
mu_z, Sigma_z = np.zeros(2), np.eye(2)
r = scipy.stats.multivariate_normal.rvs(np.ones(2), np.eye(2), size=5)
with pm.Model() as model:
  z = pm.MvNormal("z", mu=mu_z, cov=Sigma_z, shape=len(mu_z))
  f = lambda x : scipy.special.erf(x)
  r = pm.MvNormal("r", f(z), np.eye(2), observed=r)

```

What I want to do here is that setting a prior for the variable `z`, and implement some nonlinear transform on it (`erf`), and use the transformed value as the mean of the likelihood `r`.  
The error message says that the ufunc `erf` not supported for the input types. Therefore, can anybody help me solve this problem? Thank you very much!

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [March 24, 2022, 2:09pm UTC](https://discourse.pymc.io/t/how-to-input-variables-into-some-functions-in-the-model/9088/2 "2022-03-24T14:09:22Z")

</div>

If that’s exactly what you need, you can use `pm.math.erf`

---

<div class="post-metadata">

**Author:** ![W\_H](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/w_h/32/4981_2.png) [@W\_H](https://discourse.pymc.io/u/W_H)\
**Post date:** [March 24, 2022, 3:14pm UTC](https://discourse.pymc.io/t/how-to-input-variables-into-some-functions-in-the-model/9088/4 "2022-03-24T15:14:58Z")

</div>

Thanks! This function works for me. Meanwhile, it leads to one other problem. I want to construct a matrix whose diagonal is the result of `pm.math.erf(x)` . For example, just like what we can do in numpy `np.diag(scipy.special.erf(x))` . How can we implement this in pymc?

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [March 24, 2022, 3:29pm UTC](https://discourse.pymc.io/t/how-to-input-variables-into-some-functions-in-the-model/9088/5 "2022-03-24T15:29:28Z")

</div>

In general you can use `theano.tensor` (pymc3) or `aesara.tensor` (pymc 4) which implements most of numpy and scipy functions, but for the type of objects that are used by PyMC.

`pm.math` is just a helper to access some of the most common ones.

[https://aesara.readthedocs.io/en/latest/library/tensor/basic.html#aesara.tensor.var.\_tensor\_py\_operators.diagonal](https://aesara.readthedocs.io/en/latest/library/tensor/basic.html#aesara.tensor.var._tensor_py_operators.diagonal)
