There is a holiday party with 6 people. Each of the 6 people have an estimate for their gifts_brought, and each person has an estimate for their chance of receiving another person's gift (gifts_received_pct). There is no limit to the number of gifts a person can receive from another person. They can only give/receive gifts from their own team, and they cannot gift themselves their own gifts that they brought.
My real problem has 1000 different iterations/estimates for the parameters gifts_brought and gifts_received_pct and I want estimates for the total gifts received for each person for all 1000 estimates. For the purpose of this exercise I'm going to make all the estimates the same, but I want to be clear that in reality all my dataframes have different estimates for the parameters which is why I can't just do rmultinom(1000, ...).
First build the dummy code.
name <- c('Aaron', 'Susie', 'Sam', 'Emma', 'Jennifer', 'Steve')
giftsBrought <- c(5, 3, 4, 2, 3, 6)
team <- c('Sales', 'Sales', 'Sales', 'IT', 'IT', 'IT')
gifts_received_pct <- c(.2, .3, .1, .2, .2, .1) # rmultinom does not require normilazation
giftsDF <- data.frame(name, team, giftsBrought, gifts_received_pct, stringsAsFactors = FALSE)
giftsEstimationList <- list()
for(i in 1:1000){
giftsEstimationList[[i]] <- giftsDF
}
Next, this is how I would get the gifts_received calculation for just one of the dataframes:
giftsReceivedDF <- lapply(1:nrow(giftsDF), function(i){
probs <- giftsDF
probs$gifts_received_pct[probs$team != giftsDF$team[i] | probs$name == giftsDF$name[i]] <- 0 # set other_team_pct and own_pct to 0
rmultinom(1, giftsDF$giftsBrought[i], probs$gifts_received_pct)
})
Reduce(`+`, giftsReceivedDF)
I believe this is correct - when reviewing giftsReceivedDF closely, it appears that no person ever receives their own gifts, and the other team doesn't receive any of their gifts.
Where I'm stumped is how to run this through all 1000 of the dataframes in giftsEstimationList in a timely fashion. I originally tried to brute force everything with a bunch of for-loops, but I don't believe that is going to be the most efficient and time is rather important here.
A function to simulate the gift exchange. It is vectorized over all teams and iterations. It only has to loop over the maximum number of participants (3 in the example data).
Demonstrating: