R Progamming
Consider a hierarchical model with three layers: You’ll start with 100 fair coins. In round 1, you’ll flip them and keep only the heads (goodbye coins with tails!). In round 2, you’ll flip the remaining coins and keep only the heads. Finally, in round 3, you’ll flip the remaining coins and report back the number of heads Y . Write a simulation that estimates E(Y), expected value of Y. (Your answer should be intuitive...)
###. Start with 100 fair coins
>
> Y = c(); Expe_Val_Y = c()
>
> ### we hav to tossed coin in three round hence making 3 sample
of 100
>
> round = c(50, 30, 20)
>
> ### round 1 2 3 in for loop
>
> for(i in 1:3){
+
+ Simulation = rbinom(round[i], 1, 0.5)
+
+ ### Making it as head or tail
+
+ Simultaion_H_T = ifelse(Simulation == 1, "H", "T")
+
+ ### Removing Tails
+ Simultaion_H = Simultaion_H_T[-which(Simultaion_H_T == "T")]
#goodbye coins with tails
+
+ ### No of heads
+
+ Y[i] = length(Simultaion_H)
+
+ Expe_Val_Y[i] = Y[i]/round[i]
+ }
>
>
> # No of head in each round
>
> Y
[1] 25 19 10
>
> # Expcted value of Y
>
> Expe_Val_Y
[1] 0.5000000 0.6333333 0.5000000
>
Get Answers For Free
Most questions answered within 1 hours.