Simultaneous modeling of long-term and short-term trends in a state-space model


I am trying to model long term and short term trends of stock prices simultaneously in stan with a state space model.
I am not using a pre-moving average, but rather an internal stan
Weekly trend


and assuming a first-order linear trend, transforming


For stock prices, we have


but the estimated trend oscillates and sigma is estimated abnormally low.
Please advise!

Hi, @biones. I’m sorry nobody’s responded to your post, but it’s very hard to diagnose part of a model without data. Do you have a Stan program that compiles? I couldn’t quite follow the hints you’re leaving here as I’m having trouble turning them into code.

If there’s a way you can just write the model out in mathematical notation, that would also help.

The model you have is odd for a generative model in that it doesn’t seem to be modeling the data, but only the trends and its modeling trends based on other trends. Have you tried reformulating to generate a single value at a time?

In general, the answer to problems like these is to simulate data from the model and see if you can recover the true parameters in their posterior intervals.

1 Like

Thank you for your answer, sorry for the delay in replying. The code is as follows.

I feel that modeling latent variables with even deeper variables is not a Bayesian problem, but am I wrong in my perception of hierarchical Bayesianism? If anything, I feel like I am rattled by the act of taking averages.

  int<lower=0>  N;
  real y[N];

parameters {
  real<lower=0> sigma_v;
  real mu;              
  vector<lower=13,upper=17>[N] trend;
  real<lower=0,upper=3> s_trend;
  vector[N] trend_w;
  real<lower=0,upper=0.5> s_trend_w;
  real<lower=0.03,upper=1> sigma_dw;
  vector[N] trend_2w;
  real<lower=0,upper=0.02> s_trend_2w;
  real<lower=0.02,upper=0.3> s_2ww;


    real tb1;
    real tb2;


    trend[3:N] ~ normal(2*trend[2:(N-1)]-trend[1:(N-2)],s_trend);
    int rnk;





Hi @biones, I wonder if you can achieve this using a dynamic Generalized Additive Model. I maintain the {mvgam} R package (Multivariate (Dynamic) Generalized Additive Models • mvgam), which is designed for setting up complex regression models that include dynamic processes in Stan. For your problem, you could smooth over weeks using a penalized spline or GP, and you could also include an AR process to capture short-term variation. Both of these components could contribute to evolution of the hidden state process, while other components could go into the observation model. Let me know if this is something you might consider, I’d be happy to help set up the data and model so you could give it a shot.

1 Like