diff --git a/benchmark/benchmarks.jl b/benchmark/benchmarks.jl index a4fad840..78995bb7 100644 --- a/benchmark/benchmarks.jl +++ b/benchmark/benchmarks.jl @@ -36,3 +36,37 @@ Output on Jan's machine: # Check 0 allocations @btime inplace_map!(ar1_whitenoise, $var_x, $wv) + + +################################################# +# surrogates_loop! +################################################# + +using TransitionsInTimeseries + +n = 1001 +t = collect(1.0:n) +x = copy(t) + +indicators = (mean, var) +change_metrics = RidgeRegressionSlope() + +config = SlidingWindowConfig(indicators, change_metrics; + width_ind = 100, stride_ind = 1, + width_cha = 100, stride_cha = 1, whichtime = last, +) + +res = estimate_indicator_changes(config, x, t) +signif = SurrogatesSignificance(n = 100, tail = :both, p = 0.1) +flags = significant_transitions(res, signif) + +@btime estimate_indicator_changes($config, $x, $t) +# 141.557 μs (4 allocations: 40.56 KiB) +@btime significant_transitions($res, $signif) +#= +Former version of the code: 5.178 ms (1954 allocations: 3.03 MiB) +Current version of the code: 4.863 ms (738 allocations: 2.96 MiB) + +Although there is an improvement in the number of allocations, the performance +is not significantly improved. +=# \ No newline at end of file diff --git a/src/significance/surrogates_significance.jl b/src/significance/surrogates_significance.jl index ac954a0b..f98c6972 100644 --- a/src/significance/surrogates_significance.jl +++ b/src/significance/surrogates_significance.jl @@ -145,11 +145,9 @@ function sliding_surrogates_loop!( indicator_dummys, change_dummys, width_ind, stride_ind, width_cha, stride_cha, tail ) + pval_right = zeros(length(pval)) + pval_left = copy(pval_right) - if tail == :both - pval_right = zeros(length(pval)) - pval_left = copy(pval_right) - end # parallelized surrogate loop Threads.@threads for _ in 1:n_surrogates id = Threads.threadid() @@ -159,30 +157,17 @@ function sliding_surrogates_loop!( width = width_ind, stride = stride_ind) windowmap!(chametric, change_dummy, indicator_dummys[id]; width = width_cha, stride = stride_cha) - if tail == :right - pval .+= c .< change_dummy - elseif tail == :left - pval .+= c .> change_dummy - elseif tail == :both - pval_right .+= c .< change_dummy - pval_left .+= c .> change_dummy - end - end - if tail == :both - pval .= 2min.(pval_right, pval_left) + accumulate_pvals!(pval_right, pval_left, tail, c, change_dummy) end + choose_pval!(pval, pval_right, pval_left, tail) end function segmented_surrogates_loop!( indicator, chametric, c, pval, n_surrogates, sgens, indicator_dummys, change_dummys, width_ind, stride_ind, tail ) - - if tail == :both - pval_right = zeros(length(pval)) - pval_left = copy(pval_right) - end - + pval_right = zeros(length(pval)) + pval_left = copy(pval_right) # parallelized surrogate loop Threads.@threads for _ in 1:n_surrogates id = Threads.threadid() @@ -191,16 +176,26 @@ function segmented_surrogates_loop!( windowmap!(indicator, indicator_dummys[id], s; width = width_ind, stride = stride_ind) change_dummy = chametric(indicator_dummys[id]) - if tail == :right - pval .+= c .< change_dummy - elseif tail == :left - pval .+= c .> change_dummy - elseif tail == :both - pval_right .+= c .< change_dummy - pval_left .+= c .> change_dummy - end + accumulate_pvals!(pval_right, pval_left, tail, c, change_dummy) + end + choose_pval!(pval, pval_right, pval_left, tail) +end + +function accumulate_pvals!(pval_right, pval_left, tail, c, change_dummy) + if tail == :both || tail == :right + pval_right .+= c .< change_dummy end + if tail == :both || tail == :left + pval_left .+= c .> change_dummy + end +end + +function choose_pval!(pval, pval_right, pval_left, tail) if tail == :both pval .= 2min.(pval_right, pval_left) + elseif tail == :right + pval .= pval_right + elseif tail == :left + pval .= pval_left end -end \ No newline at end of file +end