Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

increase the speed of monte carlo choice simulation #78

Open
asiripanich opened this issue May 27, 2020 · 0 comments
Open

increase the speed of monte carlo choice simulation #78

asiripanich opened this issue May 27, 2020 · 0 comments

Comments

@asiripanich
Copy link
Member

The matrix approach is much faster than the sampling approach.

probabilities <- matrix(runif(1000000), 100000, 10)
probabilities_ <- probabilities - runif(100000 * 10)

ind_sapply <- sapply(1:nrow(probabilities), function(i) sample(1:10, 1, prob = probabilities[i,]))
ind_mat <- max.col(probabilities_, 'first')

table(ind_sapply)
table(ind_mat)

Unit: milliseconds
                                                                                       expr        min         lq       mean
 sapply(1:nrow(probabilities), function(i) sample(1:10, 1, prob = probabilities[i,      ])) 469.872150 619.764793 656.780946
                                                           max.col(probabilities_, "first")   1.955379   2.209599   2.308426
     median        uq         max neval cld
 655.096765 686.15894 1047.475831   100   b
   2.270803   2.36885    2.911497   100  a 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant