为 RQuantLib 锁定命名空间

namespace locked for RQuantLib

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

Error in registerNames(names, package, ".global", add) : The namespace for package "RQuantLib" is locked; no changes in the global variables list may be made.

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)

感谢您提出这个问题,很抱歉错过了几天。 GitHub 的发行票也可以。

您 post 的代码(可能引用自我的旧文档...)有问题。 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()的调用,这不能再在函数体中。所以让它成为 eg

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

其他人也一样。