Check_simplex: object is flagged as not being a simplex

check_simplex makes sure the elements of a vector sum to 1. When constructing a vector, I get a value that slightly off from 1 – not quite sury why.

  double p1_init = 0.65;
  Eigen::VectorXd rho(n_states);
  rho << p1_init, 1 - p1_init;

and later using this as an argument

density = hmm_marginal_lpdf(log_omegas, Gamma, rho);

Produces the following error message

C++ exception with description "hmm_marginal_lpdf: rho is not a valid simplex. sum(rho) = 1.000001, but should be 1" thrown in the test body.

By construction, however rho should be a simplex. Is arithmetic error introduced by a simple subtraction?

Is n_states 2? There shouldn’t be that much error in adding those two numbers.

(If it’s not, make sure to initialize the other components that aren’t in use.)

The culprit are the finite diff tests which perturb one element of rho, without adjusting the other element. So really, differentiation needs to be taken with respect to one element, and not both.

One possible approach to testing is to compose with the constraining transform. Then you can vary a single element.