This project focuses on created reuseable functions in R that can be part of a larger program. It calculates conditional probability using stochastic simulations to predict the probability of a first dice roll given the sum of two dice rolls.
This project will cover:
- Simualtion creation using loops
- Creation of reauseable function
- Test of the function (edge cases, error messages and extreme values)
Languages used: R (version 4.5.1)
Environement: RStudio
Packages: testthat
Setup of simulation
Setting up the number of successful trials (n) our loop stops at.
Created counter to store successful trials.
Utilised simlist to count the number of successes of the probabilty of interest.
Simulation loop
While loop that runs as long as our counter is < 10,000
If statement- if sum is 7 store the success ( 2 given sum is 7 in "success" as 1 and 0 otherwise.
iterate up our counter.
Then assign successes to simlist based on the index of counter.
Simulate probability with mean() of simlist.
In order to create the function conditional_sum_prob, I carried out the following tasks:
- Initiated the function creation process with the
function()function to encapsulate my code block from the simulation. - Optimised counter selection using a similar process to my Law of Large Numbers simulation to investigate the convergence to the analytical solution with different sample sizes.
This was explored with the results on the following graph:

- Visualised each of the three trail functions to pick best simulated proababilty comapred to known analytical solution.
- Final beta function defined.
For the final tests of the functions I carried out the following tests:
-
Edge cases- using the
testthatpackage I made sure the known porobabilities such as 2 given a sum of 7 are within a tolernace of 0.01. -
Extreme values- function would continue to run when values where outside of reasonable bounds.
-
If statement revision- limited values that could be entered ustilisng if statements.
-
Error messages with if statements- changed the output of format messages utilising
$to call only specific error messages.
The function I created conditional_sum_prob() calculates the probablity of your first toss being a specific value given the sum of two dice rolls is a specific value.
It has two main arguments:
first_toss- what is the value of the first toss.
sum_of_tosses- what is the sum of two dices tossed.
-
Probability of the first toss being 2 given the sum of the two is 7 would be:
conditional_sum_prob(2,7)
returns [0.167] ≈ 1/6 -
Probability of first toss being 7 given sum is 12 would be:
conditional_sum_prob(7,12)
returns ["Please enter a value from 1-6 for the first toss"] -
Probability of first toss being 3 given sum is 14 would be:
conditional_sum_prob(3,14)
returns ["Please enter a value from 2-12 for the sum of tosses"] -
Probability of first toss being 7 given sum is 14 would be:
conditonal_prob_Sum(7,14)
returns ["Please enter a value from 1-6 for the first toss"] ["Please enter a value from 2-12 for the sum of tosses"] -
Probability of first toss being 1 given sum is 8 would be:
conditonal_sum_prob(1,8)
returns [0]
|Simulation- Conditional Probability
|├── Conditional probability
|├── Beforetest_conditional_function
|├── README.md
|├── Tests_conditional_prob
|├── conditional_sum_analytical_tex
|└──conditonal_sum_pdf_analytical
Based on an example from "Probability with Applications and R" by Dr. Amy S. Wagaman and Dr. Robert P. Dobrow, Chapter 2
- Introduce a function called conditional_sum_prob() or the like to calculate the conditional probability of entered arguments and for reproducibility ✅
- Add tests such as edge case testing, reproducibility, accuracy vs theoretical probability and distribution tests ✅
- Examples of function use cases through the README ✅