Question

In: Computer Science

Simulating a conditional probability using R. Simulating the conditional probability P(A|B) requires repeated simulation of the...

Simulating a conditional probability using R.
Simulating the conditional probability P(A|B) requires repeated simulation of
the underlying random experiment, but restricting to trials in which B occurs.

Suppose 3 dice are tossed and we want to find the probability of the first die is 4 given that the sum is 10.
Modify the script ConditionalDice.R (you can find the R script file from Modules on this Canvas site) to find

P(First die is 4 | Sum is 10)

Hint: the exact answer is: 5/27.

### R: Simulating a conditional probability

#Simulating the conditional probability P(A|B) requires repeated simulation of
#the underlying random experiment, but restricting to trials in which B occurs.

### ConditionalDice.R
### Suppoese we roll 2 dice and want to find
### P(First die is 2 | Sum is 7)
### We know that answer is: 1/6.

### In this case, we repeatly toss two dice, but the only data that we keep
### are those pairs whose sum is 7.


n <- 60000
ctr <- 0

# ctr is the counter for the rolls when the sum is 7

simlist <- replicate(n, 0) ## Initialize list with 0's
while (ctr < n)
{
   trial <- sample(1:6, 2, replace=TRUE) ## Roll 2 dice
   if (sum(trial) == 7) ### Check if sum is 7
   ### If not, skip through and roll again
   ### If 7, check if first die is a 2
   {
   success <- if (trial[1] == 2) 1 else 0
   ctr <- ctr + 1
   simlist[ctr] <- success
   ### simlist records successes and failures only for
   ### dice rolls that sum to 7
   }
}

### Simulated result
simlist

mean(simlist)

Solutions

Expert Solution

n <- 60000
ctr <- 0

# ctr is the counter for the rolls when the sum is 7

simlist <- replicate(n, 0) ## Initialize list with 0's
while (ctr < n)
{
trial <- sample(1:6, 3, replace=TRUE) ## Roll 3 dice
if (sum(trial) == 10) ### Check if sum is 10
### If not, skip through and roll again
### If 10, check if first die is a 2
{
success <- if (trial[1] == 4) 1 else 0
ctr <- ctr + 1
simlist[ctr] <- success
### simlist records successes and failures only for
### dice rolls that sum to 10
}
}
### Simulated result
simlist

mean(simlist)


Related Solutions

Using the axioms of probability, prove: a. P(A U B) = P(A) + P(B) − P(A...
Using the axioms of probability, prove: a. P(A U B) = P(A) + P(B) − P(A ∩ B). b. P(A) = ∑ P(A | Bi) P(Bi) for any partition B1, B2, …, Bn.
What is the difference between independent and conditional probability? Which one requires the use of the...
What is the difference between independent and conditional probability? Which one requires the use of the Addition Rule? Explain.
What is the difference between independent and conditional probability? Which one requires the use of the...
What is the difference between independent and conditional probability? Which one requires the use of the Addition Rule? Explain.
Prove using only the axioms of probability that if A and B are events, then P(A...
Prove using only the axioms of probability that if A and B are events, then P(A ∪ B) ≤ P(A) + P(B)
The probability that A wins B is p. And the probability that B wins A is...
The probability that A wins B is p. And the probability that B wins A is q. If you win 2 more times than opponent, the game is over. What is the the probability that A two more wins and the game end?
preform a Monte Carlo simulation in R to generate the probability distribution of the sum of...
preform a Monte Carlo simulation in R to generate the probability distribution of the sum of two die (for example 1st die is 2 and second die is 3 the random variable is 2+3=5). The R-script should print out (display in R-studio) or have saved files for the following well labeled results: 1. Histrogram or barchart of probability distribution 2. Mean of probability distribution 3. Standard deviation of probability distribution
Please Provide R code as well Use R to find probability (p-value). Find probability P(X>12.3), where...
Please Provide R code as well Use R to find probability (p-value). Find probability P(X>12.3), where X follows F-distribution with degree of freedom in numerator 4 and degree of freedom in numerator 10.
Using the standard normal distribution, find each probability. a) P(0 < z < 2.23) b) P...
Using the standard normal distribution, find each probability. a) P(0 < z < 2.23) b) P (-1.75 < z < 0) c) P (-1.48 < z < 1.68) d) P (1.22 < z < 1.77) e) P (-2.31 < z < 0.32)
(Use R Programming to Code) Use the Monte Carol simulation to estimate the probability that all...
(Use R Programming to Code) Use the Monte Carol simulation to estimate the probability that all six faces appear exactly once in six tosses of fair dice.
Running a Monte Carlo simulation to calculate the probability that the daily return from S&P will...
Running a Monte Carlo simulation to calculate the probability that the daily return from S&P will be > 5%. We will assume that the historical S&P daily return follows a normal distribution with an average daily return of 0.03 (%) and a standard deviation of 0.97 (%). To begin we will generate 100 random samples from the normal distribution. For the generated samples we will calculate the mean, standard deviation, and probability of occurrence where the simulation result is greater...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT