When starting the sampling I face some NaN exceptions due to a
(or b
) gets great. Thus
square(a)
overflows. I overcome this problems by two branches, depending on the
value of the numbers. The value calculated remains the same, but the derivatives are different.
Question: Does this break the sampling process?
model {
real x;
a ~ exponential(10);
b ~ exponential(10);
if(a > 1.0e10 || b > 1.0e10)
x = exp(0.5 * log_sum_exp(2.0 * log(a), 2.0 * log(b));
else
x = sqrt(square(a) + square(b));
}
And this the code, where I receive this errors.
vector eigenvalues_2by2(vector sigma, real rho) {
if(!is_nan(sigma[1]) && !is_nan(sigma[2]) && !is_inf(sigma[1]) && !is_inf(sigma[2])
&& rho != 0.0 && sigma[1] > 1e-100 && sigma[2] > 1e-100 && sigma[1] < 1e10 && sigma[2] < 1e10) {
vector[2] EVa;
vector[2] sigma_sq = square(sigma);
vector[2] sigma_quad = square(sigma_sq);
real root = sqrt(sigma_quad[1] - 2 * sigma_sq[1] * sigma_sq[2] + 4 * square(rho) * sigma_sq[1] * sigma_sq[2] + sigma_quad[2]);
real b = sigma_sq[1] + sigma_sq[2];
EVa[1] = 0.5 * (b - root);
EVa[2] = 0.5 * (b + root);
return EVa;
} else {
matrix[2, 2] X;
X[1,1] = square(sigma[1]);
X[1,2] = sigma[1] * (sigma[2] * rho);
X[2,1] = X[1,2];
X[2,2] = square(sigma[2]);
return eigenvalues_sym(X);
}
}
}