# How to implement a GLM with a personalized link function different from the Identity function?

**URL:** <https://discourse.pymc.io/t/how-to-implement-a-glm-with-a-personalized-link-function-different-from-the-identity-function/7531>\
**Category:** Questions\
**Created:** [June 3, 2021, 8:59pm UTC](https://discourse.pymc.io/t/how-to-implement-a-glm-with-a-personalized-link-function-different-from-the-identity-function/7531 "2021-06-03T20:59:33Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![jlindbloom](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jlindbloom/32/5016_2.png) [@jlindbloom](https://discourse.pymc.io/u/jlindbloom)\
**Post date:** [June 5, 2021, 3:02am UTC](https://discourse.pymc.io/t/how-to-implement-a-glm-with-a-personalized-link-function-different-from-the-identity-function/7531/4 "2021-06-05T03:02:01Z")

</div>

IMO, your best bet is to just implement the inverse of your link function using Theano tensor operations. You were on the right track earlier, but you don’t need to actually define a tensor and a Theano function. For your case, just writing

```auto
   likelihood = Normal('y', mu=1/(1+tt.exp( - (intercept + x_coeff * x) ) ), 
                        sd=sigma, observed=y)

```

should work. Not sure, but in your earlier attempt either you were overloading the variable `x` or PyMC3 wasn’t playing nice with the tensor you defined outside the model.

The above should solve your problem, but I’ll also address your approach using `DensityDist`. You got that error since `givens` is not a keyword argument to `DensityDist`. I think you should be able to just swap `givens` with `observed` to get it to work. See [this thread](https://discourse.pymc.io/t/unobserved-variables-in-blackbox-likelihood/3945) for some examples. However, think twice before you do this! If you’re calling `scipy.special.expit()` when you defined `logl()`, PyMC3 won’t be able to use the NUTS sampler since it can’t “see” the gradient of an outside black-box function and will fall back on using Metropolis. However, if you use Theano ops when defining `logl()` then I think it can get the gradients and you wouldn’t have this issue.

In either case, your link function is simple enough that you really just should use the one-liner

```auto
likelihood = Normal('y', mu=1/(1+tt.exp( - (intercept + x_coeff * x) ) ), sd=sigma, observed=y)

```

instead of a `DensityDist`.

---

_[View the full topic](https://discourse.pymc.io/t/how-to-implement-a-glm-with-a-personalized-link-function-different-from-the-identity-function/7531)._
