Skip to content

Instantly share code, notes, and snippets.

@milktrader
Last active January 3, 2016 11:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milktrader/8458812 to your computer and use it in GitHub Desktop.
Save milktrader/8458812 to your computer and use it in GitHub Desktop.
Turbo boosting the moving method with Iterators (h/t Slender Means)
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
@milktrader
Copy link
Author

But unfortunately,

julia> @time slow = moving(Cl, kurtosis, 60);
elapsed time: 4.768658103 seconds (2079776636 bytes allocated)

julia> @time fast = fastmoving(Cl, kurtosis, 60);
ERROR: no method kurtosis((Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64))
 in next at /Users/Administrator/.julia/Iterators/src/Iterators.jl:447
 in collect at array.jl:263
 in collect at array.jl:270
 in fastmoving at /Users/Administrator/.julia/Series/src/array.jl:224

@carljv
Copy link

carljv commented Jan 17, 2014

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.

@carljv
Copy link

carljv commented Jan 17, 2014

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)

@carljv
Copy link

carljv commented Jan 17, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment