Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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中的同一绘图中绘制两个不同的用户定义函数_R_Function_Plot_Curve - Fatal编程技术网

如何在R中的同一绘图中绘制两个不同的用户定义函数

如何在R中的同一绘图中绘制两个不同的用户定义函数,r,function,plot,curve,R,Function,Plot,Curve,我需要在同一个R图中绘制2个不同的用户定义函数。 我将它们中的每一个矢量化: Vectorize创建一个函数包装器,用于将其参数的操作矢量化。VectorizeFUN,vectorize.args=arg.names,SIMPLIFY=TRUE,USE.names=TRUE 如果我分别绘制它们,我会得到正确的绘图,但是如果我尝试在同一个图形中绘制这两个函数,它将不起作用 以下是我所做的: 1第一个功能: payoff_call <- function(S, K){ if(S <

我需要在同一个R图中绘制2个不同的用户定义函数。 我将它们中的每一个矢量化:

Vectorize创建一个函数包装器,用于将其参数的操作矢量化。VectorizeFUN,vectorize.args=arg.names,SIMPLIFY=TRUE,USE.names=TRUE

如果我分别绘制它们,我会得到正确的绘图,但是如果我尝试在同一个图形中绘制这两个函数,它将不起作用

以下是我所做的:

1第一个功能:

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))
    }
  }
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"))
          }
    }
  }
vect_payoff_call <- Vectorize(payoff_call)  
vect_myBlackScholes <- Vectorize(myBlackScholes)
第一个图是正确的,但第二个图不是。
有什么建议吗?

以下是建议。请注意,我在示例中使用了ggplot2:

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')
输出

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')