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.
runif(6*N)
, then reshape as an \(N\times 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.# Do exercise 2 here
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.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.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.# given output of rbernoulli or rbinom (a vector of 0's and 1's)
# compute the length of the longest continuous run of 1's
= function(trials){
longestHeadRun 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
mean( )
and sd( )
, but if you really want to do a completely manual Monte Carlo, feel free to compute the \(t\)-statistic yourself.# Do exercise 4 here