我正在尝试使用 R 的 splinefun {stat}s 包中的 spline() 函数在 matlab 中复制 spline() 函数,但没有完全访问 matlab(我没有它的许可证)。我能够将所有必要的数据输入到 R 中,这些数据将出现在 matlab 中,但我的样条输出与 matlab 的输出平均相差 0.0036(maxdif 为 .0342,mindif 为 -.0056,stdev 为 .0094) . 我的主要问题是,matlab 的公式与 R 的公式相比如何,我的计算差异可能来自哪里?
我的代码的第一部分是将 excel 电子表格输入 R,然后计算必要的变量以获得 tau 和快速增量。在此之后,我运行样条计算,然后旋转输出以便导出回 excel。下面是基本脚本,加上一些数据来尝试看看我的计算是否存在缺陷。我使用样条曲线(自然),因为它返回最接近 matlab 模型的值。
#establishing what tau is for quick Delta calculation
today<-Sys.Date()
month<-as.Date(5/1/2016)
difday<-difftime(month,today,units=c("days"))
Tau<-as.numeric((month-today)/365)
Pu<-as.numeric(1.94)
Vol<-as.numeric(.4261)
#Pf is the representation of my fixed strike prices, the points used for interpolation
Pf<-c(Pu-.3,Pu-.25,Pu-.2,Pu-.1,Pu,Pu+.1,Pu+.2,Pu+.25,Pu+.3)
qDtable<-data.frame(matrix(ncol=length(Pf),nrow=length(month)))
colnames(qDtable)<-c(Pf)
rownames(qDtable)<-format.Date(month)
#my quick Delta calculation & table as a result
qD<-data.frame(pnorm(log(Pf/Pu)/(Vol*sqrt(Tau))))
Qd<-t(qD[1:24,1])
qDtable[1,]=c(Qd)
#setting up for spline interpolation
qDpoint<-as.numeric(qDtable[1,1:24])
ncsibyPf<-data.frame(matrix(ncol=length(Pf),nrow=length(month)))
colnames(ncsibyPf)<-Pf
rownames(ncsibyPf)<-format.Date(month)
qDvol<-data.frame(matrix(ncol=14,nrow=2)
colnames(qDvol)<-c("",0,.05,.1,.2,.3,.4,.5,.6,.7,.8,.9,.95,1)
rownames(qDvol)<-format.Date(month)
qDvol[2,2:14]<-c(.59612,.51112,.46112,.45612,.44612,.42612,.42612,.42612,.42612,.42612,.42612,.42612,.42612)
#x is the quick Vol point
x<-as.numeric(qDvol[1,2:14])
#y is the vol at the quick Vol point
y<-as.numeric(qDvol[2,2:14])
ncsivol<-data.frame(spline(x,y,xout=qDpoint,method="natural"))
nroutput<-t(ncsivol[1:24,2])
ncsibyPf[1,]=c(nroutput)
这个样条运行的基本数据点都包括在内(我认为),一切都应该正确排列。提前感谢您的帮助!