# How to vectorize this function Classic List Threaded 8 messages Open this post in threaded view
|

## How to vectorize this function

 Hello everyone,      I’ve a function with five input argument and one output number.           impVolC <- function(callM, K, T, F, r)      I hope this function can take five vectors as input, then return one vector as output. My vectorization ran into problems with the nested if-else operation. As a result, I have to write another for loop to call this function. Can anyone suggest some methods to overcome it? I put my code below, thanks. impVolC <- function(callM, K, T, F, r){  if(y >= 0){      call0 <- K*exp(-r*T)*(exp(y)*polya(sqrt(2*y)) - 0.5)      if(callM <= call0){        sig <- 1/sqrt(T)*(sqrt(gamma + y) - sqrt(gamma - y))      }else{        sig <- 1/sqrt(T)*(sqrt(gamma + y) + sqrt(gamma - y))      }  }else{      call0 <- K*exp(-r*T)*(exp(y)/2 - polya(-sqrt(-2*y)))      if(callM <= call0){        sig <- 1/sqrt(T)*(-sqrt(gamma + y) + sqrt(gamma - y))      }else{        sig <- 1/sqrt(T)*(sqrt(gamma + y) + sqrt(gamma - y))      }  }  sig } for(i in 1:length(call)){  sigV[i] <- impVolC(callM = call[i], K = df\$Strike[i], T = T, F = F, r = r_m)   } ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: How to vectorize this function

 Your function takes an argument "F" that is never used and uses an object "y" which is not defined. Give us some data to use for testing different approaches along with the answer you expect. It may be possible to use two ifelse() functions instead of the loop. ---------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77843-4352 -----Original Message----- From: R-help <[hidden email]> On Behalf Of Lynette Chang Sent: Thursday, September 20, 2018 10:09 AM To: [hidden email] Subject: [R] How to vectorize this function Hello everyone,      I’ve a function with five input argument and one output number.           impVolC <- function(callM, K, T, F, r)      I hope this function can take five vectors as input, then return one vector as output. My vectorization ran into problems with the nested if-else operation. As a result, I have to write another for loop to call this function. Can anyone suggest some methods to overcome it? I put my code below, thanks. impVolC <- function(callM, K, T, F, r){  if(y >= 0){      call0 <- K*exp(-r*T)*(exp(y)*polya(sqrt(2*y)) - 0.5)      if(callM <= call0){        sig <- 1/sqrt(T)*(sqrt(gamma + y) - sqrt(gamma - y))      }else{        sig <- 1/sqrt(T)*(sqrt(gamma + y) + sqrt(gamma - y))      }  }else{      call0 <- K*exp(-r*T)*(exp(y)/2 - polya(-sqrt(-2*y)))      if(callM <= call0){        sig <- 1/sqrt(T)*(-sqrt(gamma + y) + sqrt(gamma - y))      }else{        sig <- 1/sqrt(T)*(sqrt(gamma + y) + sqrt(gamma - y))      }  }  sig } for(i in 1:length(call)){  sigV[i] <- impVolC(callM = call[i], K = df\$Strike[i], T = T, F = F, r = r_m) } ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: How to vectorize this function

 Also: What package does polya() come from and "gamma" (as a numeric value)is undefined (it is a function). Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, Sep 20, 2018 at 9:06 AM David L Carlson <[hidden email]> wrote: > Your function takes an argument "F" that is never used and uses an object > "y" which is not defined. Give us some data to use for testing different > approaches along with the answer you expect. It may be possible to use two > ifelse() functions instead of the loop. > > ---------------------------------------- > David L Carlson > Department of Anthropology > Texas A&M University > College Station, TX 77843-4352 > > -----Original Message----- > From: R-help <[hidden email]> On Behalf Of Lynette Chang > Sent: Thursday, September 20, 2018 10:09 AM > To: [hidden email] > Subject: [R] How to vectorize this function > > Hello everyone, > >      I’ve a function with five input argument and one output number. >           impVolC <- function(callM, K, T, F, r) > >      I hope this function can take five vectors as input, then return one > vector as output. My vectorization ran into problems with the nested > if-else operation. As a result, I have to write another for loop to call > this function. Can anyone suggest some methods to overcome it? I put my > code below, thanks. > > impVolC <- function(callM, K, T, F, r){ > > >  if(y >= 0){ >      call0 <- K*exp(-r*T)*(exp(y)*polya(sqrt(2*y)) - 0.5) >      if(callM <= call0){ >        sig <- 1/sqrt(T)*(sqrt(gamma + y) - sqrt(gamma - y)) >      }else{ >        sig <- 1/sqrt(T)*(sqrt(gamma + y) + sqrt(gamma - y)) >      } >  }else{ >      call0 <- K*exp(-r*T)*(exp(y)/2 - polya(-sqrt(-2*y))) >      if(callM <= call0){ >        sig <- 1/sqrt(T)*(-sqrt(gamma + y) + sqrt(gamma - y)) >      }else{ >        sig <- 1/sqrt(T)*(sqrt(gamma + y) + sqrt(gamma - y)) >      } >  } >  sig > } > > for(i in 1:length(call)){ >  sigV[i] <- impVolC(callM = call[i], K = df\$Strike[i], T = T, F = F, r = > r_m) } > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: How to vectorize this function

 In reply to this post by Lynette Chang In addition to what the other said, if callM is a vector then an expression of the form    if (callM <= call0) is inappropriate. Objects inside the parentheses of   if()  should have length one. For example, > if (1:5 < 3) 'a' else 'b'  "a" Warning message: In if (1:5 < 3) "a" else "b" :   the condition has length > 1 and only the first element will be used instead of what you have:          if(callM <= call0){            sig <- 1/sqrt(T)*(sqrt(gamma + y) - sqrt(gamma - y))          }else{            sig <- 1/sqrt(T)*(sqrt(gamma + y) + sqrt(gamma - y))         } Here are a couple of (untested) possibilities:   M.gt.0 <- callM > call0   sig <- 1/sqrt(T)*(sqrt(gamma + y) - sqrt(gamma - y))   sig[M.gt.0] <- (1/sqrt(T)*(sqrt(gamma + y) + sqrt(gamma - y)))[M.gt.0] or     sig <- 1/sqrt(T)*(sqrt(gamma + y)  + ifelse(callM <= call0, -1, 1) * sqrt(gamma - y)) incidentally, I would write    sig <- (sqrt(gamma + y) - sqrt(gamma - y))/sqrt(T) instead of    sig <- 1/sqrt(T)*(sqrt(gamma + y) - sqrt(gamma - y)) -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509     ﻿On 9/20/18, 8:08 AM, "R-help on behalf of Lynette Chang" <[hidden email] on behalf of [hidden email]> wrote:     Hello everyone,              I’ve a function with five input argument and one output number.      impVolC <- function(callM, K, T, F, r)              I hope this function can take five vectors as input, then return one vector as output. My vectorization ran into problems with the nested if-else operation. As a result, I have to write another for loop to call this function. Can anyone suggest some methods to overcome it? I put my code below, thanks.         impVolC <- function(callM, K, T, F, r){              if(y >= 0){          call0 <- K*exp(-r*T)*(exp(y)*polya(sqrt(2*y)) - 0.5)          if(callM <= call0){            sig <- 1/sqrt(T)*(sqrt(gamma + y) - sqrt(gamma - y))          }else{            sig <- 1/sqrt(T)*(sqrt(gamma + y) + sqrt(gamma - y))          }      }else{          call0 <- K*exp(-r*T)*(exp(y)/2 - polya(-sqrt(-2*y)))          if(callM <= call0){            sig <- 1/sqrt(T)*(-sqrt(gamma + y) + sqrt(gamma - y))          }else{            sig <- 1/sqrt(T)*(sqrt(gamma + y) + sqrt(gamma - y))          }      }      sig     }         for(i in 1:length(call)){      sigV[i] <- impVolC(callM = call[i], K = df\$Strike[i], T = T, F = F, r = r_m)       }         ______________________________________________     [hidden email] mailing list -- To UNSUBSCRIBE and more, see     https://stat.ethz.ch/mailman/listinfo/r-help    PLEASE do read the posting guide http://www.R-project.org/posting-guide.html    and provide commented, minimal, self-contained, reproducible code.     ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: How to vectorize this function

Open this post in threaded view
|