如何获取“t”的值,以便我的函数“h(t)=epsilon”为固定的“epsilon”?

2024-02-24

继这个问题之后:

如果我已经生成了m=1000随机向量x_0均匀分布在随机矩阵 GOE 的球体和特征向量上:

#make this example reproducible
set.seed(101)
n <- 500
#Sample GOE random matrix
A <- matrix(rnorm(n*n, mean=0, sd=1), n, n) 
G <- (A + t(A))/sqrt(2*n)
ev <- eigen(G)
l <- ev$values
v <- ev$vectors

#sample 1000 x_0
#size of multivariate distribution
mean <- rep(0, n) 
var <- diag(n)

#simulate bivariate normal distribution
initial <- MASS::mvrnorm(n=1000, mu=mean, Sigma=var) #ten random vectors
#normalized the first possible initial value, the initial data uniformly distributed on the sphere
xmats <- lapply(1:1000, function(i) initial[i, ]/norm(initial[i, ], type="2"))

定义一个函数h_1(t):

这个函数的代码是这样的

# function
h1t <- function(t,x_0) {
  h10 <- c(x_0 %*% v[, n])
  denom <- vapply(t, function(.t) {
    sum((x_0 %*% v)^2 * exp(-4*(l - l[n]) * .t))
  }, numeric(1L))
  abs(h10) / sqrt(denom)
}

我想找到t_epsilon以便h(t_epsilon)=epsilon for epsilon=0.01.

EDIT:

接下来的答案:

find_t <- function(x, epsilon = 0.01, range = c(-50, 50)) {
  uniroot(function(t) h1t(t, x) - epsilon, range,
          tol = .Machine$double.eps)$root
}

res <- lapply(xmats, find_t)

但是,它显示错误

Error in uniroot(function(t) h1t(t, x) - epsilon, range, tol = .Machine$double.eps) : 
f() values at end points not of opposite sign

uniroot找到函数等于 0 的位置,因此您需要一个包装函数来从函数的输出中减去 epsilon:

find_t <- function(x, epsilon = 0.01, range = c(-50, 50)) {
  uniroot(function(t) h1t_modefied(t, x) - epsilon, range,
          tol = .Machine$double.eps)$root
}

We could在不同的矩阵上一次使用这个来查找输出等于 epsilon 的 t 值:

x_01t <- find_t(x_01)
x_01t
#> [1] -0.5149889

h1t_modefied(x_01t, x_01)
#> [1] 0.01

或者,更好的是,将所有矩阵放入list并通过简单的一次调用在所有这些函数上运行该函数lapply:

xmats <- list(x_01 = x_01, x_02 = x_02, x_03 = x_03, x_04 = x_04, x_05 = x_05)

res <- lapply(xmats, find_t)

res
#> $x_01
#> [1] -0.5149889
#> 
#> $x_02
#> [1] -0.2521749
#> 
#> $x_03
#> [1] -0.02756945
#> 
#> $x_04
#> [1] -0.4903002
#> 
#> $x_05
#> [1] -0.3473344

我们可以看到这些 t 值使您的hit_modefied函数输出epsilon通过将这些结果反馈回函数Map:

Map(h1t_modefied, t = res, x_0 = xmats)
#> $x_01
#> [1] 0.01
#> 
#> $x_02
#> [1] 0.01
#> 
#> $x_03
#> [1] 0.01
#> 
#> $x_04
#> [1] 0.01
#> 
#> $x_05
#> [1] 0.01
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取“t”的值,以便我的函数“h(t)=epsilon”为固定的“epsilon”? 的相关文章

随机推荐