【问题标题】:Integrate function of two variables with respect to one variable in R将两个变量的函数相对于 R 中的一个变量进行积分
【发布时间】:2021-08-14 12:17:36
【问题描述】:

我正在尝试将两个变量的函数与 R 中的一个变量进行积分,例如 \int_{0}^{1} f(x,y) dy。我已经对 f(x,y) 进行了编码,但我正在努力计算如何在 R 中计算 x 的所有值(x 也在 (0,1) 中)的积分。任何见解都会很好。

谢谢

编辑

这是我的代码(已经有建议的答案)

tdensity<-function(u,v,eta,rho){
s<-qt(u,df=eta)
r<-qt(v,df=eta)
numerador<-gamma((eta+2)/2)*gamma(eta/2)*((1+s^2/eta)*(1+r^2/eta))^((eta+1)/2)
denominador<-sqrt(1-rho^2)*(gamma((eta+1)/2))^2*(1+(s^2+r^2-2*rho*s*r)/(eta*(1-rho^2)))^((eta+2)/2)
return(numerador/denominador)
}

joedensity<-function(u,v,alpha){
w<-1-u
z<-1-v
dens<-(w^alpha+z^alpha-w*z^alpha)^(1/alpha-2)*w*z^(alpha-1)*(alpha-1+w^alpha+z^alpha-w*z^alpha)
return(dens)
}

pi<-function(u,v,theta) return((u*v)^theta)

cstarfun<-function(u,v,rho,theta,alpha,eta,K){
dens<-(1/K)*(pi(u,v,theta)*tdensity(u,v,eta,rho)+(1-pi(u,v,theta))*joedensity(u,v,alpha))
return(dens)
}

g<-Vectorize(cstarfun) # actually I don't know where to define the values for eta=4, rho=0.7, theta=2, alpha=3, K=2.5
w<-function(u) integrate(function(v) g, 0,1)$value
w<-Vectorize(w)
plot(w,from=0,to=1)

对 u 和 v 的唯一要求是它们在 (0,1) 中。再次感谢。

【问题讨论】:

    标签: r integral


    【解决方案1】:

    我会分两步完成。首先将x 的函数定义为该标量值处的积分,然后使用Vectorize 使该函数在向量上起作用。例如:

    f <- function(x, y) x^2 + y^2 + x*y
    
    # We'll assume that `f` accepts vector inputs and produces a vector   
    # output.  If not, run
    
    #  f <- Vectorize(f)
    
    # to put loops in place to accomplish that.
    
    # Then define the integral:
    
    fx <- function(x) integrate(function(y) f(x, y), 0, 1)$value
    
    # This is *not* vectorized, but we can vectorize it:
    
    fx <- Vectorize(fx)
    
    # and plot the result:
    
    plot(fx, from = 0, to = 1)
    

    reprex package (v2.0.0) 于 2021-08-14 创建

    编辑添加:在您对原始问题进行编辑后,我可以提供有关如何完成此操作的更多详细信息。我的理解是被积函数是cstarfun,你要在其中积分出v参数。如何定义fx函数有两种可能。

    如果您希望附加参数 rho,theta,alpha,eta,K 成为最终函数答案的参数,您可以将其定义为

    fx <- function(u, rho,theta,alpha,eta,K)
            integrate(function(v) cstarfun(v, rho,theta,alpha,eta,K),
                      0, 1)$value
    Vectorize(fx)
    

    那么你每次调用时都需要指定这 5 个参数。

    或者,您可以将它们设置为全局变量,并将 fx 定义为仅使用 u 作为参数,例如

    # This line is not necessary for your current definitions of the 
    # functions, but doesn't hurt much.  It will just slow things down
    # a bit.
    
    cstarfun <- Vectorize(cstarfun, "v")
    
    # Use your own choice of parameter values here!
    # These ones lead to numerical issues
    rho <- 0
    theta <- 1
    alpha <- 2
    eta <- 3
    K <- 4
    fx <- function(u)
      integrate(function(v) 
                  cstarfun(u, v, rho,theta,alpha,eta,K),
                0, 1)$value
    fx <- Vectorize(fx)
    

    【讨论】:

    • 您好,非常感谢您的回答。但是,我仍然收到错误...我的函数由 fun
    • 我将不得不要求标准的东西:请发布一个最小的可复制示例。
    • 我的错误,对不起。我现在编辑了原来的帖子
    猜你喜欢
    • 2016-05-09
    • 2014-02-12
    • 2018-03-22
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2021-09-26
    • 1970-01-01
    相关资源
    最近更新 更多