Closed bob-carpenter closed 7 years ago
Fixed with pull request #2104
I'm still getting this problem: ternary op not working, but if_else
does...was this fix merged into stan 2.16.2?
I ran into the same problem.
compile_error_code <- '
data {
int N;
int y_max;
int<lower=0> y[N];
int<lower=0,upper=1> group[N];
}
parameters {
vector<lower=0,upper=1>[2] param;
}
model {
to_vector(param) ~ uniform(0,1);
for (i in 1:N){
y[i] ~ binomial(y_max, group[i] == 0 ? param[1] : param[2]);
}
}
'
compile_warning_code <- '
data {
int N;
int y_max;
int<lower=0> y[N];
int<lower=0,upper=1> group[N];
}
parameters {
vector<lower=0,upper=1>[2] param;
}
model {
to_vector(param) ~ uniform(0,1);
for (i in 1:N){
y[i] ~ binomial(y_max, if_else(group[i] == 0,param[1],param[2]));
}
}
'
ce_data=list(y=sample(1:10, size = 40,replace = TRUE),
N=40,
y_max = 10,
group=sample(c(0,1),size=40,replace=TRUE))
model <- stan(model_code=compile_warning_code, data=ce_data,seed=42, chains=1)
compile_err_model <- stan(model_code=compile_error_code, data=ce_data,seed=42, chains=1)
> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X Yosemite 10.10.5
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rstan_2.16.2 StanHeaders_2.16.0-1 ggplot2_2.2.1
loaded via a namespace (and not attached):
[1] Rcpp_0.12.12 codetools_0.2-15 grid_3.3.3 plyr_1.8.4 gtable_0.2.0 stats4_3.3.3 scales_0.4.1
[8] rlang_0.1.1 lazyeval_0.2.0 tools_3.3.3 munsell_0.4.3 parallel_3.3.3 inline_0.3.14 colorspace_1.3-2
[15] gridExtra_2.2.1 tibble_1.3.3
> model <- stan(model_code=compile_warning_code,
data=ce_data,seed=42, chains=1)
DIAGNOSTIC(S) FROM PARSER:
Warning (non-fatal): the if_else() function is deprecated. Use the conditional operator '?:' instead.
In file included from file5030a0e0305.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/var.hpp:7:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/math/tools/config.hpp:13:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/config.hpp:39:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/config/compiler/clang.hpp:196:11: warning: 'BOOST_NO_CXX11_RVALUE_REFERENCES' macro redefined [-Wmacro-redefined]
# define BOOST_NO_CXX11_RVALUE_REFERENCES
^
<command line>:6:9: note: previous definition is here
#define BOOST_NO_CXX11_RVALUE_REFERENCES 1
^
In file included from file5030a0e0305.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:42:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp:14:17: warning: unused function 'set_zero_all_adjoints' [-Wunused-function]
static void set_zero_all_adjoints() {
^
In file included from file5030a0e0305.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:43:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints_nested.hpp:17:17: warning: 'static' function 'set_zero_all_adjoints_nested' declared in header file should be declared 'static inline' [-Wunneeded-internal-declaration]
static void set_zero_all_adjoints_nested() {
^
In file included from file5030a0e0305.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:58:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/autocorrelation.hpp:17:14: warning: function 'fft_next_good_size' is not needed and will not be emitted [-Wunneeded-internal-declaration]
size_t fft_next_good_size(size_t N) {
^
In file included from file5030a0e0305.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:298:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/arr.hpp:38:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/arr/functor/integrate_ode_rk45.hpp:13:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/numeric/odeint.hpp:61:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/numeric/odeint/util/multi_array_adaption.hpp:29:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/multi_array.hpp:21:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/multi_array/base.hpp:28:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/multi_array/concept_checks.hpp:42:43: warning: unused typedef 'index_range' [-Wunused-local-typedef]
typedef typename Array::index_range index_range;
^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/multi_array/concept_checks.hpp:43:37: warning: unused typedef 'index' [-Wunused-local-typedef]
typedef typename Array::index index;
^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/multi_array/concept_checks.hpp:53:43: warning: unused typedef 'index_range' [-Wunused-local-typedef]
typedef typename Array::index_range index_range;
^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/multi_array/concept_checks.hpp:54:37: warning: unused typedef 'index' [-Wunused-local-typedef]
typedef typename Array::index index;
^
8 warnings generated.
SAMPLING FOR MODEL 'e021365baf0c9dc2d11d141ee17adbb4' NOW (CHAIN 1).
Gradient evaluation took 3e-05 seconds
1000 transitions using 10 leapfrog steps per transition would take 0.3 seconds.
Adjust your expectations accordingly!
Iteration: 1 / 2000 [ 0%] (Warmup)
Iteration: 200 / 2000 [ 10%] (Warmup)
Iteration: 400 / 2000 [ 20%] (Warmup)
Iteration: 600 / 2000 [ 30%] (Warmup)
Iteration: 800 / 2000 [ 40%] (Warmup)
Iteration: 1000 / 2000 [ 50%] (Warmup)
Iteration: 1001 / 2000 [ 50%] (Sampling)
Iteration: 1200 / 2000 [ 60%] (Sampling)
Iteration: 1400 / 2000 [ 70%] (Sampling)
Iteration: 1600 / 2000 [ 80%] (Sampling)
Iteration: 1800 / 2000 [ 90%] (Sampling)
Iteration: 2000 / 2000 [100%] (Sampling)
Elapsed Time: 0.069626 seconds (Warm-up)
0.057414 seconds (Sampling)
0.12704 seconds (Total)
Now for the model with the ternary operator that doesn't compile
> compile_err_model <- stan(model_code=compile_error_code,
data=ce_data,seed=42, chains=1)
In file included from file5030604e93d7.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/var.hpp:7:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/math/tools/config.hpp:13:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/config.hpp:39:
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/BH/include/boost/config/compiler/clang.hpp:196:11: warning: 'BOOST_NO_CXX11_RVALUE_REFERENCES' macro redefined [-Wmacro-redefined]
# define BOOST_NO_CXX11_RVALUE_REFERENCES
^
<command line>:6:9: note: previous definition is here
#define BOOST_NO_CXX11_RVALUE_REFERENCES 1
^
In file included from file5030604e93d7.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/mat.hpp:174:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/promote_scalar.hpp:4:
/Library/Frameworks/R.framework/Versions/3.3/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.3/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);
^
file5030604e93d7.cpp:221:142: note: in instantiation of function template specialization 'stan::math::promote_scalar<double, stan::math::var>' requested here
lp_accum__.add(binomial_log<propto__>(get_base1(y,i,"y",1), y_max, (logical_eq(get_base1(group,i,"group",1),0) ? stan::math::promote_scalar<double>(get_base1(param,1,"param",1)) : stan::math::promote_scalar<double>(get_base1(param,2,"param",1)) )));
^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/log_prob_grad.hpp:44:28: note: in instantiation of function template specialization 'model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b::log_prob<true, true, stan::math::var>' requested here
= model.template log_prob<propto, jacobian_adjust_transform>
^
/Library/Frameworks/R.framework/Versions/3.3/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, model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b>' requested here
log_prob = stan::model::log_prob_grad<true, true>
^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/services/diagnose/diagnose.hpp:54:19: note: in instantiation of function template specialization 'stan::services::util::initialize<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, 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.3/Resources/library/rstan/include/rstan/stan_fit.hpp:459:45: note: in instantiation of function template specialization 'stan::services::diagnose::diagnose<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b>' requested here
return_code = stan::services::diagnose::diagnose(model,
^
/Library/Frameworks/R.framework/Versions/3.3/Resources/library/rstan/include/rstan/stan_fit.hpp:1196:11: note: in instantiation of function template specialization 'rstan::(anonymous namespace)::command<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, 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_,
^
file5030604e93d7.cpp:387:172: note: in instantiation of member function 'rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, 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<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::call_sampler)
^
1 warning and 1 error generated.
make: *** [file5030604e93d7.o] Error 1
ERROR(s) during compilation: source code errors or compiler configuration errors!
Program source:
1:
2: // includes from the plugin
3:
4:
5: // user includes
6: #define STAN__SERVICES__COMMAND_HPP// Code generated by Stan version 2.16.0
7:
8: #include <stan/model/model_header.hpp>
9:
10: namespace model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace {
11:
12: using std::istream;
13: using std::string;
14: using std::stringstream;
15: using std::vector;
16: using stan::io::dump;
17: using stan::math::lgamma;
18: using stan::model::prob_grad;
19: using namespace stan::math;
20:
21: typedef Eigen::Matrix<double,Eigen::Dynamic,1> vector_d;
22: typedef Eigen::Matrix<double,1,Eigen::Dynamic> row_vector_d;
23: typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> matrix_d;
24:
25: static int current_statement_begin__;
26:
27: stan::io::program_reader prog_reader__() {
28: stan::io::program_reader reader;
29: reader.add_event(0, 0, "start", "model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b");
30: reader.add_event(17, 17, "end", "model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b");
31: return reader;
32: }
33:
34: class model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b : public prob_grad {
35: private:
36: int N;
37: int y_max;
38: vector<int> y;
39: vector<int> group;
40: public:
41: model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b(stan::io::var_context& context__,
42: std::ostream* pstream__ = 0)
43: : prob_grad(0) {
44: ctor_body(context__, 0, pstream__);
45: }
46:
47: model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b(stan::io::var_context& context__,
48: unsigned int random_seed__,
49: std::ostream* pstream__ = 0)
50: : prob_grad(0) {
51: ctor_body(context__, random_seed__, pstream__);
52: }
53:
54: void ctor_body(stan::io::var_context& context__,
55: unsigned int random_seed__,
56: std::ostream* pstream__) {
57: boost::ecuyer1988 base_rng__ =
58: stan::services::util::create_rng(random_seed__, 0);
59: (void) base_rng__; // suppress unused var warning
60:
61: current_statement_begin__ = -1;
62:
63: static const char* function__ = "model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b";
64: (void) function__; // dummy to suppress unused var warning
65: size_t pos__;
66: (void) pos__; // dummy to suppress unused var warning
67: std::vector<int> vals_i__;
68: std::vector<double> vals_r__;
69: double DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
70: (void) DUMMY_VAR__; // suppress unused var warning
71:
72: // initialize member variables
73: context__.validate_dims("data initialization", "N", "int", context__.to_vec());
74: N = int(0);
75: vals_i__ = context__.vals_i("N");
76: pos__ = 0;
77: N = vals_i__[pos__++];
78: context__.validate_dims("data initialization", "y_max", "int", context__.to_vec());
79: y_max = int(0);
80: vals_i__ = context__.vals_i("y_max");
81: pos__ = 0;
82: y_max = vals_i__[pos__++];
83: validate_non_negative_index("y", "N", N);
84: context__.validate_dims("data initialization", "y", "int", context__.to_vec(N));
85: validate_non_negative_index("y", "N", N);
86: y = std::vector<int>(N,int(0));
87: vals_i__ = context__.vals_i("y");
88: pos__ = 0;
89: size_t y_limit_0__ = N;
90: for (size_t i_0__ = 0; i_0__ < y_limit_0__; ++i_0__) {
91: y[i_0__] = vals_i__[pos__++];
92: }
93: validate_non_negative_index("group", "N", N);
94: context__.validate_dims("data initialization", "group", "int", context__.to_vec(N));
95: validate_non_negative_index("group", "N", N);
96: group = std::vector<int>(N,int(0));
97: vals_i__ = context__.vals_i("group");
98: pos__ = 0;
99: size_t group_limit_0__ = N;
100: for (size_t i_0__ = 0; i_0__ < group_limit_0__; ++i_0__) {
101: group[i_0__] = vals_i__[pos__++];
102: }
103:
104: // validate, data variables
105: for (int k0__ = 0; k0__ < N; ++k0__) {
106: check_greater_or_equal(function__,"y[k0__]",y[k0__],0);
107: }
108: for (int k0__ = 0; k0__ < N; ++k0__) {
109: check_greater_or_equal(function__,"group[k0__]",group[k0__],0);
110: check_less_or_equal(function__,"group[k0__]",group[k0__],1);
111: }
112: // initialize data variables
113:
114: try {
115: } catch (const std::exception& e) {
116: stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
117: // Next line prevents compiler griping about no return
118: throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
119: }
120:
121: // validate transformed data
122:
123: // validate, set parameter ranges
124: num_params_r__ = 0U;
125: param_ranges_i__.clear();
126: validate_non_negative_index("param", "2", 2);
127: num_params_r__ += 2;
128: }
129:
130: ~model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b() { }
131:
132:
133: void transform_inits(const stan::io::var_context& context__,
134: std::vector<int>& params_i__,
135: std::vector<double>& params_r__,
136: std::ostream* pstream__) const {
137: stan::io::writer<double> writer__(params_r__,params_i__);
138: size_t pos__;
139: (void) pos__; // dummy call to supress warning
140: std::vector<double> vals_r__;
141: std::vector<int> vals_i__;
142:
143: if (!(context__.contains_r("param")))
144: throw std::runtime_error("variable param missing");
145: vals_r__ = context__.vals_r("param");
146: pos__ = 0U;
147: validate_non_negative_index("param", "2", 2);
148: context__.validate_dims("initialization", "param", "vector_d", context__.to_vec(2));
149: // generate_declaration param
150: vector_d param(static_cast<Eigen::VectorXd::Index>(2));
151: for (int j1__ = 0U; j1__ < 2; ++j1__)
152: param(j1__) = vals_r__[pos__++];
153: try {
154: writer__.vector_lub_unconstrain(0,1,param);
155: } catch (const std::exception& e) {
156: throw std::runtime_error(std::string("Error transforming variable param: ") + e.what());
157: }
158:
159: params_r__ = writer__.data_r();
160: params_i__ = writer__.data_i();
161: }
162:
163: void transform_inits(const stan::io::var_context& context,
164: Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
165: std::ostream* pstream__) const {
166: std::vector<double> params_r_vec;
167: std::vector<int> params_i_vec;
168: transform_inits(context, params_i_vec, params_r_vec, pstream__);
169: params_r.resize(params_r_vec.size());
170: for (int i = 0; i < params_r.size(); ++i)
171: params_r(i) = params_r_vec[i];
172: }
173:
174:
175: template <bool propto__, bool jacobian__, typename T__>
176: T__ log_prob(vector<T__>& params_r__,
177: vector<int>& params_i__,
178: std::ostream* pstream__ = 0) const {
179:
180: T__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
181: (void) DUMMY_VAR__; // suppress unused var warning
182:
183: T__ lp__(0.0);
184: stan::math::accumulator<T__> lp_accum__;
185:
186: // model parameters
187: stan::io::reader<T__> in__(params_r__,params_i__);
188:
189: Eigen::Matrix<T__,Eigen::Dynamic,1> param;
190: (void) param; // dummy to suppress unused var warning
191: if (jacobian__)
192: param = in__.vector_lub_constrain(0,1,2,lp__);
193: else
194: param = in__.vector_lub_constrain(0,1,2);
195:
196:
197: // transformed parameters
198:
199:
200: try {
201: } catch (const std::exception& e) {
202: stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
203: // Next line prevents compiler griping about no return
204: throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
205: }
206:
207: // validate transformed parameters
208:
209: const char* function__ = "validate transformed params";
210: (void) function__; // dummy to suppress unused var warning
211:
212: // model body
213: try {
214:
215: current_statement_begin__ = 13;
216: lp_accum__.add(uniform_log<propto__>(to_vector(param), 0, 1));
217: current_statement_begin__ = 14;
218: for (int i = 1; i <= N; ++i) {
219:
220: current_statement_begin__ = 15;
221: lp_accum__.add(binomial_log<propto__>(get_base1(y,i,"y",1), y_max, (logical_eq(get_base1(group,i,"group",1),0) ? stan::math::promote_scalar<double>(get_base1(param,1,"param",1)) : stan::math::promote_scalar<double>(get_base1(param,2,"param",1)) )));
222: }
223: } catch (const std::exception& e) {
224: stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
225: // Next line prevents compiler griping about no return
226: throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
227: }
228:
229: lp_accum__.add(lp__);
230: return lp_accum__.sum();
231:
232: } // log_prob()
233:
234: template <bool propto, bool jacobian, typename T_>
235: T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
236: std::ostream* pstream = 0) const {
237: std::vector<T_> vec_params_r;
238: vec_params_r.reserve(params_r.size());
239: for (int i = 0; i < params_r.size(); ++i)
240: vec_params_r.push_back(params_r(i));
241: std::vector<int> vec_params_i;
242: return log_prob<propto,jacobian,T_>(vec_params_r, vec_params_i, pstream);
243: }
244:
245:
246: void get_param_names(std::vector<std::string>& names__) const {
247: names__.resize(0);
248: names__.push_back("param");
249: }
250:
251:
252: void get_dims(std::vector<std::vector<size_t> >& dimss__) const {
253: dimss__.resize(0);
254: std::vector<size_t> dims__;
255: dims__.resize(0);
256: dims__.push_back(2);
257: dimss__.push_back(dims__);
258: }
259:
260: template <typename RNG>
261: void write_array(RNG& base_rng__,
262: std::vector<double>& params_r__,
263: std::vector<int>& params_i__,
264: std::vector<double>& vars__,
265: bool include_tparams__ = true,
266: bool include_gqs__ = true,
267: std::ostream* pstream__ = 0) const {
268: vars__.resize(0);
269: stan::io::reader<double> in__(params_r__,params_i__);
270: static const char* function__ = "model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::write_array";
271: (void) function__; // dummy to suppress unused var warning
272: // read-transform, write parameters
273: vector_d param = in__.vector_lub_constrain(0,1,2);
274: for (int k_0__ = 0; k_0__ < 2; ++k_0__) {
275: vars__.push_back(param[k_0__]);
276: }
277:
278: if (!include_tparams__) return;
279: // declare and define transformed parameters
280: double lp__ = 0.0;
281: (void) lp__; // dummy to suppress unused var warning
282: stan::math::accumulator<double> lp_accum__;
283:
284: double DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
285: (void) DUMMY_VAR__; // suppress unused var warning
286:
287:
288:
289: try {
290: } catch (const std::exception& e) {
291: stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
292: // Next line prevents compiler griping about no return
293: throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
294: }
295:
296: // validate transformed parameters
297:
298: // write transformed parameters
299:
300: if (!include_gqs__) return;
301: // declare and define generated quantities
302:
303:
304: try {
305: } catch (const std::exception& e) {
306: stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
307: // Next line prevents compiler griping about no return
308: throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
309: }
310:
311: // validate generated quantities
312:
313: // write generated quantities
314: }
315:
316: template <typename RNG>
317: void write_array(RNG& base_rng,
318: Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
319: Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
320: bool include_tparams = true,
321: bool include_gqs = true,
322: std::ostream* pstream = 0) const {
323: std::vector<double> params_r_vec(params_r.size());
324: for (int i = 0; i < params_r.size(); ++i)
325: params_r_vec[i] = params_r(i);
326: std::vector<double> vars_vec;
327: std::vector<int> params_i_vec;
328: write_array(base_rng,params_r_vec,params_i_vec,vars_vec,include_tparams,include_gqs,pstream);
329: vars.resize(vars_vec.size());
330: for (int i = 0; i < vars.size(); ++i)
331: vars(i) = vars_vec[i];
332: }
333:
334: static std::string model_name() {
335: return "model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b";
336: }
337:
338:
339: void constrained_param_names(std::vector<std::string>& param_names__,
340: bool include_tparams__ = true,
341: bool include_gqs__ = true) const {
342: std::stringstream param_name_stream__;
343: for (int k_0__ = 1; k_0__ <= 2; ++k_0__) {
344: param_name_stream__.str(std::string());
345: param_name_stream__ << "param" << '.' << k_0__;
346: param_names__.push_back(param_name_stream__.str());
347: }
348:
349: if (!include_gqs__ && !include_tparams__) return;
350:
351: if (!include_gqs__) return;
352: }
353:
354:
355: void unconstrained_param_names(std::vector<std::string>& param_names__,
356: bool include_tparams__ = true,
357: bool include_gqs__ = true) const {
358: std::stringstream param_name_stream__;
359: for (int k_0__ = 1; k_0__ <= 2; ++k_0__) {
360: param_name_stream__.str(std::string());
361: param_name_stream__ << "param" << '.' << k_0__;
362: param_names__.push_back(param_name_stream__.str());
363: }
364:
365: if (!include_gqs__ && !include_tparams__) return;
366:
367: if (!include_gqs__) return;
368: }
369:
370: }; // model
371:
372: }
373:
374: typedef model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b stan_model;
375:
376: #include <rstan/rstaninc.hpp>
377: /**
378: * Define Rcpp Module to expose stan_fit's functions to R.
379: */
380: RCPP_MODULE(stan_fit4model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_mod){
381: Rcpp::class_<rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b,
382: boost::random::ecuyer1988> >("stan_fit4model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b")
383: // .constructor<Rcpp::List>()
384: .constructor<SEXP, SEXP, SEXP>()
385: // .constructor<SEXP, SEXP>()
386: .method("call_sampler",
387: &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::call_sampler)
388: .method("param_names",
389: &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::param_names)
390: .method("param_names_oi",
391: &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::param_names_oi)
392: .method("param_fnames_oi",
393: &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::param_fnames_oi)
394: .method("param_dims",
395: &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::param_dims)
396: .method("param_dims_oi",
397: &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::param_dims_oi)
398: .method("update_param_oi",
399: &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::update_param_oi)
400: .method("param_oi_tidx",
401: &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::param_oi_tidx)
402: .method("grad_log_prob",
403: &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::grad_log_prob)
404: .method("log_prob",
405: &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::log_prob)
406: .method("unconstrain_pars",
407: &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::unconstrain_pars)
408: .method("constrain_pars",
409: &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::constrain_pars)
410: .method("num_pars_unconstrained",
411: &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::num_pars_unconstrained)
412: .method("unconstrained_param_names",
413: &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::unconstrained_param_names)
414: .method("constrained_param_names",
415: &rstan::stan_fit<model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b_namespace::model50306f116a20_917b48a33f8f03dac0f7b778c2b6092b, boost::random::ecuyer1988>::constrained_param_names)
416: ;
417: }
418:
419: // declarations
420: extern "C" {
421: SEXP file5030604e93d7( ) ;
422: }
423:
424: // definition
425:
426: SEXP file5030604e93d7( ){
427: return Rcpp::wrap("917b48a33f8f03dac0f7b778c2b6092b");
428: }
429:
430:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from file5030604e93d7.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/StanHeaders/include/stan/math/rev/core/var.hpp:7:
In file included from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/B
In addition: Warning message:
running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file5030604e93d7.cpp 2> file5030604e93d7.cpp.err.txt' had status 1
I just verified it's still an issue in RStan 2.16.2 and also still an issue in develop. I'll reopen:
bin/stanc /Users/carp/temp2/badcomp.stan --o=/Users/carp/temp2/badcomp.hpp
Model name=badcomp_model
Input file=/Users/carp/temp2/badcomp.stan
Output file=/Users/carp/temp2/badcomp.hpp
src/cmdstan/diagnose.cpp:81:30: warning: comparison of integers of different signs: 'size_t'
(aka 'unsigned long') and 'Index' (aka 'long') [-Wsign-compare]
for (size_t n = 0; n < t_samples.size(); ++n)
~ ^ ~~~~~~~~~~~~~~~~
src/cmdstan/diagnose.cpp:122:28: warning: comparison of integers of different signs: 'size_t'
(aka 'unsigned long') and 'Index' (aka 'long') [-Wsign-compare]
for (size_t n = 1; n < e_samples.size(); ++n) {
~ ^ ~~~~~~~~~~~~~~~~
src/cmdstan/diagnose.cpp:163:23: warning: comparison of integers of different signs: 'int' and
'unsigned long' [-Wsign-compare]
for (int n = 0; n < bad_n_eff_names.size() - 1; ++n)
~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~
src/cmdstan/diagnose.cpp:177:23: warning: comparison of integers of different signs: 'int' and
'unsigned long' [-Wsign-compare]
for (int n = 0; n < bad_rhat_names.size() - 1; ++n)
~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~
clang++ -Wall -I . -isystem stan/lib/stan_math/lib/eigen_3.3.3 -isystem stan/lib/stan_math/lib/boost_1.64.0 -isystem stan/lib/stan_math/lib/cvodes_2.9.0/include -std=c++1y -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I src -I stan/src -isystem stan/lib/stan_math/ -DEIGEN_NO_DEBUG -DFUSION_MAX_VECTOR_SIZE=12 -stdlib=libc++ -Wno-unknown-warning-option -Wno-unused-function -Wno-tautological-compare -Wsign-compare -DNO_FPRINTF_OUTPUT -pipe -O0 -o bin/stansummary bin/cmdstan/stansummary.o
4 warnings generated.
clang++ -Wall -I . -isystem stan/lib/stan_math/lib/eigen_3.3.3 -isystem stan/lib/stan_math/lib/boost_1.64.0 -isystem stan/lib/stan_math/lib/cvodes_2.9.0/include -std=c++1y -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I src -I stan/src -isystem stan/lib/stan_math/ -DEIGEN_NO_DEBUG -DFUSION_MAX_VECTOR_SIZE=12 -stdlib=libc++ -Wno-unknown-warning-option -Wno-unused-function -Wno-tautological-compare -Wsign-compare -DNO_FPRINTF_OUTPUT -pipe -O0 -o bin/diagnose bin/cmdstan/diagnose.o
--- Linking C++ model ---
clang++ -Wall -I . -isystem stan/lib/stan_math/lib/eigen_3.3.3 -isystem stan/lib/stan_math/lib/boost_1.64.0 -isystem stan/lib/stan_math/lib/cvodes_2.9.0/include -std=c++1y -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I src -I stan/src -isystem stan/lib/stan_math/ -DEIGEN_NO_DEBUG -DFUSION_MAX_VECTOR_SIZE=12 -stdlib=libc++ -Wno-unknown-warning-option -Wno-unused-function -Wno-tautological-compare -Wsign-compare -DNO_FPRINTF_OUTPUT -pipe -O0 -o /Users/carp/temp2/badcomp src/cmdstan/main.cpp -include /Users/carp/temp2/badcomp.hpp stan/lib/stan_math/lib/cvodes_2.9.0/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/cvodes_2.9.0/lib/libsundials_cvodes.a
In file included from <built-in>:360:
In file included from <command line>:8:
In file included from /Users/carp/temp2/badcomp.hpp:3:
In file included from stan/src/stan/model/model_header.hpp:4:
In file included from stan/lib/stan_math/stan/math.hpp:4:
In file included from stan/lib/stan_math/stan/math/rev/mat.hpp:12:
In file included from stan/lib/stan_math/stan/math/prim/mat.hpp:174:
In file included from stan/lib/stan_math/stan/math/prim/mat/fun/promote_scalar.hpp:4:
stan/lib/stan_math/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);
^~~
stan/lib/stan_math/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);
^
/Users/carp/temp2/badcomp.hpp:218:142: note: in instantiation of function template specialization
'stan::math::promote_scalar<double, stan::math::var>' requested here
...y_max, (logical_eq(get_base1(group,i,"group",1),0) ? stan::math::promote_scalar<double>(get_base1(pa...
^
stan/src/stan/model/log_prob_grad.hpp:44:28: note: in instantiation of function template specialization
'badcomp_model_namespace::badcomp_model::log_prob<true, true, stan::math::var>' requested here
= model.template log_prob<propto, jacobian_adjust_transform>
^
stan/src/stan/services/util/initialize.hpp:147:37: note: in instantiation of function template
specialization 'stan::model::log_prob_grad<true, true, badcomp_model_namespace::badcomp_model>'
requested here
log_prob = stan::model::log_prob_grad<true, true>
^
stan/src/stan/services/diagnose/diagnose.hpp:54:19: note: in instantiation of function template
specialization 'stan::services::util::initialize<badcomp_model_namespace::badcomp_model,
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,
^
src/cmdstan/command.hpp:135:49: note: in instantiation of function template specialization
'stan::services::diagnose::diagnose<badcomp_model_namespace::badcomp_model>' requested here
return_code = stan::services::diagnose::diagnose(model,
^
src/cmdstan/main.cpp:8:21: note: in instantiation of function template specialization
'cmdstan::command<badcomp_model_namespace::badcomp_model>' requested here
return cmdstan::command<stan_model>(argc,argv);
^
1 error generated.
(This also highlights a coding error in that we shouldn't get getting that warning, either.)
Meant to reopen with the last comment.
Simple program for replication from https://github.com/stan-dev/stan/issues/2101#issuecomment-329994130 (without R and with indenting):
data {
int N;
int y_max;
int<lower=0> y[N];
int<lower=0,upper=1> group[N];
}
parameters {
vector<lower=0,upper=1>[2] param;
}
model {
to_vector(param) ~ uniform(0,1);
for (i in 1:N){
y[i] ~ binomial(y_max, group[i] == 0 ? param[1] : param[2]);
}
}
It will work if you do your loop like
for (i in 1:N) { real p = group[i] == 0 ? param[1] : param[2]; y[i] ~ binomial(y_max, p); }
the underlying problem is that the function stan::lang::generate_expression
, (file src/stan/lang/generator/generate_expression.hpp
) has 2- and 3-arg variants which fail to keep track of flag is_var_context
which is true when in parameters and model block.
the fix to is always call the 4-arg version of this function.
testing this change now.
better fix:
generate a typedef with the same name in every scope in which variables may be declared for the type of declared variables
use the typedef to declare local variables
remove is_var_context
argument from generate_expression()
use full-argument version of generate_expression()
everywhere
Summary:
Example of ternary operator that translates to C++ but fails to compile.
Reproducible Steps:
Current Output:
Expected Output:
It to compile or fail to parse with a warning message.
Current Version:
v2.12.0
For the example, I ran on my mac with
and