Target += a ? b : c

The follows dies during compilation with “Compilation ERROR, function(s)/method(s) not created!”. Is this a bug?

data {
  real y; 
}
parameters {
  real a;
  real b;
}
model {
  target += (y==0) ? a : b; 
}

That should probably compile if not sample.

I just compiled it with CmdStan v2.17.1.

What did you use to compile it? Is that a message from RStan? And what version are you using?

It’s definitely a bug if it’s happening. That’s example is valid Stan code.

are you running this in RStan?
The “+=” syntax went into Stan 2.17.
It’s not in RStan 2.16.

@mitzimorris, I thought target += was special and that was in the language for a while. I could be misremembering, though.

Yes, target += has been around longer than generic variable op= that got added in 2.17. But there were some earlier issues with scope of the ternary operator that also got cleaned up. So I don’t think this is a bug any more.

1 Like

Hmm I upgraded to 2.17 and still have the same issue (this is on Mac, I’ll try it on ubuntu).

The error is from RStan (I think):
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from file1ed94bd274df.cpp:8:

It’s not a problem since I can just do the more verbose if else statement, was just confused why it wasn’t compiling.

For context: this is for a zero-inflated likelihood, so the condition is whether the observed count is zero.

you’re correct. brain fart on my part - I didn’t process the “target” part of that stmt.

@David_Knowles, thanks for the minimal example. It really does help us pinpoint the problem.

You’re definitely right. @bgoodri, want me to create a bug for this? (or @David_Knowles can)

There’s definitely a problem introduced from RStan. Looks like the C++ compiler error is stemming from stan_fit.hpp? Some more from the error message that could help:

> fit <- stan("tmp.stan")
... (compiler warnings)
In file included from file9ed5240f1735.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:174:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/promote_scalar.hpp:4:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/scal/fun/promote_scalar.hpp:32:16: error: cannot convert 'stan::math::var' to 'double' without a conversion operator
        return T(x);
               ^~~
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/scal/fun/promote_scalar.hpp:68:43: note: in instantiation of member function 'stan::math::promote_scalar_struct<double, stan::math::var>::apply' requested here
      return promote_scalar_struct<T, S>::apply(x);
                                          ^
file9ed5240f1735.cpp:196:59: note: in instantiation of function template specialization 'stan::math::promote_scalar<double, stan::math::var>' requested here
            lp_accum__.add((logical_eq(y,0) ? stan::math::promote_scalar<double>(a) : stan::math::promote_scalar<double>(b) ));
                                                          ^
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/log_prob_grad.hpp:44:28: note: in instantiation of function template specialization 'model9ed5140a1d62_tmp_namespace::model9ed5140a1d62_tmp::log_prob<true, true, stan::math::var>' requested here
          = model.template log_prob<propto, jacobian_adjust_transform>
                           ^
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/services/util/initialize.hpp:147:37: note: in instantiation of function template specialization 'stan::model::log_prob_grad<true, true, model9ed5140a1d62_tmp_namespace::model9ed5140a1d62_tmp>' requested here
            log_prob = stan::model::log_prob_grad<true, true>
                                    ^
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/services/diagnose/diagnose.hpp:54:19: note: in instantiation of function template specialization 'stan::services::util::initialize<model9ed5140a1d62_tmp_namespace::model9ed5140a1d62_tmp, boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> > >' requested here
          = util::initialize(model, init, rng, init_radius,
                  ^
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rstan/include/rstan/stan_fit.hpp:459:45: note: in instantiation of function template specialization 'stan::services::diagnose::diagnose<model9ed5140a1d62_tmp_namespace::model9ed5140a1d62_tmp>' requested here
    return_code = stan::services::diagnose::diagnose(model,
                                            ^
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rstan/include/rstan/stan_fit.hpp:1196:11: note: in instantiation of function template specialization 'rstan::(anonymous namespace)::command<model9ed5140a1d62_tmp_namespace::model9ed5140a1d62_tmp, boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> > >' requested here
    ret = command(args, model_, holder, names_oi_tidx_,
          ^
file9ed5240f1735.cpp:360:114: note: in instantiation of member function 'rstan::stan_fit<model9ed5140a1d62_tmp_namespace::model9ed5140a1d62_tmp, boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> > >::call_sampler' requested here
            &rstan::stan_fit<model9ed5140a1d62_tmp_namespace::model9ed5140a1d62_tmp, boost::random::ecuyer1988>::call_sampler)
                                                                                                                 ^
1 warning and 1 error generated.
make: *** [file9ed5240f1735.o] Error 1
...

Yeah, create an issue for it.

Thanks, @David_Knowles!

Issue created: https://github.com/stan-dev/rstan/issues/474

1 Like