# Implementing an ExGaussian with a complementary error function

**URL:** <https://discourse.pymc.io/t/implementing-an-exgaussian-with-a-complementary-error-function/5063>\
**Category:** Questions\
**Tags:** theano\
**Created:** [May 11, 2020, 11:25am UTC](https://discourse.pymc.io/t/implementing-an-exgaussian-with-a-complementary-error-function/5063 "2020-05-11T11:25:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bobrepoman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bobrepoman/32/2255_2.png) [@Bobrepoman](https://discourse.pymc.io/u/Bobrepoman)\
**Post date:** [May 11, 2020, 11:25am UTC](https://discourse.pymc.io/t/implementing-an-exgaussian-with-a-complementary-error-function/5063/1 "2020-05-11T11:25:38Z")

</div>

Hey fellow Pymc3’ers 👋

I have been working on implementing an Exponentially modified Gaussian distribution (ExGaussian [https://en.wikipedia.org/wiki/Exponentially\_modified\_Gaussian\_distribution](https://en.wikipedia.org/wiki/Exponentially_modified_Gaussian_distribution)) as part of a model i am doing. For the ExGaussian part of the model i need the complementary error function erfc, which theano seems to have implemented

```
ExG(x;m,sigma,lamb)=(lamb/2.0)*tt.exp(lamb*(m +(lamb*sigma**2)/2.0-x))*tt.erfc((m+lamb*sigma**2-x)/(tt.sqrt(2.0)*sigma))

```

But when running the full model i observe some strange behavior. Initialising with Advi returns NaN on optimization, but i checked model.check\_test\_point() and there seems to be nothing odd like NaN or inf there. Running the model with auto or adapt\_diag works, but every single sample after tuning is divergent, though surprisingly the testcase i have created can sample the model parameters fairly well.

When searching for models that have used erfc (and erf) it seems there is a problem with the gradient calculation?

> <https://stackoverflow.com/questions/61259252/how-to-get-scipy-special-functions-to-take-pymc3-rvs-as-input>

> <https://github.com/pymc-devs/pymc3/issues/1209#issuecomment-239423116>
>
> I'm trying to convert this simple JAGS model to pymc3 and I'm getting type error with scipy.stats.norm.cdf.
> JAGS model (from https://github.com/drbenvincent/bayesian2afc/blob/master/jagsmodels/model1JAGS.txt):
> model{
> v...

I know Pymc3 has an ExGaussian implemented, but as far as i know continuous distributions are for RV’s and not to be used as functions defined over a linear array of x points.

Does anyone have more info on whether or not erf and erfc works in Theano/Pymc? I would be happy to share my full model if it helps, i just think i have narrowed it down to being related to the error function, since i ran a similar model using a lognormal before and did not see these issues with divergences.

Thanks for the help 😄
