在R中绘制一个困难的功能,并通过一分解找到根

发布于 2025-02-07 10:07:56 字数 597 浏览 1 评论 0原文

eq <- function(x){ sum(exp(-Ln.M_Return[,1]/x)) / 168 - 1 } 
# Ln.M_Return[,1] is a vector of stock return, and its length is 168.

我的第一个目标是绘制此功能的情节。但是,这里总是有一个错误。 当我使用“曲线”函数时,

curve(eq(x))

它将显示一个错误消息:

Error in curve(eq(x)) : 'expr'
In addition: Warning message:
In -Ln.M_Return[, 1]/x :
  longer object length is not a multiple of shorter object length

我尝试了其他一些方法,例如plot()和xyplot(),但没有任何更改。

我的主要目的实际上是使用二次方法来找到该函数的根,这是Aumann-Serlano风险指数的公式。 因此,我选择首先绘制图并近似根的位置。 然后,我将使用一些分分码来找到根。 感谢您耐心地阅读我的问题!

eq <- function(x){ sum(exp(-Ln.M_Return[,1]/x)) / 168 - 1 } 
# Ln.M_Return[,1] is a vector of stock return, and its length is 168.

My first goal is to draw the plot for this function. However, there is always an error here.
When I use "curve" function,

curve(eq(x))

it will appear an error message:

Error in curve(eq(x)) : 'expr'
In addition: Warning message:
In -Ln.M_Return[, 1]/x :
  longer object length is not a multiple of shorter object length

I have tried some other methods, like plot() and xyplot(), but nothing changed.

My main purpose is actually using bisection method to find the root of the function, which is the formula of Aumann-Serrano riskiness index.
Thus, I choose to draw the plot first and approximate the location of the root.
Then I will use some codes of bisection to find the root.
Thanks for reading my question patiently!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落星ぅ辰 2025-02-14 10:07:56

没有您的ln.m_return,我创建了一些随机数据。您应该在curve函数中vectorize您的方程式如下:

Ln.M_Return <- runif(168,0,10)
Ln.M_Return <- as.data.frame(Ln.M_Return)

eq <- function(x){ sum(exp(-Ln.M_Return[,1]/x)) / 168 - 1 } 

g <- Vectorize(eq)

curve(g, from=1, to=100, , xlab="xvalue", ylab="yvalue", 
      col="blue", lwd=2)

output:

“在此处输入图像说明”

Without having your Ln.M_Return, I created some random data. You should Vectorize your equation in the curve function like this:

Ln.M_Return <- runif(168,0,10)
Ln.M_Return <- as.data.frame(Ln.M_Return)

eq <- function(x){ sum(exp(-Ln.M_Return[,1]/x)) / 168 - 1 } 

g <- Vectorize(eq)

curve(g, from=1, to=100, , xlab="xvalue", ylab="yvalue", 
      col="blue", lwd=2)

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文