[R] How to vectorize a function to handle two vectors

Newbie lille_knold at hotmail.com
Fri Aug 26 20:43:24 CEST 2011


Dear R-users

I am trying to "vectorize" a function so that it can handle two vectors of
inputs. I want the function to use phi (a function), k[1] and t[1] for the
first price, and so on for the second, third and fourth price. I tried to do
the mapply, but I dont know how to specify to R what input I want to be
vectors (k and t)(see in the bottom what I tried). I have read the help
file, but I still dont understand how to do it properly. Also, I've tried to
use sapply (which seems totally wrong). However, the function uses k[1] for
t 1 to 4, and thereby returns 16 different values instead of just 4. 
Can anyone tell me how to do this - I know the answer is simple, but I dont
understand how

Thank you for your time
Kinds Rikke

#------ Characteristic function of the Heston model -----#
phiHeston <- function(parameters)
{
	lambda <- parameters[1];
  	rho <- parameters[2];
   	eta <- parameters[3];
  	theta <- parameters[4];
	v0 <- parameters[5];
    
	function(u, t)
	{
	alpha <- -u*u/2 - 1i*u/2;
    	beta <- lambda - rho*eta*1i*u;
    	gamma <- eta^2/2;
    	d <- sqrt(beta*beta - 4*alpha*gamma);
    	rplus <- (beta + d)/(2*gamma);
    	rminus <- (beta - d)/(2*gamma);
    	g <- rminus / rplus;
    	D <- rminus * (1 - exp(-d*t))/ (1 - g*exp(-d*t));
    	C <- lambda * (rminus * t - 2/eta^2 * log( (1 - g*exp(-(d*t)))/(1 - g)
) );
    	return(exp(C*theta + D*v0));
	}
}


Price_call <- function(phi, k, t)
{
	integrand <-  function(u){Re(exp(-1i*u*k)*phi(u - 1i/2, t)/(u^2 + 1/4))};
    	res <- 1 - exp(k/2)/pi*integrate(integrand,lower=0,upper=Inf)$value;
    	return(res);
}

subHeston <- c(0.6067,-0.7571,0.2928,0.0707,0.0654);
kV <- c(0.9,1,1.2,1.3)
tV <- c(0.1,0.4,0.5, 1)

HestonCallVec <- function(phi, kVec, t)
{
sapply(kVec, function(k){Price_call(phi, k, t)})
}
HestonCallVec(phiHeston(subHeston), kV, 1)

subHeston <- c(0.6067,-0.7571,0.2928,0.0707,0.0654);
kV <- c(0.9,1,1.2,1.3)
tV <- c(0.1,0.4,0.5, 1)

HestonCallVec2 <- function(phi, kVec, tVec)
{
sapply(tVec, function(t){HestonCallVec(phi, kVec, t)})
}
HestonCallVec2(phiHeston(subHeston), kV, tV)
# This should give 4 values

This is what I tried for the mapply function, which returns a list, instead
of values.

HestonCallVec <- function(phi, kVec, t)
{
mapply(function(k, t){Price_call(phi, k, t)})
}
HestonCallVec(phiHeston(subHeston), kV, tV)
# This should give 4 values

--
View this message in context: http://r.789695.n4.nabble.com/How-to-vectorize-a-function-to-handle-two-vectors-tp3771705p3771705.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list