# Define a simple discrete probability distribution

I know the command: ProbabilityDistribution

I look at the detail of it, it seems a little troublesome.

For a very simple situation:

  n:       0        1       2
p(x=n)   0.3      0.4     0.3


how to define this a simple discrete probability distribution

## 1 Answer

You can first define a piecewise function

piece[x_] := Piecewise[{{0.3, x == 0}, {0.4, x == 1}, {0.3, x == 2}}]


and feed it to ProbabilityDistribution

f = ProbabilityDistribution[piece[x], {x, 0, 2, 1}] Its PDF[f, x] and CDF[f, x] You can plot it with

DiscretePlot[piece[x], {x, 0, 2}, Frame -> True, PlotRange -> {0, 0.5}]


or

DiscretePlot[PDF[f][x], {x, 0, 2}, Frame -> True, PlotRange -> {0, 0.5}] Mean[f]


1.

which is

Expectation[x, x \[Distributed] f]


1.

Also

Variance[f]


0.6

or

Probability[x <= 1, x \[Distributed] f]


0.7

etc.

Alternatively you can use EmpiricalDistribution to do the same:

emp = EmpiricalDistribution[{0.3, 0.4, 0.3} -> {0, 1, 2}]

DiscretePlot[PDF[emp][x], {x, 0, 2}, Frame -> True, PlotRange -> {0, 0.5}]


like previously

Plot[CDF[emp][x], {x, 0, 2}, Frame -> True, PlotRange -> {0, 1}] etc.

• Command EmpiricalDistribution It is just what I need. – tiankonghewo Nov 29 '16 at 11:07
• It's a bit frustrating that Mathematica doesn't have a native DiscreteDistribution – becko Nov 30 '18 at 0:47