Search code examples
rstatisticssimulationprobabilitycoin-flipping

Calculating observed values from a coin-toss simulation in R


I have a dataframe which was compiled by running a simulation on a two-coin toss 1000 times.

I.e. there is two-coins thrown in each test. The test is repeated 1000 times.

Heads are 1, tail are 2.

Here is a preview of the dataframe.

       X1   X2
1   X1  2   1
2   X2  1   1
3   X3  1   2
4   X4  1   1
5   X5  1   1
6   X6  1   2
7   X7  1   2
8   X8  1   2
9   X9  1   1
10  X10 2   1

It contains 1000 obs of 2 variables.

I want to calculated the observed values for the following conditions:

  1. The chance that both coins are heads

    sum(df.sim$X1 == 1 & df.sim$X2 == 1)/1000
    
  2. The chance that both coins will be different

    sum(df.sim$X1 == 2 & df.sim$X2 == 1)/1000
    
  3. The chance that at-least one coin will be heads.

    not sure...

How would I calculate the observed value for the condition number 3, and did I calculate the observed values correctly for the first two conditions.

I know that the values I should get for the conditions are as follows

  1. 25%

  2. 50%

  3. 75%


Solution

  • For #2, your approach doesn't consider when the first coin is heads and the second tails. But this approach would work:

    mean(df.sim$X1 != df.sim$X2)
    

    For #3, you could do the same thing as #1, but use | (OR) rather than & (AND).

    mean(df.sim$X1 == 1 | df.sim$X2 == 1)
    

    Note that using mean rather than sum allows you to skip the /1000 part.