1
$\begingroup$

I am unable to get Mathematica to return the CDF of a distribution produced by TransformedDistribution. It "executes" for 6 hours on my machine and then returns a blank plot. Could someone execute the code below and explain why this is happening and offer a solution ? Is there something wrong with my code ?

beg = AbsoluteTime[];
dist1 = ExponentialDistribution[0.1];
pdf1 = PDF[dist1, x];
cdf1 = CDF[dist1, x];
plot1 = Plot[pdf1, {x, 0, 100}, Frame -> True, 
FrameLabel -> {"X", "PDF"}, PlotLabel -> "PDF of X (dist1)", 
PlotStyle -> Red, PlotRange -> All]
plot2 = Plot[cdf1, {x, 0, 100}, Frame -> True, 
FrameLabel -> {"X", "CDF"}, PlotLabel -> "CDF of X (dist1)", 
PlotStyle -> Red, PlotRange -> All]

dist2 = NormalDistribution[53, 2.4];
pdf2 = PDF[dist2, x];
cdf2 = CDF[dist2, x];
plot3 = Plot[pdf2, {x, 0, 100}, Frame -> True, 
FrameLabel -> {"X", "PDF"}, PlotLabel -> "PDF of X (dist2)", 
PlotStyle -> Red, PlotRange -> All]
plot4 = Plot[cdf2, {x, 0, 100}, Frame -> True, 
FrameLabel -> {"X", "CDF"}, PlotLabel -> "CDF of X (dist2)", 
PlotStyle -> Red, PlotRange -> All]
end = AbsoluteTime[];
executiontime = (end - beg)/60  (* minutes *)

Here is the output, so far so good.

Output1

Now here is where the problem is.

beg = AbsoluteTime[];
dist3 = TransformedDistribution[
a + b, {a \[Distributed] dist1, b \[Distributed] dist2}]
pdf3 = PDF[dist3, x];
cdf3 = CDF[dist3, x];
plot5 = Plot[pdf3, {x, 0, 100}, Frame -> True, 
FrameLabel -> {"X", "PDF"}, PlotLabel -> "PDF of X (dist3)", 
PlotStyle -> Red, PlotRange -> All]
plot6 = Plot[cdf3, {x, 0, 100}, Frame -> True, 
FrameLabel -> {"X", "CDF"}, PlotLabel -> "CDF of X (dist3)", 
PlotStyle -> Red, PlotRange -> All]
end = AbsoluteTime[];
executiontime = (end - beg)/60  (* minutes *)

And here is the output.

Ooutput2

Thanks for any help.

$\endgroup$
2
  • $\begingroup$ @Belasarius and wolfies, thank you for the workarounds. Any idea why my original code did not work ? Was the CDF function unable to do the integration ? In a perfect world should an error message have been produced ? $\endgroup$
    – Steve
    Mar 19 '14 at 15:01
  • $\begingroup$ No,sorry. I really don't know why that happens. But usually in Mma one learn that is better to circumvent those non-working edges than fighting them :) $\endgroup$ Mar 19 '14 at 17:17
1
$\begingroup$

There is nothing wrong with your code. Mathematica just fails to calculate the CDF.

But you can always go by calculating the CDF explicitly (and numerically):

cdf3N[y_?NumericQ] := NIntegrate[pdf3, {x, 0., y}, Method -> "LocalAdaptive"]
Plot[cdf3N[x], {x, 0, 100}, Frame -> True, FrameLabel -> {"X", "CDF"},
     PlotLabel -> "CDF of X (dist3)", PlotStyle -> Red, PlotRange -> All]

Mathematica graphics

If you're going to do serious work with this, you should try to find the better integration method for your function (which is not always a trivial task).

$\endgroup$
1
$\begingroup$

Given: $X\sim \text{Exponential}(\lambda)$ and $Y\sim N(\mu,\sigma^2)$. Then, if $X$ and $Y$ are independent, the joint pdf of $(X,Y)$ is say $f(x,y)$:

enter image description here

Let $Z = X + Y$. You seek the cdf of $Z$, i.e. $P(Z<z) = P(X+Y<z)$:

enter image description here

All done.

Here is a plot of the solution cdf given your parameter values (i.e. with $\lambda = 10$):

Plot[cdf /. {λ -> 10, μ -> 53, σ -> 2.4}, {z, 0, 100}, AxesLabel -> {z, "cdf"}]  

enter image description here

Notes

  1. The Prob function used above is from the mathStatica package for Mathematica. As disclosure, I should add that I am one of the authors.
$\endgroup$
3
  • $\begingroup$ Hi! Could you please add the code instead of an image? Thanks! $\endgroup$ Mar 19 '14 at 17:14
  • $\begingroup$ @belisarius The problem is the parsing of the Greek characters in Stackexchange code ... here goes anyway ... $\endgroup$
    – wolfies
    Mar 19 '14 at 17:22
  • $\begingroup$ This thingie does it for you meta.mathematica.stackexchange.com/q/1043/193 (I already edited your code using it) $\endgroup$ Mar 19 '14 at 17:43

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.