'Julia BoundErrors

I'm new to Julia and I'm trying to convert a function I've written in R to Julia and I'm having some troubles.

So my function in R is

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

but when I tried to write in Julia

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)

I get an error. I think it's in the call for z[1] and z[2] but I'm not sure. Can anyone help me?

Many thanks



Solution 1:[1]

Your s is a matrix, so exp(s) calculates the matrix exponential, not the element-wise exponential. Also you have to use the dot for element-wise operations. I tried but I don't find the same results as R, I hope this helps nevertheless.

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stéphane Laurent