Hi,
I have the following piece of code that performs a simple bootstrap, but which is taking a very long time. I was wondering If I made obvious mistakes in the implmentation, although @code_warntype
seems to tell me everything’s alright on the type-stability side. Here’s the MWE :
using Distributions, Random, Statistics, Test, BenchmarkTools
function f(M,D::Vector{T},t_star,m::Integer) where T
n = length(D)
mu_mat = D .^(0:(m+1))' .* exp.(t_star .* D)
κ = zeros(T,(M,m+1))
μ = zeros(T,(M,m+2))
facts = factorial.(big.(0:m-1))
bins = binomial.(big.(0:m-1),big.(0:m-1)')
# this first loop has independant executions, it's the bootstrap loop and can be reordered as you want.
@inbounds for i in 1:M
μ[i,:] = mean(mu_mat[sample(1:n,n,replace=true),:],dims=1)
μ[i,2:end] ./= μ[i,1]
κ[i,1:2] .= log(μ[i,1]),μ[i,2]
for k in 3:(m+1) # this loop cannot be reordered.
κ[i,k] = μ[i,k]
for j in 1:(k-2)
κ[i,k] -= bins[k-1,j]*κ[i,j+1]*μ[i,k-j]
end
end
κ[i,2:end] ./= facts
end
return κ
end
Random.seed!(123)
D = BigFloat.(rand(LogNormal(),10000));
M = 10 # should be 10000
@btime f(M,D,-1,20)
Do you see something obvious that hurts my perfs ?
Edit : I posted a better version of the MWE, making first comment from @Oscar_Smith irrelevant, sorry.