Random Experiments With R
Random Experiments With R
http://www.math.nyu.edu/faculty/goodman/teaching/mathFin/index.html
Always check the class message board before doing any work on the assignment.
Example 3: This is the same as Example 1, except that the seed has been set
to 1. Note that these are not the numbers you got the first two times.
Example 4: This is the same as Example 3, with the same seed. The numbers
should be the same as Example 3.
Example 5: This is the same as Examples 3 and 4, but with a different seed.
The numbers should be different from the Example 3 and Example 4 numbers.
Example 6: Here we create a big list with a lot of independent standard nor-
mal random variables. The R command hist creates a histogram plot. The
1
code gives several arguments to this function, each on its own line for clarity.
The first argument is the numbers, which are in the list x. The second ar-
gument is the R variable breaks, which is the number of bins to use. Using
more bins makes each bin smaller. Experiment with different numbers of bins,
say 10 and 100. The R variable main is what you want printed at the top of
the histogram plot. You can see that we created a string called Title for this
purpose. The R command that creates Title embeds the number of samples
into the text. That’s what sprintf is for. The last argument, probability,
tells R to plot the estimated probability density instead of the bin counts. The
1 2
picture should be approximately f (x) = √12π e− 2 x . To check this, the code
computes and prints f (0) = √12π . Check whether the printed number matches
the plotted value for x = 0. Note that the hist statement appears twice in
(almost) exactly the same form. The first time it makes a popup window on
your screen with the histogram plot. The second time it makes a .pdf file called
NormalHistogram.pdf with the same plot. The titles of the popup and the .pdf
are slightly different. Look for the difference in the R code.
2
On top on the right is the number of samples Tk so that Tk > t. The code prints
the theoretical and empirical probability for two t values. The agreement is not
great for n = 1000, but it gets better for larger n values.