-1

I'm trying to write a simple script in R to output a vector of binomially distributed values such that the sum of the values is in a given range. I know I will need to use the command rbinom to draw values from the binomial distribution but I can't figure out how to write code so that values are drawn one at a time until the sum is in the given range (and not exceeding the upper bound) and then the codes stops.

I have basic experience using R and MATLAB but that's as far as my programming experience goes.

Thanks for the help!

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
  • 1
    Can you provide code for what you have tried so far? Can you be more specific about "a certain range"? The more details you provide, the easier it is to help. – jdobres Feb 27 '19 at 16:21
  • 1
    Welcome to SO. Please take a bit of time to read the [tour](https://stackoverflow.com/tour) to learn how this site works. You will find it a useful place to get help with code that you have tried to work on. But this is not a place to ask people to write code for you from scratch. – dww Feb 27 '19 at 16:22
  • 3
    I would generate the a *long* sequence of binomial outcomes and then use cumsum() to select the subset that meet your criteria. Repeatedly calling rbinom() will be slow. – Chase Feb 27 '19 at 16:23

2 Answers2

1

Here's a long way that can help illustrate:

set.seed(101)
total  <- 0
trials <- 0
limit  <- 10
while(total < limit) {
  trials <- trials + 1
  total <- total + rbinom(1, 1, 0.5)
  cat("Number of trials: ", trials, "\t", "Total is: ", total, "\n")
}
#> Number of trials:  1      Total is:  0 
#> Number of trials:  2      Total is:  0 
#> Number of trials:  3      Total is:  1 
#> Number of trials:  4      Total is:  2 
#> Number of trials:  5      Total is:  2 
#> Number of trials:  6      Total is:  2 
#> Number of trials:  7      Total is:  3 
#> Number of trials:  8      Total is:  3 
#> Number of trials:  9      Total is:  4 
#> Number of trials:  10     Total is:  5 
#> Number of trials:  11     Total is:  6 
#> Number of trials:  12     Total is:  7 
#> Number of trials:  13     Total is:  8 
#> Number of trials:  14     Total is:  9 
#> Number of trials:  15     Total is:  9 
#> Number of trials:  16     Total is:  10

However, calling rbinom is going to be resource intensive. A different approach would be to set the n argument of rbinom to something large and than use something like:

large_trials <- rbinom(60, 1, 0.5)
large_trials
#>  [1] 1 0 0 0 1 1 0 1 1 1 0 0 0 1 0 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 0 1 1 0 0
#> [36] 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 1 1 0

which.min(cumsum(large_trials) < limit)
#> [1] 22
JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
0

Well, one simple fact could help: if you have binomial i.i.d. random variables, then for sum of them there is simple rule

B1(n,p) + B2(m,p) = B3(n+m,p)

Thus sum is also distributed as binomial. And you could apply this rule for as long sequence as you want. You know n, know p, you could calculate probabilities of summed value and see if they fit within the range. You could also get how many binomial samples you need

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64