Question about random number generation in user-defined function

268 views
Skip to first unread message

Fred Wu

unread,
Apr 26, 2017, 8:56:12 AM4/26/17
to Stan users mailing list
Dear Stan users, 

I am new to RStan and would like to ask a question regarding generating a normal variate in my Stan code. 

Part of my user-defined function need to generate normal variate. However, encounter the following errors:

ERROR: random number generators only allowed in transformed data block, generated quantities block or user-defined functions with names ending in _rng; found function=multi_normal_rng in block=function argument


I tested on a simple function as below, but it resulted in the same error message. 

vector test( vector m0, matrix C0 ) {

         vector[13]       y0;

        y0 = multi_normal_rng(m0, C0);

       return y0;
}

Did I use multi_normal_rng in a correct way?

Thanks and Regards

Fred



Ben Goodrich

unread,
Apr 26, 2017, 9:34:46 AM4/26/17
to Stan users mailing list
On Wednesday, April 26, 2017 at 8:56:12 AM UTC-4, Fred Wu wrote:
ERROR: random number generators only allowed in transformed data block, generated quantities block or user-defined functions with names ending in _rng; found function=multi_normal_rng in block=function argument

Your user-defined function has to end with _rng if it does random number generation internally.

Ben

Michael Betancourt

unread,
Apr 26, 2017, 9:46:49 AM4/26/17
to stan-...@googlegroups.com
In particular, you can use random number generators only
in the generated quantities block.  The target density defined
in the model block has to be deterministic.

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

Fred Wu

unread,
Apr 26, 2017, 9:50:07 AM4/26/17
to Stan users mailing list
Thanks for the reply. I think I now understand what it means ending in _rng. 

Bob Carpenter

unread,
Apr 26, 2017, 1:47:26 PM4/26/17
to stan-...@googlegroups.com
See the manual chapter on user-defined functions.

The alternative to an RNG is to use a parameter, but an RNG
will be much much faster (both in iteration time and in mixing)
if you can get away with it.

- Bob

Fred Wu

unread,
Apr 26, 2017, 9:47:45 PM4/26/17
to Stan users mailing list
Thanks for the reply Bob. 

I am actually working on a state-space model using FFBS and Simulation Smoothing algorithm via Kalman Filter. 
There are some problems by using inverse-gamma prior for the variance parameters. Therefore, I would like to try the cauchy prior. 

I have written them as user-defined functions.

My further question is: 
   Can I embedded them in the Stan code, which defined the prior of variance parameters in Stan but generating the posterior sample of state process via other algorithm? 

Kind Regards

Fred

Bob Carpenter

unread,
Apr 27, 2017, 1:52:26 PM4/27/17
to stan-...@googlegroups.com

> On Apr 26, 2017, at 9:47 PM, Fred Wu <jianyun...@gmail.com> wrote:
>
> Thanks for the reply Bob.
>
> I am actually working on a state-space model using FFBS and Simulation Smoothing algorithm via Kalman Filter.
> There are some problems by using inverse-gamma prior for the variance parameters. Therefore, I would like to try the cauchy prior.

That's easy in Stan. We've been moving toward things like (half) normal
priors when we don't need the fat tails, or sometimes just using Student-t
with 4 degrees of freedom to get something in between.

> I have written them as user-defined functions.
>
> My further question is:
> Can I embedded them in the Stan code, which defined the prior of variance parameters in Stan but generating the posterior sample of state process via other algorithm?

I can't quite follow what you want to do with what here. Stan
functions are always embedded in Stan code in some snese. In R,
you can expose the Stan functions and use them for whatever you want.

- Bob
Reply all
Reply to author
Forward
0 new messages