问题
I am new to R hence asking here (haven't been able to find very helpful tutorials for simulation that are detailed.)
The problem statement is this
- Simulate a coin toss for 20 times and record the number of heads & longest run of heads.
- Simulate a coin toss and record the number of flips necessary until 2,3,4 heads occur in sequence (consecutively) (negative binomial?)
- Make 100 runs with different seeds to find the distribution of items recorded.
How does one go about solving this in the programming language R ?
For 1, I did the following:
n=20 #no of trials
y=NULL #initializing a vector of NULL values
for (i in 1:n) {
x=runif(1) #random uniform
if (x<0.5) { #if condition for assigning heads / tails
y[i]='H'
} else {
y[i]='T'
}
}
y #print the vector storing the heads and tails.
For 2, I understand that this is a case of negative binomial because it says "until 2,3,4 heads occur in sequence" . But I am not sure how to proceed to write the code in R or come up with the logic.
For 3, I am guessing have to calculate the number of heads got in 100 runs, run lengths of heads. But what does it imply to set different seeds? I am unsure of that.
note This is a homework problem, yes (I am not able to tag it so). I do not need the code, just some pointers/ corrections/clarifications/suggestions would help.
edit For simulating 2, I tried out the following code on MATLAB -
head_count = 0;
flip_count = 0;
while (head_count < 3) #counting for 3 multiple heads
flip = rand(1);
flip_count = flip_count+1;
if (flip > 0.5)
head_count = head_count + 1;
else
head_count = 0; #invalidate head counts if not successive.
end
end
disp(flip_count)
Please let me know if this logic is correct. And I can recreate it using R.
回答1:
As this is homework, I would like to give you some pointers for #2 and #3.
For #2, make a loop which keeps doing coin tosses and count the number of heads in a row. At every toss increase the count of tosses by 1 and when reaching the number of heads requested, just return the count of tosses.
To get you started, this will do nbTosses tossesL
coin <- c('h','t')
ComputeNbTosses <- function(targetTosses) {
nbtosses = 0;
while (nbtosses < targetTosses) {
nbtosses = nbtosses + 1
}
return(nbtosses);
}
You just need to amend it to take in parameter targetHeads and exit the while loop only when you have reached the tagetHeads.
For #3, put #2 in a function and then use set.seed(integer)
, to set the seed. Random numbers on a computer are not really random. Most of random generators allow you to set a seed. If you use the same seed you will get twice the same sequence of pseudo random numbers, this allows for debugging.
EDIT: I changed #2 from a recursion to a normal loop, it will be easier to code for him.
EDIT (Tip): This will count how many heads in a row you have, now you have to make the loop stop when you reach the target value of heads:
coin <- c('h','t')
ComputeNbTosses <- function(targetTosses) {
nbTosses = 0;
nbHeadsInRow = 0;
allTosses = c();
while (nbTosses < targetTosses) {
toss = sample(coin,1,T)
allTosses = c(allTosses, toss);
if (toss == 'h') {
nbHeadsInRow = nbHeadsInRow + 1;
} else {
nbHeadsInRow = 0;
}
nbTosses = nbTosses + 1
}
ret = list();
ret$nbTosses = nbTosses;
ret$nbHeadsInRowAtEnd = nbHeadsInRow;
ret$allTosses = allTosses;
return(ret);
}
ComputeNbTosses(5);
ComputeNbTosses(8);
This will print how many heads in row you had when it reached nbTosses. This is not what you want. What you want is:
- change the argument from
targetTosses
totargetHeadsInRow
- change the condition in the
while
so that it stops whennbHeadsInRow
reachestargetHeadsInRow
. - what you need to return is
nbTosses
(the number of tosses to reach your condition)
Let me know if anything is not clear.
来源:https://stackoverflow.com/questions/21392665/homework-simulating-coin-tosses-until-consecutive-heads-using-r