Reputation: 59
I want to plot 3D surface of pre-defined function which include switch statement.If I run my code, "longer object length is not a multiple of shorter object length" and "Error in get(as.character(FUN), mode = "function", envir = envir) : object 'f' of mode 'function' was not found" these two are printed without result I intended. Simplified code is as below. Anyone can fix this problem?
BlackScholes<-function(S, K, r, q, vol, T, result){
switch(result,
callprice = S+k+T,
putprice = K+T,
calldelta = exp(-q * T),
putdelta = exp(-q * T)
}
x<-seq(1950000,2700000,by = 5000)
y<-seq(0,30,by = 1)
f<-BlackScholes(x,220000,0.014,0,0.2,y,"calldelta")
z<-outer(x,y,f)
persp(x,y,z)
Upvotes: 0
Views: 98
Reputation: 424
When you save your function call in f
, you are saving the result of the function, not the function call itself. In order to use your function as the FUN
parameter in outer()
, you should pass the named parameters of BlackScholes()
as extra arguments to outer()
.
z <- outer(
x,
y,
FUN = 'BlackScholes',
K = 220000,
r = 0.014,
q = 0,
vol = 0.2,
result = 'callprice'
)
persp(x,y,z)
Also, you should probably refrain from naming any variables T
, since it's R shorthand for the logical TRUE
.
Upvotes: 1