# How to create and evaluate logp of Deterministic MvNormal as part of PyMC model? (Or use Potential)

**URL:** <https://discourse.pymc.io/t/how-to-create-and-evaluate-logp-of-deterministic-mvnormal-as-part-of-pymc-model-or-use-potential/11959>\
**Category:** v5\
**Tags:** modeling\
**Created:** [April 23, 2023, 9:05pm UTC](https://discourse.pymc.io/t/how-to-create-and-evaluate-logp-of-deterministic-mvnormal-as-part-of-pymc-model-or-use-potential/11959 "2023-04-23T21:05:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![quantumstrider](https://avatars.discourse-cdn.com/v4/letter/q/edb3f5/32.png) [@quantumstrider](https://discourse.pymc.io/u/quantumstrider)\
**Post date:** [April 23, 2023, 9:05pm UTC](https://discourse.pymc.io/t/how-to-create-and-evaluate-logp-of-deterministic-mvnormal-as-part-of-pymc-model-or-use-potential/11959/1 "2023-04-23T21:05:40Z")

</div>

I’m fitting for the parameters of a multi-variate (3D) Gaussian: the 3 means, the 3 standard deviations, and only one correlation coefficient for the covariance matrix (I’m allowing a covariance between only two variables, the other off-diagonal terms of the covariance matrix are assumed to be zero). I am new to PyMC. I have a working version based on scipy+numpy where I’ve used the pytensor @as\_op decorator on a forward\_model() function that takes in the vector of means and the covariance matrix, and then spits out the probability for a bunch of points according to the current Gaussian. This works but it’s very slow to sample and PyMC chooses the Slice sampler by default – I think because I’m using things like scipy.stats.multivariate\_normal and np.tensordot inside my forward\_model() function, instead of using pymc.MvNormal and pytensor.tensor operations (which I think would allow for NUTS sampling).

I have a very basic question – how do I convert the following 2 lines of scipy code to instead use pm.MvNormal and pytensor.tensor.dotproduct so that I can put it all directly into my “with pm.Model() as model” context without needing a separate decorated forward\_model function?

`mvn = scipy.stats.multivariate_normal(mean=vector_means,cov=matrix_covariances)`  
`probs = mvn.pdf(points)`

Here points is a list of size-3 [x,y,z] pairs at which we will evaluate the probability given the prescribed 3D Gaussian parameters vector\_means and matrix\_covariances (the latter are concatenated from pm.Uniform Stochastic Random Variable objects).

I tried to do

`mvn = pm.MvNormal('mvn',mu=vector_means,cov=matrix_covariances)`  
`probs = pm.Deterministic('probs', pt.exp(pm.logp(mvn,points)))`

but while pm.sample does work, it is treating mvn as another random variable to sample (whereas it should only be sampling the 3 mean, 3 standard deviation and 1 correlation coefficient parameters).

So I guess I want mvn to be some kind of Deterministic (or Potential?) object which is fully determined by the 7 stochastic RVs in its inputs vector\_means and matrix\_covariances. Finally I tried

`mvn = pm.Deterministic('mvn',pm.MvNormal('mvn_inside',mu=vector_means,cov=matrix_covariances))`  
`probs = pm.Deterministic('probs', pt.exp(pm.logp(mvn,points)))`

but then I get an error for the pm.logp call:  
`TypeError: When RV is not a pure distribution, value variable must have the same type`

Any suggestions? Is there a standard way of evaluating MvNormal as part of a PyMC model?

---

<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:** [April 24, 2023, 6:16am UTC](https://discourse.pymc.io/t/how-to-create-and-evaluate-logp-of-deterministic-mvnormal-as-part-of-pymc-model-or-use-potential/11959/2 "2023-04-24T06:16:22Z")

</div>

You can use `pm.MvNormal.dist(...)` to create a distribution that is not a model variable. Then you can retrieve it’s logp the same way with `pm.logp`.
