5
$\begingroup$

I am working with the following code:

\[ScriptCapitalD] = 
 TransformedDistribution[u - Log[u], 
  u \[Distributed] UniformDistribution[{1, 2}]]
Plot[PDF[\[ScriptCapitalD], x], {x, 1, 2}, Filling -> Axis]

Basically, I want to transform a uniform random variable, $u$ into $u-\log{u}$, and plot the pdf of the new random variable. However, Mathematica seems to take a long time using the function TransformedDistribution and moreover, I am not being able to retrieve a Plot, so perhaps the function is not working well under my transformation.

Do you have any suggestions on how to obtain a solution for this?

$\endgroup$
8
  • 1
    $\begingroup$ Cross-posted at math.stackexchange.com/questions/3625412/…. $\endgroup$
    – JimB
    Apr 15 '20 at 18:03
  • $\begingroup$ I don't understand why @m0nhawk deleted his/her correct answer. $\endgroup$
    – user64494
    Apr 15 '20 at 18:25
  • $\begingroup$ @user64494 $U$ and $\log U$ are not independent as assumed in that answer. $\endgroup$
    – JimB
    Apr 15 '20 at 18:34
  • $\begingroup$ @JimB: I've demonstrated how to correct it, considering two IID. $\endgroup$
    – user64494
    Apr 15 '20 at 18:48
  • 1
    $\begingroup$ Your question on the Math StackExchange site was (essentially) about $u-\log u$. Here did you mean $u+\log u$ or $u-\log u$ ? $\endgroup$
    – JimB
    Apr 15 '20 at 19:58
5
$\begingroup$

It might just be with the Plot command that is giving you trouble.

dist = TransformedDistribution[ u - Log[u], u \[Distributed] UniformDistribution[{1, 2}]];
pdf = PDF[dist, z] // FullSimplify

results in

Density function

To plot that one can use

Plot[Evaluate[pdf[[1, 1, 1]]], {z, 1, 2 - Log[2]}, PlotRange -> {Automatic, {0, Automatic}}]

Plot of density function

As a check:

zz = RandomVariate[dist, 100000];
Show[Histogram[zz, 100, "PDF"],
 Plot[Evaluate[pdf[[1, 1, 1]]], {z, 1, 2 - Log[2]}, PlotRange -> {Automatic, {0, 30}}]]

Histogram and density

Addition:

The resulting Piecewise function from PDF[dist, z] has two pieces that are "identities" that are true when 1 <= z <= 2 - Log[2] but when Plot evaluates the function with machine precision numbers, things go weird. So here is a cleaned up version of the resulting probability density function that plays well with Plot:

dist = TransformedDistribution[ u - Log[u], u \[Distributed] UniformDistribution[{1, 2}]];
pdf = PDF[dist, z] // TrigToExp // FunctionExpand;
pdf = Piecewise[{{pdf[[1, 1, 1]], 1 <= z <= 2 - Log[2]}}, 0]

Cleaned up pdf

Plot[pdf, {z, 0.9, 3/2}, PlotStyle -> Thickness[0.01], PlotRangeClipping -> None]

Plot of pdf

$\endgroup$
9
  • 1
    $\begingroup$ Something to adjust in view of the result of Plot[Evaluate[pdf[[1, 1, 1]]], {z, 0, 4}, PlotRange -> {Automatic, {0, Automatic}}] . $\endgroup$
    – user64494
    Apr 15 '20 at 20:30
  • $\begingroup$ Also Plot[Evaluate[pdf], {z, 0, 4}, PlotRange -> {Automatic, {0, Automatic}}] produces an error communication and an empty plot. $\endgroup$
    – user64494
    Apr 15 '20 at 20:43
  • $\begingroup$ @user64494 Thanks, that makes it better: I'll modify the PlotRange as you suggest. On your other comment: that happens because of the conditions of the pdf are not always true when using machine precision numbers that Plot uses. I'll put in a fix for that. $\endgroup$
    – JimB
    Apr 15 '20 at 20:53
  • $\begingroup$ Looks good, thanks! Is there a way to introduce a parameter when working with """RandomVariate?""" Say """dist[a_] = TransformedDistribution[a*u - Log[u], u [Distributed] UniformDistribution[{1, 2}]]; zz[a_] = RandomVariate[dist[a], 100000];"""? And then use """Manipulate""" over the parameter $a$? $\endgroup$ Apr 15 '20 at 20:53
  • 1
    $\begingroup$ @user64494 I'm sure someone could supply the steps. And it was not just made by hand; my small brain helped a bit. $\endgroup$
    – JimB
    Apr 15 '20 at 21:26

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.