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}, 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}, 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 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}}, 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.87053496,"math_prob":0.9460344,"size":603,"snap":"2022-05-2022-21","text_gpt3_token_len":150,"char_repetition_ratio":0.120200336,"word_repetition_ratio":0.0,"special_character_ratio":0.24378109,"punctuation_ratio":0.15044248,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9963,"pos_list":[0,1,2,3,4,5,6,7,8,9,10],"im_url_duplicate_count":[null,1,null,1,null,1,null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-01-23T09:57:43Z\",\"WARC-Record-ID\":\"<urn:uuid:d473075b-398f-4a16-b74e-6a14c85543df>\",\"Content-Length\":\"153432\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1f580665-1e8b-4e96-ab9d-161c1085e97d>\",\"WARC-Concurrent-To\":\"<urn:uuid:8f59d5f8-15a0-4c78-901d-5cab025cfd18>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://mathematica.stackexchange.com/questions/219592/best-way-of-transforming-random-variable\",\"WARC-Payload-Digest\":\"sha1:D5IW32AY6GPFCHSIYHGHWZJ5JNHMLLIX\",\"WARC-Block-Digest\":\"sha1:B6P5YGEFFGBOTUJTPT5PAPQFOBDWUTJV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-05/CC-MAIN-2022-05_segments_1642320304217.55_warc_CC-MAIN-20220123081226-20220123111226-00588.warc.gz\"}"}