STAT340: Discussion 2: Monte Carlo
STAT340: Discussion 2: Monte Carlo
Copyright By Assignmentchef assignmentchef
link to source
XKCD comic
You can do these exercises individually, but we recommend you work on them in a small group. Choose 2 of the following exercises to complete.
Estimating Robbins constant (mean distance between points in a cube).
Randomly generate 2 points ((x_1,y_1,z_1)), ((x_2,y_2,z_2)) uniformly in the unit cube a total of (N) times (at least (1000), but the more the better!)
hint: you can easily generate all the coordinates you need at once with runif(6*N), then reshape as an (Ntimes 6) matrix (one column for each coordinate component, with each row representing a pair of points) and then perform the arithmetic in the next step by using vectorized operations on the columns (i.e.using each column all at once) to improve computational efficiency.
if you are having difficulties with the above^ you can always use the naive way of running a for loop N times, where in each step of the loop you generate 2 points (6 coordinates total) and then perform the arithmetic in the next step.
Next, compute the standard Euclidean distance between each pair of points and find the mean distance. (Bonus: plot the distribution of these distances!)
Calculate your percentage error from the true value.
Bonus: can you increase the accuracy of your estimation by using more points? How low can you get the error?
Super bonus: Repeat the above for another 2D or 3D object of your choice (how about a triangle or a sphere?)
# Do exercise 2 here
How many heads in a row should you see in (N) flips of a fair coin?
Start by randomly flipping a fair coin ((p=0.5)) a total of (N=10) times (hint: use either rbernoulli function from purrr or rbinom with n=10 and size=1) and record how many heads (defined as a value of (1)) in a row you observe (this has been implemented in the function longestHeadRun function below for you.
Repeat the above step (M) times (at least (1000) times, but this time, dont use an extremely large (M), since we will repeat the previous step for other values of (N)). What is the mean length of the largest run of heads in (10) flips?
NOTE: (N) here is the size of each experiment (i.e.each experiment consists of (N) flips), whereas (M) is how many experiments are performed. It is common in Monte Carlo methods to have two types of parameters, one type for the properties of each experiment, and one type that determines how many experiments are done. Increasing (N) (number of flips in each experiment) will increase the mean-run-length, whereas increasing (M) (number of experiments) will increase the precision of your mean-run-length estimate for a particular number of flips.
Now, repeat the above (you may use the same (M)) for at least 3 other values of (N) (again, feel free to do more if you wish!). Display your results in a table.
NOTE this step should be easy if youve written your code with good style. I recommend writing a function that does all the above for any given (N) and (M) and maybe (p), e.g.findMeanRun = function(N,M,p=0.5){}. Then, for different values of (N) and (M) you can simply change the arguments given to the function, e.g.findMeanRun(10,1000) or findMeanRun(20,1000), etc, then put them in a data frame.
ALSO NOTE the above function syntax^ sets N and M as arguments to the function without default values, but sets 0.5 as the default value of the argument p. For a different example, see this.
Validate your results against other peoples results (for example, this post). Are your results consistent with others?
Bonus: run a few more values of (N) and plot the results, showing the mean run length vs number of flips (N). (bonus: what happens if you increase (M)?)
Super bonus if you still want MORE: Like the post referenced above, can you fit a smooth curve through the points?
# given output of rbernoulli or rbinom (a vector of 0s and 1s)
# compute the length of the longest continuous run of 1s
longestHeadRun = function(trials){
with(rle(trials),max(c(0,lengths[values==1])))
# demo (output hidden for brevity)
longestHeadRun(c(0,0,0,0,0,0,0,0,0,0,0,0)) # returns 0
longestHeadRun(c(1,0,1,1,0,1,1,1,0,0,0,0)) # returns 3
# Do exercise 3 here
Estimating a (t)-distribution with (N-1) degrees of freedom.
Choose an arbitrary (mu) and (sigma>0) to use for the rest of the problem (you may choose the standard normal (N(0,1)) if you really wish, but wheres the fun in that?).
Start by sampling (N=2) values from the normal distribution with mean (mu) and standard deviation (sigma) (note this counts as (1) experiment) and calculate the (t)-statistic of your sample. Recall the (t)-statistic for a sample (X) is defined as [t=frac{overline{X}-mu}{s/sqrt{N}}~,~~~s=sqrt{frac{1}{N-1}sum_{i=1}^{N}(X_i-overline{X})^2}] where (overline{X}) is the sample mean and (s) is the sample standard deviation
NOTE: Make sure youre actually computing the (s) for this sample, NOT just using (sigma) here!
You can use the built-in mean( ) and sd( ), but if you really want to do a completely manual Monte Carlo, feel free to compute the (t)-statistic yourself.
Also NOTE: Similar to the note in exercise 3.b., (N) here is the size of each experiment and (M) is how many experiments are performed. Increasing (N) gives a (t)-distribution with a different number of degrees of freedom (namely, (N-1)), whereas increasing (M) gives a more accurate estimate of each distribution of a particular degree.
Repeat the above step (M) times (similar to exercise 3.b., use at least (1000) times, but dont use an extremely large (M) since we will repeat this for other values of (N)).
Youve just simulated drawing from a (t)-distribution with (N-1=1) degree of freedom! Now plot the resultant values in a density plot.
For comparison, plot the theoretical distribution with (1) degree of freedom (this page may be helpful). For best results, overlay this on top of the previous plot, but youre having trouble with this, you can also plot them side-by-side.
Repeat the above steps for at least 3 other values of (N) (for example 3, 6, 11, but feel free to choose your own or choose more than 3!). For each (N), plot both your simulated distribution and the theoretical distribution.
NOTE: again, like the note in exercise 3.c., this should be easy if you used a function!
Bonus: What do you notice about these distributions? What do they converge to and why?
# Do exercise 4 here
CS: assignmentchef QQ: 1823890830 Email: [email protected]
Reviews
There are no reviews yet.