# Piecewise and ProbabilityDistribution malfunction when used together

Yesterday I opened this question, what had a happy end after all.

But when I tried to model more complex discrete random variables using Piecewise instead of If I discovered that it doesnt work at all!! Example:

1.This code works perfectly:

(*Our pseudo-generating function*)
g[x_, m_, c_, bigD_, t_, d_] :=
((c/bigD)/x + (bigD - t - c)/bigD + (t - d) x/bigD + (d/bigD) x^2)^m

(*Probability mass function of k*)
f[k_, m_, c_, bigD_, t_, d_] :=
If[k == 0,
Sum[Coefficient[g[x, m, c, bigD, t, d], x, i], {i, -m, 0}],
Coefficient[g[x, m, c, bigD, t, d], x, k]]

(*We make a probability distribution*)
dist[m_, bigD_: 6, t_: 2, d_: 0, c_: 0] :=
ProbabilityDistribution[f[k, m, c, bigD, t, d], {k, 0, 2 m, 1},
Assumptions -> {bigD \[Element] Integers, 0 <= d < bigD,
0 <= c < bigD, 1 <= t < bigD, m \[Element] Integers && m > 0}];

(* Testing some statistics*)
{Mean[#], StandardDeviation[#],
Table[Evaluate@CDF[#, k], {k, 0, 5}]} &@dist[5]


2.But this alternative code (based in the function g above described) doesnt work from dist2:

(*Alternative probability mass function of k*)
h[k_, m_, c_, bigD_, t_, d_] := Piecewise[
{
{Sum[Coefficient[g[x, m, c, bigD, t, d], x, i], {i, -m, 0}], k == 0},
{Coefficient[g[x, m, c, bigD, t, d], x, k], k != 0}
}]

(*Probability distribution based on h (this is where the code seems fail)*)
dist2[m_, bigD_: 6, t_: 2, d_: 0, c_: 0] :=
ProbabilityDistribution[h[k, m, c, bigD, t, d], {k, 0, 2 m, 1},
Assumptions -> {bigD \[Element] Integers, 0 <= d < bigD,
0 <= c < bigD, 1 <= t < bigD, m \[Element] Integers && m > 0}];

(* The test reveal that dist2 is not working as expected *)
Table[Evaluate@PDF[dist2[5], k], {k, 0, 5}]


I discovered that the function h evaluates correctly, and give the same results than f, but when I used together with ProbabilityDistribution to define dist2 it seems that the code dont work correctly.

There is a way to fix this? And in general, there is a way to use piecewise functions (with more than two pieces) together with ProbabilityDistribution? Thank you in advance.

UPDATE: the Boole function, as someone commented, is not working either but I found an (ugly) solution by the moment that is based in nesting If statements to define a piecewise function of more than two pieces inside of ProbabilityDistribution.

• I had similar problems recently (cf. (129690)). One is well advised to avoid using Piecewise and turn to Boole instead. – gwr Mar 24 '17 at 10:36
• @gwr sorry to say but Boole is not working either. I mean that If, Piecewise and Boole evaluates correctly but when you put inside of ProbabilityDistribution only the first one works. – Masacroso Mar 24 '17 at 13:12