Description
-
Open Rstudio. Click on the plus button under File on the right upper side. Then click on “R Script” to create a new R script.
-
Flipping a fair coin once, P(Head)=P(Tail)= 0.5
-
Function: rbinom(1,1,.5)
-
We expect the result to be 1 or 0.
-
0 represents tail and 1 represents Head.
-
In R type rbinom(1,1,.5) and click on Run.
As you can see the result is tail (0).
-
rbinom(10,10, 0.5) will toss 10 coins each 10 times and return number of heads out of 10 tosses for each coin.
-
Each toss is a head with probability 0.8.
What is the most common number? Why?
-
Each toss is a head with probability 0.2.
What is the most common number? Why?
The first argument of rbinom() is number of experiments.
The second argument is the number of coins flips.
The third argument is the probability of a 1 (“heads”).
For example, rbinom(100000,10,.5) is flipping 10 fair coins, and repeating the experiment 100,000 times.
-
Generate 100 experiments of flipping 10 coins, each with 30% probability.
-
What is the most common number? Why?
-
-
Size= number of coin flips
-
-
-
p= the probability of seeing one head in a coin flip
-
-
-
Random variable denotes number of heads.
-
-
~ 10, . 5
-
Pr = 5 ?
Simulation:
-
Repeat this experiment 100,000 times: “number of draws=100,000”
-
flips <- rbinom(100000,10,.5)
-
flips contains 100000 numbers, each between 0 and 10 (number of heads).
-
mean(flips == 5), returns percentage of number “5” among 100000 numbers.
-
dbinom(5,10,.5) returns probability of seeing 5 heads out of 10 tosses, for a fair coin using exact calculation.
-
Note that if you re-run it, you will get the same result.
-
As you can see, the result of exact calculation is 0.2460938 which is very close to the result of our simulation 0.24769
dbinom(k,10,.5) returns Pr = = ( )
-
If you flip 10 coins each with a 30% probability of coming up heads, what is the probability exactly 2 of them are heads?
-
Compare your simulation with the exact calculation.
-
For exercise 2,
-
Part a) use 10000 experiments and report the result.
-
Part b) use 100000000 experiments and report the result.
-
Compare the result of part a and part b, with the exact calculation. What is your conclusion?
If ~ 10, . 5 , what is the E[ ]? using calculation E = 5.
-
Simulation: run the experiment 100,000 times.
-
flips <- rbinom (100000, 10, .5 )
-
mean (flips): the average number of heads
Result of simulation is close to 5
If ~ 100, . 2 , what is the E[ ]? using calculation E = 20.
-
Simulation: run the experiment 100,000 times.
-
flips <- rbinom (100000, 100, .2 )
-
mean (flips): the average number of heads
Result of simulation is close to 20
-
What is the expected value of a binomial distribution where 25 coins are flipped, each having a 30% chance of heads?
-
Compare your simulation with the exact calculation.
If ~ 10, . 5 , what is the Var[ ]? using calculation Var =2.5.
-
Simulation: run the experiment 100,000 times.
-
X <- rbinom (100000, 10, .5 )
-
var(X): the variance
Result of simulation is close to 2.5
If ~ 100, . 2 , what is the Var[ ]? using calculation Var = 16.
-
Simulation: run the experiment 100,000 times.
-
X <- rbinom (100000, 100, .2 )
-
var(X): the variance
Result of simulation is close to 16
-
What is the variance of a binomial distribution where 25 coins are flipped, each having a 30% chance of heads?
-
Compare your simulation with the exact calculation.