# Error evaluating 1D integral for convolution

**URL:** <https://discourse.mc-stan.org/t/error-evaluating-1d-integral-for-convolution/29877>\
**Category:** Modeling\
**Tags:** rstan, fitting-issues\
**Created:** [December 23, 2022, 9:59pm UTC](https://discourse.mc-stan.org/t/error-evaluating-1d-integral-for-convolution/29877 "2022-12-23T21:59:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksusvita](https://avatars.discourse-cdn.com/v4/letter/k/3ab097/32.png) [@ksusvita](https://discourse.mc-stan.org/u/ksusvita)\
**Post date:** [December 23, 2022, 9:59pm UTC](https://discourse.mc-stan.org/t/error-evaluating-1d-integral-for-convolution/29877/1 "2022-12-23T21:59:32Z")

</div>

Hello, I have a problem evaluating an integral. The error is about ill-type arguments for integrate\_1d function. I double check with the Stan documentation and some forum discussions, but still, I got the same error.

This is my Stan script:

```stan
functions {
    real integrand_fgd(real u, real xc, real[] theta, real[] x_r, int x_i) {
      real mu = theta[1];
      real sigma = theta[2];
      real ti = theta[3];
      real a = (mu/sigma) * (mu/sigma);
      real b = mu/(sigma*sigma);

      real gt = gamma_lpdf(u| a, b) + gamma_lpdf(u-ti| a, b);
      return exp(gt);
    }
}

data {
  int N;
  vector[N] x;

  // prior parameter
  vector[2] pr_sigma;
  vector[2] pr_mu;
}

transformed data {
  real x_r[0];
  int x_i[0];
}

parameters {
  real<lower=0> mu;
  real<lower=0> sigma;
}

model {
  //prior
  target += inv_gamma_lpdf(sigma| pr_sigma[1], pr_sigma[2]); //prior of sigma
  target += normal_lpdf(mu| pr_mu[1], pr_mu[2]) - normal_lccdf(0| pr_mu[1], pr_mu[2]); //prior for mu

  for(i in 1:N) {
    real left_limit = x[i];
    target += log(2) + log(integrate_1d(integrand_fgd,
                                        left_limit,
                                        positive_infinity(),
                                        {mu, sigma, left_limit},
                                        x_r, x_i, 1e-8));
  }
}

```

---

<div class="post-metadata">

**Author:** ![caesoma](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/caesoma/32/19767_2.png) [@caesoma](https://discourse.mc-stan.org/u/caesoma)\
**Post date:** [December 28, 2022, 9:19am UTC](https://discourse.mc-stan.org/t/error-evaluating-1d-integral-for-convolution/29877/2 "2022-12-28T09:19:47Z")

</div>

Can you post the error message you are getting? It may have additional hints on what part of the arguments is raising this error.

Stan’s `integrand` type involves several types, so there’s probably many places it could go wrong.

---

<div class="post-metadata">

**Author:** ![ksusvita](https://avatars.discourse-cdn.com/v4/letter/k/3ab097/32.png) [@ksusvita](https://discourse.mc-stan.org/u/ksusvita)\
**Post date:** [December 28, 2022, 5:04pm UTC](https://discourse.mc-stan.org/t/error-evaluating-1d-integral-for-convolution/29877/3 "2022-12-28T17:04:26Z")

</div>

Sure. This is the error I got:

````stan
Error in stanc(filename, allow_undefined = TRUE) : 0

Semantic error in 'string', line 33, column 27 to line 37, column 55:

Ill-typed arguments supplied to function 'integrate_1d'. Available signatures: 
((real, real, real[], data real[], data int[]) => real, real, real, real[], data real[], data int[]) => real
((real, real, real[], data real[], data int[]) => real, real, real, real[], data real[], data int[], data real) => real
Instead supplied arguments of incompatible type: (real, real, real[], real[], int) => real, real, real, real[], real[], int[], real.
```stan
````

---

<div class="post-metadata">

**Author:** ![caesoma](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/caesoma/32/19767_2.png) [@caesoma](https://discourse.mc-stan.org/u/caesoma)\
**Post date:** [December 29, 2022, 8:56am UTC](https://discourse.mc-stan.org/t/error-evaluating-1d-integral-for-convolution/29877/4 "2022-12-29T08:56:49Z")

</div>

The error message says the function expects an integrand with type including `data real[], data int[]` but gets `real[], int[]`. The [documentation on integrate\_1d](https://mc-stan.org/docs/functions-reference/functions-1d-integrator.html) mentions those being “data values” and “integer data” but just say `real` and `int`, so that can be confusing.  
I remember being puzzled something similar with an [ODE solver](https://mc-stan.org/docs/functions-reference/functions-ode-solver.html#functions-ode-solver), but maybe the [algebraic solver](https://mc-stan.org/docs/functions-reference/functions-algebraic-solver.html#functions-algebraic-solver) explains it better.

It also says `int` is provided when where `data int[]` is expected, but you are listing `int x_i[0]`, so I’d guess that is not the problem. In any case you should be able to debug the inputs to make sure the type is correct and get it working.
