# Fitting a model with a Poisson process

**URL:** <https://discourse.mc-stan.org/t/fitting-a-model-with-a-poisson-process/5178>\
**Category:** Modeling\
**Created:** [August 12, 2018, 1:55am UTC](https://discourse.mc-stan.org/t/fitting-a-model-with-a-poisson-process/5178 "2018-08-12T01:55:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexis](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Alexis](https://discourse.mc-stan.org/u/Alexis)\
**Post date:** [August 12, 2018, 1:55am UTC](https://discourse.mc-stan.org/t/fitting-a-model-with-a-poisson-process/5178/1 "2018-08-12T01:55:24Z")

</div>

Hi all, I’m trying to fit a model that includes a Poisson process

\begin{equation} \begin{split} X\_t & = PoissonProcess(\lambda) \hspace{12pt} 0 \leq t \leq 100 \\ Y\_i &\sim Normal(X\_i, 0.05 ) \hspace{12pt} i = 1, \cdots 100 \end{split} \end{equation}

For illustration, see the picture below. The Poisson process X\_t with rate \lambda generates 3 arrivals, at t=(20,50,80), which corresponds to dividing the timeline into 4 pieces. We observe only Y as noisy versions of the 4 means (0,1,2,3). Then given the data we want to do inference on at least \lambda, but X\_t or the arrival times would be ideal.

![poissonpic](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/c/c2e4720bcfa929860eb1543151396e52c204ba6c.png)

* * *

Here’s what I have so far. It’s complaining about

1. passing the array “int x[]” on line 3, and more importantly
2. passing the poisson distribution to the normal distribution, near the end

Are these two insurmountable? Please let me know.

```stan
//Setting up the poisson model
functions {
    real cusum(int n, int x[]){
      int x2[n];
      x2[1] = x[1];
      for(i in 2:n){
        x2[i] = x2[i-1] + x[i];
      }
      return x2;
    }
  }
data {
    int<lower=0> T; // how long the time series is
    real Y[T]; // the observed data
  }
parameters {
  real<lower=0> lambda; // poisson rate
}
model {
  real lambda_vec[T];

  lambda ~ gamma(0.08,1); //prior
  lambda_vec = rep_array(lambda, T);

  Y ~ normal(cusum(poisson(lambda_vec)), 0.05);
  
}

```

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [August 22, 2018, 10:22am UTC](https://discourse.mc-stan.org/t/fitting-a-model-with-a-poisson-process/5178/2 "2018-08-22T10:22:34Z")

</div>

We have a built-in `cumulative_sum` function, so you don’t need to write that one. That would require `n == size(x)`.

> [@Alexis](#):
>
> normal(cusum(poisson(lambda\_vec))

This isn’t legal Stan code. I’m not quite sure what you’re trying to do here. Do you want `poisson` to be the Poisson log probability mass function? If so, it needs a variate. If you wanted those to be latent, that’s not something Stan can do unless they’re small and can be marginalized out.
