3
$\begingroup$

DiscreteUniformDistribution does not seem to work as it should. For a very simple example, let's consider a discrete random variable with PMF given by prob(-1)=1/2 and prob(1)=1/2. First, we create the distribution "manually".

prob[x_] := Piecewise[{{1/2, x == -1}, {1/2, x == 1}}, 0];
discrete = ProbabilityDistribution[prob[i], {i, -1, 1, 1}];
PDF[discrete, {-1, 0, 1}]

This gives the expected response

{1/2, 0, 1/2}

Now let's try to create the same probability mass function using DiscreteUniformDistribution. DiscreteUniformDistribution requires that the random variable have sequential integer values, so we first create a random variable with values 0 and 1, then transform the random variable.

dist = DiscreteUniformDistribution[{0, 1}];
PDF[dist, {-1, 0, 1}]

which gives

{0, 1/2, 1/2}

So far, so good. Now let's transform to get our desired result.

discrete2 = TransformedDistribution[2 y - 1, y \[Distributed] dist];
PDF[discrete2, {-1, 0, 1}]

gives

{1/2, 1/2, 1/2}

Oops. We should have prob[0]=0. The problem seems to be that DiscreteUniformDistribution does not really give a discrete distribution, as

PDF[dist][.5]

gives

1/2

In fact, PDF[dist] gives prob[x] = 1/2 for $0 \leq x \leq 1$

Perhaps I misunderstand how DiscreteUniformDistribution is supposed to work. I would appreciate any insights.

Edit

Thank you to all that replied. The issue seems to be built into the way that Mathematica deals with discrete distributions. Wolfram Research support replied with the following:

Although this result is incorrect for the discrete distribution you are considering, this behavior is documented as a possible issue at

https://reference.wolfram.com/language/ref/PDF.html#128024948

I asked which part of the documentation referred to this issue, and the reply was:

The relevant section under 'Possible Issues' begins with "The PDF of a distribution whose measure is incompatible with the Lebesgue measure or counting measure on the integer lattice may not evaluate or may give an incorrect result:" The example in the documentation uses a TransformedDistribution of a DiscreteUniformDistribution, very similarly to your example.

Here is the relevant part of the documentation: enter image description here

I don't have a background in Measure Theory. Can anyone explain what it means to be "incompatible with the Lebesque Measure or counting measure on the integer lattice"? It seems that the problem is that the PDF has a delta function at the origin (which would be common for a discrete distribution). Perhaps this can be overcome with the solutions offered by the people that graciously replied.

