# Calculating conditional expectation for uniform distribution over a trapezoid

I have a bivariate uniform distribution, defined over a trapezoid $$\Omega$$ with vertices $$(2, 2), (1, -2), (-2, -2), (-2, 2)$$:

$$\vec{\xi} = (\xi_1, \xi_2) \sim \mathcal U (\Omega).$$

How can I represent this distribution in Mathematica and calculate the conditional expectation $$E(\xi_1 | \xi_2)$$?

Note that I have already made four attempts (#1, #2, #3, #4) without any success.

• This is now your fifth attempt to post the exact same question, and once again it is unclear. I have significantly edited your question, trying to improve it as much as possible. Please, do not go on posting such low-quality questions. Try to make them clear. Help yourself with online translation tools if English is not your native language. Commented Jan 14 at 13:49
• @Domen I think your excellent edits made the question clear. (Athough, I would drop the $\vec{\xi} = (\xi_1, \xi_2) \sim \mathcal U (\Omega).$ as it is not necessary given the common loose statistical notation in some questions.)
– JimB
Commented Jan 14 at 23:31
• @JimB, thanks – I didn't really know how to include the naming ($\xi_1$ and $\xi_2$) of the variables, hence the equation ... Please, feel free to remove it :) Commented Jan 15 at 11:06

First, you need to set up your trapezoid region. You can use Polygon:

Ω = Polygon[{{2, 2}, {1, -2}, {-2, -2}, {-2, 2}}];


Then, you need to prepare your distribution. You want a uniform distribution over this quadrilateral, which you can construct using ProbabilityDistribution and RegionMember:

dist = ProbabilityDistribution[Boole[RegionMember[Ω, {ξ1, ξ2}]], {ξ1, -2, 2}, {ξ2, -2, 2}]
(* ProbabilityDistribution[Boole[
(\[FormalX]1 | \[FormalX]2) ∈ Reals &&
2 + \[FormalX]1 >= 0 && -2 <= \[FormalX]2 <= 2 && 4 \[FormalX]1 <= 6 + \[FormalX]2],
{\[FormalX]1, -2, 2}, {\[FormalX]2, -2, 2}] *)


You see that the distribution was already automatically "parametrized" into a set of 3 inequalities, which define your trapezoid.

Now, you can use Expectation together with Conditioned to calculate $$E(\xi_1 | \xi_2)$$:

Expectation[ξ1 \[Conditioned] ξ2 == x, {ξ1, ξ2} \[Distributed] dist] /. x -> ξ2
(* ConditionalExpression[1/8 (-2 + ξ), ! (ξ >= 2 || ξ <= -2)] *)


Therefore: $$E(\xi_1 | \xi_2) = \frac{\xi_2}{8}-\frac{1}{4}.$$

@Domen 's answer is the appropriate answer for "how to represent this function in Mathematica" (and should be the selected answer). But if one didn't have the functions ProbabilityDistribution, RegionMember, and Expectation, then the following brute-force method could be used in a few other languages:

(* Joint distribution for a bivariate uniform distribution of area 14 *)
jointpdf[x1_, x2_] := Piecewise[{{1/14, (-2 <= x2 <= 2 && -2 <= x1 <= 1) || (1 < x1 <= 2) && -6 + 4 x1 <= x2 <= 2}}, 0]

(* Marginal pdf of x2 *)
pdfx2[x2_] := Integrate[jointpdf[x1, x2], {x1, -\[Infinity], \[Infinity]}] //  FullSimplify

(* Conditional mean:  mean of x1 given x2 *)
conditionalMean = Integrate[x1  pdf[x1, x2]/pdfx2[x2], {x1, -2, 2}] // FullSimplify