0

I need to plot 2 different user defined function in the same R plot. I vectorize each of them:

Vectorize creates a function wrapper that vectorizes the action of its argument FUN. Vectorize(FUN, vectorize.args = arg.names, SIMPLIFY = TRUE,USE.NAMES = TRUE)

If I plot them separately I get the correct plot, however if I try to plot both functions in the same graph, it does not work.

Here is what I did:

1) first function:

payoff_call <- function(S, K){
  if(S < 0 | K < 0){
    return(print("The input S and K  must be > 0"))
    }else{
      return(max(S-K,0))
    }
  }

2) second function:

myBlackScholes <- function(S, K, tau, r, sigma, type = c("call", "put")) {
  if(S < 0 | K < 0 | tau < 0 | sigma < 0) {
    return(print("The input S , K , tau and sigma must be > 0"))
  } else
    {
    d1 <- (log(S/K) + (r + 0.5*sigma^2)*tau)/(sigma*sqrt(tau))
    d2 <- d1 - sigma*sqrt(tau)
    if(type == "call"){
      output <- cbind(
      V_BS_Call = S*pnorm(d1) - K*exp(-r*(tau))*pnorm(d2), #fair value call
      delta_call = pnorm(d1), #delta call
      vega_call = S*sqrt(tau)*dnorm(d1), #vega call
      theta_call = -S*dnorm(d1)*sigma/(2*sqrt(tau)) - r*K*exp(-r*tau)*pnorm(d2), #theta call
      rho_call = K*tau*exp(-r*tau)*pnorm(d2), #rho call
      kappa_call = -exp(-r*tau)*(pnorm(-d2)-1), #kappa call
      gamma_call = dnorm(d1)/(S*sigma*sqrt(tau)))#gamma call
      return(output) 
      } else if(type == "put"){
        output <- cbind(
        V_BS_Put = K*exp(-r*(tau))*pnorm(-d2) - S*pnorm(-d1), #fair value put
        delta_put = pnorm(d1)-1, #delta put
        vega_put =  S*sqrt(tau)*dnorm(d1), #vega put same as vega call
        theta_put = -S*dnorm(d1)*sigma/(2*sqrt(tau)) + r*K*exp(-r*tau)*pnorm(-d2), #theta put
        rho_put = -K*tau*exp(-r*tau)*pnorm(-d2), #rho put
        kappa_put = exp(-r*tau)*pnorm(-d2), #kappa put
        gamma_put = dnorm(d1)/(S*sigma*sqrt(tau))) #gamma put
        return(output) 
        } else{
            return(print("Wrong type in input"))
          }
    }
  }

3) I vectorize each function:

vect_payoff_call <- Vectorize(payoff_call)  
vect_myBlackScholes <- Vectorize(myBlackScholes)

4) I plot the 2 functions, for S starting at 0 to 100:

plot(x = 0:100, y = vect_payoff_call(0:100, 50),
     type="l", col="blue", lty = 1, lwd = 1,
     main = "Long Call Option Payoff function", xlab = "S", ylab = expression(f(S)))

plot(x = 0:100, y = vect_myBlackScholes(0:100,50, 1, 0.12, 0.3, "call")[1,], type="l", col="green", lty = 1, lwd = 1, add=TRUE)

The first plot is correct, but the second is not. Any suggestion?

1 Answer 1

1

Here is how. Note that I use ggplot2 in my example:

library(ggplot2)

x <- seq(0,2, by=0.1)
my_square <- function(x) x^2
my_cube <- function(x) x^3

my_data <- data.frame(argx = x, my_square = my_square(x),
                      my_cube = my_cube(x))

ggplot(my_data) +
  geom_point(aes(argx, my_square, color = 'x^2')) +
  geom_line(aes(argx, my_square, color = 'x^2')) +
  geom_point(aes(argx, my_cube, color = 'x^3')) +
  geom_line(aes(argx, my_cube, color = 'x^3')) +
  theme_bw() + 
  labs(x = 'x', y = 'y') +
  scale_color_manual(values = c('x^2' = 'red', 'x^3' = 'green'), name = 'function')

Output plot

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.