$\endgroup$
7
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bbgodfrey
    Jan 26 at 2:04
  • 2
    $\begingroup$ At least for me, Mathematica almost always has a consistent set of grammatical laws of which many I do not understand the subtleties. Here the issue (I think) is that PDF[discrete2, some list] does not work but PDF[discrete2,0] does give the correct and expected answer. PDF[dist][0.5] also doesn't work as you or I might expect but PDF[dist, 0.5] does. I am certain that someone here can explain the issue and you and I will soon have an epiphany. $\endgroup$
    – JimB
    Jan 26 at 3:28
  • 2
    $\begingroup$ And PDF[discrete2, #] & /@ {-1, 0, 1} also works. $\endgroup$
    – JimB
    Jan 26 at 4:08
  • 1
    $\begingroup$ @nlmath I think your problem may be simplified somewhat. The key issue I found in the results you highlight is the fact that the expression for the PDF of the simplest DiscreteUniformDistribution you considered does not restrict the values of its argument to integers only (evaluate PDF[dist] with your definitions), whereas the PDF of the "hand-made" discrete distribution does (compare to PDF[discrete]). That behavior does not seem right to me. $\endgroup$
    – MarcoB
    Jan 26 at 13:10
  • 1
    $\begingroup$ I can see that Wolfram might want to hedge on TransformedDistribution's for discrete random variables but even the standard PDF (and as you mention really a pmf) for a Poisson (or any other discrete distribution) doesn't give the complete probability mass function when the pure function is used. So I guess the workaround is just for the user to know what are the values that produce non-zero probabilities within the min and max provided by the pure function. $\endgroup$
    – JimB
    Jan 30 at 5:14

3 Answers 3

2
$\begingroup$

According to the manual:

enter image description here

Therefore, instead of writing:

PDF[discrete2, {-1, 0, 1}]

you should write:

PDF[discrete2,#]& /@ {-1, 0, 1}

enter image description here

$\endgroup$
5
  • $\begingroup$ I thought so at first, but the documentation of PDF EXPLICITLY says that PDF threads over lists in the Scope -> Parametric Distributions -> fifth example from the top. Indeed, if you obtain an explicit result from PDF[dist], it is expressed as a Function object with explicit Listable attribute. So the usage shown in OP should work according to the documentation, but it does not. $\endgroup$
    – MarcoB
    Jan 26 at 14:41
  • 1
    $\begingroup$ @nlmath I think something is fishy here and the documentation is pretty misleading. Please report this to "support@wofram.com". And if they answer, please post the answer here. $\endgroup$ Jan 26 at 15:37
  • 1
    $\begingroup$ The Function object does not include the "discreteness" of a discrete random variable. So any real number can be given which can lead to a wrong answer. For example, PDF[Poisson[4]][2.3] gives 0.165527 rather than 0. Seems like an oversight in the programming rather than just misleading documentation. $\endgroup$
    – JimB
    Jan 26 at 16:48
  • $\begingroup$ @DanielHuber, @JimB, I added further information provided by Wolfram Support to the original post. As @MarcoB said, since the Function version is Listable, it should work. I really don't think the PDF function is properly defined for a discrete distribution (which should be a PMF according to the documentation). $\endgroup$
    – nlmath
    Jan 30 at 2:34
  • 1
    $\begingroup$ Thank you for posting Wolframs answer. It does not seem to be really satisfiable. An error is an error, even if documented. $\endgroup$ Jan 30 at 19:31
2
$\begingroup$

@DanielHuber 's answer is the way to go if you want to use what's explicitly sanctioned in the documentation. But if you're willing to try a workaround, here's one approach.

The issue is that when you obtain the PDF as a pure function, the "discreteness" of the random variable is not included in the pure function for a discrete random variable. (I don't know if this is a bug, oversight, or a feature.) But one can many times add in that discreteness and get the correct results.

Your example is the following:

discrete2 = TransformedDistribution[2 y - 1, y \[Distributed] DiscreteUniformDistribution[{0, 1}]];
pdf = PDF[discrete2]

PDF as a pure function

That function is Listable but doesn't include the fact that the domain of positive probability consists of just the values -1 and 1. That can be added in several ways.

pdf = pdf /. Inequality[-1, LessEqual, \[FormalX], LessEqual, 1] -> \[FormalX] == -1 || \[FormalX] == 1

Adjusted pdf #1

or

pdf = pdf /. Inequality[-1, LessEqual, \[FormalX], LessEqual, 1] -> \[FormalX]^2 == 1

Adjusted pdf #2

There is a great speed increase using the pure function if many values of the pdf are to be obtained. Here is an example using a Poisson distribution:

AbsoluteTiming[pdf = PDF[PoissonDistribution[3]] /. 
  \[FormalX] >= 0 -> \[FormalX] ∈ NonNegativeIntegers;
 pdf[Range[100]];]
(* {0.0011996, Null} *)

AbsoluteTiming[PDF[PoissonDistribution[3], Range[100]];]
(* {0.0072985, Null} *)
$\endgroup$
1
  • $\begingroup$ Thank you for suggesting this workaround and for the interesting results with the speed increase. $\endgroup$
    – nlmath
    Jan 30 at 2:35
1
$\begingroup$

Thanks again to all that replied. The issue seems to be built into the way that Mathematica deals with discrete distributions.

For me, this workaround will work, where I have defined my own version of the discrete distribution.

discreteUniformDistribution[{imin_, imax_}] := ProbabilityDistribution[Piecewise[{{1/(imax - imin + 1), Or @@ Thread[x == Range[imin, imax]]}}, 0], {x, imin, imax, 1}]

This is general for a one-dimensional univariate discrete random distribution. Then

dist = discreteUniformDistribution[{0, 1}]; PDF[dist]

gives

enter image description here

and

discrete2 = TransformedDistribution[2 y - 1, y \[Distributed] dist]; PDF[discrete2, {-1, 0, 1}]

gives the desired result

enter image description here

$\endgroup$

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.