Fixing parameter values in the transformed parameters block

Suppose I have three parameter vectors A, B and C. I would like to know whether it is equivalent to (1) have A be length N - 1, or (2) define A_ and set A[1] = 0 in the transformed parameters block (code shown below). The latter has the advantage of simplifying my code, but I’ve tried both approaches and am getting different results. Am I correct to expect the results to be the same?

Option (1)

parameters {
    real A[N - 1];
    real B[N];
    real C[N];
}
model {
    for (n in 1:N) {
        if (n == 1) {
            X[n] ~ normal(B[n] + C[n], 1);
        }
        else {
            X[n] ~ normal(A[n - 1] + B[n] + C[n], 1);
        }
    }
}

Option (2)

parameters {
    real A_[N];
    real B[N];
    real C[N];
}
transformed parameters {
    real A[N] = A_;
    A[1] = 0;
}
model {
    X ~ normal(A + B + C, 1)
}

Hey! Welcome to the Stan forum!

I’d do something that’s a bit of a combination of the two.

parameters {
    real A_[N-1];
    real B[N];
    real C[N];
}
transformed parameters {
    real A[N];
    A[1] = 0;
    A[2:N] = A_;
}
model {
    // Idk if you meant to write real...
    // I suppose you meant something like "normal"?
    // ...it'S probably just an example...

    //for (n in 1:N) {
    //    X[n] ~ real(A[n] + B[n] + C[n], 1); 
    //}

    // ...but remember that you can vecorize a lot of ~ calls:
    X ~ normal(A + B + C, 1)
}

Hope this get’s you what you need!

Cheers!
Max

Hi Max, thanks for your help. I’ll switch to your version, but for the sake of my understanding, in the Option (2) that I wrote, am I using (the possibly nonzero) A_[1] during inference?

Yes. Lets say N=6, then A_ is length 5 and A is length 6. Then, A[1] = 0, A[2] = A_[1] and A[3] = A_[2], … A[6] = A_[5].

Or A[2:N] = A_[1:(N-1)], so all the A_ are used.

EDIT: Whoops, I misread your post! Sorry! No, you are not “using” it “in” your model, but it’s still present. Like adding an unrestricted Uniform r.v. to your model. This is probably why your results are different.

1 Like

Sorry! I misread your question.

Everything in parameters is “in” your model, just not explicitly. Implicitly every parameter gets an \text{Uniform}(-\infty,+\infty) lpdf assigned, which is added to the target (the log-prob accumulator). So, “unused” parameters are still implicitly in your model.

2 Likes