在R中编码一个多重积分函数 [英] Coding a multiple integral function in R

查看:167
本文介绍了在R中编码一个多重积分函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是将以下内容转换为函数,我想知道如何用R代码编写以下双整数?:($ \ bar {x} = \ mu $):

With the goal of turning the following into a function, I was wondering how I can write the following double integral in terms of R codes?: ($\bar{x} = \mu$):

推荐答案

假设pi0pi1以向量化的方式实现函数$ \ pi_0 $和$ \ pi_1 $,则可能的解决方案是:

Assuming pi0 and pi1 implement your functions $\pi_0$ and $\pi_1$ in a vectorized way, a possible solution is:

integral <- function(n, mu, s, pi0, pi1) {

  C <- (2 * pi)^(-n/2) 
  C * integrate(f = function(sigmavec) sapply(sigmavec, function(sigma) {
    integrate(f = function(delta) {

      exp(-n/2 * ((mu / sigma - delta)^2 + (s / sigma)^2)) * pi1(delta)

    }, lower = -Inf, upper = Inf)$value

  }) * pi0(sigmavec) / (sigmavec^n), lower = 0, upper = Inf)$value

}

# Tests
integral(n = 1, mu = 0, s = 1, pi0 = dnorm, pi1 = dnorm)
# [1] 0.0473819
integral(n = 1, mu = 0, s = 1, pi0 = function(sigma) 1/sigma, pi1 = dcauchy)
# [1] 0.2615783

这篇关于在R中编码一个多重积分函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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