Skip to content

Error message: object of type 'closure' is not subsettable

4 messages · Newbie, Berend Hasselman

#
Dear R-users

I need to calibrate kappa, rho, eta, theta, v0 in the following code, see
below. However when I run it, I get:

y <- function(kappahat, rhohat, etahat, thetahat, v0hat) {sum(difference(k,
t, S0, X, r, implvol, q, kappahat, rhohat, etahat, thetahat, v0hat)^2)}
Error in dots[[1L]][[1L]] : object of type 'closure' is not subsettable

And I don't know what this mean and what I am doing wrong. Can anyone help
me?
Here is my code and data set. 

Best 
Rikke

http://r.789695.n4.nabble.com/file/n3752886/S%26P_500_calls%2C_jan-jun_2010.csv
S%26P_500_calls%2C_jan-jun_2010.csv 

marketdata <- read.csv(file="S&P 500 calls, jan-jun 2010.csv", header=TRUE,
sep=";")
spot <- read.csv(file="S&P 500 spot and return 2010.csv", header=TRUE,
sep=";")

#------------------------- Values ----------------------------------
#### Data imported
S0 <- 1136.03
X <- marketdata[1:460,9]
t <- marketdata[1:460,17]/365		#Notice the T is measured in years now
implvol <- marketdata[1:460,12]
k <- log(X/(S0*exp(r-q)*t))

###### Initial values
kappa <- 0.0663227			# Lambda = -kappa
rho <- -0.6678461
eta <- 0.002124704
theta <- 0.0001421415
v0 <- 0.0001421415

q <- 0.02145608
r <- 0.01268737

#-----------------------------------------------------------------------------
#### The price of a Call option (Eq. (5.6) of The Volatility Surface,
Gatheral)
# In terms of log moneyness

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

Price_callVec <- function(phi, k, t)
{
	mapply(Price_call, phi, k, t)
}

# The characteric formula for the Heston model (Eq. XX)
phiHeston <- function(kappa, rho, eta, theta, v0)
{	
	lambda <- - kappa
	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)/(eta^2)
		rminus <- (beta - d)/(eta^2)
		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))
	}
}

## Calculating the Heston model price with fourier
HestonCall<-function(k,t)
{
res<-Price_callVec(phiHeston(kappa, rho, eta, theta, v0),k,t)
return(res)
}

##### Vectorizing the function to handle vectors of strikes and maturities
HestonCallVec <- function(k,t)
{
mapply (HestonCall, k, t)
}

lb <- c(0, -0.9, 0, 0, 0)
ub <- c(100, 0.9, 0.5, 1, 1)

difference <- function(k, t, S0, X, r, implvol, q, kappa, rho, eta, theta,
v0)
{
return(HestonCallVec(k,t) - BS_Call(S0, X, t, r, implvol, q))
}

y <- function(kappahat, rhohat, etahat, thetahat, v0hat) {sum(difference(k,
t, S0, X, r, implvol, q, kappahat, rhohat, etahat, thetahat, v0hat)^2)}
nlminb(start=list(kappa, rho, eta, theta, v0), objective = y, lower =lb,
upper =ub)

--
View this message in context: http://r.789695.n4.nabble.com/Error-message-object-of-type-closure-is-not-subsettable-tp3752886p3752886.html
Sent from the R help mailing list archive at Nabble.com.
#
Newbie wrote:
You haven't given all your data. Spot csv is missing.

You are using nlminb incorrectly.
It expects the objective function to take a numeric vector as argument as
clearly stated in the documentation.
Which should have been clear after your first post.

This would possibly help (NOT tested because of lack of data)

y <- function(par) {kappahat<-par[1]; rhohat<-par[2]; etahat<-par[3];
thetahat<-par[4]; v0hat<-par[5]; sum(difference(k, t, S0, X, r, implvol, q,
kappahat, rhohat, etahat, thetahat, v0hat)^2)}

nlminb(start=c(kappa, rho, eta, theta, v0), objective = y, lower =lb, upper
=ub)

Berend


