Skip to content

change check behavior to only use double types #1779

Open
@bob-carpenter

Description

@bob-carpenter

Description

stan/math/prim/err/check_simplex.hpp is doing its checks on the autodiff type that it's supplied.

Here's the current code for a vector of scalar type T_prob.

  check_nonzero_size(function, name, theta);
  if (!(fabs(1.0 - theta.sum()) <= CONSTRAINT_TOLERANCE)) {
    std::stringstream msg;
    T_prob sum = theta.sum();
    msg << "is not a valid simplex.";
    msg.precision(10);
    msg << " sum(" << name << ") = " << sum << ", but should be ";
    std::string msg_str(msg.str());
    throw_domain_error(function, name, 1.0, msg_str.c_str());
  }

That first sum and subtraction from 1 is being done with autodiff types.

check_cholesky_factor_corr is just as bad. This habit probably started because the first ones like positive and negative didn't require any intermediate expressions.

Someone needs to go through all of these things and make sure that the values are extracted and that we don't factor an autodiff variable for a covariance matrix test.

This one's actually pretty important for performance since it hits every parameter defined with a constrained type other than the simple lower and upper bounds and orderings: unit vectors, simplexes, correlation cholesky factors, and covariance and correlation matrices.

It'd be good to spot check the other ones so there aren't anything like this fabs(1 - x) thing going on anywhere, because that'll also drop things on the stack.

Current Version:

v3.1.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions