Newbie
2011-Aug-18 15:25 UTC
[R] Error message: object of type 'closure' is not subsettable
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)}> nlminb(start=list(kappa, rho, eta, theta, v0), objective = y, lower =lb, > upper =ub)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.
Berend Hasselman
2011-Aug-19 07:29 UTC
[R] Error message: object of type 'closure' is not subsettable
Newbie wrote:> > 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)} >> nlminb(start=list(kappa, rho, eta, theta, v0), objective = y, lower =lb, >> upper =ub) > 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 > > .............. > > 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) >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.
Newbie
2011-Aug-21 18:04 UTC
[R] Error message: object of type 'closure' is not subsettable
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.
Aparna Sampath
2012-Feb-28 08:23 UTC
[R] Error message: object of type 'closure' is not subsettable
Hi All I am trying to use the unlist() in R to a list variable. The following statements are within a function. { denominator <- sqrt(s1 / res.em1$n + s2 / res.em2$n) returnValue <- l2 / (denominator + 11) attr(returnValue,"numerator") <- l2 attr(returnValue,"denominator") <- denominator returnValue } And when I try to unlist the variable returnValue numerators <- unlist(testStatistics["numerator",]) denominators <- unlist(testStatistics["denominator",]) I get the following error:>Error in testStatistics["numerator", ] :object of type 'closure' is not subsettable I read some threads in R help on this error and they had asked to check if we are using the right datatype to the right function. But in my case it is pretty straightforward since I just list it in one function and try to unlist it later. Any suggestions? Thanks for the help :) -- View this message in context: http://r.789695.n4.nabble.com/Error-message-object-of-type-closure-is-not-subsettable-tp3752886p4427399.html Sent from the R help mailing list archive at Nabble.com.