Giter VIP home page Giter VIP logo

Comments (7)

stevengj avatar stevengj commented on August 29, 2024

Apparently your integrand returned a NaN? Have you checked that your integrands work in the intervals you specify?

(I should also note that for 2d integrals you are usually better off with a 2d integration routine like HCubature.jl than nesting 1d adaptive quadratures.)

from quadgk.jl.

aaabdulhaq avatar aaabdulhaq commented on August 29, 2024

Thank you for your response. Yes, the integral converges on the ranges as I've computed the same result in Mathematica. I don't know how to implement 2d integration in HCubature as they require all integrals' ranges to be in (0,1). In my case, the integrals are dependent and indefinite.

from quadgk.jl.

stevengj avatar stevengj commented on August 29, 2024

Yes, the integral converges on the ranges as I've computed the same result in Mathematica.

The error message indicates that your integrand function is returning NaN for some values of the argument. This may indicate a bug in your code.

I don't know how to implement 2d integration in HCubature as they require all integrals' ranges to be in (0,1). In my case, the integrals are dependent and indefinite.

You use a change of variables as described here: https://github.com/stevengj/cubature#infinite-intervals

(This is what QuadGK is doing internally.)

from quadgk.jl.

aaabdulhaq avatar aaabdulhaq commented on August 29, 2024

I followed you but the problem is still there! Following is the simplest code with change of variable implemented.

using Statistics,Distributions,Distributed,StatsFuns,Roots,QuadGK
p=3;d=0.25;h=13.84283;n1=1;n2=4;n=n1+n2;

function fz1(z)
function fv1(v)
lam=(n1/n2)*(((z+sqrt(n1)*d)^2)+v/(1-v))+(2*n*sqrt(n1)*d*z)/(n2)+((n*d)^2)/(n2);
return(nchisqcdf(p,lam,(n*h)/n2)*chisqpdf(p-1,v/(1-v))*(1/((1-v)^2)));end;
return(quadgk(fv1,0,1,rtol=1e-10)[1]);end;

fz1(0) # okay
fz1(-1)   # problem
fz1(-2)   # problem
fz1(-3)   # okay

julia> fz1(-3)
0.9964662237851157

julia> fz1(-2)
ERROR: DomainError with 0.5:
integrand produced NaN in the interval (0.0, 1.0)
Stacktrace:
 [1] evalrule(::getfield(Main, Symbol("#fv1#3")){Int64}, ::Float64, ::Float64, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::typeof(LinearAlgebra.norm)) at C:\Users\AH\.julia\packages\QuadGK\fTRFu\src\QuadGK.jl:81
 [2] do_quadgk(::getfield(Main, Symbol("#fv1#3")){Int64}, ::Array{Float64,1}, ::Int64, ::Type{Float64}, ::Float64, ::Float64, ::Int64, ::typeof(LinearAlgebra.norm)) at C:\Users\AH\.julia\packages\QuadGK\fTRFu\src\QuadGK.jl:124
 [3] #quadgk at C:\Users\AH\.julia\packages\QuadGK\fTRFu\src\QuadGK.jl:170 [inlined]
 [4] #quadgk#15 at C:\Users\AH\.julia\packages\QuadGK\fTRFu\src\QuadGK.jl:244 [inlined]
 [5] #quadgk at .\none:0 [inlined]
 [6] fz1(::Int64) at d:\julia\int.jl:57
 [7] top-level scope at none:0

julia> fz1(-1)
ERROR: DomainError with 0.5:
integrand produced NaN in the interval (0.0, 1.0)
Stacktrace:
 [1] evalrule(::getfield(Main, Symbol("#fv1#3")){Int64}, ::Float64, ::Float64, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::typeof(LinearAlgebra.norm)) at C:\Users\AH\.julia\packages\QuadGK\fTRFu\src\QuadGK.jl:81
 [2] do_quadgk(::getfield(Main, Symbol("#fv1#3")){Int64}, ::Array{Float64,1}, ::Int64, ::Type{Float64}, ::Float64, ::Float64, ::Int64, ::typeof(LinearAlgebra.norm)) at C:\Users\AH\.julia\packages\QuadGK\fTRFu\src\QuadGK.jl:124
 [3] #quadgk at C:\Users\AH\.julia\packages\QuadGK\fTRFu\src\QuadGK.jl:170 [inlined]
 [4] #quadgk#15 at C:\Users\AH\.julia\packages\QuadGK\fTRFu\src\QuadGK.jl:244 [inlined]
 [5] #quadgk at .\none:0 [inlined]

julia> fz1(-3)
0.9964662237851157

julia> fz1(-0)
0.9964662237851157

from quadgk.jl.

aaabdulhaq avatar aaabdulhaq commented on August 29, 2024

Now I compare these results with Mathematica code:

In[4]:= p = 3; n1 = 1; n2 = 4; n = n1 + n2; d = 0.25; h = 13.84;

In[5]:= INT1[
   z_?NumericQ] := (Return[
     NIntegrate[
      CDF[NoncentralChiSquareDistribution[p, 
         n1/n2*((z + Sqrt[n1]*d)^2 + v) + (2.*n*Sqrt[n1]*d*z)/
          n2 + (n*d)^2/n2], (n*h)/n2]*
       PDF[ChiSquareDistribution[p - 1], v][[1]][[1]][[1]], {v, 
       0., \[Infinity]}]];);

In[6]:= {INT1[-3], INT1[-2], INT1[-1], INT1[0]}

Out[6]= {0.996462, 0.998166, 0.998166, 0.996462}

from quadgk.jl.

aaabdulhaq avatar aaabdulhaq commented on August 29, 2024

The results are of course okay for the arguments -3 and 0; but, for others, Julia refuses to answer and reports NaN. But, Mathematica reports the answer.

from quadgk.jl.

stevengj avatar stevengj commented on August 29, 2024

The problem is that your integrand is returning NaN for e.g. z=-1 and v=0.1, exactly as it says:

using StatsFuns
p=3;d=0.25;h=13.84283;n1=1;n2=4;n=n1+n2;
function fv(z, v)
    lam=(n1/n2)*(((z+sqrt(n1)*d)^2)+v/(1-v))+(2*n*sqrt(n1)*d*z)/(n2)+((n*d)^2)/(n2)
    return(nchisqcdf(p,lam,(n*h)/n2)*chisqpdf(p-1,v/(1-v))*(1/((1-v)^2)))
end

gives

julia> fv(-1, 0.1)
NaN

in particular, the nchisqcdf function is returning NaN. With the help of a couple of @show statements, I can see that you are evaluating:

julia> nchisqcdf(3, -0.06597222222222221, 17.303537499999997)
NaN

It looks like nchisqcdf doesn't like any negative value for the second argument. If you think this is wrong, you could file an issue with the StatsFuns.jl package.

However, in JuliaStats/StatsFuns.jl#19 it was stated that you are not supposed to call nchisqcdf directly (it is undocumented), and are supposed to use Distributions.jl instead.

from quadgk.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.