---
title: "Warm-up worksheet #3: Solutions"
author: "Milica Cudina"
date: "`r Sys.Date()`"
output: pdf_document
---
<!-- The author of this template is Dr. Gordan Zitkovic.-->
<!-- The code chunk below contains some settings that will  -->
<!-- make your R code look better in the output pdf file.  -->
<!-- If you are curious about what each option below does, -->
<!-- go to https://yihui.org/knitr/options/ -->
<!-- If not, feel free to disregard everything ...  -->
```{r echo=FALSE, warning=FALSE, include=FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  fig.align="center",
  fig.pos="t",
  fig.width=12,
  strip.white = TRUE
)
```
<!-- ... down to here. -->

# Simulations
## Problem 1.  
Draw 100 simulated values from $Bernoulli(p=0.2)$. What is the proportion of "successes" in your simulated batch?

*Solution:*
```{r}
x=rbinom(100,1,0.2)
sum(x)/100
```

## Problem 2.  
Draw 1000 simulated values from $Binomial(n=100, p=0.2)$. Plot the histogram of your simulated values.

*Solution:*
```{r}
x=rbinom(1000,100,0.2)
hist(x, breaks=seq(-0.5,100.5,1),
     main="Histogram of my simulated values from Binomial(100,0.2)", 
     xlab="My simulated values", 
     ylab="Counts",
     col="pink")
```

## Problem 3.  
Consider the following two-step experiment. First you draw a simulated value from a Bernoulli(p=1/3). If the drawn value from the Bernoulli equals $0$, then you draw a simulated value from Binomial(n=50,p=0.5). On the other hand, if the drawn value from the Bernoulli equals $1$, then you draw a simulated value from Binomial(100,0.5). 

You should repeat the above two-step experiment $1000$ times and draw the histogram of the simulated values. 

*Solution:*
```{r}
nsim=1000
sims<-c()
for(i in 1:nsim){
  coin<-rbinom(n=1,size=1,prob=1/3)
  if(coin==0){
    new.sim<-rbinom(1,50,0.5)
  } else {
    new.sim<-rbinom(1,100,0.5)
  }
  sims<-c(sims,new.sim)
}
hist(sims,breaks=seq(-0.5,100,1),
     main="Histogram of my simulated values", 
     xlab="My simulated values", 
     ylab="Counts",
     col="purple")
```

