Starting from:
$35

$29

Assignment 01 Binomial Distribution Solution

    • 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.

    • Press ctrl+ S to save the file and rename it.

Flipping a coin in R




    • 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).

    • Click on the button between Run and Source few times to re-run rbinom(1,1,.5) few times.

    • Now using rbinom(10,1,.5), flip a coin 10 times.

    • Re-run the code to see different outcomes.

    • rbinom(1,10, 0.5) will return number of heads in 10 tosses.

    • rbinom(10,10, 0.5) will toss 10 coins each 10 times and return number of heads out of 10 tosses for each coin.

    • Re-run rbinom(10,10, 0.5).

    • rbinom(10,10, 0.8) will return number of heads in 10 tosses of 10 unfair coins.

    • Each toss is a head with probability 0.8.













What is the most common number? Why?
    • rbinom(10,10, 0.2) will return number of heads in 10 tosses of 10 unfair coins.

    • Each toss is a head with probability 0.2.













What is the most common number? Why?
More generally,




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.
Exercise 1




    • Generate 100 experiments of flipping 10 coins, each with 30% probability.

    • What is the most common number? Why?
    • Binomial Distribution has two parameters:
        ◦ ~                 (        ,   )



        ◦ Size= number of coin flips

        ◦ p= the probability of seeing one head in a coin flip

        ◦ Random variable  denotes number of heads.
    • We flip a fair coins 10 times. What is the probability of seeing 5 heads?
    • ~                 10, . 5

    • Pr  = 5 ?

    • We flip a fair coins 10 times. What is the probability of seeing 5 heads?




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.
The result is 0.24769.

    • 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

If    ~                 10, . 5 , then
dbinom(k,10,.5) returns Pr    =    =    (  )

Exercise 2




    • 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.
Exercise 3



    • 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
Exercise 4




    • 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
Exercise 5




    • 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.

More products