6
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ Strongly related $\endgroup$ Commented Mar 3, 2014 at 14:38
  • $\begingroup$ 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. $\endgroup$ Commented Mar 3, 2014 at 14:46

2 Answers 2

1
$\begingroup$

Update: CategoricalDistribution is a built-in symbol as of Version 12.1 in 2020. Much of the requested functionality is incorporated. However since categories are inherently non-numeric, functions such as Mean are not implemented for categorical distributions.

https://reference.wolfram.com/language/ref/CategoricalDistribution.html

$\endgroup$
9
$\begingroup$

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
$\endgroup$
4
  • $\begingroup$ This is community wiki, i.e. people are welcome to edit and improve it. $\endgroup$
    – Szabolcs
    Commented Mar 3, 2014 at 19:28
  • 1
    $\begingroup$ Wolfram should probably mention "categorical distributions" in the help page for EmpiricalDistribution, they are equivalent as far as I can tell. $\endgroup$
    – A.G.
    Commented Mar 3, 2014 at 23:07
  • $\begingroup$ @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. $\endgroup$
    – Szabolcs
    Commented Mar 3, 2014 at 23:11
  • $\begingroup$ Done. Thanks for suggesting it. $\endgroup$
    – A.G.
    Commented Mar 5, 2014 at 0:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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