Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Suppose I want to do calculations with a random variable $X$ that has a simple categorical distribution (a.k.a generalised Bernoulli distribution or discrete distribution).

I was expecting to be able to work with a distribution "symbolically". That is, I was expecting that there would be a function like CategoricalDistribution, that we could use like this.

distX = CategoricalDistribution[{1/3, 1/2, 1/6}];

But no distributions in tutorial/DiscreteDistributions looked like they were suitable. An example of how the distribution would be used would be

{Probability[x == 0, x \[Distributed] dist],
 Probability[x == 1, x \[Distributed] dist],
 Probability[x == 2, x \[Distributed] dist]}

would output

{1/3, 1/2, 1/6}

In this particular case, we can do a workaround, by doing

dist = TransformedDistribution[
  x1 + x2, {x1 \[Distributed] BernoulliDistribution[1/2], 
   x2 \[Distributed] BernoulliDistribution[1/3]}]

Unfortunately, this is not always possible, even with only 3 possible outcomes. For example, we cannot simulate CategoricalDistribution[{1/2, 0, 1/2}] like this, as we cannot have independent Bernoulli random variables that behave like this.

I was hoping the categorical distribution might be a special case of another distribution. Especially the MultinomialDistribution with 1 as its first argument looks useful, but I cannot transform that distribution into a distribution that gives an integer. Again it seems like I am missing a function like CategoricalDistribution to do so. To give an example

Position[RandomVariate@MultinomialDistribution[1, {1/2, 0, 1/2}], 
  1][[1, 1]]

"has the right distribution", but unfortunately I cannot use Position inside TransformedDistribution.

I am aware of functions like RandomChoice, but really I only want to do manipulations on distributions. Does anybody know how to do this?

share|improve this question
Strongly related – Jacob Akkerboom Mar 3 '14 at 14:38
I suppose dist = ProbabilityDistribution[Piecewise[{{1/2, y == 0}, {0, y == 1}, {1/2, y == 2}} ], {y, 0, 2, 1}], or dist = ProbabilityDistribution[Switch[y, 0, 1/2, 1, 0, 2, 1/2], {y, 0, 2, 1}]; do pretty much what I want. Maybe I should delete/close the question. – Jacob Akkerboom Mar 3 '14 at 14:46

EmpiricalDistribution can assign probabilities to each element in a set of discrete values:

Here's an example:

In[1]:= d = EmpiricalDistribution[{1/3, 1/2, 1/6} -> {1, 2, 3}];

In[2]:= Mean[d]
Out[2]= 11/6

In[3]:= PDF[d, x]
Out[3]= 1/3 Boole[1 == x] + 1/2 Boole[2 == x] + 1/6 Boole[3 == x]

In[4]:= CDF[d, x]
Out[4]= 1/3 Boole[1 <= x] + 1/2 Boole[2 <= x] + 1/6 Boole[3 <= x]

In[5]:= RandomVariate[d, 10]
Out[5]= {1, 3, 1, 2, 1, 2, 2, 1, 2, 1}

In[6]:= Probability[x == 1, x \[Distributed] d]
Out[6]= 1/3
share|improve this answer
This is community wiki, i.e. people are welcome to edit and improve it. – Szabolcs Mar 3 '14 at 19:28
Wolfram should probably mention "categorical distributions" in the help page for EmpiricalDistribution, they are equivalent as far as I can tell. – A.G. Mar 3 '14 at 23:07
@A.G. Why don't you suggest it? There's a give feedback button at the bottom of the page. Maybe even link back here to prove that it's not easy to find. – Szabolcs Mar 3 '14 at 23:11
Done. Thanks for suggesting it. – A.G. Mar 5 '14 at 0:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.