Thnaks, It is true, but when I apply @benchmark v3 is 6 times slower as v1,
also has a large allocation and I do not want it. For me speed is important
and v3 is not without visual noise, too. Any more thoughts?
ben1 = @benchmark mapeBase_v1(a,f)
BenchmarkTools.Trial:
samples: 848
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 32.00 bytes
allocs estimate: 1
minimum time: 4.35 ms (0.00% GC)
median time: 5.87 ms (0.00% GC)
mean time: 5.89 ms (0.00% GC)
maximum time: 7.57 ms (0.00% GC)
ben2 = @benchmark mapeBase_v3(a,f)
BenchmarkTools.Trial:
samples: 145
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 977.03 kb
allocs estimate: 14
minimum time: 32.69 ms (0.00% GC)
median time: 33.91 ms (0.00% GC)
mean time: 34.55 ms (0.10% GC)
maximum time: 49.03 ms (3.25% GC)
On Tuesday, 25 October 2016 09:43:20 UTC+2, Jeffrey Sarnoff wrote:
>
> This may do what you want.
>
> function mapeBase_v3(actuals::Vector{Float64}, forecasts::Vector{Float64})
> # actuals - actual target values
> # forecasts - forecasts (model estimations)
>
> sum_reldiffs = sumabs((x - y) / x for (x, y) in zip(actuals, forecasts) if
> x != 0.0) # Generator
>
> count_zeros = sum( map(x->(x==0.0), actuals) )
> count_nonzeros = length(actuals) - count_zeros
> sum_reldiffs, count_nonzeros
> end
>
>
>
>
> On Tuesday, October 25, 2016 at 3:15:54 AM UTC-4, Martin Florek wrote:
>>
>> Hi all,
>> I'm new in Julia and I'm doing refactoring. I have the following function:
>>
>> function mapeBase_v1(A::Vector{Float64}, F::Vector{Float64})
>> s = 0.0
>> count = 0
>> for i in 1:length(A)
>> if(A[i] != 0.0)
>> s += abs( (A[i] - F[i]) / A[i])
>> count += 1
>> end
>> end
>>
>> s, count
>>
>> end
>>
>> I'm looking for a simpler variant which is as follows:
>>
>> function mapeBase_v2(A::Vector{Float64}, F::Vector{Float64})
>> # A - actual target values
>> # F - forecasts (model estimations)
>>
>> s = sumabs((x - y) / x for (x, y) in zip(A, F) if x != 0) # Generator
>>
>> count = length(A) # ???
>> s, countend
>>
>>
>> However with this variant can not determine the number of non-zero elements.
>> I found option with length(A[A .!= 0.0]), but it has a large allocation.
>> Please, someone knows a solution with generator, or variant v1 is very good
>> choice?
>>
>>
>> Thanks in advance,
>> Martin
>>
>>