optimization - Optimize routine in R -
i'm new programming in r , have problem summarize in small routine. seems simple time takes... around 15 minutes 500 iterations. how speed up? in advance!.
edit: routine calculation of probabilities simulates sample (500) of product of conditional probabilities (on pd.max.s1.i). problem has large dimensions due combinatorics of d, , s. why looked idea structures (data.frame, for) use in routine because seems slow run, , possibility other structures more optimal.
ncd = 3; nud = 4; nua = 2 pd.max.s1.i <- matrix( c( 0.00, 0.95, 0.99, 0.00, 0.80, 0.95, 0.00, 0.50, 0.70, 0.00, 0.25, 0.30, 0.00, 0.00, 0.10), nrow = nud + 1, ncol = nua + 1, byrow = t) # probabilidades rownames(pd.max.s1.i) = paste("d", 0 : nud, sep = "") colnames(pd.max.s1.i) = paste("a", 0 : nua, sep = "") pd.max.s1.i=as.data.frame(pd.max.s1.i) d = expand.grid(0 : 4, 0 : 4, 0 : 4) d = d[rowsums (d) == nud,] rownames(d) = paste0("d", apply(d, 1, paste0, collapse="")) = expand.grid(0 : 2 ,0 : 2 ,0 : 2) = a[rowsums (a) <= nua,] rownames(a) = paste0("a", apply(a, 1, paste0, collapse="")) s = expand.grid(0 : 1, 0 : 1, 0 : 1) rownames(s) = paste0("s", apply(s, 1, paste0, collapse="")) fun.prd.pa = function(pa, idp, idr, idc, ncd){ # productorio de probabilidades ppa = 1 (i in 1 : ncd) { ppi = pa[idr[1,i] + 1, idc[1,i] + 1] if (idp[1,i] == 0) ppi = 1 - ppi ppa = ppa * ppi } return(ppa) } (ite in 1: 500) { (xd1 in rownames(d)){ (xaa in rownames(a)){ (xs1 in rownames(s)){ p1 = fun.prd.pa (pd.max.s1.i, s[xs1,], d[xd1,], a[xaa,], ncd) } } } }
Comments
Post a Comment