r - Calculate the Area under a Curve -
i calculate area under curve integration without defining function such in integrate()
.
my data looks this:
date strike volatility 2003-01-01 20 0.2 2003-01-01 30 0.3 2003-01-01 40 0.4 etc.
i plotted plot(strike, volatility)
@ volatility smile. there way integrate plotted "curve"?
the auc approximated pretty looking @ lot of trapezium figures, each time bound between x_i
, x_{i+1}
, y{i+1}
, y_i
. using rollmean of zoo package, can do:
library(zoo) x <- 1:10 y <- 3*x+25 id <- order(x) auc <- sum(diff(x[id])*rollmean(y[id],2))
make sure order x values, or outcome won't make sense. if have negative values somewhere along y axis, you'd have figure out how want define area under curve, , adjust accordingly (e.g. using abs()
)
regarding follow-up : if don't have formal function, how plot it? if have values, thing can approximate definite integral. if have function in r, can calculate definite integrals using integrate()
. plotting formal function possible if can define it.
Comments
Post a Comment