Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R match.fun(f)中出错:参数;“第1条”;缺少,没有默认值_R_Arguments_Default_Integrate - Fatal编程技术网

R match.fun(f)中出错:参数;“第1条”;缺少,没有默认值

R match.fun(f)中出错:参数;“第1条”;缺少,没有默认值,r,arguments,default,integrate,R,Arguments,Default,Integrate,在R中设置这些积分项有困难。我创建了“term1”以更容易地将其插入积分代码中,但在尝试不同的代码后,我不断收到各种错误消息。任何帮助或发现问题都将不胜感激 ## S<-readline(prompt="Enter underlying instrument price:") X<-readline(prompt="Enter strike price:") V<-readline(prompt="Enter absolute volatility in dollars:

在R中设置这些积分项有困难。我创建了“term1”以更容易地将其插入积分代码中,但在尝试不同的代码后,我不断收到各种错误消息。任何帮助或发现问题都将不胜感激

## 
S<-readline(prompt="Enter underlying instrument price:") 
X<-readline(prompt="Enter strike price:") 
V<-readline(prompt="Enter absolute volatility in dollars:") 
r<-readline(prompt="Enter risk-free rate (%):") 
q<-readline(prompt="Enter dividend yield (%):") 
T<-readline(prompt="Enter time to maturity, in fraction of years:") 
t=0 

## 
S<-as.numeric(S) 
X<-as.numeric(X) 
V<-as.numeric(V) 
r<-as.numeric(r)/100 
q<-as.numeric(q)/100 
T<-as.numeric(T) 

##Bond Price 
B<-exp(r*(T-t))

##Volatility
vol<-function(start,end,rate,yield,B) {
if(r==q){
V*(sqrt((B-1)/(2*(r-q))))
}
else{ 
V*(sqrt(T-t))
}
}

##d
d<-(S*B-X)/vol()

##N(d)
term1<-(exp(-(r^2)/2)/sqrt(2*pi))

#Call
Nc<-function(term1){
Nc<-((integrate(term1,-Inf,d)))}

#Put
Np<-function(term1){
Np<-(-(integrate(term1,-Inf,d)))}

好的,大问题是:你读过函数
integrate
的文档了吗

指定给该函数的第一个参数必须是函数

第二个问题是,您定义的函数Nc不返回任何输出(您可以通过不将
integrate
的结果分配给对象,而只是返回它来解决这个问题。Np也是如此。Np的另一个问题是,它实际上返回了一个错误“一元运算符的意外参数”

不管怎样,下面是一些更改的代码:

## Assign some "random" numbers to the variables, so that I can run the script
S<-100
X<-110
V<-3
r<-10
q<-15
# Is it a good idea to have variable called T? T stands also for TRUE
T <- 10
t=0 

## 
S<-as.numeric(S) 
X<-as.numeric(X) 
V<-as.numeric(V) 
r<-as.numeric(r)/100 
q<-as.numeric(q)/100 
T<-as.numeric(T) 

##Bond Price 
B<-exp(r*(T-t))

##Volatility
vol<-function(start,end,rate,yield,B) {
        if(r==q){
                V*(sqrt((B-1)/(2*(r-q))))
        }
        else{ 
                V*(sqrt(T-t))
        }
}

##d
d<-(S*B-X)/vol()

# make term1 a function of r, so that there is actually something to integrate...
term1<-function(r) {(exp(-(r^2)/2)/sqrt(2*pi))}

# Do not assign the result of integration to an object, otherwise there will be no output from the function
Nc<-function(term1){
        ((integrate(term1,-Inf,d)))}

# In order to use the minus sign, you have to extract the value from the result of function "integrate"
Np<-function(term1){
        (-(integrate(term1,-Inf,d)$value))}
##为变量指定一些“随机”数字,以便我可以运行脚本

扫描我得到一个
S
B
X
r
,和一个
d
,Pat?例如,用户输入值S=100,X=100,r=0.05,然后根据这些值计算B和d。请停止在注释中添加代码。改为编辑问题。抱歉,这是我第一次问问题,我不知道您需要enti重新编写代码来修复两个积分函数,我不知道你可以编辑这个问题。谢谢你的编辑。这不是“需要全部代码”但更多的是给出一个小例子,有人可以帮你解决问题。尽管如此,我还没有验证你的计算是否正确,以及我设置term1的方式是否正确。但它应该向你展示一种如何使用函数
integrate
。此外,Np的问题是由减号引起的……这个函数可以关闭吗投票者请提供一些关于否决票的反馈?比如..代码到底有什么问题?这样我就可以在有问题的时候修复它…代码对我有效,但我显然没有声誉去支持它。对不起,但是谢谢你的帮助如果它解决了你的问题,你可以接受它作为一个答案
## Assign some "random" numbers to the variables, so that I can run the script
S<-100
X<-110
V<-3
r<-10
q<-15
# Is it a good idea to have variable called T? T stands also for TRUE
T <- 10
t=0 

## 
S<-as.numeric(S) 
X<-as.numeric(X) 
V<-as.numeric(V) 
r<-as.numeric(r)/100 
q<-as.numeric(q)/100 
T<-as.numeric(T) 

##Bond Price 
B<-exp(r*(T-t))

##Volatility
vol<-function(start,end,rate,yield,B) {
        if(r==q){
                V*(sqrt((B-1)/(2*(r-q))))
        }
        else{ 
                V*(sqrt(T-t))
        }
}

##d
d<-(S*B-X)/vol()

# make term1 a function of r, so that there is actually something to integrate...
term1<-function(r) {(exp(-(r^2)/2)/sqrt(2*pi))}

# Do not assign the result of integration to an object, otherwise there will be no output from the function
Nc<-function(term1){
        ((integrate(term1,-Inf,d)))}

# In order to use the minus sign, you have to extract the value from the result of function "integrate"
Np<-function(term1){
        (-(integrate(term1,-Inf,d)$value))}