Sampling from a categorical distribution

Hi, what would be the best way to sample from a categorical distribution with arbitrary probabilities for multiple entries? Is there an available expression for this or do we have to code it in a macro ourselves?

Also, can there be multi-line expression macros? what would be the recommended syntax for them?

Hi Hokyung,

what would be the best way to sample from a categorical distribution with arbitrary probabilities for multiple entries? Is there an available expression for this or do we have to code it in a macro ourselves?

MWorks has several built-in random number generators. If none of these meet your needs, probably the easiest way to implement a new one is via embedded Python code. I can provide an example, if that would be helpful.

Also, can there be multi-line expression macros? what would be the recommended syntax for them?

You can split the expression across multiple lines if you enclose it in parentheses. For example:

%define six = (
    1 +
    2 +
    3
    )

But you’re still limited to a single expression. If you want to split up a calculation in to multiple steps, you’ll have to use a statement macro and store the intermediate results in variables.

Cheers,
Chris