计算曲线下的面积 [英] Calculate the Area under a Curve
问题描述
我想计算曲线下的面积以进行积分,而无需定义诸如 integrate()
之类的函数.
I would like to calculate the area under a curve to do integration without defining a function such as in integrate()
.
我的数据如下所示:
Date Strike Volatility
2003-01-01 20 0.2
2003-01-01 30 0.3
2003-01-01 40 0.4
etc.
我绘制了 plot(strike,volatile)
以查看波动率微笑.有没有办法整合这个绘制的曲线"?
I plotted plot(strike, volatility)
to look at the volatility smile. Is there a way to integrate this plotted "curve"?
推荐答案
AUC 通过查看大量梯形图很容易近似,每次都在 x_i
, x_{ 之间i+1}
、y{i+1}
和 y_i
.使用zoo包的rollmean,可以做到:
The AUC is approximated pretty easily by looking at a lot of trapezium figures, each time bound between x_i
, x_{i+1}
, y{i+1}
and y_i
. Using the rollmean of the zoo package, you can do:
library(zoo)
x <- 1:10
y <- 3*x+25
id <- order(x)
AUC <- sum(diff(x[id])*rollmean(y[id],2))
确保您对 x 值进行排序,否则您的结果将毫无意义.如果沿 y 轴的某处有负值,则必须弄清楚如何准确定义曲线下的区域,并进行相应调整(例如使用 abs()
)
Make sure you order the x values, or your outcome won't make sense. If you have negative values somewhere along the y axis, you'd have to figure out how exactly you want to define the area under the curve, and adjust accordingly (e.g. using abs()
)
关于你的后续:如果你没有正式的函数,你会如何绘制它?因此,如果您只有值,那么您唯一可以近似的就是定积分.即使你有 R 中的函数,你也只能使用 integrate()
计算定积分.绘制形式函数只有在您也可以定义它的情况下才有可能.
Regarding your follow-up : if you don't have a formal function, how would you plot it? So if you only have values, the only thing you can approximate is a definite integral. Even if you have the function in R, you can only calculate definite integrals using integrate()
. Plotting the formal function is only possible if you can also define it.
这篇关于计算曲线下的面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!