# Creating a probability distribution using Piecewise [closed]

The following "Manipulate" shows the behaviour of functions that I hope, if collected according to the rules shown further down, can be transformed into a probability distribution:

Manipulate[
Show[
{Plot[(P + f P - x)/(f^2 P^2), {P, x/(1 + f), x},
PlotStyle -> {Red}],
Plot[(f - P - f P + x)/(f^2 (-1 + P)^2), {P, x, (f + x)/(1 + f)},
PlotStyle -> {Blue}],
Plot[((-1 + f) P + x)/(f^2 P^2), {P, x, -(x/(-1 + f))},
PlotStyle -> {Green}],
Plot[(f + P - f P - x)/(f^2 (-1 + P)^2), {P, (f - x)/(-1 + f), x},
PlotStyle -> {Black}]},
PlotRange -> {{0, 1}, {0, 25}}],
{f, 0, 1},
{x, 0, 1}]


The rules for collecting the distribution are:

For "Green":

((-1 + f) P + x)/(f^2 P^2)
x < P <= -(x/(-1 + f)) < 1/2


For "Blue":

(f - P - f P + x)/(f^2 (-1 + P)^2)
1/2 <= x <= P <= -(x/(-1 + f))


For "Red":

(P + f P - x)/(f^2 P^2)
x/(1 + f) <= P <= x <= 1/2


For "Black":

(f + P - f P - x)/(f^2 (-1 + P)^2)
1/2 < (f - x)/(-1 + f) <= P <= x


I have made the following attempt, but I suspect I may be doing something wrong. If you exmine the above Manipulate expression, and Plot the result of the Piecewise procedure below, I cannot find much connection.

\[ScriptCapitalD] =
FullSimplify[
ProbabilityDistribution[
{"CF",
Piecewise[
{{((-1 + f) P + x)/(f^2 P^2), x < P <= -(x/(-1 + f)) < 1/2},
{(f - P - f P + x)/(f^2 (-1 + P)^2), 1/2 <= x <= P <= -(x/(-1 + f))},
{(P + f P - x)/(f^2 P^2), x < x/(1 + f) <= P <= x <= 1/2},
{(f + P - f P - x)/(f^2 (-1 + P)^2), 1/2 < (f - x)/(-1 + f) <= P <= x}},
0]},
{x, 0, 1},
Assumptions -> 0 <= P <= 1]]

Show[
{With[{f = 3/4}, Plot[-(1/(f^2 (-1 + P)^2)), {P, 0, 1}]],
With[{f = 3/4}, Plot[1/(f^2 (-1 + P)^2), {P, 0, 1}]],
With[{f = 3/4}, Plot[-(1/(f^2 P^2)), {P, 0, 1}]]},
PlotRange -> {{0, 1}, {-10, 100}}]


Here is the plot of the above and here is one from the Manipulate expression. ### Edit

JimB requested some clarification.

While Manipulate apparently shows two distributions, what is really happening is a switching, so to speak. For values of $$x$$ between 0 and 1/2 the functions for "Red" and "Green" are applicable, while for values of $$x$$ above 1/2 "Blue" and "Black" are applicable. Also, $$f$$ is a parameter of the distribution, effectively restricting its variance. Note that $$P$$ is the random variable (on the horizontal axis), not $$x$$ and not $$f$$. should have made that clearer.

So, there is some complication, because the distribution I am looking to obtain must account for the described switching.

• I think you have a typo: (f - P - f P + x)/(f^2 (-1 + P)^2), {1/2 <= x <= P <= -(x/(-1 + f))} should be {(f - P - f P + x)/(f^2 (-1 + P)^2), 1/2 <= x <= P <= -(x/(-1 + f))}. Notice the position of the parenthesis – mattiav27 Sep 9 '18 at 9:32
• The typo is fixed in the latest. – user120911 Sep 9 '18 at 12:22
• " I cannot find much connection." What do you mean by that? Please be more explicit about your issue. (Also, for Mathematica 10.4, I don't have a "CF" option for ProbabilityDistribution. Is that something in a newer version of Mathematica?) – JimB Sep 9 '18 at 14:24
• JimB, first the "CF" probably shows my ignorance; I thought this was a name field. Second, by no connection I mean that I was expecting to be output a formula or formulas that resembled the distribution illustrated by the Manipulate chart. I was not expecting something that could be negative and I was not expecting a steadily rising graph. In short I was expecting a distribution closed by the x axis. – user120911 Sep 9 '18 at 14:50
• From the Manipulate code it looks like you have two random variables but your expectation (personal expectation not statistical expectation) is that a single formula should result. Are p and F parameters? Again, please be more explicit. – JimB Sep 9 '18 at 19:05

Here's one way to put everything into a single function. This will not give you a symbolic function in terms of x and f but such a function is so complicated/busy-looking that I don't see how it would be useful.

g[x_, f_] := Module[{gg, const, P},
gg = Piecewise[{
{((-1 + f) P + x)/(f^2 P^2), x <= P <= -(x/(-1 + f)) && x <= 1/2},
{(P + f P - x)/(f^2 P^2),    x/(1 + f) <= P <= x <= 1/2},
{(f - P - f P + x)/(f^2 (-1 + P)^2), x <= P <= (f + x)/(1 + f) && 1/2 < x <= 1},
{(f + P - f P - x)/(f^2 (-1 + P)^2), (f - x)/(-1 + f) <= P <= x && 1/2 < x <= 1}}, 0];
const = Integrate[gg, {P, 0, 1}];
gg = FullSimplify[PiecewiseExpand[gg/const]];
ProbabilityDistribution[gg, {P, 0, 1}]]

Manipulate[\[ScriptCapitalD] = g[x, f];
Plot[PDF[\[ScriptCapitalD], P], {P, 0, 1}, PlotRange -> All],
{{x, 0.5}, 0, 1, Appearance -> "Labeled"},
{{f, 0.5}, 0, 1, Appearance -> "Labeled"}] I venture the following solution:

Since the distribution switches between consisting of different segments of four distinct functions at $x = 0.5$, it makes sense to examine the case below and above that point.

The switching pattern is as follows:

$x < 0.5$:

((-1 + f) P + x)/(f^2 P^2)
x <= P <= -(x/(-1 + f))

(P + f P - x)/(f^2 P^2)
x/(1 + f) <= P <= x


$x > 0.5$:

(f - P - f P + x)/(f^2 (-1 + P)^2)
x <= P <= -(x/(-1 + f))

(f + P - f P - x)/(f^2 (-1 + P)^2)
(f - x)/(-1 + f) <= P <= x


My solution is therefore:

\[ScriptCapitalD]A = ProbabilityDistribution[{"PDF", Piecewise[{{((-1 + f) P + x)/(f^2 P^2), x <=  P <= -(x/(-1 + f))}, {(P + f P - x)/(f^2 P^2), x/(1 + f) <=  P <=  x}}, 0]}, {P, 0, 1}, Assumptions -> {0 < x <= 1/2, 0 < f < 1}]

\[ScriptCapitalD]B = ProbabilityDistribution[{"PDF", Piecewise[{{(f - P - f P + x)/(f^2 (-1 + P)^2), x <= P <= (f + x)/(1 + f)}, {(f + P - f P - x)/(f^2 (-1 + P)^2), (f - x)/(-1 + f) <= P <= x}}, 0]}, {P, 0, 1}, Assumptions -> {1/2 <=  x <  1, 0 < f < 1}]

• Almost there. You'll need to use Method->"Normalize" so that the PDF's integrate to 1. Or you'll need to include the appropriate constant as you define the ProbabilityDistribution. – JimB Sep 10 '18 at 13:07