Help on a state space modeling

Hello guys, I am trying to estimate a state space model on some simulated time series data (which are generated by interactions between 3 nodes). So, from below state equation and observation equation, I would like to recover parameters x, A, C, W, and E.

x[t] = Ax[t-1] + W
y[t] = Cx[t] + E

I am new to STAN and I would like to get some helps on how to make the model to recover the parameters better.

I am especially facing difficulties with setting priors for the parameters and setting proper models. (From other posts, I understand that setting proper priors is crucial, but I am lack of those intuitions yet)

prepare data

N = 500
A = np.array([[0.9, 0.0, 0.0 ], [-0.5, 0.9, 0.0 ], [0.5, 0.0, 0.9]])
C = np.array([[0.8, 0.3, 0 ], [0, 0.7, 0.5], [0.4, 0.2, 0.7]])
varW = 0.02
varE = 0.02
M = 3
y = np.zeros((M,N))
x = np.zeros((M,N))
x[:,0] = np.random.multivariate_normal(np.zeros(M),varWnp.eye(M))
y[:,0] = np.random.multivariate_normal(np.zeros(M),varE
for t in np.arange(1,N):
x[:,t] =,x[:,t-1]) + np.random.multivariate_normal(np.zeros(M),varWnp.eye(M))
y[:,t] =,x[:,t]) + np.random.multivariate_normal(np.zeros(M),varE

The Stan code used

data {
    int<lower =1> N;
    int<lower=1> M;
    matrix[M,N] y;

    matrix[M,M] A;
    real<lower=0>  E;
    real<lower=0>  W;
    matrix[M,N] x;
    matrix[M,M] C;

model {    
    for (m in 1:M){
        x[m,1] ~ normal(0,W);
        y[m,1] ~ normal(0,E);

    for (t in 2:N){
         x[1:M,t] ~ normal(A*x[1:M,t-1], W);
         y[1:M,t] ~ normal(C*x[1:M,t] , E);

I have to admit that my Stan code needs a lot of corrections. And with the current Stan code, I get the following warnings and no convergence for the parameters.


WARNING:pystan:Maximum (flat) parameter count (1000) exceeded: skipping diagnostic tests for n_eff and Rhat.
To run all diagnostics call pystan.check_hmc_diagnostics(fit)
WARNING:pystan:1 of 500 iterations ended with a divergence (0.2 %).
WARNING:pystan:Try running with adapt_delta larger than 0.8 to remove the divergences.
WARNING:pystan:17 of 500 iterations saturated the maximum tree depth of 10 (3.4 %)
WARNING:pystan:Run again with max_treedepth larger than 10 to avoid saturation
WARNING:pystan:Chain 1: E-BFMI = 0.146
WARNING:pystan:Chain 2: E-BFMI = 0.00324
WARNING:pystan:E-BFMI below 0.2 indicates you may need to reparameterize your model

It will be really grateful if I can get some help and make this model to recover the parameters.



Hey, I think Stan won’t fit anything well that has symmetries in the parameter space. On first approximation your model looks like it has a high degree of freedom and it isn’t obvious to me what would make it identifiable. Ie what would make it so that it’s MAP estimate would be given by only one set of parameters.

You’re missing priors on W and E as well.

You can’t have every parameter free in this sort of model. I link to some state space stuff connected to stan here: R package ctsem - hierarchical continuous time dynamic models