Hola, me gustaria saber como óptimizar el siguiente código para que corra en menos tiempo de ejecución: sim1 <- function(nreps) { nb1 <- 10 # 10 blue marbles in Urn 1 n1 <- 18 # number of marbles in Urn 1 at 1st pick n2 <- 13 # number of marbles in Urn 2 at 2nd pick count <- 0 # number of repetitions in which get blue from Urn 2 for (i in 1:nreps) { nb2 <- 6 # 6 blue marbles orig. in Urn 2 # pick from Urn 1 and put in Urn 2; is it blue? if (runif(1) < nb1/n1) nb2 <- nb2 + 1 # pick from Urn 2; is it blue? if (runif(1) < nb2/n2) count <- count + 1 } return(count/nreps) # est. P(pick blue from Urn 2) } Gracias, Tania [[alternative HTML version deleted]]
Hola, ¿qué tal? Prueba esta alternativa: sim2 <- function(nreps) { nb1 <- 10 # 10 blue marbles in Urn 1 nb2 <- 6 # 6 blue marbles orig. in Urn 2 n1 <- 18 # number of marbles in Urn 1 at 1st pick n2 <- 13 # number of marbles in Urn 2 at 2nd pick count <- 0 # number of repetitions in which get blue from Urn 2 nb2 <- nb2 + (runif(nreps) < nb1 / n1) mean( runif(nreps) < nb2 / n2 ) #return(count/nreps) # est. P(pick blue from Urn 2) }> system.time(sim1(10000))user system elapsed 0.092 0.000 0.094> system.time(sim2(10000))user system elapsed 0.000 0.000 0.001 Un saludo, Carlos J. Gil Bellosta http://www.datanalytics.com 2013/1/17 Tania Patiño <taniuxpc en gmail.com>:> Hola, me gustaria saber como óptimizar el siguiente código para que corra > en menos tiempo de ejecución: > > sim1 <- function(nreps) { > nb1 <- 10 # 10 blue marbles in Urn 1 > n1 <- 18 # number of marbles in Urn 1 at 1st pick > n2 <- 13 # number of marbles in Urn 2 at 2nd pick > count <- 0 # number of repetitions in which get blue from Urn 2 > for (i in 1:nreps) { > nb2 <- 6 # 6 blue marbles orig. in Urn 2 > # pick from Urn 1 and put in Urn 2; is it blue? > if (runif(1) < nb1/n1) nb2 <- nb2 + 1 > # pick from Urn 2; is it blue? > if (runif(1) < nb2/n2) count <- count + 1 > } > return(count/nreps) # est. P(pick blue from Urn 2) > } > > > Gracias, > > Tania > > [[alternative HTML version deleted]] > > > _______________________________________________ > R-help-es mailing list > R-help-es en r-project.org > https://stat.ethz.ch/mailman/listinfo/r-help-es >
O incluso esto (aún mejor): sim2 <- function(nreps) { nb1 <- 10 # 10 blue marbles in Urn 1 nb2 <- 6 # 6 blue marbles orig. in Urn 2 n1 <- 18 # number of marbles in Urn 1 at 1st pick n2 <- 13 # number of marbles in Urn 2 at 2nd pick count <- 0 # number of repetitions in which get blue from Urn 2 nb2 <- nb2 + (runif(nreps) < nb1 / n1) rbinom(1, nreps, nb2/n2) / nreps } Un saludo, Carlos J. Gil Bellosta http://www.datanalytics.com 2013/1/17 Carlos J. Gil Bellosta <cgb en datanalytics.com>:> Hola, ¿qué tal? > > Prueba esta alternativa: > > sim2 <- function(nreps) { > nb1 <- 10 # 10 blue marbles in Urn 1 > nb2 <- 6 # 6 blue marbles orig. in Urn 2 > n1 <- 18 # number of marbles in Urn 1 at 1st pick > n2 <- 13 # number of marbles in Urn 2 at 2nd pick > count <- 0 # number of repetitions in which get blue from Urn 2 > > nb2 <- nb2 + (runif(nreps) < nb1 / n1) > mean( runif(nreps) < nb2 / n2 ) > > #return(count/nreps) # est. P(pick blue from Urn 2) > } > >> system.time(sim1(10000)) > user system elapsed > 0.092 0.000 0.094 >> system.time(sim2(10000)) > user system elapsed > 0.000 0.000 0.001 > > Un saludo, > > Carlos J. Gil Bellosta > http://www.datanalytics.com > > > > 2013/1/17 Tania Patiño <taniuxpc en gmail.com>: >> Hola, me gustaria saber como óptimizar el siguiente código para que corra >> en menos tiempo de ejecución: >> >> sim1 <- function(nreps) { >> nb1 <- 10 # 10 blue marbles in Urn 1 >> n1 <- 18 # number of marbles in Urn 1 at 1st pick >> n2 <- 13 # number of marbles in Urn 2 at 2nd pick >> count <- 0 # number of repetitions in which get blue from Urn 2 >> for (i in 1:nreps) { >> nb2 <- 6 # 6 blue marbles orig. in Urn 2 >> # pick from Urn 1 and put in Urn 2; is it blue? >> if (runif(1) < nb1/n1) nb2 <- nb2 + 1 >> # pick from Urn 2; is it blue? >> if (runif(1) < nb2/n2) count <- count + 1 >> } >> return(count/nreps) # est. P(pick blue from Urn 2) >> } >> >> >> Gracias, >> >> Tania >> >> [[alternative HTML version deleted]] >> >> >> _______________________________________________ >> R-help-es mailing list >> R-help-es en r-project.org >> https://stat.ethz.ch/mailman/listinfo/r-help-es >>
Tania, aquí se optimiza vectorizando el bucle (ver The Art of R Programming: A Tour of Statistical Software Design de Norman Matloff) sim3 <- function(nreps) { nb1 <- 10 # 10 blue marbles in Urn 1 nb2 = 6 # 6 blue marbles orig. in Urn 2 n1 <- 18 # number of marbles in Urn 1 at 1st pick n2 <- 13 # number of marbles in Urn 2 at 2nd pick count <- 0 # number of repetitions in which get blue from Urn 2 u <- matrix(c(runif(2*nreps)),nrow=nreps,ncol=2) cndt <- (u[,1] <= nb1/n1) & (u[,2] <= (nb2+1)/n2) | (u[,1] > nb1/ n1) & (u[,2] <= nb2/n2) return(mean(cndt)) # est. P(pick blue from Urn 2) } Un saludo. Olivier PD: creo que la función que propone Carlos lleva a una estimación sesgada. > system.time(sim3(100000)) user system elapsed 0.039 0.018 0.058 > system.time(sim1(100000)) user system elapsed 2.002 0.035 2.034 -- ____________________________________ Olivier G. Nuñez Email: onunez en unex.es http://matematicas.unex.es/~onunez Tel : +34 663 03 69 09 Departamento de Matemáticas Universidad de Extremadura ____________________________________ El 17/01/2013, a las 20:00, Tania Patiño escribió:> nb1 <- 10 # 10 blue marbles in Urn 1 > n1 <- 18 # number of marbles in Urn 1 at 1st pick > n2 <- 13 # number of marbles in Urn 2 at 2nd pick
Hola, ¿qué tal? Sí, mi opción 2 ha sido un craso error. Aquí va otra un poco más fina: sim4 <- function(nreps) { nb1 <- 10 # 10 blue marbles in Urn 1 nb2 <- 6 # 6 blue marbles orig. in Urn 2 n1 <- 18 # number of marbles in Urn 1 at 1st pick n2 <- 13 # number of marbles in Urn 2 at 2nd pick # number of cases in which you pick blue in first urn n.blue <- rbinom(1, nreps, nb1 / n1) n.blue.1 <- rbinom(1, nreps - n.blue, nb2 / n2) n.blue.2 <- rbinom(1, n.blue, (nb2+1) / n2) (n.blue.1 + n.blue.2) / nreps }> system.time(sim4(100000))user system elapsed 0.000 0.000 0.001> system.time(sim1(100000))user system elapsed 0.904 0.000 0.918 Un saludo, Carlos J. Gil Bellosta http://www.datanalytics.com 2013/1/18 Olivier Nuñez <onunez en unex.es>:> Tania, > > aquí se optimiza vectorizando el bucle (ver The Art of R Programming: A Tour > of Statistical Software Design de Norman Matloff) > > sim3 <- function(nreps) { > > nb1 <- 10 # 10 blue marbles in Urn 1 > nb2 = 6 # 6 blue marbles orig. in Urn 2 > n1 <- 18 # number of marbles in Urn 1 at 1st pick > n2 <- 13 # number of marbles in Urn 2 at 2nd pick > count <- 0 # number of repetitions in which get blue from Urn 2 > u <- matrix(c(runif(2*nreps)),nrow=nreps,ncol=2) > cndt <- (u[,1] <= nb1/n1) & (u[,2] <= (nb2+1)/n2) | (u[,1] > nb1/n1) & > (u[,2] <= nb2/n2) > return(mean(cndt)) # est. P(pick blue from Urn 2) > } > > Un saludo. Olivier > > PD: creo que la función que propone Carlos lleva a una estimación sesgada. > >> system.time(sim3(100000)) > user system elapsed > 0.039 0.018 0.058 >> system.time(sim1(100000)) > user system elapsed > 2.002 0.035 2.034 > > -- ____________________________________ > > Olivier G. Nuñez > Email: onunez en unex.es > http://matematicas.unex.es/~onunez > Tel : +34 663 03 69 09 > Departamento de Matemáticas > Universidad de Extremadura > > ____________________________________ > > > > > > El 17/01/2013, a las 20:00, Tania Patiño escribió: > > >> nb1 <- 10 # 10 blue marbles in Urn 1 >> n1 <- 18 # number of marbles in Urn 1 at 1st pick >> n2 <- 13 # number of marbles in Urn 2 at 2nd pick > > > _______________________________________________ > R-help-es mailing list > R-help-es en r-project.org > https://stat.ethz.ch/mailman/listinfo/r-help-es
Carlos, tu ultima opción es de lejos la más rápida entre las insesgadas. Por cierto, ya que te tengo, el adjetivo "craso" se puede utilizar con otro sustantivo que no sea "error"? Un abrazo. Olivier> Hola, ¿qué tal? > > Sí, mi opción 2 ha sido un craso error. Aquí va otra un poco más fina: > > sim4 <- function(nreps) { > nb1 <- 10 # 10 blue marbles in Urn 1 > nb2 <- 6 # 6 blue marbles orig. in Urn 2 > n1 <- 18 # number of marbles in Urn 1 at 1st pick > n2 <- 13 # number of marbles in Urn 2 at 2nd pick > > # number of cases in which you pick blue in first urn > n.blue <- rbinom(1, nreps, nb1 / n1) > > n.blue.1 <- rbinom(1, nreps - n.blue, nb2 / n2) > n.blue.2 <- rbinom(1, n.blue, (nb2+1) / n2) > > (n.blue.1 + n.blue.2) / nreps > } > >> system.time(sim4(100000)) > user system elapsed > 0.000 0.000 0.001 >> system.time(sim1(100000)) > user system elapsed > 0.904 0.000 0.918 > > Un saludo, > > Carlos J. Gil Bellosta > http://www.datanalytics.com > > 2013/1/18 Olivier Nuñez <onunez en unex.es>: >> Tania, >> >> aquí se optimiza vectorizando el bucle (ver The Art of R Programming: A Tour >> of Statistical Software Design de Norman Matloff) >> >> sim3 <- function(nreps) { >> >> nb1 <- 10 # 10 blue marbles in Urn 1 >> nb2 = 6 # 6 blue marbles orig. in Urn 2 >> n1 <- 18 # number of marbles in Urn 1 at 1st pick >> n2 <- 13 # number of marbles in Urn 2 at 2nd pick >> count <- 0 # number of repetitions in which get blue from Urn 2 >> u <- matrix(c(runif(2*nreps)),nrow=nreps,ncol=2) >> cndt <- (u[,1] <= nb1/n1) & (u[,2] <= (nb2+1)/n2) | (u[,1] > nb1/n1) & >> (u[,2] <= nb2/n2) >> return(mean(cndt)) # est. P(pick blue from Urn 2) >> } >> >> Un saludo. Olivier >> >> PD: creo que la función que propone Carlos lleva a una estimación sesgada. >> >>> system.time(sim3(100000)) >> user system elapsed >> 0.039 0.018 0.058 >>> system.time(sim1(100000)) >> user system elapsed >> 2.002 0.035 2.034 >> >> -- ____________________________________ >> >> Olivier G. Nuñez >> Email: onunez en unex.es >> http://matematicas.unex.es/~onunez >> Tel : +34 663 03 69 09 >> Departamento de Matemáticas >> Universidad de Extremadura >> >> ____________________________________ >> >> >> >> >> >> El 17/01/2013, a las 20:00, Tania Patiño escribió: >> >> >>> nb1 <- 10 # 10 blue marbles in Urn 1 >>> n1 <- 18 # number of marbles in Urn 1 at 1st pick >>> n2 <- 13 # number of marbles in Urn 2 at 2nd pick >> >> >> _______________________________________________ >> R-help-es mailing list >> R-help-es en r-project.org >> https://stat.ethz.ch/mailman/listinfo/r-help-es >-- ____________________________________ Olivier G. Nuñez Email: onunez en unex.es http://kolmogorov.unex.es/~onunez Tel : +34 663 03 69 09 Departamento de Matemáticas Universidad de Extremadura
Hola, ¿qué tal? Pues, por eliminación, parece que sí: http://books.google.com/ngrams/graph?content=craso%2Ccraso+error%2C+error+craso&year_start=1800&year_end=2000&corpus=21&smoothing=3&share Un saludo, Carlos J. Gil Bellosta http://www.datanalytics.com El día 18 de enero de 2013 11:14, "Olivier Nuñez" <onunez en unex.es> escribió:> Carlos, > > tu ultima opción es de lejos la más rápida entre las insesgadas. > Por cierto, ya que te tengo, el adjetivo "craso" se puede utilizar con otro > sustantivo que no sea "error"? > Un abrazo. Olivier > >> Hola, ¿qué tal? >> >> Sí, mi opción 2 ha sido un craso error. Aquí va otra un poco más fina: >> >> sim4 <- function(nreps) { >> nb1 <- 10 # 10 blue marbles in Urn 1 >> nb2 <- 6 # 6 blue marbles orig. in Urn 2 >> n1 <- 18 # number of marbles in Urn 1 at 1st pick >> n2 <- 13 # number of marbles in Urn 2 at 2nd pick >> >> # number of cases in which you pick blue in first urn >> n.blue <- rbinom(1, nreps, nb1 / n1) >> >> n.blue.1 <- rbinom(1, nreps - n.blue, nb2 / n2) >> n.blue.2 <- rbinom(1, n.blue, (nb2+1) / n2) >> >> (n.blue.1 + n.blue.2) / nreps >> } >> >>> system.time(sim4(100000)) >> user system elapsed >> 0.000 0.000 0.001 >>> system.time(sim1(100000)) >> user system elapsed >> 0.904 0.000 0.918 >> >> Un saludo, >> >> Carlos J. Gil Bellosta >> http://www.datanalytics.com >> >> 2013/1/18 Olivier Nuñez <onunez en unex.es>: >>> Tania, >>> >>> aquí se optimiza vectorizando el bucle (ver The Art of R Programming: A Tour >>> of Statistical Software Design de Norman Matloff) >>> >>> sim3 <- function(nreps) { >>> >>> nb1 <- 10 # 10 blue marbles in Urn 1 >>> nb2 = 6 # 6 blue marbles orig. in Urn 2 >>> n1 <- 18 # number of marbles in Urn 1 at 1st pick >>> n2 <- 13 # number of marbles in Urn 2 at 2nd pick >>> count <- 0 # number of repetitions in which get blue from Urn 2 >>> u <- matrix(c(runif(2*nreps)),nrow=nreps,ncol=2) >>> cndt <- (u[,1] <= nb1/n1) & (u[,2] <= (nb2+1)/n2) | (u[,1] > nb1/n1) & >>> (u[,2] <= nb2/n2) >>> return(mean(cndt)) # est. P(pick blue from Urn 2) >>> } >>> >>> Un saludo. Olivier >>> >>> PD: creo que la función que propone Carlos lleva a una estimación sesgada. >>> >>>> system.time(sim3(100000)) >>> user system elapsed >>> 0.039 0.018 0.058 >>>> system.time(sim1(100000)) >>> user system elapsed >>> 2.002 0.035 2.034 >>> >>> -- ____________________________________ >>> >>> Olivier G. Nuñez >>> Email: onunez en unex.es >>> http://matematicas.unex.es/~onunez >>> Tel : +34 663 03 69 09 >>> Departamento de Matemáticas >>> Universidad de Extremadura >>> >>> ____________________________________ >>> >>> >>> >>> >>> >>> El 17/01/2013, a las 20:00, Tania Patiño escribió: >>> >>> >>>> nb1 <- 10 # 10 blue marbles in Urn 1 >>>> n1 <- 18 # number of marbles in Urn 1 at 1st pick >>>> n2 <- 13 # number of marbles in Urn 2 at 2nd pick >>> >>> >>> _______________________________________________ >>> R-help-es mailing list >>> R-help-es en r-project.org >>> https://stat.ethz.ch/mailman/listinfo/r-help-es >> > > > -- > ____________________________________ > > > Olivier G. Nuñez > Email: onunez en unex.es > http://kolmogorov.unex.es/~onunez > Tel : +34 663 03 69 09 > Departamento de Matemáticas > Universidad de Extremadura > > > ____________________________________ > > > >