Three Interesting Problems: Part 1.

We’re going to be working (in groups) on solving three interesting problems (and learning some Mathematica along the way). I created this text cell by typing Command-7 (or choosing Format-Style-Text from the menus). It lets me include writing and math formulas in a Mathematica notebook which won’t be evaluated as code.

Sampling Random Numbers

Try executing the command below (by putting your cursor in the cell and using Shift-Enter):

RandomVariate[UniformDistribution[{0,1}]]

If you use the RandomVariate command with UniformDistribution, Mathematica will give you a random number sampled (uniformly) from the interval {0,1}. You can change the interval

RandomVariate[UniformDistribution[{5,10}]]

and if you add some more parameters, you can get a list or table of random values

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

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

Experiment with the commands above until you can produce a list of 100 lists of 3 random numbers, each between 4 and 6.

Making plots of PDFs and CDFs.

We can use the histogram command to plot the (experimental) PDF and CDF of random numbers that we generate in different ways. The command below will approximate a graph of the PDF of a uniformly distributed random variable on [0,5] by taking 1,000,000 samples.

Histogram[
RandomVariate[UniformDistribution[{0,5}],1000000],
100,"PDF"]

We can change the graph to the CDF by changing “PDF” to “CDF”:

Histogram[
RandomVariate[UniformDistribution[{0,5}],1000000],
100,"CDF"]

Now we know from class that the PDF of the uniformly distributed random variable on [0,5] is 

	(Subscript[μ, X])(ω) =({{{, ({{(1/5), 0≤ ω ≤ 5}, {0, otherwise}})}})  
	
We can plot this function with

Plot[1/5,{ω,0,5}]

(How did I make the little omega? Escape w escape will do it: ω.)

Now we can display the plot along with the histogram using Show:

Show[{
Histogram[
RandomVariate[UniformDistribution[{0,5}],1000000],
100,"PDF"],
Plot[1/5,{ω,0,5}]
}]

Looks like a pretty good match!

Remember from class that we can find the CDF from the PDF by integrating:

	(Subscript[F, X])(ω) = (∫_(-∞)^ω)(Subscript[μ, X])(t)dt

Now we don’t really need a computer to integrate

	(Subscript[μ, X])(ω) =({{{, ({{(1/5), 0≤ ω ≤ 5}, {0, otherwise}})}})  
 
 but it’s still fun:

Integrate[1/5,{t,0,ω}]

Wait-- wasn’t I supposed to integrate from -∞? I was, but I cheated: the function (Subscript[μ, X])is 0 for all t < 0 anyway, so I might as well not bother integrating over that region. We can plot the CDF against the histogram to check that they match

Show[{
Histogram[
RandomVariate[UniformDistribution[{0,5}],1000000],
100,"CDF"],
Plot[ω/5,{ω,0,5}]
}]

The first problem.

Suppose:

X is uniformly distributed in [0,1]
	Y = (X^2)
	
What is the PDF of Y? What is the CDF of Y?

To gather data, we’ll have to take the squares of random numbers which are uniformly distributed between 0 and 1 and plot histograms of their PDF and CDF. We know how to generate a list of random numbers, so let’s concentrate on the squaring part.

We can define a function f(x) = x^2 by typing:

f[x_] := x^2

You can verify that this works by trying f for a few different values of x:

f[5]

Now we can apply a function to a list of numbers with the Map operator, which is written /@ in Mathematica. It’s used like this:

f /@ {1,2,3,4,5}

and it gives you a list of function outputs.

Gathering data.

Your first job is to create histograms of the squares of 1,000,000 uniformly distributed random numbers between 0 and 1. These histograms, by the way, are called the “sample PDF” and “sample CDF” or “experimental PDF” and “experimental CDF”.

Do that in the next few cells. Remember to describe your work with text cells (Command-7) and put your code in code cells (they are the default type, but you can change any cell to a code cell with Command-8).

Doing theoretical calculations.

Remember from class that our procedure for finding the CDF and PDF of a derived variable has three steps:

1. Write the event “Y ≤ ω” as an event (Subscript[E, ω]) for the r.v. X using the relationship Y=(X^2). 

2. Now find the probability of (Subscript[E, ω]) by integrating the probability mass function for X: P((Subscript[E, ω])) = (∫_(Subscript[E, ω]))(Subscript[μ, X])(t) dt. We know that the CDF for Y is given by (Subscript[F, Y])(ω) = P(Y ≤ ω) = P((Subscript[E, ω])), so you now know the CDF (Subscript[F, Y])(ω).

3. We know that the PDF (Subscript[μ, Y])(ω) = (d/(dω))(Subscript[F, Y])(ω). So differentiate your formula for (Subscript[F, Y])(ω) from step 2.

Write that out below. If you want to use Mathematica to do the differentiating and integrating, you can use

Integrate[g[x],{x,a,b}]

to integrate a function g(x) over (a,b) with respect to x and

D[g[x],x]

to differentiate. For example:

Integrate[Sin[x],{x,0,π}]

D[Sin[x],x]

If you don’t want to use the “Basic Math Assistant” Palette to help you write out the math in Mathematica, a pro tip is that you can work on paper, take a picture with your phone, and drag and drop the picture into this notebook.

Checking theory against data.

Now show the histograms of the CDF and PDF of Y obtained by experiment together with the plots of the functions you derived for the CDF and PDF. Use the “Histogram”, “Plot” and “Show” commands to do this, as we practiced above.

Submitting your work.

Once you’ve done all this, save your Mathematica notebook as a PDF (you can either print to a PDF or use “Save As”) and submit it to Gradescope as a group assignment with the members of your group. Congratulations! You’ve done the first interesting problem!