Assessed_Worksheet_3
.docx
keyboard_arrow_up
School
University of Manchester *
*We aren’t endorsed by this school
Course
CHEM20312
Subject
Mathematics
Date
Apr 27, 2024
Type
docx
Pages
10
Uploaded by ProfStork4172 on coursehero.com
Assessed Worksheet 3
Mariam Mustapha
2023-11-03
Assessed Worksheet 3
Element 010-3
Maths for Biosciences MOD005667
The Problem: Manufacturing Biased Coins
You work for a company that manufacturers biased coins to sell to as novelty gifts. The coins are weighted to favour either heads or tails when flipped. Your task is to determine how many coin flips it takes to reliably determine if a coin is fair or biased. You want to create coins with 50%, 55%, 60%, and 65% chance of getting heads. Note, the 50% coin is a fair coin.
Part 1: Building the coin flip simulator (10 marks)
Begin by writing an R function that simulates coin flips. The function should take as input the number of coin flips you want to simulate, and the probability of getting heads. It should return a 2 X 2 matrix containing the number of heads and the number of tails recorded. You can use chatGPT to help you write your function.
simulate_coin_flips <-
function
(num_flips, probability_heads) {
# Initialize counters for heads and tails
heads_count <-
0
tails_count <-
0
# Simulate coin flips
for
(i in
1
:
num_flips) {
# Generate a random number between 0 and 1
random_number <-
runif
(
1
)
# Determine if it's heads or tails based on the probability
if
(random_number <
probability_heads) {
heads_count <-
heads_count +
1
} else
{
tails_count <-
tails_count +
1
}
}
# Create a 2x2 matrix with results
result_matrix <-
matrix
(
c
(heads_count, tails_count, tails_count, heads_count), nrow =
2
, byrow =
TRUE
,
dimnames =
list
(
c
(
"Heads"
, "Tails"
), c
(
"Observed_Heads"
, "Observed_Tails"
)))
return
(result_matrix)
}
# Example usage:
set.seed
(
123
) # Setting seed for reproducibility
result <-
simulate_coin_flips
(
num_flips =
1000
, probability_heads = 0.5
)
print
(result)
## Observed_Heads Observed_Tails
## Heads 507 493
## Tails 493 507
Part 2: Experimental design (30 marks)
Design a series of experiments using your function that will allow you to determine the relationship between number of coin flips, and whether you can detect if each of your coins are biased. Each individual experiment will consist of a single call to the coin flip simulator you wrote in Part 1 combined with your statistical evaluation of the results of that simulation. The appropriate statistical test to determine if a series of coin flips are fair is the Chi-Squared test.
Think about what you will need to vary in each case to determine this relationship. Also, think about how many times you will need to repeat an experiment to understand how consistent the results are when you repeat the same experiment multiple times. I suggest you begin by experimenting with your coin flip simulator to get a feel for how it behaves while you design your series of experiments.
Describe your experimental approach as clearly and succinctly as possible (no more than 100 words). You will be marked on the clarity of your explanation.
To see if the coins are biassed, I would alter the number of flips (n) and the probability of heads (p) in each simulation and compare the observed and predicted frequencies of heads and tails using the Chi-Squared test. For example, I’d simulate 10, 50, 100, 500, and 1000 flips with p = 0.5, 0.6, 0.7, 0.8, and 0.9 and compute the p-
value for each simulation. A low p-value indicates bias, while a high p-value indicates
fairness. I would run each simulation ten times and compare the p-value average and
standard deviation.
Part 3: Data collection (25 marks)
Perform the experiments you have described in part 2 using R. Your R code should perform your experiments and store the results in a data frame. For each experiment
you should store in your data frame the experimental parameters (number of flips,
probability of heads), and the results of the Chi-squared for the simulation (Chi-
squared statistic, p value). Note that if you have written your coin flip simulator correctly, the output of the simulator will be in the correct matrix form to feed directly into the chisq.test() function in R.
Print your data frame to your final document. If your data frame contains more than 50 rows, print the first 50 rows.
# Function to perform the coin flip experiment and calculate Chi-
Squared test
perform_experiment <-
function
(num_flips, probability_heads) {
# Simulate coin flips
simulation_result <-
simulate_coin_flips
(num_flips, probability_heads)
# Perform Chi-Squared test
chi_squared_result <-
chisq.test
(simulation_result)
# Extract relevant information
chi_squared_statistic <-
chi_squared_result
$
statistic
p_value <-
chi_squared_result
$
p.value
# Create a data frame with experimental parameters and results
experiment_data <-
data.frame
(
Num_Flips =
num_flips,
Probability_Heads =
probability_heads,
Chi_Squared_Statistic =
chi_squared_statistic,
P_Value =
p_value
)
return
(experiment_data)
}
# Set up parameters for experiments
num_flips_values <-
c
(
100
, 500
, 1000
)
probability_heads_values <-
c
(
0.3
, 0.5
, 0.7
)
num_repetitions <-
10
# Initialize an empty data frame to store results
results_df <-
data.frame
()
# Perform experiments
for
(num_flips in
num_flips_values) {
for
(probability_heads in
probability_heads_values) {
for
(i in
1
:
num_repetitions) {
experiment_result <-
perform_experiment
(num_flips, probability_heads)
results_df <-
rbind
(results_df, experiment_result)
}
}
}
# Print the first 50 rows of the results data frame
print
(
head
(results_df, 50
))
## Num_Flips Probability_Heads Chi_Squared_Statistic P_Value
## X-squared 100 0.3 27.380 1.671511e-07
## X-squared1 100 0.3 24.500 7.430984e-07
## X-squared2 100 0.3 30.420 3.479225e-08
## X-squared3 100 0.3 16.820 4.109788e-05
## X-squared4 100 0.3 52.020 5.493820e-13
## X-squared5 100 0.3 30.420 3.479225e-08
## X-squared6 100 0.3 33.620 6.700028e-09
## X-squared7 100 0.3 40.500 1.966160e-10
## X-squared8 100 0.3 48.020 4.218937e-12
## X-squared9 100 0.3 16.820 4.109788e-05
## X-squared10 100 0.5 0.000 1.000000e+00
## X-squared11 100 0.5 0.020 8.875371e-01
## X-squared12 100 0.5 0.500 4.795001e-01
## X-squared13 100 0.5 0.020 8.875371e-01
## X-squared14 100 0.5 1.620 2.030918e-01
## X-squared15 100 0.5 0.500 4.795001e-01
## X-squared16 100 0.5 0.980 3.221988e-01
## X-squared17 100 0.5 0.020 8.875371e-01
## X-squared18 100 0.5 0.500 4.795001e-01
## X-squared19 100 0.5 0.020 8.875371e-01
## X-squared20 100 0.7 16.820 4.109788e-05
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help