2 HarvardX: PH125.3x Data Science: Probability
2.1 Section 1: Discrete Probability
2.1.1 Monte Carlo szimuláció
Key points -Monte Carlo simulations model the probability of different outcomes by repeating a random process a large enough number of times that the results are similar to what would be observed if the process were repeated forever.
-The sample function draws random outcomes from a set of options.
-The replicate function repeats lines of code a set number of times. It is used with sample and similar functions to run Monte Carlo simulations.
Code: The rep function and the sample function
beads <- rep(c("red", "blue"), times = c(2,3)) # create an urn with 2 red, 3 blue
beads # view beads object
## [1] "red" "red" "blue" "blue" "blue"
sample(beads, 1) # sample 1 bead at random
## [1] "blue"
replicate() funkcióval eltudjuk készíteni a Monte Carlo szimulációnkat, amivel a valós eredménykhez közeli érétket kaphatunk. Tehát 10000-szer futtatva 5 db golyó közül 3 kék és 2 piros fog kijönni százalékos szinten is.
B <- 10000 # number of times to draw 1 bead
events <- replicate(B, sample(beads, 1)) # draw 1 bead, B times
tab <- table(events) # make a table of outcome counts
tab # view count table
## events
## blue red
## 6019 3981
prop.table(tab) # view table of outcome proportions
## events
## blue red
## 0.6019 0.3981