performance - Reduice time computation using parallel package in R -


i searched couldn't find similar question, apologies in advance if duplicate question. trying generate data frame within loop in r.

what want use package parallel compute function n=10^9 differents values.

so code did:

1- generate sample of data, , parameters of model :

data=data.frame(c=rnorm(10,150,12),k=rnorm(10,95,7),s=rnorm(10,125,9.5),t=rnorm(10,25,5)) round(data, digits = 0) para_h<-c(0.001,0.002,0.0000154,0.00052,-0.68) 

2- function use :

fc_q<-function(x,para_h,data){   t=data$t; s=data$s; k=data$k; r=0.05/250   w=para_h[1];b=para_h[2];a=para_h[3];  c= para_h[4];  neta=para_h[5]    nu=(1/(neta^2))*(((1-2*neta)^(1/2))-1)   u=1i*x ; z=length(s)    fc_q <- rep(na, z)   (i in 1:z){     a_q=0 ; b_q=0     steps<-round(t[i]*250,0)       (j in 1:steps){       a_q= a_q+ r*u + w*b_q-(1/2)*log(1-2*a*(neta^4)*b_q)       b_q= b*b_q+u*nu+ (1/neta^2)*(1-sqrt((1-2*a*(neta^4)*b_q)*( 1- 2*c*b_q - 2*u*neta)))     }     fc_q[i]= exp(log(s[i])*u + a_q + b_q*(0.0012))*exp(-r*t[i])   }   return(fc_q) } 

the problem have due dimensions of computation n=10^9 takes 1 hours on computer.

this code used cumputation loops for.

n=10^9 ; alpha=2 ; delta= 0.25; lambda=(2*pi)/(n*delta); r=0.05/250  res=c() (i in 1:n){   phi= ((fc_q(((delta*(i-1))-(alpha+1)*1i),para_h,data))/(alpha^2+alpha-(delta*(i-1))^2+1i*(2*alpha+1)*(delta*(i-1))))*delta*exp(1i*(delta*(i-1))*b)   res=rbind(res,phi) }   

this code take lot of hours, want use thing :

library(parallel) cl <- makecluster(detectcores()) result <- clusterapply(cl,1:10^9,fc_q) values <- do.call(rbind,result) stopcluster(cl) 

is possible reduce execution time using package parallel , if so, please suggest me solution. known, i´m using lot of bad things r, not figure out better solution.

any correction , suggestion improve process! please feel free share extant code in r.

thanks.

your question bit open-ended. if goal run code on multiple cores / processors, 1 of simplest solutions use mclapply parallel package. since uses forking, not work out-of-the-box on windows machine.

to use it:

num_cores <- 12 res <- mclapply(1:n, function(i) ((fc_q(((delta*(i-1))-(alpha+1)*1i),para_h,data))/(alpha^2+alpha-(delta*(i-1))^2+1i*(2*alpha+1)*(delta*(i-1))))*delta*exp(1i*(delta*(i-1))*b),                  mc.cores = num_cores) 

mclapply returns list.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -