3D平滑圖
df2<- read.xlsx("/Users/changshuowen/111_1/Course/Fixed Income Securities and Derivatives/HW10/123/data/output.xlsx")
tenor<- df2[,2]
K<- df2[,3]
vol<- df2[,4]
surface<- interp(tenor, K, vol)
expiry<- surface$x
strike<- surface$y
volatility<- surface$z
options(rgl.printRglwidget = TRUE)
nbcol = 100
color = rev(rainbow(nbcol, start = 0/6, end = 4/6))
zcol = cut(volatility, nbcol)
persp3d(expiry, strike, volatility, ticktype = "detailed", col = color[zcol])
points3d(tenor, K, vol, col="yellow")
3D散佈圖
df<- read.xlsx("/Users/changshuowen/111_1/Course/Fixed Income Securities and Derivatives/HW10/123/data/output.xlsx")
df2<- read.xlsx("/Users/changshuowen/111_1/Course/Fixed Income Securities and Derivatives/HW10/123/data/USDJPY_Ksig.xlsx")
delta<- colnames(df2[-1])
n_d<- length(delta)
tenor<- df2$Exp[-which(df2$Exp == " ")]
n_t<- length(tenor)
# data cleaning
tenor_final<- tenor[rep(1:n_t, n_d) %>% sort()]
tenor_count<- rep(1:n_t, n_d) %>% sort()
delta_final<- delta[rep(1:n_d, n_t)]
K_final<- c()
sigma_final<- c()
for (i in 1:n_t){
K_final<- c(K_final, df2[2*i,-1] %>% as.numeric())
sigma_final<- c(sigma_final, df2[2*i-1,-1] %>% as.numeric())
}
df_final<- data.frame(delta = delta_final, Tenor = tenor_final, tenor = df$tenor, K = K_final, sigma = sigma_final)
p <- plot_ly(
df_final, x = ~df$tenor, y = ~K_final, z = ~sigma_final,
color = ~tenor_final, name = tenor_final, line = list(width = 6),
sort = FALSE) %>%
add_markers(marker = list(size = 5), showlegend = F) %>%
layout(scene = list(xaxis = list(title = "Tenor"),
yaxis = list(title = 'K'),
zaxis = list(title = "sigma")))
p