2610 3

[程序分享] 投硬币最长正面反面统计量 [推广有奖]

  • 6关注
  • 4粉丝

已卖:129份资源

教授

4%

还不是VIP/贵宾

-

威望
0
论坛币
461 个
通用积分
17.6768
学术水平
20 点
热心指数
22 点
信用等级
15 点
经验
3264 点
帖子
524
精华
0
在线时间
1199 小时
注册时间
2009-12-26
最后登录
2025-5-23

楼主
凸集分离定理 发表于 2014-4-22 03:37:08 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. #林共进教授一次讲座提到他喜欢出一道题,题目是让学生交出抛100次硬币的书面记录
  2. #他的目的是检验那些学生没有认真抛硬币
  3. #他使用的统计量是最长连续出现正面反面的长度
  4. #这个统计量没有解析解,他说作为一道编程模拟习题布置给大家
  5. #今天我突然想起,就把他做出来
  6. #目的是求出抛100次硬币的统计量是几
  7. #我们的答案中位数6, max=24,min=3
  8. #所以抛硬币赌博很容易出现连续的正面或反面
  9. #这与直觉是相违背的
  10. CountAndCut = function(x){
  11.   # this funciton want to serch the
  12.   # initial x=1 and cut the vector x
  13.   # then  we can solve this problem using recurse method
  14.   # note at least k=1
  15.   # so my function is wright
  16.   
  17.   k = 1
  18.   N = length(x)
  19.   for(i in 1:(N-1)){
  20.     if((x[i] == x[i+1])&(x[i]==1))
  21.       k =k+1
  22.     else
  23.       break
  24.   }
  25.   z1 = k
  26.   z2 = x[(i+1):N]
  27.   return(list(z1,z2))
  28. }

  29. SearchContinuousHead = function(x){
  30.   # input head=0 or tail=1 represent 0&1
  31.   # lsit [[1]] is the longest x=1
  32.   num = length(x)
  33.   y1 = CountAndCut(x)[[1]]
  34.   y2 = CountAndCut(x)[[2]]
  35.   num=length(y2)
  36.   
  37.   while(num>=2){
  38.     y1 = max(y1,CountAndCut(y2)[[1]])
  39.     y2 = CountAndCut(y2)[[2]]
  40.     num = length(y2)
  41.   }
  42.   return(list(y1,y2))
  43.   
  44. }

  45. # so we want to check the Statistic the longest x=1 with
  46. # num =100 ,so I will run a loop=1000
  47. # to check the value of this Statistic
  48. # as the head and the tail is the same
  49. # so I did't need to count k=0
  50. num=100
  51. Count = rep(NA,1000)
  52. for(i in 1:1000){
  53.   x = rbinom(n=num, size=1, prob=0.5)
  54.   Count[i]=SearchContinuousHead(x)[[1]]
  55.   }
  56. median(Count)
  57. max(Count)
  58. min(Count)
  59. # median(Count)
  60. # [1] 6
  61. # max(Count)
  62. # [1] 24           ####非常惊人的数字
  63. # min(Count)
  64. # [1] 3
  65. # now we make a conclusion that the Statistic=6
  66. # so if you want to cheating your teacher
  67. # you need to creat a continuous tail or head near to 6
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:统计量 Continuous Conclusion statistic represent 统计

已有 1 人评分经验 学术水平 热心指数 信用等级 收起 理由
jmpamao + 60 + 1 + 1 + 1 鼓励积极发帖讨论

总评分: 经验 + 60  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

沙发
凸集分离定理 发表于 2014-4-22 04:31:36
  1. > length(which(Count==3))/1000
  2. [1] 0.032
  3. > length(which(Count==4))/1000
  4. [1] 0.151
  5. > length(which(Count==5))/1000
  6. [1] 0.259
  7. > length(which(Count==6))/1000
  8. [1] 0.244
  9. > length(which(Count==7))/1000
  10. [1] 0.136
  11. > length(which(Count==8))/1000
  12. [1] 0.093
  13. > length(which(Count==9))/1000
  14. [1] 0.038
  15. > length(which(Count==10))/1000
  16. [1] 0.014
  17. > length(which(Count==11))/1000
  18. [1] 0.018
  19. > length(which(Count==12))/1000
  20. [1] 0.006
  21. > length(which(Count==13))/1000
  22. [1] 0.006
  23. > length(which(Count==15))/1000
  24. [1] 0.001
  25. > length(which(Count==16))/1000
  26. [1] 0.001
  27. > length(which(Count==24))/1000
  28. [1] 0.001
  29. > mean(Count)
  30. [1] 6.04
复制代码


藤椅
jmpamao 发表于 2014-4-23 19:44:32
正反面投币统计量,有意思
CountAndCut 部分的 K始终=1,特别0的时候K=1, 别扭。就加了当0,K=0。
  1. cut = function(x){
  2.   k<- 1
  3.   N = length(x)
  4.   for(i in 1:(N-1)){
  5.     if(x[i]==0){k=0;break}
  6.     if((x[i] == x[i+1])&(x[i]==1))
  7.       k =k+1      
  8.     else
  9.       break
  10.   }
  11.   z1 = k
  12.   z2 = x[(i+1):N]
  13.   return(list(z1,z2))
  14. }

  15. y=c()
  16. count=function(x){
  17.   if(length(x)==1) return(y)
  18.   else {
  19.          y<<-c(y,cut(x)[[1]])
  20.          count(cut(x)[[2]])        
  21. }
  22. }
  23. set.seed(1)
  24. dat=rbinom(100,1,0.5)
  25. count(dat)
复制代码

  1. > dat
  2.   [1] 0 0 1 1 0 1 1 1 1 0 0 0 1 0 1 0 1 1 0 1 1 0
  3. [23] 1 0 0 0 0 0 1 0 0 1 0 0 1 1 1 0 1 0 1 1 1 1
  4. [45] 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 1 0 0 0 1 0
  5. [67] 0 1 0 1 0 1 0 0 0 1 1 0 1 1 0 1 0 0 1 0 1 0
  6. [89] 0 0 0 0 1 1 1 1 0 0 1 1
  7. > count(dat)
  8. [1] 0 0 2 0 4 0 0 0 1 0 1 0 2 0 2 0 1 0 0 0 0 0 1
  9. [24] 0 0 1 0 0 3 0 1 0 6 0 0 2 0 1 0 0 0 0 0 2 0 1
  10. [47] 0 0 0 1 0 0 1 0 1 0 1 0 0 0 2 0 2 0 1 0 0 1 0
  11. [70] 1 0 0 0 0 0 4 0 0 2
复制代码


最后replicate










板凳
凸集分离定理 发表于 2014-4-24 03:28:33
jmpamao 发表于 2014-4-23 19:44
正反面投币统计量,有意思
CountAndCut 部分的 K始终=1,特别0的时候K=1, 别扭。就加了当0,K=0。
谢谢,我那个程序没有考虑很短的情况,那天晚上突然想到那个讲座了,x比较长的话应该是没错的,因为全部一样的概率很低
(1/2)^10=0.0009765625.但是其实写的不好,还是应该写一个递归的函数
已有 1 人评分学术水平 收起 理由
jmpamao + 2 其实是根据你的思路做的~ 欢迎来一月一包活.

总评分: 学术水平 + 2   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-5 05:27