dimensionality testing for inits

35 views
Skip to first unread message

Bob Carpenter

unread,
Nov 13, 2012, 4:36:13 PM11/13/12
to stan...@googlegroups.com
I just pushed a change that adds dimensionality
testing for initializations.

It throws a std::runtime_error exception if the
dimensions don't match.

The gm/command is configured to print out the
message with an indication that it occurred during
inits.

RStan could be doing something similar in wrapping
the init call to the model with a try/catch where
the catch reports the error.

- Bob

Jiqiang Guo

unread,
Nov 13, 2012, 8:15:41 PM11/13/12
to stan...@googlegroups.com
I have added the try/catch in RStan a couple days ago. 

During testing, I found a problem for reading data: testing the variable existence is done after 
checking dims; so if a variable is missing, it would say:

> fit1b <- stan(fit = fit1, data = list(mu2 = 1), chains = 1) # variable mu missing 
Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) : 
  mismatch in number dimensions declared and found in context; dims declared=(1); dims found=()

This is an example code from which we can see the problem:

class model16726ca5dafd_c2 : public prob_grad_ad {
private:
    vector<matrix_d> mu;
public:
    model16726ca5dafd_c2(stan::io::var_context& context__,
        std::ostream* pstream__ = 0)
        : prob_grad_ad::prob_grad_ad(0) {
        static const char* function__ = "model16726ca5dafd_c2_namespace::model16726ca5dafd_c2(%1%)";
        (void) function__; // dummy call to supress warning
        size_t pos__;
        (void) pos__; // dummy call to supress warning
        std::vector<int> vals_i__;
        std::vector<double> vals_r__;
        context__.validate_dims("mu", context__.to_vec(3,4,5));
        mu = std::vector<matrix_d>(3,matrix_d(4,5));
        if (!context__.contains_r("mu"))
            throw std::runtime_error("variable mu not found.");

--
Jiqiang



- Bob

--



Bob Carpenter

unread,
Nov 14, 2012, 2:16:15 PM11/14/12
to stan...@googlegroups.com
Thanks -- I'll fix that and check in a patch.

All this error-checking dependency stuff is
driving me crazy. I should just check that the
variable exists as the first step in validating
dimensions (with an appropriate error message)
and just get rid of that second generated test.

I'll ping you when the fix is in.

- Bob
> On Tue, Nov 13, 2012 at 4:36 PM, Bob Carpenter <ca...@alias-i.com <mailto:ca...@alias-i.com>> wrote:
>
> I just pushed a change that adds dimensionality
> testing for initializations.
>
> It throws a std::runtime_error exception if the
> dimensions don't match.
>
> The gm/command is configured to print out the
> message with an indication that it occurred during
> inits.
>
> RStan could be doing something similar in wrapping
> the init call to the model with a try/catch where
> the catch reports the error.
>
> - Bob
>
> --
>
>
>
> --
>
>
Reply all
Reply to author
Forward
0 new messages