6
$\begingroup$

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

$\endgroup$
8
$\begingroup$

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}]

enter image description here

Its PDF[f, x]

enter image description here

and CDF[f, x]

enter image description here

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}]

enter image description here

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}]

enter image description here

etc.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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