Erratic seed behavior with rbinom(prob=0.5) in R

前端 未结 2 1346
野性不改
野性不改 2021-01-07 16:12

I have found what I would consider erratic behavior (but for which I hope there is a simple explanation) in R\'s use of seeds in conjunction with rbinom()

2条回答
  •  庸人自扰
    2021-01-07 16:54

    So let's turn our comments into an answer. Thanks to Ben Bolker for putting us on the right track with a link to the code: https://svn.r-project.org/R/trunk/src/nmath/rbinom.c and the suggestion to track down where unif_rand() is called.

    A quick scan and it seems that the code is broken into two sections, delimited by the comments:

    /*-------------------------- np = n*p >= 30 : ------------------- */
    

    and

    /*---------------------- np = n*p < 30 : ------------------------- */
    

    Inside each of these, the number of calls to unif_rand is not the same (two versus one.)

    So for a given size (n), your random seed may end up in a different state depending on the value of prob (p): whether size * prob >= 30 or not.

    With that in mind, all the results you got with your examples should now make sense:

    # these end up in the same state
    rbinom(n=1,size=60,prob=0.4) # => np <  30
    rbinom(n=1,size=60,prob=0.3) # => np <  30
    
    # these don't
    rbinom(n=1,size=60,prob=0.5) # => np >= 30
    rbinom(n=1,size=60,prob=0.3) # => np <  30
    
    # these don't
    {rbinom(n=1,size=60,prob=0.5)  # np >= 30
     rbinom(n=1,size=50,prob=0.3)} # np <  30
    {rbinom(n=1,size=60,prob=0.1)  # np <  30
     rbinom(n=1,size=50,prob=0.3)} # np <  30
    
    # these do
    {rbinom(n=1,size=60,prob=0.3)  # np <  30
     rbinom(n=1,size=50,prob=0.5)} # np <  30
    {rbinom(n=1,size=60,prob=0.1)  # np <  30
     rbinom(n=1,size=50,prob=0.3)} # np <  30
    

提交回复
热议问题