
In this course, learn to program simulations in R for Monte Carlo methods, including simulated inference with likelihoods and confidence intervals, and working with normal, Poisson, uniform, and exponential variables.
Install R and RStudio as the first step, using the downloadable instructions in the resources section to get started, as they work across current versions.
Explore how R treats variables as active in-memory objects, with vectors as the core data structure and matrices, data frames, and lists, while using RStudio as a convenient front end.
Learn to create vectors in R with c and colon, compare = and the gets symbol for assignment, and distinguish vector class versus mode in RStudio.
Explore how to generate and manipulate sequences in R using the sequence and replicate functions, including start, end, and increment arguments, vector creation, and subscripting for indexing.
Sort vectors to arrange values and use order to return indices for sorting data frames or multi-dimensional structures, enabling you to align related vectors like temperatures and ozone.
Apply the natural log to each matrix element, use the apply family to compute row and column minima and maxima, and sweep to subtract 1 and divide by the maximum.
Learn how to subset vectors in R using indices, which, and negative subscripts to access or exclude elements, assign values, and manage missing values within arrays and sequences.
R treats matrices as two dimensional numeric arrays used in matrix algebra and regression. Create and name matrices with matrix(data, nrow, ncol), and perform elementwise and matrix operations.
Explore list structures in R, how data frames give way to lists in analysis, and how regression or simulation returns complex outputs; model horse kicks deaths with Poisson assumptions.
Predict death counts using a Poisson model with Dpois, compare predicted versus observed from the horse kicks simulation, and store results in a data frame.
Learn to use the R sample function to draw random rows from a data frame, with or without replacement, and sample by row names for Monte Carlo simulations.
Practice writing algebraic expressions in R, assign Z, and build vectors and matrices with appropriate functions, then test with x in A and B in section 1.
Practice crafting R expressions with x, a, and b; study operator precedence, exponentiation, and assignment; work with vectors, matrices, sequences, and identity matrices.
Explore R expressions through practice with vectors, modulo operations, and logical indexing to exclude values divisible by 2, 3, or 7, and manage queues with length and which for subsetting.
Explore Monte Carlo simulation with a coin-toss game to estimate Peter's cumulative winnings over 50 tosses using sample, replicate, and cume sum.
Use a simulation approach to run four 50-toss games, plot cumulative winnings, and compare exact binomial probability of breaking even with a reproducible set.seed run.
Learn to implement Monte Carlo experiments in R by writing a named function for one game, then repeat simulations to estimate fortune, lead frequency, and break-even probabilities.
write a simulation function in r by using the last expression as return, replicate trials, tabulate results with table, plot frequencies, and compute breaking-even probability via binomial.
Explore a Monte Carlo coin-toss simulation to approximate exact probabilities, track the final fortune, count lead changes, and identify the maximum cumulative winnings.
Run a thousand coin-toss simulations in R, storing f, l, and m to analyze lead frequency, and plot the distribution of maximum winnings in 50 plays.
A random permutation is a random ordering of a set of objects, that is, a permutation-valued random variable. The use of random permutations is often fundamental to fields that use randomized algorithms such as coding theory, cryptography, and simulation. A good example of a random permutation is the shuffling of a deck of cards: this is ideally a random permutation of the 52 cards.
Simulate random hat permutations to count correct matches using boolean vectors and 0/1 sums; explore the distribution of matches and the mean as the number of hats grows.
Learn to use R to simulate the hat permutation problem: create an ordered hat vector, generate a random permutation with sample, and count matches with the original order.
Investigate the hat problem through random permutations, converting booleans to ones and zeros, counting matches with a user defined R function, and examining how hat count affects matches.
Explore the hat problem through Monte Carlo style simulations by replicating random permutations of 10 hats, tallying matches, and estimating the distribution and expected value (mean) of matches.
This lecture demonstrates simulating random hat permutations to estimate the probability of no matches across 2 to 20 hats; uses apply and replication to compute and plot the expected value.
Use R to simulate the hat-check problem by representing hats with 1s and 2s, running 1000 trials to estimate matches, probability of 10+ matches, and expected matches.
Perform a Monte Carlo simulation in R of 20 hats in two colors, using scramble hats and replicate function to estimate matches and probability of at least 10 correct colors.
Simulate collecting a complete set of baseball cards using R's sample function to draw 20 cards from 10 players, with and without replacement, and count unique players.
Simulate a complete set of 586 baseball cards by sampling with replacement. Implement a collector function in R to sample 3000 cards and estimate the probability using Monte Carlo.
Use a Monte Carlo simulation to find the optimal number of baseball card purchases, balancing random five-cent packs and twenty-five-cent dealer cards to minimize total expected cost.
simulate buying baseball cards in R to estimate total cost using 500 trials and replicate to compute the expected cost across purchase levels, with a minimum near 950 cards.
Attempt the collecting state quarters exercise and recognize you may need concepts from later baseball cards videos; a solution will be posted later in the course.
simulate buying 100 quarters from 50 states with replacement, measure unique states, replicate results, and estimate 43 1/3 unique coins and about $13.26 to complete the set.
Explore streaky batting behavior with a Monte Carlo approach; assess the significance of hitting streaks and build a function to compute run lengths in binary sequences using a padded vector.
Compute zero-base-hit sequences in game data, derive streak lengths from consecutive zeros, filter to nonzeros, and determine the longest streak with a custom function.
Explore a nonparametric bootstrap approach to baseball batting streaks by permuting 116 at-bats, estimating the distribution of the longest streak, and assessing significance with Monte Carlo simulations.
Explore counting switches in binary sequences, compute the number of switches with a difference-based method, and compare observed switches to a 10000-run Monte Carlo simulation in R.
Assess Utley's 35-game hitting streak with a non-parametric permutation of 116 hits in 160 games, and analyze switches via diff and histogram across thousands of samples to judge unusual behavior.
Monte Carlo methods (or Monte Carlo experiments) are a broad class of computational algorithms that rely on repeated random sampling to obtain numerical results. They are often used in physical and mathematical problems and are most useful when it is difficult or impossible to use other mathematical methods. Monte Carlo methods are mainly used in three distinct problem classes: optimization, numerical integration, and generating draws from a probability distribution.
Use Monte Carlo simulations with independent uniform arrival times for Sam and Amy to estimate the probability Amy arrives before Sam, visualized via a scatterplot and area region.
Compute the mean of the differences between each pair, defined as aney's arrival minus sam's. The mean is 0.479 hours, with a small standard error; 25% arrive earlier.
Statistical inference is the process of deducing properties of an underlying distribution by analysis of data. Inferential statistical analysis infers properties about a population: this includes testing hypotheses and deriving estimates. The population is assumed to be larger than the observed data set; in other words, the observed data is assumed to be sampled from a larger population.
Apply trimmed means to reduce outlier effects in Monte Carlo inference, compute the mean squared error and its standard error from simulated samples.
Apply Monte Carlo inference with median-based summaries for non-normal data, compute standard errors and confidence intervals, and illustrate an upper confidence limit for variance from normal samples.
Extend Monte Carlo simulations to estimate upper confidence limits for sigma squared and assess empirical coverage under normal and chi-squared populations, including bootstrapping and jackknife ideas.
Apply Monte Carlo simulation to estimate the population median theta of an exponential distribution using 1000 samples of size 21 and compare the median's distribution to the exact density.
Use Monte Carlo simulation to compare two estimators for the total number of taxis: the maximum of observed cabs and twice the sample mean, under a uniform population.
Compare two estimators for the taxi count, max of five observations versus twice the mean, using Monte Carlo simulations to assess bias, standard error, and absolute error via box plots.
Use a Monte Carlo simulation to estimate expected total travel time over a five-day week, probability of at least one late class, and longest travel time with its standard error.
Use Monte Carlo simulation to estimate a student’s travel time and lateness probability, with normal times mean 20 and standard deviation 4 over five days, and find longest travel time.
A stochastic simulation is a simulation that traces the evolution of variables that can change stochastically (randomly) with certain probabilities.
In probability and statistics, a probability distribution assigns a probability to each measurable subset of the possible outcomes of a random experiment, survey, or procedure of statistical inference. Examples are found in experiments whose sample space is non-numerical, where the distribution would be a categorical distribution; experiments whose sample space is encoded by discrete random variables, where the distribution can be specified by a probability mass function; and experiments with sample spaces encoded by continuous random variables, where the distribution can be specified by a probability density function. More complex experiments, such as those involving stochastic processes defined in continuous time, may demand the use of more general probability measures.
Map from the uniform distribution to non-uniform distributions using f(x), with histograms, scatter plots, and autocorrelation to validate unbiased Monte Carlo sampling.
We demonstrate that applying the inverse transform to a uniform sample generates exponential random variables, with x equal to the negative logarithm of u. Using 10,000 simulations, we compare histograms of the uniform-derived and directly generated exponential samples to show similar shapes.
Simulate discrete random variables by using a uniform input to generate binomial outcomes, and derive the cumulative distribution function and probability mass function to model discrete data in R.
simulate a binomial variable with a homemade generator and recursive function, using 100 trials at 50% success, and compare the Monte Carlo histogram to the binomial function for confidence intervals.
Plot the actual and simulated binomial distributions from 10-event trials with 70 percent probability, then validate with a 95% confidence interval showing close agreement with real values.
The idea of the method is as follows: one starts with an initial guess which is reasonably close to the true root, then the function is approximated by its tangent line (which can be computed using the tools ofcalculus), and one computes the x-intercept of this tangent line (which is easily done with elementary algebra). This x-intercept will typically be a better approximation to the function's root than the original guess, and the method can be iterated.
Apply the Newton-Raphson method to find roots by iterating with the first derivative, starting from an initial guess and a tolerance, guarding against divergence with a maximum iterations cap.
Generate random variables using a binomial-based approach in R, plot a histogram, compare it to the binomial mass function, and validate the transform against the actual variable.
Learn to simulate a binomial random variable in R with n=25 and p=0.2, generate 5000 samples, plot a histogram, and compare with the theoretical probability mass function.
Explore how to simulate random variables with uniform sampling, compare density and cumulative distribution estimates to theoretical values, and evaluate performance between loop-based and direct transformation methods in R.
Inverse transform sampling (also known as inversion sampling, the inverse probability integral transform, the inverse transformation method, Smirnov transform, golden rule,) is a basic method for pseudo-random number sampling, i.e. for generating sample numbers at random from any probability distribution given its cumulative distribution function (cdf).
The basic idea is to uniformly sample a number between 0 and 1, interpreted as a probability, and then return the largest number from the domain of the distribution such that . For example, imagine that is the standardnormal distribution (i.e. with mean 0, standard deviation 1). Then if we choose , we would return 0, because 50% of the probability of a normal distribution occurs in the region where . Similarly, if we choose , we would return 1.95996...; if we choose , we would return 2.5758...; if we choose , we would return 4.7534243...; if we choose , we would return 4.891638...; if we choose , we would return 8.1258906647...; if we choose , we would return 8.2095361516... etc. Essentially, we are randomly choosing a proportion of the area under the curve and returning the number in the domain such that exactly this proportion of the area occurs to the left of that number. Intuitively, we are unlikely to choose a number in the tails because there is very little area in them: We'd have to pick a number very close to 0 or 1.
Learn to simulate exponential and chi squared distributions from a uniform distribution using inverse transforms, compare methods, and assess performance.
Investigate transforming known functions to simulate samples, note the difficulty of obtaining the standard normal from uniform input, and apply the inverse method to discrete distributions.
Simulate Poisson distributions with large lambda using a 3*sqrt(lambda) window and 10,000 trials. Compare a uniform-based simulation to the native R function.
In mathematics, rejection sampling is a basic technique used to generate observations from a distribution. It is also commonly called the acceptance-rejection method or "accept-reject algorithm" and is a type of Monte Carlo method. The method works for any distribution in with a density.
Rejection sampling is based on the observation that to sample a random variable one can sample uniformly from the region under the graph of its density function.
Explore the accept-reject method for continuous distributions by simulating from a uniform rectangle and accepting points under the target density, using the beta distribution with shape parameters a and b.
Apply the acceptance–rejection method to simulate the area under a triangle by partitioning the interval into ranges with f(x)=x on [0,1] and f(x)=2−x on [1,2], and plot a histogram.
Explore the Poisson distribution as a discrete random variable, its lambda-driven probability and cumulative density functions, and how to simulate Poisson outcomes from uniform inputs.
Explore simulating a Poisson distribution by transforming uniform and exponential random variables, compare performance between uniform-based, exponential-based, and vectorized native functions, and assess efficiency over thousands of trials.
In numerical analysis, numerical integration constitutes a broad family of algorithms for calculating the numerical value of a definite integral, and by extension, the term is also sometimes used to describe the numerical solution of differential equations.
Use the trapezoidal rule to estimate the area under f(x)=4x^3 on [0,1]. Create a function accepting f, a, b, and n to compute width and sum trapezoids.
Use Simpson's rule to approximate the area under f(x) from a to b with parabolic segments, exact for cubic functions and more accurate than trapezoidal for polynomials.
Apply simulation techniques to estimate the area under a curve between a and b using trapezoidal and Simpson's rules. Explore positive one-dimensional cases and real case studies like disease spread.
Learn to estimate definite integrals in R by using the trapezoidal rule with subintervals, computing x and f(x) values, and summing the areas, then apply Simpson's rule for quadratic accuracy.
Explore Simpson's rule with parabolic interpolation to approximate the area under f from a to b, using midpoint subintervals on even and odd segments and h/3 scaling.
Compute the area under the normal curve using little phi and big Phi, applying Simpson's rule to estimate the cumulative distribution across z values.
Derive z-values from tail areas of the phi function by reversing the computation of the pdf and cumulative area, using provided functions to determine significance and confidence levels.
demonstrates hit-and-miss monte carlo integration to estimate the area under a curve, using uniform sampling over a bounding rectangle and extending to two dimensions with negative values.
Generate uniform x and y samples and use a hit-or-miss test against f to estimate the integral. The approach works but needs many simulations, showing limited efficiency.
Explore in R how to invert the normal cumulative distribution function using phi functions and Newton-Raphson, converting area under the curve into z scores and standard deviations.
In statistics, resampling is any of a variety of methods for doing one of the following:
Common resampling techniques include bootstrapping, jackknifing and permutation tests.
Explore permutation tests and notched box plots to detect median differences, noting when medians fall outside notches and small-sample limits; verify with a six-way anova on weight, and residual checks.
Perform a permutation test to compare two unbalanced groups by pooling observations, generating random splits, and evaluating mean differences to assess significance beyond a t-test.
Learn how to perform permutation tests to compare distributions in R, using the Chckwts example to assess mean differences with KS and exact tests, plus simulation-based p-values.
Develop skills in permutation tests, K-S test distributional summaries, compare groups, and implement an exercise to analyze correlations with normal and lognormal data in R.
Explore implementing an exact and permutation-based Spearman rank correlation test in R, comparing exact p-values to simulation-based p-values across normal and lognormal data.
Build two object oriented classes in R and run seed dispersal simulations to demonstrate polymorphism, generic functions, and mean or variance methods across S3 and S4.
Explore seed dispersal analysis by building a trap transect class in r, with a constructor function and methods to manage distances, trap area, and seed counts in a transect.
Create a trap transect class in R, implement a constructor, and define print and mean methods to summarize weighted seed dispersal along a transect.
Simulate the radial distribution of seed rain around a plant from transect data using rejection sampling and gamma or exponential decay models.
Compare gamma density (rate 1/2) with an exponential density for seed dispersal around a plant, then use rejection sampling with exponential draws and a uniform threshold to estimate radial density.
Compare lognormal and weibull transect density models for seed dispersal, and validate simulated radial dispersal with acceptance–rejection sampling against exact radial PDFs; build functions to simulate transects and radial dispersal.
Create a transect holder object to model the seed shadow around a plant, fit transect distances to a nominated distribution (exponential, lognormal, gamma, Weibull) using the SDR function in Mass.
Explore building and testing a generic simulate function for transect holder objects and perform Monte Carlo seed dispersal simulations using Weibull distributions, q-q plots, and acceptance rejection sampling.
R Programming for Simulation and Monte Carlo Methods focuses on using R software to program probabilistic simulations, often called Monte Carlo Simulations. Typical simplified "real-world" examples include simulating the probabilities of a baseball player having a 'streak' of twenty sequential season games with 'hits-at-bat' or estimating the likely total number of taxicabs in a strange city when one observes a certain sequence of numbered cabs pass a particular street corner over a 60 minute period. In addition to detailing half a dozen (sometimes amusing) 'real-world' extended example applications, the course also explains in detail how to use existing R functions, and how to write your own R functions, to perform simulated inference estimates, including likelihoods and confidence intervals, and other cases of stochastic simulation. Techniques to use R to generate different characteristics of various families of random variables are explained in detail. The course teaches skills to implement various approaches to simulate continuous and discrete random variable probability distribution functions, parameter estimation, Monte-Carlo Integration, and variance reduction techniques. The course partially utilizes the Comprehensive R Archive Network (CRAN) spuRs package to demonstrate how to structure and write programs to accomplish mathematical and probabilistic simulations using R statistical software.