9
$\begingroup$

Bug introduced in 10.4.1 or earlier and somewhat fixed in 12.0.0


This question maybe related to (95436) but neither its answer nor the documentation for ProbabilityDistribution state whether we may build a discrete probability distribution using a step size $dx$ different from $1$.

Minimum Working Example

Let's say that we want to build a discrete probability distribution (say a prior distribution) for some random variable $X$ which can only take values in the range $[0,1]$. We do know that $Pr(X=\frac{1}{12}) = 0.25$, $Pr(X=\frac{1}{6})=0.5$, $Pr(X=\frac{1}{4})=0.25$ and the probability is zero for all other values.

From reading the documentation one might assume that we can use Piecewise within ProbabilityDistribution using a discrete range specification with $dx = \frac{1}{12}$ to build a discrete probability distribution matching our prior information:

priorDistribution = ProbabilityDistribution[
    Piecewise[
      {
        { 0.25, x == 1/12 },
        { 0.50, x == 1/6  },
        { 0.25, x == 1/4  }
      }
    ],
    { x, 0, 1, 1/12 }
]

But this surprisingly will not work in Version 11.0 :

PDF[priorDistribution, 1/4]

PDF[ProbabilityDistribution[[Piecewise] 0.25 [FormalX]==1/12 0.5 [FormalX]==1/6 0.25 [FormalX]==1/4 0 True ,{[FormalX],0,1,1/12}],0.25]

RandomVariate[ priorDistribution, 10 ]

RandomVariate::noimp: Sampling from ProbabilityDistribution[[Piecewise] 0.25 [FormalX]==1/12 0.5 [FormalX]==1/6 0.25 [FormalX]==1/4 0 True ,{[FormalX],0,1,1/12}] is not implemented.

Am I missing something? It seems that ProbabilityDistribution only accepts $dx = 1$ (I tried $dx = 2$ for a different range and it also did not work).

Update (Version 12.0.0):

While the above given example still does not work, the limitation with regard to dx has been fixed (at least with regard to dx > 1):

dist = ProbabilityDistribution[
      0.25 Boole[ x == 4]
    + 0.50 Boole[ x == 6]
    + 0.25 Boole[ x == 12 ]
    ,
    { x, 0, 12, 2}
]

PDF[ dist, 4 ] 

0.25

Update as of Version 12.3 (and possibly earlier?) and later

The documentation for ProbabilityDistribution now shows, that for discrete distributions, $dx$ has to be $1$. In the section Possible Issues the documentation states that TransformedDistribution can be used to build discrete distributions with noninteger support and it in fact uses my very example. ;-)

$\endgroup$
3
  • 1
    $\begingroup$ If you try something simple like: dist = ProbabilityDistribution[1/6, {x, 1, 11, 2}], it appears that it works with calculations like Mean or Variance, but not with RandomVariate. It doesn't work with your example at all. $\endgroup$
    – wolfies
    Sep 1, 2016 at 20:25
  • 1
    $\begingroup$ I have reported the issue to WRI [CASE:3705612] and will update this post once I have gotten a feedback. $\endgroup$
    – gwr
    Sep 2, 2016 at 13:49
  • 2
    $\begingroup$ The irregular behavior with Piecewise has been confirmed by WRI today. I just added the bugs tag and header accordingly. $\endgroup$
    – gwr
    Sep 9, 2016 at 8:59

1 Answer 1

5
$\begingroup$

It does seem to be broken for increments other than one. The workaround is either to rescale or else use EmpiricalDistribution

priorDistribution =
  ProbabilityDistribution[
   Piecewise[{
     {0.25, x == 1},
     {0.50, x == 2},
     {0.25, x == 3}}],
   {x, 1, 12, 1}];

DiscretePlot[PDF[priorDistribution, 12 x], {x, 0, 1, 1/12}]

enter image description here

Alternatively,

wts = {0.25, 0.50, 0.25};

data = {1/12, 1/6, 1/4};

priorDistribution =
  EmpiricalDistribution[wts -> data];

DiscretePlot[PDF[priorDistribution, x], {x, 0, 1, 1/12}]

(* plot same as above *)

$\endgroup$
2
  • $\begingroup$ Would you think this should be called a bug? $\endgroup$
    – gwr
    Sep 2, 2016 at 5:40
  • 1
    $\begingroup$ @gwr - looks like one to me. $\endgroup$
    – Bob Hanlon
    Sep 2, 2016 at 13:21

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.