stan-dev / rstan

RStan, the R interface to Stan
https://mc-stan.org
1.04k stars 269 forks source link

Code from Manual 2.17 section 9.2 fails to compile #477

Open DoktorMike opened 6 years ago

DoktorMike commented 6 years ago

Summary:

I tried to compile the code from section 9.2 in the stan-reference 2.17 ,i.e., the QR reparameterization of multiple linear regression and it fails with error message:

error: call of overloaded ‘sqrt(int)’ is ambiguous

Description:

The following code is compiled:

data {
    int<lower=0> N; // number of data items
    int<lower=0> K; // number of predictors
    matrix[N, K] x; // predictor matrix
    vector[N] y;    // outcome vector
}
transformed data {
    matrix[N, K] Q_ast;
    matrix[K, K] R_ast;
    matrix[K, K] R_ast_inverse;
    // thin and scale the QR decomposition
    Q_ast = qr_Q(x)[, 1:K] * sqrt(N - 1);
    R_ast = qr_R(x)[1:K, ] / sqrt(N - 1);
    R_ast_inverse = inverse(R_ast);
}
parameters {
    real alpha;          // intercept
    vector[K] theta;     // coefficients on Q_ast
    real<lower=0> sigma; // error scale
}
model {
    y ~ normal(Q_ast * theta + alpha, sigma); // likelihood
}
generated quantities {
    vector[K] beta;
    beta = R_ast_inverse * theta; // coefficients on x
}

which yields the following output.

