How do I emulate DiscreteUniformDistribution in Mathematica?
I would use Piecewise
.
pdist = ProbabilityDistribution[Piecewise[Map[{1/2, x == #1} &, {0, 1}], 0], {x, 0, 1, 1}]
PDF[pdist, {0.5, 0, 1}]
(*{0, 1/2, 1/2}*)
UPDATE: this problem has been resolved and is working correctly in v11.1.1.0.
Your syntax:
mine = ProbabilityDistribution[1/2, {x, 0, 1, 1}];
... appears to be valid to define a discrete pmf using Mathematica's ProbabilityDistribution
function. As such, this appears to be a bug in Mathematica.
The problem you have identified occurs with the PDF
function ... for instance:
PDF[mine, 1/3]
1/2
(whereas the answer should of course be 0), but it also occurs with other functions such as the Probability
function which also gets it wrong:
Probability[x == 1/3, Distributed[x, mine]]
1/2
Another way to work around this (without using Piecewise
) is to explicitly force the discreteness using Boole
, as per:
mine2 = ProbabilityDistribution[Boole[x == 0 || x == 1] 1/2 , {x, 0, 1, 1}];
This will then work correctly, ... but it really should not be necessary:
PDF[mine2, 1/3]
0
Probability[x == 1/3, Distributed[x, mine2]]
0
Another approach is to use the mathStatica
add-on to Mathematica, which you can set up exactly as you desired:
f = 1/2; domain[f] = {x, 0, 1} && {Discrete};
and which fully understands the discrete nature of the pmf:
Prob[x == 1/3, f]
0
etc ...