--
View this message in context: http://r.789695.n4.nabble.com/Error-message-object-of-type-closure-is-not-subsettable-tp3752886p3754511.html
Sent from the R help mailing list archive at Nabble.com.
2 days later
#
Thank you for your help, even though there was such an obvious mistake, Im
sorry for that
I have now tried to incorporate your suggested solution, but just as last
time (the other post that you referred to), I get the values of the initial
parameters when I run nlminb. 
I have changed the code a bit, see below, due to some error messages. 
Can anyone see what I am doing wrong? 

Thank you in advance!

http://r.789695.n4.nabble.com/file/n3758834/S%26P_500_calls%2C_jan-jun_2010.csv
S%26P_500_calls%2C_jan-jun_2010.csv 

setwd("F:/Data til speciale/")

############## Calibration of Heston model parameters


marketdata <- read.csv(file="S&P 500 calls, jan-jun 2010.csv", header=TRUE,
sep=";")

BS_Call <- function(S0, K, T, r, sigma, q)
{
	sig <- sigma * sqrt(T)
	d1 <- (log (S0/K) + (r - q + sigma^2/2) * T ) / sig
	d2 <- d1 - sig
	Presentvalue <- exp(-r*T)
	return (S0 * exp(-q*T) * pnorm(d1) - K*Presentvalue*pnorm(d2))
}


#------------------------- Values ----------------------------------
#### Data imported
S0 <- 1136.02
X <- marketdata[1:460,9]
t <- marketdata[1:460,17]/365		#Notice the T is measured in years now
implvol <- marketdata[1:460,12]

k <- log(X/(S0*exp(r-q)*t))

###### Initial values
kappa <- 0.0663227			# Lambda = -kappa
rho <- -0.6678461
eta <- 0.002124704
theta <- 0.0001421415
v0 <- 0.0001421415

q <- 0.02145608
r <- 0.01268737

#-----------------------------------------------------------------------------
#### The price of a Call option (Eq. (5.6) of The Volatility Surface,
Gatheral)
# In terms of log moneyness

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


# The characteric formula for the Heston model (Eq. XX)
phiHeston <- function(kappa, rho, eta, theta, v0)
{	
	lambda <- -kappa
	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)/(eta^2)
		rminus <- (beta - d)/(eta^2)
		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))
	}
}

## Calculating the Heston model price with fourier
HestonCall<-function(k,t)
{
res<-Price_call(phiHeston(kappa, rho, eta, theta, v0),k,t)
return(res)
}


##### Vectorizing the function to handle vectors of strikes and maturities
HestonCallVec <- function(k,t)
{
mapply (HestonCall, k, t)
}

lb <- c(0, -0.9, 0, 0, 0)
ub <- c(100, 0.9, 0.5, 1, 1)
#BS_Call(S0, exp(k), t, r, implvol, q)

difference <- function(k, t, S0, r, implvol, q, kappa, rho, eta, theta, v0)
{
return(HestonCallVec(k,t) - BS_Call(S0, exp(k), t, r, implvol, q))
}
difference(k,t,S0, r, implvol, q, kappa, rho, eta, theta, v0)
y <- function(par) {kappahat<-par[1]; rhohat<-par[2]; etahat<-par[3];
thetahat<-par[4]; v0hat<-par[5]; sum(difference(k, t, S0, r, implvol, q,
kappahat, rhohat, etahat, thetahat, v0hat)^2)}
nlminb(start=c(kappa, rho, eta, theta, v0), objective = y, lower =lb, upper
=ub)

--
View this message in context: http://r.789695.n4.nabble.com/Error-message-object-of-type-closure-is-not-subsettable-tp3752886p3758834.html
Sent from the R help mailing list archive at Nabble.com.
2 days later
#
I think I have found my problem, but I dont know how to correct it. I have
found an old post saying that it might be a problem if the starting values
are evaluated at Inf (see link here
http://r.789695.n4.nabble.com/Help-about-nlminb-function-td3089048.html)

But how can I run nlminb without the starting values being evaluated at
Inf.? 
What I more specifically want to do, is to set C and D equal to 0 for the
starting values, but to have it evaluated for Inf for the rest (C and D
equal to what the function says). Does anyone have a suggestion on how to do
this? Is the solution to do loops instead of vectorisation? Please help me
Thanks in advance!

--
View this message in context: http://r.789695.n4.nabble.com/Error-message-object-of-type-closure-is-not-subsettable-tp3752886p3764513.html
Sent from the R help mailing list archive at Nabble.com.