Three Interesting Problems: Part 2.

We’re going to continue our work on the “interesting problems” by considering the sum of two random numbers which are uniformly sampled between 0 and 1. As you read this notebook, please execute each piece of code (in place) and feel free to edit it and try again. You will type your work into the notebook (in the places indicated) before submitting it.

We learned in the last problem how to sample numbers and make a histogram. So let’s start there.

RandomVariate[UniformDistribution[{0,1}],10]

We’d like to sum pairs of these numbers, so we have to generate lists of lists of two random numbers. Remember that we can do this by modifying the input to the “RandomVariate” command:

RandomVariate[UniformDistribution[{0,1}],{10,2}]

We can sum all the numbers in a list with the Total command:

Total[{2,3,4}]

So we can get a list of totals by using Map or /@ to apply the Total function to each of the interior lists:

Total /@ RandomVariate[UniformDistribution[{0,1}],{10,2}]

Work for your group.

Use Total and RandomVariate together with Histogram to create a plot of the experimental PDF and CDF of the sum of two uniform random variates in [0,1]. Do this in the cells immediately below this one.

Areas and pairs of random variates

If we sample two coordinates uniformly and randomly in [0,1], we could think of them as the x and y coordinates of points in the unit square. We can plot these points with the ListPlot command:

ListPlot[RandomVariate[UniformDistribution[{0,1}],{10,2}],PlotRange->{{0,1},{0,1}},AspectRatio->1]

If we change to a LOT of points, we can see that they fill in the square “uniformly”.

ListPlot[RandomVariate[UniformDistribution[{0,1}],{10000,2}],PlotRange->{{0,1},{0,1}},AspectRatio->1]

Work for your group.

Experiment with the number of points plotted until you’re convinced that they cover the square uniformly. Show plots for the different numbers of points you tried, and add some explanation. What does it mean for the points to “look uniform”?

Area and random sampling

In fact, the fraction of these random samples which fall within a given subset E ⊂ [0,1] ×[0,1] is given by Area(E)/Area([0,1] × [0,1]).

You are now going to do an experiment to verify this. Consider the region of the square consisting of points within 1 unit of the origin:

RegionPlot[0 < x < 1 ∧ 0 < y < 1 ∧ x^2 + y^2 ≤ 1,{x,0,1},{y,0,1}]

Work for your group.

Now you need to do several things: 

	1. Compute the area of this subset of the square (use calculus or geometry). 
	2. Write a function f of a two element list which returns 1 if the point is inside the region above and 0 if not.
	3. Apply the function to a list of samples generated by RandomVariate[UniformDistribution[{0,1}],{n,2}], and use the Mean function to get the fraction of samples inside the region.
	4. Verify by experimenting with various values of n that as n -> ∞ the fraction of samples inside the region converges to its area. You should add a written explanation and a plot which shows that your experimental means are closing in on the correct value. How do the errors relate to  the number of samples n?

You are going to do this work by adding cells below this one. You should definitely include text cells (Command-7) explaining your work. Also include graphs or plots if it helps you explain (consider ListPlot, for instance). For each of these questions, copy the question into a text cell, then add another text cell explaining your approach or answer, then add computation cells and text cells as needed below.

There are a few programming things which will help here. You can write a function of a list as

f[{x_,y_}] := x + y

f[{3,2}]

The If command will help you distinguish between cases. The syntax is 

	If[ (test), (value if test is true), (value if test is false)]

For example, let’s rewrite f to return 1 if the sum of entries in the list is < 4 and 0 otherwise.

f[{x_,y_}] := If[x + y < 4,1,0]

f[{1,1}]

f[{5,7}]

The Mean function gives you the average value of elements in a list:

Mean[{3,4,5,7}]

Areas and sums of random variates.

You now need to work out the CDF of the sum of two random variables in [0,1]. We can do this by figuring out the CDF and differentiating to get the PDF. Observe that the portion of the unit square where the sum of the coordinates is < λ is easy to picture:

SumLessThan[λ_] := RegionPlot[0 < x < 1 ∧ 0 < y < 1 ∧ x + y ≤ λ,{x,0,1},{y,0,1}]

SumLessThan[5/3]

Work for your group

Experiment with this function until you understand the geometry of the region. Please describe the region in words in the cell below this one.

Area and sums, continued.

Now observe that if 

X is distributed in [0,1] with pdf (Subscript[μ, X])(x) = 1
Y is distributed in [0,1] with pdf (Subscript[μ, Y])(y) = 1
Z = X + Y

then the CDF

	(Subscript[F, Z])(λ) = P(X + Y ≤ λ) = Area(portion of unit square with x + y ≤ λ)

Work for your group.

Find this CDF explicitly as a formula. Then differentiate it to get the PDF for Z. You can use Mathematica’s “D” command to differentiate if you like, or do the work on paper and include a photograph.

It may help to use the Piecewise function in Mathematica:

f[x_] := Piecewise[{{x^2,x<0},{3x,x≥ 0}}]

Plot[f[x],{x,-3,3}]

Combining Theory and Practice -- Work for your group.

Use the Show command to plot your CDF and PDF for Z together with the experimental PDF and CDF you derived above. Make sure that they match! If they don’t, one of the two computations is wrong and you should find the problem and fix it.

Now find an experimental PDF and CDF for sums of three, four, and “lots” of univariate random variates and plot them. These are harder to find theoretically (at least at this point), but you can make some observations about how they look compared to your original PDF and CDF.