file269b5443f783.cpp: In member function ‘void model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr::ctor_body(stan::io::var_context&, unsigned int, std::ostream*)’: file269b5443f783.cpp:142:240: error: call of overloaded ‘sqrt(int)’ is ambiguous stan::math::assign(Q_ast, multiply(stan::model::rvalue(qr_Q(x), stan::model::cons_list(stan::model::index_omni(), stan::model::cons_list(stan::model::index_min_max(1, K), stan::model::nil_index_list())), "qr_Q(x)"),sqrt((N - 1)))); ^ In file included from /usr/include/features.h:419:0, from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33, from /usr/include/stdint.h:26, from /usr/lib/gcc/x86_64-linux-gnu/7/include/stdint.h:9, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/memory/stack_alloc.hpp:6, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core/autodiffstackstorage.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4, from file269b5443f783.cpp:8: /usr/include/x86_64-linux-gnu/bits/mathcalls.h:149:1: note: candidate: double sqrt(double) MATHCALL (sqrt,, (Mdouble x)); ^ In file included from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/config/no_tr1/cmath.hpp:21:0, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/math/tools/config.hpp:19, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core/var.hpp:7, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core.hpp:12, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4, from file269b5443f783.cpp:8: /usr/include/c++/7/cmath:463:3: note: candidate: constexpr float std::sqrt(float) sqrt(float x) ^~~~ /usr/include/c++/7/cmath:467:3: note: candidate: constexpr long double std::sqrt(long double) sqrt(long double x) ^~~~ /usr/include/c++/7/cmath:475:5: note: candidate: constexpr typename gnu_cxx::__enable_if<std::is_integer<_Tp>::value, double>::type std::sqrt(_Tp) [with _Tp = int; typename gnu_cxx::__enable_if<std::is_integer<_Tp>::value, double>::type = double] sqrt(_Tp x) ^~~~ In file included from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/pow.hpp:5:0, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/inc_beta.hpp:7, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/grad_inc_beta.hpp:12, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal.hpp:50, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/arr.hpp:9, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:13, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4, from file269b5443f783.cpp:8: /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/sqrt.hpp:50:16: note: candidate: stan::math::var stan::math::sqrt(const stan::math::var&) inline var sqrt(const var& a) { ^~~~ In file included from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/prim/mat.hpp:209:0, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:12, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4, from file269b5443f783.cpp:8: /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/prim/mat/fun/sqrt.hpp:32:5: note: candidate: typename stan::math::apply_scalar_unary<stan::math::sqrt_fun, T>::return_t stan::math::sqrt(const T&) [with T = int; typename stan::math::apply_scalar_unary<stan::math::sqrt_fun, T>::return_t = double] sqrt(const T& x) { ^~~~ file269b5443f783.cpp:144:238: error: call of overloaded ‘sqrt(int)’ is ambiguous stan::math::assign(R_ast, divide(stan::model::rvalue(qr_R(x), stan::model::cons_list(stan::model::index_min_max(1, K), stan::model::cons_list(stan::model::index_omni(), stan::model::nil_index_list())), "qr_R(x)"),sqrt((N - 1)))); ^ In file included from /usr/include/features.h:419:0, from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33, from /usr/include/stdint.h:26, from /usr/lib/gcc/x86_64-linux-gnu/7/include/stdint.h:9, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/memory/stack_alloc.hpp:6, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core/autodiffstackstorage.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4, from file269b5443f783.cpp:8: /usr/include/x86_64-linux-gnu/bits/mathcalls.h:149:1: note: candidate: double sqrt(double) MATHCALL (sqrt,, (Mdouble x)); ^ In file included from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/config/no_tr1/cmath.hpp:21:0, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/math/tools/config.hpp:19, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core/var.hpp:7, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/core.hpp:12, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4, from file269b5443f783.cpp:8: /usr/include/c++/7/cmath:463:3: note: candidate: constexpr float std::sqrt(float) sqrt(float x) ^~~~ /usr/include/c++/7/cmath:467:3: note: candidate: constexpr long double std::sqrt(long double) sqrt(long double x) ^~~~ /usr/include/c++/7/cmath:475:5: note: candidate: constexpr typename gnu_cxx::enable_if<std::is_integer<_Tp>::value, double>::type std::sqrt(_Tp) [with _Tp = int; typename gnu_cxx::enable_if<std::is_integer<_Tp>::value, double>::__type = double] sqrt(_Tp __x) ^~~~ In file included from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/pow.hpp:5:0, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/inc_beta.hpp:7, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/grad_inc_beta.hpp:12, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal.hpp:50, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/arr.hpp:9, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:13, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4, from file269b5443f783.cpp:8: /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/scal/fun/sqrt.hpp:50:16: note: candidate: stan::math::var stan::math::sqrt(const stan::math::var&) inline var sqrt(const var& a) { ^~~~ In file included from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/prim/mat.hpp:209:0, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/rev/mat.hpp:12, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math.hpp:4, from /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/model/model_header.hpp:4, from file269b5443f783.cpp:8: /home/michael/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/stan/math/prim/mat/fun/sqrt.hpp:32:5: note: candidate: typename stan::math::apply_scalar_unary<stan::math::sqrt_fun, T>::return_t stan::math::sqrt(const T&) [with T = int; typename stan::math::apply_scalar_unary<stan::math::sqrt_fun, T>::return_t = double] sqrt(const T& x) { ^~~~ make: *** [file269b5443f783.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 STANSERVICESCOMMAND_HPP// Code generated by Stan version 2.17.0 7: 8: #include <stan/model/model_header.hpp> 9: 10: namespace model269b4e3a3303_multiple_linear_regression_qr_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", "model269b4e3a3303_multiple_linear_regression_qr"); 30: reader.add_event(31, 31, "end", "model269b4e3a3303_multiple_linear_regression_qr"); 31: return reader; 32: } 33: 34: class model269b4e3a3303_multiple_linear_regression_qr : public prob_grad { 35: private: 36: int N; 37: int K; 38: matrix_d x; 39: vector_d y; 40: matrix_d Q_ast; 41: matrix_d R_ast; 42: matrix_d R_ast_inverse; 43: public: 44: model269b4e3a3303_multiple_linear_regression_qr(stan::io::var_context& context, 45: std::ostream* pstream = 0) 46: : prob_grad(0) { 47: ctor_body(context, 0, pstream); 48: } 49: 50: model269b4e3a3303_multiple_linear_regression_qr(stan::io::var_context& context, 51: unsigned int random_seed, 52: std::ostream* pstream = 0) 53: : prob_grad(0) { 54: ctor_body(context__, random_seed, pstream); 55: } 56: 57: void ctor_body(stan::io::var_context& context, 58: unsigned int random_seed, 59: std::ostream* pstream) { 60: boost::ecuyer1988 base_rng = 61: stan::services::util::create_rng(random_seed, 0); 62: (void) base_rng; // suppress unused var warning 63: 64: current_statement_begin = -1; 65: 66: static const char* function = "model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr"; 67: (void) function__; // dummy to suppress unused var warning 68: size_t pos; 69: (void) pos; // dummy to suppress unused var warning 70: std::vector vals_i__; 71: std::vector vals_r; 72: double DUMMY_VAR(std::numeric_limits::quiet_NaN()); 73: (void) DUMMY_VAR; // suppress unused var warning 74: 75: // initialize member variables 76: try { 77: current_statement_begin = 6; 78: context__.validate_dims("data initialization", "N", "int", context.to_vec()); 79: N = int(0); 80: vals_i = context.vals_i("N"); 81: pos = 0; 82: N = vals_i[pos++]; 83: current_statement_begin = 7; 84: context.validate_dims("data initialization", "K", "int", context.to_vec()); 85: K = int(0); 86: vals_i = context.vals_i("K"); 87: pos = 0; 88: K = vals_i[pos++]; 89: current_statement_begin = 8; 90: validate_non_negative_index("x", "N", N); 91: validate_non_negative_index("x", "K", K); 92: context.validate_dims("data initialization", "x", "matrix_d", context__.to_vec(N,K)); 93: validate_non_negative_index("x", "N", N); 94: validate_non_negative_index("x", "K", K); 95: x = matrix_d(static_cast(N),static_cast(K)); 96: vals_r = context.vals_r("x"); 97: pos = 0; 98: size_t x_m_mat_lim = N; 99: size_t x_n_mat_lim = K; 100: for (size_t n_mat = 0; n_mat < x_n_mat_lim; ++n_mat) { 101: for (size_t m_mat = 0; m_mat < x_m_mat_lim; ++m_mat__) { 102: x(m_mat,n_mat) = vals_r[pos++]; 103: } 104: } 105: current_statement_begin__ = 9; 106: validate_non_negative_index("y", "N", N); 107: context.validate_dims("data initialization", "y", "vector_d", context.to_vec(N)); 108: validate_non_negative_index("y", "N", N); 109: y = vector_d(static_cast(N)); 110: vals_r = context.vals_r("y"); 111: pos = 0; 112: size_t y_i_vec_lim = N; 113: for (size_t i_vec = 0; i_vec < y_i_vec_lim__; ++i_vec) { 114: y[i_vec] = vals_r[pos++]; 115: } 116: 117: // validate, data variables 118: current_statement_begin__ = 6; 119: check_greater_or_equal(function,"N",N,0); 120: current_statement_begin = 7; 121: check_greater_or_equal(function,"K",K,0); 122: current_statement_begin = 8; 123: current_statement_begin = 9; 124: // initialize data variables 125: current_statement_begin = 12; 126: validate_non_negative_index("Q_ast", "N", N); 127: validate_non_negative_index("Q_ast", "K", K); 128: Q_ast = matrix_d(static_cast(N),static_cast(K)); 129: stan::math::fill(Q_ast,DUMMY_VAR); 130: current_statement_begin = 13; 131: validate_non_negative_index("R_ast", "K", K); 132: validate_non_negative_index("R_ast", "K", K); 133: R_ast = matrix_d(static_cast(K),static_cast(K)); 134: stan::math::fill(R_ast,DUMMY_VAR); 135: current_statement_begin = 14; 136: validate_non_negative_index("R_ast_inverse", "K", K); 137: validate_non_negative_index("R_ast_inverse", "K", K); 138: R_ast_inverse = matrix_d(static_cast(K),static_cast(K)); 139: stan::math::fill(R_ast_inverse,DUMMY_VAR); 140: 141: current_statement_begin = 16; 142: stan::math::assign(Q_ast, multiply(stan::model::rvalue(qr_Q(x), stan::model::cons_list(stan::model::index_omni(), stan::model::cons_list(stan::model::index_min_max(1, K), stan::model::nil_index_list())), "qr_Q(x)"),sqrt((N - 1)))); 143: current_statement_begin = 17; 144: stan::math::assign(R_ast, divide(stan::model::rvalue(qr_R(x), stan::model::cons_list(stan::model::index_min_max(1, K), stan::model::cons_list(stan::model::index_omni(), stan::model::nil_index_list())), "qr_R(x)"),sqrt((N - 1)))); 145: current_statement_begin = 18; 146: stan::math::assign(R_ast_inverse, inverse(R_ast)); 147: 148: // validate transformed data 149: current_statement_begin = 12; 150: current_statement_begin = 13; 151: current_statement_begin = 14; 152: 153: // validate, set parameter ranges 154: num_params_r = 0U; 155: param_ranges_i.clear(); 156: current_statement_begin = 21; 157: ++num_params_r; 158: current_statement_begin = 22; 159: validate_non_negative_index("theta", "K", K); 160: num_params_r += K; 161: current_statement_begin = 23; 162: ++num_params_r__; 163: } catch (const std::exception& e) { 164: stan::lang::rethrow_located(e, current_statement_begin, prog_reader()); 165: // Next line prevents compiler griping about no return 166: throw std::runtime_error(" IF YOU SEE THIS, PLEASE REPORT A BUG "); 167: } 168: } 169: 170: ~model269b4e3a3303_multiple_linear_regression_qr() { } 171: 172: 173: void transform_inits(const stan::io::var_context& context__, 174: std::vector& params_i, 175: std::vector& params_r, 176: std::ostream* pstream) const { 177: stan::io::writer writer(params_r__,params_i); 178: size_t pos; 179: (void) pos; // dummy call to supress warning 180: std::vector vals_r; 181: std::vector vals_i; 182: 183: if (!(context.contains_r("alpha"))) 184: throw std::runtime_error("variable alpha missing"); 185: vals_r = context.vals_r("alpha"); 186: pos = 0U; 187: context.validate_dims("initialization", "alpha", "double", context.to_vec()); 188: double alpha(0); 189: alpha = vals_r[pos++]; 190: try { 191: writer.scalar_unconstrain(alpha); 192: } catch (const std::exception& e) { 193: throw std::runtime_error(std::string("Error transforming variable alpha: ") + e.what()); 194: } 195: 196: if (!(context.contains_r("theta"))) 197: throw std::runtime_error("variable theta missing"); 198: vals_r = context.vals_r("theta"); 199: pos = 0U; 200: validate_non_negative_index("theta", "K", K); 201: context.validate_dims("initialization", "theta", "vector_d", context.to_vec(K)); 202: vector_d theta(static_cast(K)); 203: for (int j1 = 0U; j1 < K; ++j1) 204: theta(j1) = vals_r[pos++]; 205: try { 206: writer.vector_unconstrain(theta); 207: } catch (const std::exception& e) { 208: throw std::runtime_error(std::string("Error transforming variable theta: ") + e.what()); 209: } 210: 211: if (!(context.contains_r("sigma"))) 212: throw std::runtime_error("variable sigma missing"); 213: vals_r = context.vals_r("sigma"); 214: pos = 0U; 215: context.validate_dims("initialization", "sigma", "double", context.to_vec()); 216: double sigma(0); 217: sigma = vals_r[pos++]; 218: try { 219: writer.scalar_lb_unconstrain(0,sigma); 220: } catch (const std::exception& e) { 221: throw std::runtime_error(std::string("Error transforming variable sigma: ") + e.what()); 222: } 223: 224: params_r = writer.data_r(); 225: params_i = writer.data_i(); 226: } 227: 228: void transform_inits(const stan::io::var_context& context, 229: Eigen::Matrix<double,Eigen::Dynamic,1>& params_r, 230: std::ostream* pstream) const { 231: std::vector params_r_vec; 232: std::vector params_i_vec; 233: transform_inits(context, params_i_vec, params_r_vec, pstream); 234: params_r.resize(params_r_vec.size()); 235: for (int i = 0; i < params_r.size(); ++i) 236: params_r(i) = params_r_vec[i]; 237: } 238: 239: 240: template <bool propto, bool jacobian, typename T> 241: T log_prob(vector<T>& params_r, 242: vector& params_i, 243: std::ostream* pstream = 0) const { 244: 245: T__ DUMMY_VAR(std::numeric_limits::quiet_NaN()); 246: (void) DUMMY_VAR; // suppress unused var warning 247: 248: T lp(0.0); 249: stan::math::accumulator<T> lp_accum; 250: 251: try { 252: // model parameters 253: stan::io::reader<T> in(params_r__,params_i); 254: 255: T alpha; 256: (void) alpha; // dummy to suppress unused var warning 257: if (jacobian) 258: alpha = in.scalar_constrain(lp); 259: else 260: alpha = in.scalar_constrain(); 261: 262: Eigen::Matrix<T,Eigen::Dynamic,1> theta; 263: (void) theta; // dummy to suppress unused var warning 264: if (jacobian) 265: theta = in.vector_constrain(K,lp); 266: else 267: theta = in.vector_constrain(K); 268: 269: T sigma; 270: (void) sigma; // dummy to suppress unused var warning 271: if (jacobian) 272: sigma = in.scalar_lb_constrain(0,lp); 273: else 274: sigma = in.scalar_lb_constrain(0); 275: 276: 277: // transformed parameters 278: 279: 280: 281: // validate transformed parameters 282: 283: const char* function = "validate transformed params"; 284: (void) function; // dummy to suppress unused var warning 285: 286: // model body 287: 288: current_statement_begin = 26; 289: lp_accum.add(normal_log(y, add(multiply(Q_ast,theta),alpha), sigma)); 290: 291: } catch (const std::exception& e) { 292: stan::lang::rethrow_located(e, current_statement_begin__, prog_reader()); 293: // Next line prevents compiler griping about no return 294: throw std::runtime_error(" IF YOU SEE THIS, PLEASE REPORT A BUG "); 295: } 296: 297: lp_accum.add(lp); 298: return lp_accum.sum(); 299: 300: } // logprob() 301: 302: template <bool propto, bool jacobian, typename T> 303: T_ logprob(Eigen::Matrix<T,Eigen::Dynamic,1>& paramsr, 304: std::ostream* pstream = 0) const { 305: std::vector<T> vec_params_r; 306: vec_params_r.reserve(params_r.size()); 307: for (int i = 0; i < params_r.size(); ++i) 308: vec_params_r.push_back(params_r(i)); 309: std::vector vec_params_i; 310: return logprob<propto,jacobian,T>(vec_params_r, vec_params_i, pstream); 311: } 312: 313: 314: void get_param_names(std::vector& names) const { 315: names.resize(0); 316: names__.push_back("alpha"); 317: names.push_back("theta"); 318: names.push_back("sigma"); 319: names__.push_back("beta"); 320: } 321: 322: 323: void get_dims(std::vector<std::vector >& dimss) const { 324: dimss.resize(0); 325: std::vector dims; 326: dims.resize(0); 327: dimss__.push_back(dims); 328: dims.resize(0); 329: dims.push_back(K); 330: dimss.push_back(dims); 331: dims.resize(0); 332: dimss__.push_back(dims); 333: dims.resize(0); 334: dims.push_back(K); 335: dimss.push_back(dims); 336: } 337: 338: template 339: void write_array(RNG& base_rng, 340: std::vector& params_r, 341: std::vector& params_i, 342: std::vector& vars, 343: bool include_tparams = true, 344: bool include_gqs = true, 345: std::ostream* pstream = 0) const { 346: vars.resize(0); 347: stan::io::reader in(params_r,params_i); 348: static const char* function = "model269b4e3a3303_multiple_linear_regression_qr_namespace::write_array"; 349: (void) function; // dummy to suppress unused var warning 350: // read-transform, write parameters 351: double alpha = in.scalar_constrain(); 352: vector_d theta = in.vector_constrain(K); 353: double sigma = in.scalar_lb_constrain(0); 354: vars.push_back(alpha); 355: for (int k_0__ = 0; k_0 < K; ++k_0) { 356: vars.push_back(theta[k_0]); 357: } 358: vars.push_back(sigma); 359: 360: if (!include_tparams) return; 361: // declare and define transformed parameters 362: double lp = 0.0; 363: (void) lp; // dummy to suppress unused var warning 364: stan::math::accumulator lp_accum__; 365: 366: double DUMMY_VAR(std::numeric_limits::quiet_NaN()); 367: (void) DUMMY_VAR; // suppress unused var warning 368: 369: try { 370: 371: 372: 373: // validate transformed parameters 374: 375: // write transformed parameters 376: 377: if (!include_gqs) return; 378: // declare and define generated quantities 379: current_statement_begin = 29; 380: validate_non_negative_index("beta", "K", K); 381: vector_d beta(static_cast(K)); 382: (void) beta; // dummy to suppress unused var warning 383: 384: stan::math::initialize(beta, std::numeric_limits::quiet_NaN()); 385: stan::math::fill(beta,DUMMY_VAR); 386: 387: 388: current_statement_begin = 30; 389: stan::math::assign(beta, multiply(R_ast_inverse,theta)); 390: 391: // validate generated quantities 392: current_statement_begin__ = 29; 393: 394: // write generated quantities 395: for (int k_0 = 0; k_0 < K; ++k_0) { 396: vars.push_back(beta[k_0__]); 397: } 398: 399: } catch (const std::exception& e) { 400: stan::lang::rethrow_located(e, current_statement_begin, prog_reader()); 401: // Next line prevents compiler griping about no return 402: throw std::runtime_error(" IF YOU SEE THIS, PLEASE REPORT A BUG "); 403: } 404: } 405: 406: template 407: void write_array(RNG& base_rng, 408: Eigen::Matrix<double,Eigen::Dynamic,1>& params_r, 409: Eigen::Matrix<double,Eigen::Dynamic,1>& vars, 410: bool include_tparams = true, 411: bool include_gqs = true, 412: std::ostream* pstream = 0) const { 413: std::vector params_r_vec(params_r.size()); 414: for (int i = 0; i < params_r.size(); ++i) 415: params_r_vec[i] = params_r(i); 416: std::vector vars_vec; 417: std::vector params_i_vec; 418: write_array(base_rng,params_r_vec,params_i_vec,vars_vec,include_tparams,include_gqs,pstream); 419: vars.resize(vars_vec.size()); 420: for (int i = 0; i < vars.size(); ++i) 421: vars(i) = vars_vec[i]; 422: } 423: 424: static std::string model_name() { 425: return "model269b4e3a3303_multiple_linear_regression_qr"; 426: } 427: 428: 429: void constrained_param_names(std::vector& param_names__, 430: bool include_tparams = true, 431: bool include_gqs = true) const { 432: std::stringstream param_name_stream; 433: param_name_stream.str(std::string()); 434: param_name_stream << "alpha"; 435: param_names.push_back(param_name_stream__.str()); 436: for (int k_0 = 1; k_0 <= K; ++k_0) { 437: param_name_stream.str(std::string()); 438: param_name_stream__ << "theta" << '.' << k_0; 439: param_names.push_back(param_name_stream.str()); 440: } 441: param_name_stream.str(std::string()); 442: param_name_stream__ << "sigma"; 443: param_names.push_back(param_name_stream.str()); 444: 445: if (!include_gqs__ && !include_tparams) return; 446: 447: if (!include_gqs) return; 448: for (int k_0__ = 1; k_0 <= K; ++k_0) { 449: param_name_stream.str(std::string()); 450: param_name_stream << "beta" << '.' << k_0__; 451: param_names.push_back(param_name_stream.str()); 452: } 453: } 454: 455: 456: void unconstrained_param_names(std::vector& param_names__, 457: bool include_tparams = true, 458: bool include_gqs = true) const { 459: std::stringstream param_name_stream; 460: param_name_stream.str(std::string()); 461: param_name_stream << "alpha"; 462: param_names.push_back(param_name_stream__.str()); 463: for (int k_0 = 1; k_0 <= K; ++k_0) { 464: param_name_stream.str(std::string()); 465: param_name_stream__ << "theta" << '.' << k_0; 466: param_names.push_back(param_name_stream.str()); 467: } 468: param_name_stream.str(std::string()); 469: param_name_stream__ << "sigma"; 470: param_names.push_back(param_name_stream.str()); 471: 472: if (!include_gqs__ && !include_tparams) return; 473: 474: if (!include_gqs) return; 475: for (int k_0__ = 1; k_0 <= K; ++k_0) { 476: param_name_stream.str(std::string()); 477: param_name_stream << "beta" << '.' << k_0__; 478: param_names.push_back(param_name_stream__.str()); 479: } 480: } 481: 482: }; // model 483: 484: } 485: 486: typedef model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr stan_model; 487: 488: #include <rstan/rstaninc.hpp> 489: /* 490: Define Rcpp Module to expose stan_fit's functions to R. 491: / 492: RCPP_MODULE(stan_fit4model269b4e3a3303_multiple_linear_regression_qrmod){ 493: Rcpp::class<rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, 494: boost::random::ecuyer1988> >("stan_fit4model269b4e3a3303_multiple_linear_regression_qr") 495: // .constructor() 496: .constructor<SEXP, SEXP, SEXP>() 497: // .constructor<SEXP, SEXP>() 498: .method("call_sampler", 499: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::call_sampler) 500: .method("param_names", 501: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::param_names) 502: .method("param_names_oi", 503: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::param_names_oi) 504: .method("param_fnames_oi", 505: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::param_fnames_oi) 506: .method("param_dims", 507: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::param_dims) 508: .method("param_dims_oi", 509: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::param_dims_oi) 510: .method("update_param_oi", 511: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::update_param_oi) 512: .method("param_oi_tidx", 513: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::param_oi_tidx) 514: .method("grad_log_prob", 515: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::grad_log_prob) 516: .method("log_prob", 517: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::log_prob) 518: .method("unconstrain_pars", 519: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::unconstrain_pars) 520: .method("constrain_pars", 521: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::constrain_pars) 522: .method("num_pars_unconstrained", 523: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::num_pars_unconstrained) 524: .method("unconstrained_param_names", 525: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::unconstrained_param_names) 526: .method("constrained_param_names", 527: &rstan::stan_fit<model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr, boost::random::ecuyer1988>::constrained_param_names) 528: ; 529: } 530: 531: // declarations 532: extern "C" { 533: SEXP file269b5443f783( ) ; 534: } 535: 536: // definition 537: 538: SEXP file269b5443f783( ){ 539: return Rcpp::wrap("multiple_linear_regression_qr"); 540: } 541: 542: Error in compileCode(f, code, language = language, verbose = verbose) : Compilation ERROR, function(s)/method(s) not created! file269b5443f783.cpp: In member function ‘void model269b4e3a3303_multiple_linear_regression_qr_namespace::model269b4e3a3303_multiple_linear_regression_qr::ctor_body(stan::io::var_context&, unsigned int, std::ostream)’: file269b5443f783.cpp:142:240: error: call of overloaded ‘sqrt(int)’ is ambiguous stan::math::assign(Q_ast, multiply(stan::model::rvalue(qr_Q(x), stan::model::cons_list(stan::model::index_omni(), stan::model::cons_list(stan::model::index_min_max(1, K), stan::model::nil_index_list())), "qr_Q(x)"),sqrt((N - 1)))); ^ In file included from /usr/include/features.h:419:0, from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33,

In addition: Warning message: running command '/usr/lib/R/bin/R CMD SHLIB file269b5443f783.cpp 2> file269b5443f783.cpp.err.txt' had status 1

Reproducible Steps:

Run the following R script

library(rstan)
library(Rcpp)
library(bayesplot)
library(ggplot2)
library(dautility)
m <- stan_model(file = "multiple_linear_regression_qr.stan")

where "multiple_linear_regression_qr.stan" contains the code given above.

RStan Version:

The version of RStan is 2.17.2

R Version:

R version 3.4.3 (2017-11-30)

package version date source
abind 1.4-5 2016-07-21 CRAN (R 3.4.2) assertthat 0.1 2013-12-06 CRAN (R 3.4.2) base
3.4.3 2017-12-01 local
bayesplot 1.4.0 2017-09-12 cran (@1.4.0) bindr 0.1 2016-11-13 CRAN (R 3.4.2) bindrcpp 0.2 2017-06-17 CRAN (R 3.4.2) colorspace 1.3-2 2016-12-14 CRAN (R 3.4.2) compiler 3.4.3 2017-12-01 local
cowplot 0.9.1 2017-11-16 CRAN (R 3.4.2) data.table 1.10.0 2016-12-03 CRAN (R 3.4.2) datasets
3.4.3 2017-12-01 local
dautility 2.0.0 2017-11-19 local
devtools
1.13.4 2017-11-09 CRAN (R 3.4.2) digest 0.6.12 2017-01-27 CRAN (R 3.4.2) dplyr 0.7.4 2017-09-28 CRAN (R 3.4.2) ellipse 0.3-8 2013-04-13 CRAN (R 3.4.2) entropy 1.2.1 2014-11-14 CRAN (R 3.4.2) fortunes 1.5-4 2016-12-29 CRAN (R 3.4.2) GGally 1.3.2 2017-08-02 CRAN (R 3.4.2) ggmcmc 1.1 2016-06-28 CRAN (R 3.4.2) ggplot2 2.2.1 2016-12-30 CRAN (R 3.4.2) glue 1.2.0 2017-10-29 CRAN (R 3.4.2) graphics 3.4.3 2017-12-01 local
grDevices
3.4.3 2017-12-01 local
grid 3.4.3 2017-12-01 local
gridExtra 2.3 2017-09-09 cran (@2.3)
gtable 0.2.0 2016-02-26 CRAN (R 3.4.2) httr 1.2.1 2016-07-03 CRAN (R 3.4.2) inline 0.3.14 2015-04-13 CRAN (R 3.4.2) ISOweek 0.6-2 2011-09-07 CRAN (R 3.4.2) jsonlite 1.5 2017-06-01 cran (@1.5)
lattice 0.20-35 2017-03-25 CRAN (R 3.4.2) lazyeval 0.2.0 2016-06-12 CRAN (R 3.4.2) lubridate 1.6.0 2016-09-13 CRAN (R 3.4.2) magrittr 1.5 2014-11-22 CRAN (R 3.4.2) MASS 7.3-47 2017-04-21 CRAN (R 3.4.2) mclust 5.3 2017-05-21 CRAN (R 3.4.2) memoise 1.0.0 2016-01-29 CRAN (R 3.4.2) methods 3.4.3 2017-12-01 local
munsell 0.4.3 2016-02-13 CRAN (R 3.4.2) parallel 3.4.3 2017-12-01 local
pkgconfig 2.0.1 2017-03-21 CRAN (R 3.4.2) plyr 1.8.4 2016-06-08 CRAN (R 3.4.2) R6 2.2.2 2017-06-17 CRAN (R 3.4.2) RColorBrewer 1.1-2 2014-12-07 CRAN (R 3.4.2) Rcpp 0.12.14 2017-11-23 CRAN (R 3.4.3) reshape 0.8.6 2016-10-21 CRAN (R 3.4.2) reshape2 1.4.2 2016-10-22 CRAN (R 3.4.2) rlang 0.1.4 2017-11-05 CRAN (R 3.4.2) rstan 2.17.2 2017-12-21 CRAN (R 3.4.3) scales 0.4.1 2016-11-09 CRAN (R 3.4.2) slackr 1.4.2 2016-07-20 CRAN (R 3.4.2) ssh.utils 1.0 2014-07-24 CRAN (R 3.4.2) StanHeaders 2.17.1 2017-12-20 CRAN (R 3.4.3) stats 3.4.3 2017-12-01 local
stats4 3.4.3 2017-12-01 local
stringi 1.1.6 2017-11-17 cran (@1.1.6) stringr 1.2.0 2017-02-18 CRAN (R 3.4.2) tibble 1.3.4 2017-08-22 CRAN (R 3.4.2) tidyr
0.6.1 2017-01-10 CRAN (R 3.4.2) timeDate 3012.100 2015-01-23 CRAN (R 3.4.2) tools 3.4.3 2017-12-01 local
utils * 3.4.3 2017-12-01 local
withr 2.1.0 2017-11-01 CRAN (R 3.4.2) yaml 2.1.15 2017-12-01 cran (@2.1.15) zoo 1.8-0 2017-04-12 CRAN (R 3.4.2)

Operating System:

Linux einstein 4.13.0-21-generic #24-Ubuntu SMP Mon Dec 18 17:29:16 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

g++ (Ubuntu 7.2.0-8ubuntu3) 7.2.0

bob-carpenter commented 6 years ago

Thanks for the detailed bug report.

I don't have problems compiling that model with the develop version of CmdStan (with matching Stan and math libs). It also works on my Mac with RStan (Version 2.17.2, GitRev: 2e1f913d3ca3) with clang++ Apple LLVM version 7.0.2 (clang-700.1.81).

bgoodri commented 6 years ago

We have had that bug off and on for several years now. Sometimes you have to do

sqrt(N - 1.0)

but Andrew gets mad when stuff like that appears in the manual.

On Wed, Jan 3, 2018 at 2:11 PM, Bob Carpenter notifications@github.com wrote:

Thanks for the detailed bug report.

I don't have problems compiling that model with the develop version of CmdStan (with matching Stan and math libs). It also works on my Mac with RStan (Version 2.17.2, GitRev: 2e1f913 https://github.com/stan-dev/rstan/commit/2e1f913d3ca3678128f159b3d17d3d1f9b82704e) with clang++ Apple LLVM version 7.0.2 (clang-700.1.81).

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstan/issues/477#issuecomment-355099223, or mute the thread https://github.com/notifications/unsubscribe-auth/ADOrquYJBKCUm1iuPsxz9nsKYodPCulRks5tG9DKgaJpZM4RSHc3 .

bob-carpenter commented 6 years ago

Any idea what's causing it? Can we fix it in the math lib?

DoktorMike commented 6 years ago

Running the code with sqrt(N-1.0) indeed compiles without problem on my machine. I saw a similar issue with lower and upper bounds of parameters where 1 and 1.0 produced rather different results. Not sure if it's related.

bgoodri commented 6 years ago

We have fixed it several times already, e.g.

https://github.com/stan-dev/math/issues/327

On Wed, Jan 3, 2018 at 2:37 PM, Bob Carpenter notifications@github.com wrote:

Any idea what's causing it? Can we fix it in the math lib?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstan/issues/477#issuecomment-355105753, or mute the thread https://github.com/notifications/unsubscribe-auth/ADOrqqjQzrFQJ8kSa98YMzRL1LAVg9ibks5tG9bwgaJpZM4RSHc3 .

bob-carpenter commented 6 years ago

I opened a new issue in stan-math (see above) and assigned it to myself. For some reason, sqrt() didn't get the fix the other functions did. I'll try to get to the bottom of it, but may need help reproducing issue since it works OK as is on my machine.