作业:使用R模拟抛硬币直到连续的掷硬币 [英] Homework: Simulating coin tosses until consecutive heads using R
问题描述
我是R的新手,因此在这里提出了疑问(未能找到非常有用的模拟详细教程.)
I am new to R hence asking here (haven't been able to find very helpful tutorials for simulation that are detailed.)
问题陈述是这样
- 模拟抛硬币20次并记录正面数 &最长的元首.
- 模拟抛硬币并记录所需的翻动次数,直到依次(连续)出现2、3、4个头(负二项式?)
- 用不同的种子运行100次以查找记录的项目的分布.
- 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.
如何用编程语言R解决这个问题?
How does one go about solving this in the programming language R ?
对于1,我做了以下事情:
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.
对于2,我理解这是负二项式的情况,因为它说直到依次出现2、3、4个头".但是我不确定如何继续用R编写代码或提出逻辑.
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.
对于3,我猜想必须计算100次运行中的磁头数量,即磁头的游程长度.但是,设置不同种子意味着什么?我不确定.
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.
编辑为了模拟2,我在MATLAB上尝试了以下代码-
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)
请让我知道此逻辑是否正确.我可以使用R重新创建它.
Please let me know if this logic is correct. And I can recreate it using R.
推荐答案
由于这是家庭作业,所以我想给您一些有关#2和#3的指针.
As this is homework, I would like to give you some pointers for #2 and #3.
对于#2,进行一个循环,继续进行抛硬币并计算连续的头数.在每次掷球时,将掷球数增加1,而在达到要求的杆头数时,只需返回掷球数即可.
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.
为了让您入门,这将使nbTosses tossesL
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);
}
您只需要对其进行修改,以接受参数targetHeads并仅在到达tagetHeads时退出while循环.
You just need to amend it to take in parameter targetHeads and exit the while loop only when you have reached the tagetHeads.
对于#3,将#2放入函数中,然后使用set.seed(integer)
设置种子.计算机上的随机数并不是真正的随机数.大多数随机生成器都允许您设置种子.如果使用相同的种子,则将获得两次相同的伪随机数序列,这可以进行调试.
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.
我将#2从递归更改为普通循环,为他编写代码会更容易.
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);
这将打印您到达nbTosses时的行头数.这不是您想要的.您想要的是:
This will print how many heads in row you had when it reached nbTosses. This is not what you want. What you want is:
- 将参数从
targetTosses
更改为targetHeadsInRow
- 更改
while
中的条件,以使其在nbHeadsInRow
达到targetHeadsInRow
时停止. - 您需要退货的是
nbTosses
(达到您的状况所需要的抛掷次数)
- 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.
这篇关于作业:使用R模拟抛硬币直到连续的掷硬币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!