# Best way of transforming random variable

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?

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

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

To plot that one can use

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


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}}]]


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]


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


• Something to adjust in view of the result of Plot[Evaluate[pdf[[1, 1, 1]]], {z, 0, 4}, PlotRange -> {Automatic, {0, Automatic}}] . Apr 15 '20 at 20:30
• Also Plot[Evaluate[pdf], {z, 0, 4}, PlotRange -> {Automatic, {0, Automatic}}] produces an error communication and an empty plot. Apr 15 '20 at 20:43
• @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.
– JimB
Apr 15 '20 at 20:53
• 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$? Apr 15 '20 at 20:53
• @user64494 I'm sure someone could supply the steps. And it was not just made by hand; my small brain helped a bit.
– JimB
Apr 15 '20 at 21:26