RStan/PyStan refactoring opportunities: validating arguments

10 views
Skip to first unread message

Allen Riddell

unread,
Jul 24, 2014, 3:36:41 PM7/24/14
to stan...@googlegroups.com
Hi everyone,

As I update PyStan for 2.4, I'm noticing one opportunity for refactoring into
stan::common: validating the union "ctrl" which is identical in PyStan and RStan:

union {
struct {
int iter; // number of iterations
int refresh; //
sampling_algo_t algorithm;
int warmup; // number of warmup
int thin;
bool save_warmup; // weather to save warmup samples (always true now)
int iter_save; // number of iterations saved
int iter_save_wo_warmup; // number of iterations saved wo warmup
bool adapt_engaged;
double adapt_gamma;
double adapt_delta;
double adapt_kappa;
unsigned int adapt_init_buffer;
unsigned int adapt_term_buffer;
unsigned int adapt_window;
double adapt_t0;
sampling_metric_t metric; // UNIT_E, DIAG_E, DENSE_E;
double stepsize; // defaut to 1;
double stepsize_jitter;
int max_treedepth; // for NUTS, default to 10.
double int_time; // for HMC, default to 2 * pi
} sampling;
struct {
int iter; // default to 2000
int refresh; // default to 100
optim_algo_t algorithm; // Newton, (L)BFGS
bool save_iterations; // default to false
double init_alpha; // default to 0.001, for (L)BFGS
double tol_obj; // default to 1e-12, for (L)BFGS
double tol_grad; // default to 1e-8, for (L)BFGS
double tol_param; // default to 1e-8, for (L)BFGS
double tol_rel_obj; // default to 1e4, for (L)BFGS
double tol_rel_grad; // default to 1e7, for (L)BFGS
int history_size; // default to 5, for LBFGS only
} optim;
struct {
double epsilon; // default to 1e-6, for test_grad
double error; // default to 1e-6, for test_grad
} test_grad;
} ctrl;


Here's one of the checks:

void validate_args() {
if (init_radius < 0) {
std::stringstream msg;
msg << "Invalid value for parameter init_r (found "
<< init_radius << "; require >= 0).";
throw std::invalid_argument(msg.str());
}



Best wishes,

-a

Bob Carpenter

unread,
Jul 24, 2014, 3:50:23 PM7/24/14
to stan...@googlegroups.com
Great! Daniel's the best one to talk to about it.

The big issue is that changes will impact PyStan, RStan, and CmdStan.

Rather than using unions, I'd recommend the Boost variant package,

http://www.boost.org/doc/libs/1_55_0/doc/html/variant.html

which we're using in the parser. I can help get that going for
a particular application if you need it. The nice part about it
is that it gives you type-safe access to unions.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

Reply all
Reply to author
Forward
0 new messages