# What is a valid PDF structure? How to use PDF and ProbabilityDistribution for custom distributions?

## Problem

I like Mathematica. I think it is awesome for a lot of things (and symbolic notation is nifty). Rather than doing things manually, I would like to take advantage of Mathematica's internal objects for handling probability and statistics. However, I find that doing so is very unintuitive -- at least to me.

For example, if I want to specify the PDF of a die, I need to be specific about the ProbabilityDistribution[... {x,1,6,1}]

For example what is below works great:

Die = ProbabilityDistribution[PDF[UniformDistribution[{0, 6}], x], {x, 1, 6, 1}];
Roll[die_] := RandomVariate[die, 1];

So what about the probability that the sum of two dice rolls is less than 8, given that the sum of the two dice is odd?

Probability[Subscript[x, 1] + Subscript[x, 2] < 8 \[Conditioned] Mod[Subscript[x, 1] + Subscript[x, 2], 2] != 0, {Subscript[x, 1] \[Distributed] Die, Subscript[x, 2] \[Distributed] Die}]

This works. Yet, the more intuitive (to me) way to have programmed this would have been:

Probability[Subscript[x, 1] + Subscript[x, 2] < 8 \[Conditioned] OddQ[Subscript[x, 1] + Subscript[x, 2]], {Subscript[x, 1] \[Distributed] Die, Subscript[x, 2] \[Distributed] Die}]

Which doesnt work.

Ok, so let's get a bit more tricky. What about the classical drawing marbles from a bag? Using the same format as above, I can get no results: (Probably should just copy-paste this into a notebook...)

BagOfMarbles[redMarbles_, blueMarbles_] := ProbabilityDistribution[PDF[\!$$\* TagBox[GridBox[{{"\[Piecewise]", GridBox[{{FractionBox["redMarbles", RowBox[{"redMarbles", "+","blueMarbles"}]], "Red"},{FractionBox["blueMarbles", RowBox[{"redMarbles", "+", "blueMarbles"}]], "Blue"}},AllowedDimensions->{2, Automatic},Editable->True,GridBoxAlignment->{"Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}},GridBoxItemSize->{"Columns" -> {{Automatic}}, "ColumnsIndexed" -> {}, "Rows" -> {{1.}}, "RowsIndexed" -> {}},GridBoxSpacings->{"Columns" -> {Offset[0.27999999999999997], {Offset[0.84]}, Offset[0.27999999999999997]}, "ColumnsIndexed" -> {}, "Rows" -> {Offset[0.2], {Offset[0.4]}, Offset[0.2]}, "RowsIndexed" -> {}},Selectable->True]}},GridBoxAlignment->{"Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}},GridBoxItemSize->{"Columns" -> {{Automatic}}, "ColumnsIndexed" -> {}, "Rows" -> {{1.}}, "RowsIndexed" -> {}},GridBoxSpacings->{"Columns" -> {Offset[0.27999999999999997], {Offset[0.35]}, Offset[0.27999999999999997]}, "ColumnsIndexed" -> {}, "Rows" -> {Offset[0.2], {Offset[0.4]}, Offset[0.2]}, "RowsIndexed" -> {}}],"Piecewise",DeleteWithContents->True,Editable->False,SelectWithContents->True,Selectable->False]$$ , x], {x, Red, Blue}];

So far, it should in principle work...

Probability[x == Red , x \[Distributed] BagOfMarbles[3, 5]]

and then nothing.

So can someone please explain what qualifies as a valid PDF strucutre

• ProbabilityDistribution[PDF[UniformDistribution[{0, 6}], x], {x, 1, 6, 1}]; is a very strange way to describe a dice. I am surprised it even works. You are using an inbuilt continuous Uniform distribution, taking the PDF of it, and then using that to manually construct a discrete Uniform distribution that describes a dice. What you want is: Die = DiscreteUniformDistribution[{1, 6}] – wolfies Aug 25 '16 at 18:28
• Your first problem is because OddQ immediately gives False if its argument is not already numeric, so your expression is effectively Subscript[x, 1] + Subscript[x, 2] < 8 \[Conditioned] False. Your second problem is because you shouldn't wrap the argument of ProbabilityDistribution in PDF, you should just give the PDF directly. For example, you could just have defined die = ProbabilityDistribution[1/6, {x, 1, 6, 1}]. – user484 Aug 25 '16 at 18:41
• So what is the difference between Mathematica's ProbabilityDistribution and PDF? When do you use which? – SumNeuron Aug 27 '16 at 4:39
• PDF is a property of a distribution. Distributions have many properties: PDF, CDF, Mean, Variance, Moment, and others. You use ProbabilityDistribution if you know the PDF (or CDF or SurvivalFunction or HazardFunction) to construct the associated distribution. Once constructed, the distribution can be used to calculate any of the other properties that Mathematica knows. – Bob Hanlon Aug 27 '16 at 21:08

It is probably simpler to define a distribution for the number of reds e.g.

d1 = ProbabilityDistribution[Piecewise[{{3/8, x == 0}, {5/8, x == 1}}, x], {x, 0, 1, 1}]

or equivalently

d2 = BernoulliDistribution[5/8]

As expected, this gives

Probability[x == 1, Distributed[x, d1]]
(* 5/8 *)

EDIT

I couldn't find a way of generating a discrete distribution other than over a sequence of integers. However, we can may map such a list to any other finite list. For example

index[Blue] = 0;
index[Red] = 1;

d3 = ProbabilityDistribution[Piecewise[{{3/8, x == index[Blue]}, {5/8, x == index[Red]}}, x], {x, 0, 1, 1}];

Probability[x == index[Blue], Distributed[x, d3]]
(* 3/8 *)

Probability[x == index[Red], Distributed[x, d3]]
(* 5/8 *)
• I could get it to work with numerical values, but out of curiousity, is there a way for this to work with things like colors? – SumNeuron Aug 27 '16 at 4:40
• Hey mikado, that works. I was wondering why if Mathematica can do symbolic notation, and surely Red is a structure of some kind with metadata (which is why it formats as a red square), then why it couldnt be used in this calculation as well. The purpose of this was to try and make some functions that would demonstrate to others as clearly as possible basic frequency probability, hence the naming of functions like "Roll[Die]" – SumNeuron Aug 28 '16 at 10:44
• @SumNeuron if you want to understand internal structure, evaluate FullForm[Red] – mikado Aug 28 '16 at 11:10
• @SumNeuron It seems you want a categorical distribution, which AFAICT Mathematica can do only indirectly by using numbers as labels for the categories. (Another way: dist = EmpiricalDistribution[{3/8, 5/8} -> Range[2]]. One issue is that PDF is not really defined at the categorical level (although you could have a discrete mass function), and certainly CDF is not defined unless the level is at least ordinal. One can get random variates from categorical distributions: RandomChoice[{3/8, 5/8} -> {Red, Blue}, 10]. – Michael E2 Oct 5 '16 at 19:49