我是朱莉娅的新手,我正试图把我用R写成的函数转换成Julia,我遇到了一些麻烦。
所以我在R中的函数是
jgaus<-function(s,thetaW){
z<-qnorm(1-exp(-s))
if(any(z==Inf)){
z<- -qnorm(exp(-s))
}
inside<--(1/(2*(1-thetaW^2)))*(z[1]^2-2*thetaW*z[1]*z[2]+z[2]^2)
expinside<-exp(inside)
dens<-(1/(2*pi*sqrt(1-thetaW^2)))*expinside*prod(exp(-s)/dnorm(z))
return(dens)
}
jgaus(rbind(c(0.4,0.3),c(0.5,0.6)),0.4)
# z matrix
[,1] [,2]
[1,] -0.4407971 -0.6458700
[2,] -0.2702880 -0.1226595
# dens values: the first is for the vector (-0.44,-0.65) and the second for (-0.27,-0.12)
[1] 0.902662
[1] 1.451246但当我试着用朱莉娅写的时候
function jgaus(s,thetaW)
z = quantile(Normal(0,1), 1-exp(-s))
if any(z==Inf)
z = -quantile(Normal(0,1), exp(-s))
end
inside = -(1/(2*(1-thetaW^2)))*(z[1]^2-2*thetaW*z[1]*z[2]+z[2]^2)
expinside = exp(inside)
(1/(2*pi*sqrt(1-thetaW^2)))*expinside*prod(exp(-s)/pdf(Normal(0,1), z))
end
jgaus.([0.4 0.3; 0.5 0.6],0.4)我犯了个错误。我想这是在呼唤z1和z2,但我不确定。有谁可以帮我?
非常感谢
发布于 2022-02-26 15:38:56
您的s是一个矩阵,所以exp(s)计算矩阵指数,而不是按元素进行指数计算.此外,您还必须使用点对元素进行操作。我试过了,但我没有找到和R一样的结果,我希望这会有所帮助。
using Random, Distributions
function jgaus(s, thetaW)
z = map(p -> quantile.(Normal(0,1), 1 .- exp.(-p)), s)
if any(z==Inf)
z = -map(p -> quantile.(Normal(0,1), exp.(-p)), s)
end
inside = -(1/(2*(1-thetaW^2)))*(z[1].^2-2*thetaW*z[1].*z[2]+z[2].^2)
expinside = exp.(inside)
exp_minus_s = map(x -> exp.(-x), s)
pdf_z = map(x -> pdf.(Normal(0,1), x), z)
(1/(2*pi*sqrt(1-thetaW^2)))*expinside.*prod.(exp_minus_s ./ pdf_z)
end
jgaus([[0.4; 0.3], [0.5; 0.6]], 0.4)https://stackoverflow.com/questions/71277112
复制相似问题