In your simulation you want to tally up the number of successes (heads) for a series of 5 independent Bernoulli trials from 3 realization (or draws, or observations).
In R this can be done using the rbinom(n, size, prob)
function, where n
is the number of observations, size
is the number of trials and prob
the probability of success in each trial. See also here for the function reference (I am sure this is similar in other programming languages as well).
The tallied number of successes for 5 trials can then be calculated as (assuming a fair coin):
> set.seed(123)
> rbinom(3, 5, .5)
[1] 2 3 2
In this case, we have 2 successes (first observation), 3 successes (second observation), 2 successes (third observation).
So Group 1, 2, 3 are observations with 5 trials in each observation.
Edit: added illustration
Here's an illustration of a Galton Board that will make it much clearer what the differences between trials and observations are:
Each ball's path represents one observation (red arrows) passing through 6 left/right decisions or six trials (blue triangles) eventually ending up in one of the bins. The exact probability ($Pr$) of landing in one of the bins can be calculated using the binomial probability mass function $$Pr(X = k) = {n \choose k}p^k(1 - p)^{n-k},$$ where $X$ represents a random variable, $k$ the number of successes (e.g. bounce left), $n$ the number trials, and $p$ the probability of the outcome of each trial.
In R you can use the dbinom()
function to calculate the exact probabilities of falling in each of the bins (from left to right and assuming $p=0.5$):
dbinom(6, 6, .5)
#[1] 0.015625
dbinom(5, 6, .5)
#[1] 0.09375
dbinom(4, 6, .5)
#[1] 0.234375
dbinom(3, 6, .5)
#[1] 0.3125
dbinom(2, 6, .5)
#[1] 0.234375
dbinom(1, 6, .5)
#[1] 0.09375
dbinom(0, 6, .5)
#[1] 0.015625
Or through simulation (in this case 100,000 observations and 6 trials):
bounces <- rbinom(100000, 6, .5)
mean(bounces == 6)
#[1] 0.01554
mean(bounces == 5)
#[1] 0.09325
mean(bounces == 4)
#[1] 0.23468
mean(bounces == 3)
#[1] 0.31349
mean(bounces == 2)
#[1] 0.23486
mean(bounces == 1)
#[1] 0.09287
mean(bounces == 0)
#[1] 0.01531