Help with a change point model

Thank you for your reply… You are of course right. The operative observation was that the if branch causes discontinuity and that if it were smoother the problem wouldn’t exist, which made a swath of other things click for me.

For example, @idontgetoutmuch proposes a continuous time change point model here. In the model, i is incremented and \tau is a change point, he engineers a smooth if statement using logit^{-1}(2(i-\tau)). The logit function is small when i-\tau<0 and then suddenly large when i-\tau>0, the scale of the transition is determined by the constant (2 in this case).

With that tweak added the following version is accurate and near instant. Thank you for your help @nhuurre!

data {
  int M;
  int N;
  int a;
  int b;
  int r_max;
  real y[M,N];
}
parameters {
  real<lower=0,upper=1> theta;
}
model {
  for(i in (a-r_max):(a+r_max)) {
    for(j in (b-r_max):(b+r_max)) {
      real d = sqrt((a-i)^2+(b-j)^2);
      if(d>r_max) continue;
      target+=log_mix(inv_logit(20*(d-r_max*theta)),
                      normal_lpdf(y[i,j]|0,1),
                      log_mix(0.8,normal_lpdf(y[i,j]|7,.1),normal_lpdf(y[i,j]|0,1)));
    }
  }
}
1 Like