# Is there a way to call NUTS directly to sample a customized likelihood together with its gradient?

**URL:** <https://discourse.pymc.io/t/is-there-a-way-to-call-nuts-directly-to-sample-a-customized-likelihood-together-with-its-gradient/4665>\
**Category:** Questions\
**Created:** [March 11, 2020, 3:47pm UTC](https://discourse.pymc.io/t/is-there-a-way-to-call-nuts-directly-to-sample-a-customized-likelihood-together-with-its-gradient/4665 "2020-03-11T15:47:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![astrojhgu](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@astrojhgu](https://discourse.pymc.io/u/astrojhgu)\
**Post date:** [March 11, 2020, 3:47pm UTC](https://discourse.pymc.io/t/is-there-a-way-to-call-nuts-directly-to-sample-a-customized-likelihood-together-with-its-gradient/4665/1 "2020-03-11T15:47:13Z")

</div>

I have a log-prob function and its gradient. Because FFT is involved in the log-prob function and its gradient, it is not possible to perform autodiff. So I do the differential by hand.

Is there a (documented) way to call the NUTS sampler directly?

I did notice there is [a way](http://mattpitkin.github.io/samplers-demo/pages/pymc3-blackbox-likelihood/) to create a customized distribution, which however does not mention how to supply the gradient.

Thanks.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [March 12, 2020, 6:56am UTC](https://discourse.pymc.io/t/is-there-a-way-to-call-nuts-directly-to-sample-a-customized-likelihood-together-with-its-gradient/4665/2 "2020-03-12T06:56:25Z")

</div>

The recommanded approach is create a custom Theano Ops with its gradient. You can see a example here: [https://docs.pymc.io/Advanced\_usage\_of\_Theano\_in\_PyMC3.html#writing-custom-theano-ops](https://docs.pymc.io/Advanced_usage_of_Theano_in_PyMC3.html#writing-custom-theano-ops) and a more in complex example here: [https://docs.pymc.io/notebooks/blackbox\_external\_likelihood.html](https://docs.pymc.io/notebooks/blackbox_external_likelihood.html)

---

<div class="post-metadata">

**Author:** ![nkaimcaudle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/nkaimcaudle/32/2142_2.png) [@nkaimcaudle](https://discourse.pymc.io/u/nkaimcaudle)\
**Post date:** [March 13, 2020, 9:51am UTC](https://discourse.pymc.io/t/is-there-a-way-to-call-nuts-directly-to-sample-a-customized-likelihood-together-with-its-gradient/4665/3 "2020-03-13T09:51:44Z")

</div>

I am not sure but perhaps this is what you are after [Isolate NUTS into a new library](https://discourse.pymc.io/t/isolate-nuts-into-a-new-library/3974/7)
