-
-
Save milktrader/8458812 to your computer and use it in GitHub Desktop.
julia> using Series, MarketData, FactCheck | |
julia> @time fast = fastmoving(Cl, mean, 60); | |
elapsed time: 0.305857977 seconds (72646744 bytes allocated) | |
julia> @time slow = moving(Cl, mean, 60); | |
elapsed time: 4.925385985 seconds (2079575504 bytes allocated) | |
julia> @fact fast[end].value => roughly(slow[end].value) | |
Success :: :(fast[end].value) => :(roughly(slow[end].value)) | |
julia> 4.925385985/0.305857977 | |
16.103506710240225 |
This is pretty easy to fix. If you look at the next
method for Partition, you'll see it initially makes arrays of the partitions, but then converts them to a tuple at the end.
function next(it::Partition, state)
(s, p0) = state
(x, s) = next(it.xs, s)
ans = p0; ans[end] = x
p = similar(p0)
overlap = max(0, it.n - it.step)
for i in 1:overlap
p[i] = ans[it.step + i]
end
# when step > n, skip over some elements
for i in 1:max(0, it.step - it.n)
if done(it.xs, s)
break
end
(x, s) = next(it.xs, s)
end
for i in (overlap + 1):(it.n - 1)
if done(it.xs, s)
break
end
(x, s) = next(it.xs, s)
p[i] = x
end
(tuple(ans...), (s, p)) # <<<<<< Tupled!!
end
If you make a version of this type, say APartition
, which is exactly the same, but simply removes that tuple conversion, then kurtosis
, and any function that requires an Array should work. mean
works because there is a very generic mean(iteratable)
method that works with the tuple partitions.
Really, it's not 100% clear why having partition use tuples instead of arrays is better, especially given the limited number of functions that can dispatch on arbitrary tuples. Maybe that's an issue/PR for the Iterators repo.
The array version even seems to be faster. (Though for some reason more memory-intensive?)
julia> @time map(mean, apartition(randn(50_000), 60))
elapsed time: 0.010084782 seconds (3430204 bytes allocated)
julia> @time map(mean, partition(randn(50_000), 60))
elapsed time: 0.022321433 seconds (5587344 bytes allocated)
One last comment (hopefully). You may get a slight-to-moderate speedup from just calling map
on the partitions, instead of collect(imap(f, partition(...)))
. If you're just going to collect the entire IMap
right away, you might as well just use map
.
But unfortunately,