Hello, good morning,
I had a following code block failing when I’m doing a loop for get a vector of results in a monte carlo simulation. Can’t understand why I got the issue above:
in line below:
mc_estimate = sum([in(sample(omega), union(A, B)) for x in 1:i if x % 1000 == 0]) / i;
ERROR: LoadError: MethodError: no method matching zero(::Type{Any})
Closest candidates are:
zero(::Type{Union{Missing, T}}) where T at missing.jl:105
zero(::Union{Type{P}, P}) where P<:Dates.Period at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\periods.jl:53
zero(::Histogram{T, N, E}) where {T, N, E} at C:\Users\jcbritobr\.julia\packages\StatsBase\Q76Ni\src\hist.jl:538
...
Stacktrace:
[1] zero(#unused#::Type{Any})
@ Base .\missing.jl:106
[2] reduce_empty(#unused#::typeof(+), #unused#::Type{Any})
@ Base .\reduce.jl:311
[3] reduce_empty(#unused#::typeof(Base.add_sum), #unused#::Type{Any})
@ Base .\reduce.jl:320
[4] mapreduce_empty(#unused#::typeof(identity), op::Function, T::Type)
@ Base .\reduce.jl:343
[5] reduce_empty(op::Base.MappingRF{typeof(identity), typeof(Base.add_sum)}, #unused#::Type{Any})
@ Base .\reduce.jl:329
[6] reduce_empty_iter
@ .\reduce.jl:355 [inlined]
[7] mapreduce_empty_iter(f::Function, op::Function, itr::Vector{Any}, ItrEltype::Base.HasEltype)
@ Base .\reduce.jl:351
[8] _mapreduce(f::typeof(identity), op::typeof(Base.add_sum), #unused#::IndexLinear, A::Vector{Any})
@ Base .\reduce.jl:400
[9] _mapreduce_dim
@ .\reducedim.jl:318 [inlined]
[10] #mapreduce#672
@ .\reducedim.jl:310 [inlined]
[11] mapreduce
@ .\reducedim.jl:310 [inlined]
[12] #_sum#682
@ .\reducedim.jl:878 [inlined]
[13] _sum
@ .\reducedim.jl:878 [inlined]
[14] #_sum#681
@ .\reducedim.jl:877 [inlined]
[15] _sum
@ .\reducedim.jl:877 [inlined]
[16] #sum#679
@ .\reducedim.jl:873 [inlined]
[17] sum(a::Vector{Any})
@ Base .\reducedim.jl:873
[18] top-level scope
@ c:\Workspace\julia\statistics\samples.jl:49
in expression starting at c:\Workspace\julia\statistics\samples.jl:48
The code below:
# %% Monte Carlo simulation for above problem
using StatsBase, Plots
N=10^4;
omega = collect(Ω);
simulations = Vector(undef, N);
mc_estimate = 0;
for i in 1:N
mc_estimate = sum([in(sample(omega), union(A, B)) for x in 1:i if x % 1000 == 0]) / i;
simulations[i] = mc_estimate;
end
println("The result of monte carlo simulation is $mc_estimate");
plot(1:N, simulations);
plot!(x -> 0.6, 1:N)