# Hoping for some guidance / help with implementing custom log likelihood and gradient for research project (details below)

**URL:** <https://discourse.mc-stan.org/t/hoping-for-some-guidance-help-with-implementing-custom-log-likelihood-and-gradient-for-research-project-details-below/24598>\
**Category:** General\
**Created:** [September 28, 2021, 1:05am UTC](https://discourse.mc-stan.org/t/hoping-for-some-guidance-help-with-implementing-custom-log-likelihood-and-gradient-for-research-project-details-below/24598 "2021-09-28T01:05:30Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [October 8, 2021, 5:33pm UTC](https://discourse.mc-stan.org/t/hoping-for-some-guidance-help-with-implementing-custom-log-likelihood-and-gradient-for-research-project-details-below/24598/14 "2021-10-08T17:33:00Z")

</div>

Here is another example without having to change stanc3:

Stan model:

> <https://github.com/stan-dev/cmdstanpy/blob/2ed87fe2780e80d04d3dc783fac4ddb4762adb22/docsrc/examples/bernoulli_external.stan>

hpp file: [cmdstanpy/make\_odds.hpp at 2ed87fe2780e80d04d3dc783fac4ddb4762adb22 · stan-dev/cmdstanpy · GitHub](https://github.com/stan-dev/cmdstanpy/blob/2ed87fe2780e80d04d3dc783fac4ddb4762adb22/docsrc/examples/make_odds.hpp)

Note that the function is inside the namespace `bernoulli_external_model` which is the model name + the \_model suffix.

Then you need to set the following in the make/local file:

```
USER_HEADER=path/to/hpp/file.hpp
STANCFLAGS += --allow-undefined

```

an example with cmdstanr:

```r
library(cmdstanr)

stan_file <- "bernoulli_external.stan"

mod <- cmdstan_model(
  stan_file,
  cpp_options = list(USER_HEADER="/full/path/to/the/external/file/make_odds.hpp"),
  stanc_options = list("allow-undefined")
)
data_list <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1))
fit <- mod$sample(data = data_list)

```

Note that at the moment the USER\_HEADER at least for cmdstanr has to be the full absolute path.

---

_[View the full topic](https://discourse.mc-stan.org/t/hoping-for-some-guidance-help-with-implementing-custom-log-likelihood-and-gradient-for-research-project-details-below/24598)._
