I have a function in R that takes almost 1 second to run. So I think it is not something heavy. It looks like this:
DataList <- cons1mpdf(tradedays[i])
  tempQ <- DataList[[1]]
  Qpdf <- function(X) pmax(tempQ(X),0)
  norINTEG <- function(X)  Qpdf(X) / X^(-RRA)
Then I want to take an integral from this function and use the result in a miximization problem like this:
a <-  integral( norINTEG ,  DataList[[2]] ,  DataList[[3]] )
  realPDF <- function(X)  norINTEG(X) / a
  zBerkowitz[i] <-  qnorm(  pmax( 0,  integral(realPDF ,  DataList[[2]] ,   head(nextmonth.close[date == tradedays[i]] , 1)    ) ) )
But the calculation of a takes almost 1 minute for the RRA of bigger than 3. All of this code is in for loop and this integral causes my loop to run in a lot of time. The problem gets even worse because I want to use this result in a maximization problem which will take several days to run.
This is the part of the problem I want to solve:
Where do you think I do something wrong? How can I improve it?


 
    