Hi, I was trying to implement a simpler version of this state space model

the model is just random walk with noise marginalized using the kalman filter:

```
// _test.stan
data {
int<lower=0> N;
vector[N] y;
real P0;
real x0;
}
parameters {
real<lower=0> sigma_y; // process error
real<lower=0> sigma_x; // observation error
}
transformed parameters {
vector[N] x; //predicted mean x_t|y_1:t-1
vector[N] m; //filtered mean x_t|y_1:t
vector[N] S; //measurement variance, y_t|y_1:t-1
vector[N] P_pred; //predicted var x_t|y_1:t-1
{
vector[N] P; //filtered var x_t|y_1:t
real R; //measurement variance, y_t|x_t
real K; //Kalman gain, depends on t but not stored
//Filtering
R = sigma_y * sigma_y;
x[1] = x0;
P_pred[1] = P0;
for (t in 1:N) {
//Prediction step
if (t>1) {
x[t] = m[t-1];
P_pred[t] = P[t-1] + sigma_x*sigma_x;
}
print(t, " ", x[t]);
//Update step
S[t] = P_pred[t] + R;
K = P_pred[t]/S[t];
m[t] = x[t] + K*(y[t] - x[t]); //The measurement is just noise added to signal, so mu==m_pred
P[t] = P_pred[t] - K*S[t]*K;
}
}
}
model {
sigma_x ~ cauchy(0,1);
sigma_y ~ cauchy(0,1);
y ~ normal(x, sqrt(S));
}
```

I feed the model the following data

```
par(mfrow=c(2,1), mar=c(2,2,1,0.5))
N <- 100
x <- rep(0, N)
y <- rep(0, N)
y[1] <- x[1]
sigma_x <- 0.25
sigma_y <- 0.3
for(i in 2:N) {
x[i] <- x[i-1] + rnorm(1, 0, sigma_x)
y[i] <- x[i] + rnorm(1, 0, sigma_y)
}
plot(x, type="l", col="red"); grid()
points(y, pch=16, col="blue")
m <- cmdstanr::cmdstan_model("_test.stan")
fit <- m$sample(data=list(N=length(y), y=y, x0=y[1], P0=1), chains=1, iter_warmup = 250, iter_sampling = 250)
```

when I run the sampler everything goes fine and the parameters are correctly estimated. However, at the beginning of the sampling I get the warnings:

Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:

Chain 1 Exception: normal_lpdf: Location parameter[3] is -nan, but must be finite! (in ‘/tmp/RtmpkkXaSX/model-16531619875d.stan’, line 46, column 2 to column 25)

Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,

Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.

I cannot quite get where the problem is, I guess it is related with the initial values or something…

thank you very much