Search code examples
randomconditional-statementsjuliananmontecarlo

using isnan in julia doesn't catch all the NaNs


NaNs keep getting introduced into my results for some reason, despite using isnan to filter them.

This code generates NaNs in the X variable sometimes but not always, and I'm confused as to how it's not getting filtered out. See the comments # in the code.

X = zeros(size(prices)[2]) #X is an 8x1 Vector{Float64}

nonan = 0
for p in 1:averaging_iterations #averaging iterations = 50
    nonan += 1
    sim = simulate(prices,ideal_transform,picks,s_LEN,chosen_dists,prediction_length,cops); #generates an 8x1 Vector{Float64}
    (sum(map(isinf,sim)) + sum(map(isnan,sim))) == 0 ? X .+= sim : nonan -= 1 #I expect this to only add to X if there are no NaNs or Infs
end
X ./= max(nonan,1)

The underlying code in simulate() is fairly involved, but I can post it if that helps. I don't know where the NaNs get generated in the first place. Any help would be appreciated.


Solution

  • the problem you're running into is that the sum of finite numbers can be NaN. this can occur if there are partial sums that sum to both positive and negative inf.