为RQuantLib锁定的名称空间

时间:2018-12-29 15:58:17

标签: r quantlib

在RStudio / Windows上运行。最后一行失败:

  

registerNames(名称,软件包“。全局”,添加)中的错误:   程序包“ RQuantLib”的名称空间已锁定;全球没有变化   可以列出变量。

library(RQuantLib)

myStrike <- 1240
myPrice <- 1410

volVec <- c(0.1, 0.2, 0.3, 0.4, 0.5)
priceVec <- c(1500,1400,1300,1200,1100)
myType <- "put"
rfRate <- 0.02
maturity <- 360/360
vol <- 0.3


EO = EuropeanOption(type = myType,price = myPrice,strike = myStrike, dividendYield = 0,riskFreeRate = rfRate, maturity = maturity,volatility = vol )
EOres = EuropeanOptionArrays(type = myType,price = priceVec,strike = myStrike, dividendYield = 0,riskFreeRate = rfRate, maturity = maturity,volatility = volVec )
summary(EO)
plotOptionSurface(EOres)

1 个答案:

答案 0 :(得分:2)

感谢您提出此问题,并为几天之久未得到答复表示歉意。 GitHub上的问题单也可以。

您发布的代码(可能引用了我的旧文档...)是错误的。 EuropeanOption()EuropeanOptionArrays()的第二个参数现在是underlying,而不是price。所以我们做这个

library(RQuantLib)

myStrike <- 1240
myPrice <- 1410

volVec <- c(0.1, 0.2, 0.3, 0.4, 0.5)
priceVec <- c(1500,1400,1300,1200,1100)
myType <- "put"
rfRate <- 0.02
maturity <- 360/360
vol <- 0.3


EO <- EuropeanOption(type = myType, underlying = myPrice,strike = myStrike,
                     dividendYield = 0, riskFreeRate = rfRate, maturity = maturity,
                     volatility = vol )
EOres <- EuropeanOptionArrays(type = myType, underlying = priceVec,strike = myStrike, 
                              dividendYield = 0,riskFreeRate = rfRate, maturity = maturity,
                              volatility = volVec )
summary(EO)
plotOptionSurface(EOres)

接下来,在函数plotOptionSurface()中,我们需要注释掉对globalVariables()的调用,该调用不再可以存在于函数主体中。因此,例如

pos <- function(EOres, ylabel="", xlabel="", zlabel="", fov=60) {
    if (requireNamespace("rgl", quietly=TRUE)) {
        #utils::globalVariables(c("clear3d", "bg3d", "ligh3d", "rgl.viewpoint", "rgl.surface", "tgl.texts"))
        axis.col <- "black"
        text.col <- axis.col
        ylab <- ylabel
        xlab <- xlabel
        zlab <- zlabel
        y <- EOres

        ## clear scene:
        rgl::clear3d()
        rgl::clear3d(type="bbox")
        rgl::clear3d(type="lights")

        ## setup env:
        rgl::bg3d(color="#DDDDDD")
        rgl::light3d()

        rgl::rgl.viewpoint(fov=fov)

        x <- 1:nrow(y)
        z <- 1:ncol(y)
        x <- (x-min(x))/(max(x)-min(x))
        y <- (y-min(y))/(max(y)-min(y))
        z <- (z-min(z))/(max(z)-min(z))
        rgl::rgl.surface(x, z, y, alpha=0.6, lit=TRUE, color="blue")
        rgl::rgl.lines(c(0,1), c(0,0), c(0,0), col=axis.col)
        rgl::rgl.lines(c(0,0), c(0,1), c(0,0), col=axis.col)
        rgl::rgl.lines(c(0,0),c(0,0), c(0,1), col=axis.col)
        rgl::rgl.texts(1,0,0, xlab, adj=1, col=text.col)
        rgl::rgl.texts(0,1,0, ylab, adj=1, col=text.col)
        rgl::rgl.texts(0,0,1, zlab, adj=1, col=text.col)

        ## add grid (credit's to John Fox scatter3d)
        xgridind <- round(seq(1, nrow(y), length=25))
        zgridind <- round(seq(1, ncol(y), length=25))
        rgl::rgl.surface(x[xgridind], z[zgridind], y[xgridind,zgridind],
                         color="darkgray", alpha=0.5, lit=TRUE,
                         front="lines", back="lines")

        ## animate (credit to rgl.viewpoint() example)
        start <- proc.time()[3]
        while ((i <- 36*(proc.time()[3]-start)) < 360) {
            rgl::rgl.viewpoint(i,i/8);
        }
    } else {
        message("Please install the 'rgl' package before using this function.")
    }
}

现在我们可以使用结果列表的元素进行调用,例如

pos(EOres[[1]])  # value

和其他同上。