4

I hope you are well. I was wondering if you could help me with the question provided in the attached link, please. Below the link I attach an R-code that solves the problem recursively for particular values of the parameters of the distributions involved. However, I realized that this method is inefficient. Thanks a lot for your help.

How to obtain the probability distribution of a sum of dependent discrete random variables more efficiently

library(boot)     # The library boot is necessary to use the command inv.logit.

TMax <- 500       # In this R-code, I am using TMax instead of using T.
M <- 2000
beta0 <- 1
beta1 <- 0.5 
Prob_S <- function(k, r){        # In this R-code, I am using r instead of using t.
    if(r == 1){
        Aux <- dbinom(x = k, size = M, prob = inv.logit(beta0))
        }
    if(r %in% 2:TMax){
        Aux <- 0
        for(u in 0:k){
            Aux <- Aux + dbinom(x = k - u, size = M - u, 
                prob = inv.logit(beta0 + beta1 * u)) * Prob_S(u, r - 1)
            }
        }
    Aux
    }

m <- 300
P <- Prob_S(k = m, r = TMax)    # Computing P takes a loooong time.   :(
Student1981
  • 319
  • 1
  • 8
  • 1
    at a glance, it looks like you compute the value of `Prob_S(m, n)` for the same `(m,n)` many many times. You should store the values each time you compute a new one, and attempt to look it up if it may already be computed. – MichaelChirico Sep 03 '17 at 02:53
  • 2
    this is essentially the same problem as the naive recursive implementation of fibonnaci numbers, which can be overcome with dynamic programming, see here: https://www.ics.uci.edu/~eppstein/161/960109.html – MichaelChirico Sep 03 '17 at 02:55
  • Excellent comment, MichaelChirico. I solved it. Thanks a lot for your help. – Student1981 Sep 03 '17 at 05:00
  • @Student1981 Can you provide the answer you've reached? – F. Privé Sep 03 '17 at 07:25
  • @Student1981 ; just for info boot::inv.logit == plogis – user20650 Sep 03 '17 at 13:03

0 Answers0