-1

Having trouble setting up these integral terms in R. I created "term1" to more easily insert it into the integral code, but I keep getting various error messages after trying different codes. any help or spotting the issue would be appreciated.

## 
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)))}

These are the errors i am getting

> Nc(term1)
Error in get(as.character(FUN), mode = "function", envir = envir) : 
object 'term1' of mode 'function' was not found 

> Nc()
Error in match.fun(f) : argument "term1" is missing, with no default 

1 Answers1

-2

Well, the big question is: Have you read the documentation for function integrate ?

The first argument you assign to that function has to be a function.

The second problem is, that function Nc, which you have defined, does not return any output (you can fix that by not assigning the result of integrate to an object and instead just returning it. Same goes for the Np. One more problem with Np is, that it actually returns an error "unexpected argument to unary operator"

Anyway, here is the code with some changes:

## 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))}
ira
  • 2,542
  • 2
  • 22
  • 36
  • That being said, I haven't verified whether your calculations are correct and whether the way I have set up term1 is correct. But it should show you a way how to use the function `integrate`. Also, the problem with Np is caused by the minus sign... – ira Nov 09 '16 at 18:49
  • Can the down voter please provide some feedback on the down vote? Like.. what exactly is supposedly the problem with the code? So that I can fix it if there is any... – ira Nov 09 '16 at 18:57
  • the code worked for me, but i apparently don't have the reputation to up vote it. Sorry, but thanks for your help – Chason Reynolds Nov 09 '16 at 20:10
  • If it solved your problem, you can accept it as an answer – ira Nov 09 '16 at 20:21