Home > Mathematica, Mathematics > What to Expect

What to Expect

I found this question on Mathematica Stackexhange interesting. I  learned a lot from wolfies answer. I post this not as an answer to the question of closed form but to illustrate consistency of a number of approaches.

The calculations take some time but it does not seem onerous.

Insights into the expectation can be obtained from the region of of interest.

``` ms[p_] := Show[Plot3D[(1 - x)^y, {x, 0, 1}, {y, 2, 5}, PlotStyle -> {LightBlue, Opacity[0.5]} , PerformanceGoal -> "Speed", PlotRange -> {0, 1}], Plot3D[(1 - x)^y, {x, 0, 1}, {y, 2, 5}, MeshFunctions -> {#3 &}, Mesh -> {{1 - p}}, MeshStyle -> {Red, Thick}, RegionFunction -> Function[{x, y, z}, (1 - x)^y "Speed", PlotRange -> {0, 1}], AxesLabel -> (Style[#, 20] & /@ {x, y, (1 - x)^y}), ImageSize -> 500] Manipulate[ms[t], {{t, 0, "p"}, 0, 1, Appearance -> "Labeled"}] ```

Or more easily with:
``` Manipulate[ RegionPlot[(1 - x)^y < 1 - p, {x, 0, 1}, {y, 2, 5}], {p, 0, 1}] ```

Now to numerically calculate the expectation:
``` int[p_] := Integrate[ Boole[{x, y} \[Element] ImplicitRegion[0 < x < 1 && 2 < y < 5, {x, y}]] x/3, {x, 0, 1}, {y, Log[1 - p]/Log[1 - x], 5}] td = TransformedDistribution[(1 - x)^y, {x, y} \[Distributed] UniformDistribution[{{0, 1}, {2, 5}}]]; d[p_] := Probability[z < 1 - p, z \[Distributed] td] result[p_] := int[p]/d[p] ex[p_] := Expectation[ x \[Conditioned] (1 - x)^y < 1 - p, {x, y} \[Distributed] UniformDistribution[{{0, 1}, {2, 5}}]] Definition[rp] rv[p_, n_] := Module[{r}, r = RandomVariate[UniformDistribution[{{0, 1}, {2, 5}}], n]; Mean[Pick[r, ((1 - #1)^#2 < 1 - p) & @@@ r][[All, 1]]]] rv1000[p_] := rv[p, 1000]; ```
The above firstly uses wolfies method, then just asks Mathematica to calculate explicitly and the simulates.
Tabulating:
``` grid = Prepend[ Through[{Identity, result, ex, rv1000}[#]] & /@ Range[0.1, 0.9, 0.1], Style[#, Bold] & /@ {"p", "Derived", "Expectation", "Estimated (N=1000)"}]; gr = Grid[grid, Frame -> All, Background -> {None, {{White, LightBlue}}}] ```

Plotting (using `Table` to minimize computation time):
``` plt = ListLinePlot[Table[ex[t], {t, 0.1, 0.9, 0.1}], Frame -> True, FrameLabel -> {"p", "Conditional Expectation x"}, BaseStyle -> 16, ImageSize -> 600] ```

This does little to answer the closed form question but I found it fun.