# Setting up uniform distribution over non-rectangular support

$$X$$ and $$Y$$ are uniformly distributed over the triangle $$T$$ in the first quadrant of the $$x$$-$$y$$ plane with vertices $$(0,0)$$, $$(1,0)$$, and $$(0,1)$$, so that

$$T=\{(x,y)\mid0\le x\le1,0\le y\le1-x\}$$

The joint PDF is then

$$f_{X,Y}(x,y)=\begin{cases}2&\text{for }(x,y)\in T\\0&\text{otherwise}\end{cases}$$

I'm asked to find various moments, covariance, correlation, etc. which are all easy to find by hand, but I'd like to check my results in Mathematica. Are there built-in symbols that can handle this sort of distribution?

The documentation for UniformDistribution suggests the support must be a rectangle. Trying to insert 1 - x as the upper bound on y gives an incorrect, non-uniform PDF:

PDF[UniformDistribution[{{0, 1}, {0, 1 - x}}], {x, y}]
(* Piecewise[{{(1 - x)^(-1), x >= 0 && y >= 0 && 1 - x >= 0 && 1 - x - y >= 0}}, 0] *)


i.e.

$$f_{X,Y}(x,y)=\begin{cases}\frac1{1-x}&\text{for }(x,y)\in T\\0&\text{otherwise}\end{cases}$$

I know I can define my own PDF f,

f[x_, y_] := Piecewise[{{2, 0 <= x <= 1 && 0 <= y <= 1 - x}}]


and compute moments and co. by integrating f accordingly. But is there a way to set up a distribution dist so that I can check my work with Expectation or Moment etc.?

You can use Triangle as a region. I use Region here purely for visualization. It can be omitted.

reg = Region@Triangle[{{0, 0}, {1, 0}, {0, 1}}]


As Roman said, you can apply RegionMoment to it:

RegionMoment[reg, {1, 0}]
(* 1/6 *)

RegionMoment[reg, {1, 0}]/RegionMoment[reg, {0, 0}]
(* 1/3 *)


For more complicated things, you can evaluate any integral over the region:

Integrate[Indexed[x, 1], x \[Element] reg]
(* 1/6 *)


For numerical verification, you can use RandomPoint.

Moment[RandomPoint[reg, 10000], 1]
(* {0.332256, 0.335306} *)

• Good point! In fact, you don't even need the Region in there: RegionMoment[Triangle[{ ... }], {1, 0}] works as well. (+1) – MarcoB Dec 19 '19 at 19:42

A Dirichlet distribution with parameters $$(1,1,1)$$ will represent a uniform distribution over exactly the triangle you seek:

ListPlot[
RandomVariate[DirichletDistribution[{1, 1, 1}], 10000],
AspectRatio -> Automatic
] You can then obtain expectations, moments, etc using the usual statistical machinery in MMA. So for instance, to reproduce the results shown in Roman's answer,

$$E(X)$$:

Moment[DirichletDistribution[{1, 1, 1}], {1, 0}]     (* Out: 1/3  *)


$$E(Y^2)$$:

Moment[DirichletDistribution[{1, 1, 1}], {0, 2}]     (* Out: 1/6  *)


$$E(XY)$$:

Moment[DirichletDistribution[{1, 1, 1}], {1, 1}]     (* Out: 1/12 *)


You can define an implicit region

J = ImplicitRegion[x >= 0 && y >= 0 && 1 - x >= 0 && 1 - x - y >= 0, {x, y}];


and then calculate its moments with RegionMoment.

The area is $$1/2$$, not $$2$$ as you had found:

RegionMoment[J, {0, 0}]
(*    1/2    *)


For example, we get the expectation value $$E[X]=1/3$$:

RegionMoment[J, {1, 0}]/RegionMoment[J, {0, 0}]
(*    1/3    *)


$$E[Y^2]=1/6$$:

RegionMoment[J, {0, 2}]/RegionMoment[J, {0, 0}]
(*    1/6    *)


$$E[XY]=1/12$$:

RegionMoment[J, {1, 1}]/RegionMoment[J, {0, 0}]
(*    1/12    *)

• You can use Triangle as a region directly. – Szabolcs Dec 19 '19 at 19:19
• @Szabolcs yes in this particular case you can. The method presented here is much more general though. I was trying to answer the general question posed in the title. – Roman Dec 19 '19 at 20:23

After some further investigation, it appears I can do what I want by using

dist = ProbabilityDistribution[2 Boole[0 <= x <= 1 && 0 <= y <= 1 - x], {x, 0, 1}, {y, 0, 1}]


Then e.g. $$E[X]$$, $$E[Y^2]$$, and $$E[XY]$$ are given respectively by

Moment[dist, {1, 0}]
(* 1/3 *)

Moment[dist, {0, 2}]
(* 1/6 *)

Moment[dist, {1, 1}]
(* 1/12 *)