R Hessian矩阵 [英] R Hessian Matrix

查看:375
本文介绍了R Hessian矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建函数的Hessian矩阵如下:

I need to create the Hessian matrix of a function given as:

func <- expression(sin(x+y)+cos(x-y))
vars <- c("x", "y")

二阶导数作为表达式,我需要评估它们很多次,所以我列出了一阶导数和二阶导数列表。

I need the second order derivatives as expressions too, and I need to evaluate them lot of times, so I made a list of first order derivatives, and a list of list of second order derivatives.

funcD <- lapply(vars, function(v) D(func, v))
funcDD <- list(); for (i in 1:length(vars)) funcDD[[i]] <- lapply(vars, function(v) D(funcD[[i]], v))

到目前为止,它有效。

> funcDD
[[1]]
[[1]][[1]]
-(sin(x + y) + cos(x - y))

[[1]][[2]]
-(sin(x + y) - cos(x - y))


[[2]]
[[2]][[1]]
cos(x - y) - sin(x + y)

[[2]][[2]]
-(cos(x - y) + sin(x + y))

现在的问题:
如何创建一个包含评估表达式的值的矩阵?
尝试外部,没有工作。

Now the questions: How can I create a matrix containing values of the evaluated expressions? Tried outer, didn't work.

> h <- outer(c(1:length(vars)), c(1:length(vars)), function(r, c) eval(funcDD[[r]][[c]], envir = list(x = 1, y = 2)))
Error in funcDD[[r]] : subscript out of bounds

其他问题:
有更优雅的方式存储二阶导数表达式吗?例如,可以将表达式存储在矩阵中而不是列表列表中?

Other question: Is there a more elegant way to store the second order derivative expressions? For instance is it possible to store expressions in a matrix instead of lists of lists?

第三个问题:
是否可以得到表达式变量的向量?以上我使用我作为输入手动输入的vars< - c(x,y)是否需要或有get_variables类似的方法?

Third question: Is it possible to get a vector of variables of an expression? Above I used vars <- c("x", "y") which I entered as input manually, is it necessary or is there a "get_variables"-like method?

推荐答案

问题二的答案是大部分是,它提供了几乎立即回答你的问题:

The answer to question two is ,"mostly yes" and it offers an almost immediate answer to your question:

funcD <- sapply(vars, function(v) D(func, v))
funcDD <- matrix(list(), 2,2)
for (i in 1:length(vars)) 
        funcDD[,i] <- sapply(vars, function(v) D(funcD[[i]], v))
funcDD
#---------
     [,1]       [,2]      
[1,] Expression Expression
[2,] Expression Expression
> funcDD[1,1]
[[1]]
-(sin(x + y) + cos(x - y))

主要的资格是,需要使用列表而不是表达式作为矩阵所持有的对象类型。表达式并不真正符合原子对象的要求,您可以轻松地提取该值并将其用作调用,这可能比将其作为表达式更方便:

The "mostly" qualification is that one needs to use "list" rather than "expression" as the object type that the matrix is holding. Expressions don't really qualify as atomic objects, and you could easily extract the value and use it as a call, which might even be more convenient than having it as an expression:

> is.expression(funcDD[1,1])
[1] FALSE
> funcDD[1,1][[1]]
-(sin(x + y) + cos(x - y))
> class(funcDD[1,1][[1]])
[1] "call"



结果是所需要的是相同的结构,所以这个调用每个矩阵元素与评估环境相同的特定向量,并将它们全部作为矩阵返回:

Turns out what was wanted was the same structure, so this calls each matrix element with the same specific vector as the evaluation environment and returns them all as a matrix.:

matrix(sapply(funcDD, eval, env=list(x=0, y=pi)), length(vars))
#---------
     [,1] [,2]
[1,]    1   -1
[2,]   -1    1

这篇关于R Hessian矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