# LHS vs RHS sampling advantages, sum-to-zero QR based

The following code does the transformation of the QR based sum-to-zero constraint on the
LHS.

Is there any advantage(s) if we use LHS sampling, instead of data transformation on the RHS?

``````data {
int N;
}
transformed data {
matrix[N, N] M = diag_matrix(rep_vector(1.0, N));
matrix[N - 1, N] Q;
M[N, 1:N - 1] =  rep_row_vector(-1.0, N - 1);
M[N, N] = 1;
Q = qr_Q(M)[, 1:N - 1]';  // "pseudo"-inverse
}
parameters {
row_vector[N-1] x_raw;
}
transformed parameters {
row_vector[N] x = x_raw * Q;
}
model {
x ~ normal(0, inv_sqrt(1.0 - 1.0 / N));
}
``````

Do you have a link to the other the alternative sum-to-zero QR thing? Or the RHS Stan code?

Hi Ben,

sure the link is here and following posts:

RHS zero-to-sum QR

Meanwhile I’ve tested both and the results look they do the same. The LHS could be used for an internal Stan sampling statement.

I always asked myself, why should somebody use a transformation on the LHS, when it can be done on the RHS? Is there some efficiency bonus?

I don’t see any computational difference in the two models. Both have a matrix vector multiply. The sampling statement is gonna work the same if x is a row vector or a column vector.

Do you have a Wikipedia link on the QR sum to zero thing? I was trying to prove to myself that these two codes were equivalent but didn’t manage to figure out what’s happening.

There is no big magic behind, it’s just the QR-decomposition of a contr.sum contrast matrix. And since Q*Q^{t} = I we can do both LHS and RHS sampling. I think we can close the topic as there is no difference in between both ways.