0

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:

enter image description here

enter image description here

Where do you think I do something wrong? How can I improve it?

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
Novic
  • 351
  • 1
  • 11
  • 2
    Where is `integral` defined? `cons1mpdf`? What does your data look like, is it positive, non-negative, all-real, ...? Please make this question [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by listing non-base packages, including *sample* data, and any other code that is relevant to your question. – r2evans May 02 '18 at 19:18
  • 'a' is the integral here. cons1mpdf produces a risk-neutral PDF function besides a minimum and maximum for integral boundaries. The data is options markets data which is all used in the cons1mpdf function, and all I need is that. Is it possible to share files here in order to make the codes reproducible? – Novic May 02 '18 at 22:46
  • 1
    Not exactly what I was getting at. If you read the link that I provided before, you'll discussion on a preferred technique of making a question completely self-supporting, meaning sample data, required functions, and supporting code are all included. This mandates effort on the part of the OP, since questions are typically ignored or closed where there are many pages of unrelated code, and/or where things are hard to follow. So, you use a function named `integral`, doesn't exist (for me). Your PDF function is a black box that I cannot replicate. Your data is unknown. Do you understand? – r2evans May 02 '18 at 22:50
  • Yes, I get it thanks for the guidance. The integral function is in 'pracma' package. I think the PDF is not so important at this level. So simply use any pdf function like normal. In this case, there is no need to have any data. Just the PDF. If I could share the whole code with data, it could be very helpful I think. It's only 10 MB. – Novic May 02 '18 at 23:13
  • 10MB is not something to post in the question, links to data are typically frowned upon since links go stale, and your answers -- though informing -- don't make it easy for answers. Good luck, Ali. This seems like an interesting question, but you're asking a bit more for this question than "model questions" on SO. I just don't have the time to figure out all intentions and references before trying to attack the actual question. Cheers. – r2evans May 02 '18 at 23:17
  • You are right. I thought maybe there are more efficient ways of doing these things. Thank you so much for your time. – Novic May 02 '18 at 23:28

0 Answers0