sampling form a categorical distribution in rstan

589 views
Skip to first unread message

Linas Mockus

unread,
Apr 20, 2015, 8:31:16 PM4/20/15
to stan-...@googlegroups.com
Hi,

Can anybody give an idea how to sample from a categorical distribution?  The categorical response belongs to the set {100, 200, 500} and probability of each response is 1/3.  I guess it could be done with simplex[3] and categorical but what is the elegant way?

Thank you,
Linas

Bob Carpenter

unread,
Apr 26, 2015, 8:34:49 AM4/26/15
to stan-...@googlegroups.com
What do you need it for? The generated quantities block has a categorical_rng.

There's no way to sample discrete parameters (other than hacks we don't
recommend) in the model.

If you need the parameters for inference, you want to marginalize
them out.

I don't know what you mean by belonging to the set {100, 200, 500}.
Our categorical outcomes are always 1:K for some K. If you need
other numbers, you'll have to multiply or map them back again, say
using an array:

int values[3];
values[1] <- 100;
values[2] <- 200;
values[3] <- 500;

c <- categorical_rng(rep_vector(1.0 / 3.0, 3));
r <- values[c];

- Bob
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.

Linas Mockus

unread,
Apr 27, 2015, 8:38:59 AM4/27/15
to stan-...@googlegroups.com, Bob Carpenter
Bob,

Thanks for the suggestion.

The problem in very simplified form looks like linear regression: y =
a*x+eps where a is a parameter which can take values from the set {100,
200, 500} with equal probability. I need to estimate the parameter a.

Thank you,
Linas
--
Thank you,
Linas

Ben Goodrich

unread,
Apr 27, 2015, 10:25:48 AM4/27/15
to stan-...@googlegroups.com
On Monday, April 27, 2015 at 8:38:59 AM UTC-4, Linas Mockus wrote:
The problem in very simplified form looks like linear regression: y =  
a*x+eps where a is a parameter which can take values from the set {100,  
200, 500} with equal probability. I need to estimate the parameter a.
 
With Stan, that entails doing a mixture model.

Ben

Linas Mockus

unread,
Apr 27, 2015, 10:27:30 AM4/27/15
to stan-...@googlegroups.com, Ben Goodrich
Can you please refer to some example?

Thank you,
Linas
--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/h40n4uT9RFU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Thank you,
Linas

Ben Goodrich

unread,
Apr 27, 2015, 10:31:00 AM4/27/15
to stan-...@googlegroups.com
On Monday, April 27, 2015 at 10:27:30 AM UTC-4, Linas Mockus wrote:
Can you please refer to some example?

There are some in the manual in the chapter on finite mixtures.

Ben

Reply all
Reply to author
Forward
0 new messages