Hi everyone, I’m wondering if it’s possible to use Stan for the following problem:
I have 2 time-series y(t) and z(t) and the relation I want to model is
z(t) = \int_{t - b}^{t} y(s)\mathrm{d}s ,
with b a positive real number. The idea I’m using now is basically re-writing the integral as a dot product of 2 vectors: z(t) = v(t, b)\cdot y(t) where the vector v(t, b) equals to 1 in the interval (t - b, t) and then it decays smoothly to zero outside of those values. Does that sound like a reasonable approach? I feel like there must be a better way so I’m looking for suggestions (below my current code).
functions {
real smoothstep(real x) {
// A smooth version of the step function
if (x < 0) {return 0;}
if (x > 1) {return 1;}
return 6*x^5 - 15*x^4 + 10*x^3;
}
vector smoothstep_vector(int T, real x) {
vector[T] result;
for (t in 1:T) {
result[t] = smoothstep(t - x);
}
return result;
}
}
data {
int T;
int S; // ignore the first S points
int y[T];
int z[T];
}
transformed data {
vector[T] vy = to_vector(y);
}
parameters {
real<lower=0> win;
}
model {
win ~ normal(10, 1);
for (t in S:T) {
z[t] ~ poisson(1e-5 + vy[:t]' * smoothstep_vector(t, t - win));
}
}
Thank you all! :)