tech.v3.datatype.rolling

expanding-window-ranges

(expanding-window-ranges n-elems)

Return a reader of expanding window ranges used for cumsum type operations.

fixed-rolling-window

(fixed-rolling-window item window-size window-fn {:keys [relative-window-position edge-mode _datatype], :or {relative-window-position :center, edge-mode :clamp}, :as options})(fixed-rolling-window item window-size window-fn)

Return a lazily evaluated rolling window of window-fn applied to each window. The iterable or sequence is padded such that there are the same number of values in the result as in the input with repeated elements padding the beginning and end of the original sequence. If input is an iterator, output is an lazy sequence. If input is a reader, output is a reader.

:Options

  • :relative-window-position - Defaults to :center - controls the window's relative positioning in the sequence.
  • :edge-mode - Defaults to :clamp - either :zero in which case window values off the edge are zero for numeric types or nil for object types or :clamp - in which case window values off the edge of the data are bound to the first or last values respectively.

Example (all results are same length):

user> (require '[tech.v3.datatype :as dtype])
nil
user> (require '[tech.v3.datatype.rolling :as rolling])
nil
user> (require '[tech.v3.datatype.functional :as dfn])
nil
  user> (rolling/fixed-rolling-window (range 20) 5 dfn/sum {:relative-window-position :left})
[0 1 3 6 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85]
user> (rolling/fixed-rolling-window (range 20) 5 dfn/sum {:relative-window-position :center})
[3 6 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 89 92]
user> (rolling/fixed-rolling-window (range 20) 5 dfn/sum {:relative-window-position :right})
[10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 89 92 94 95]
user>

fixed-rolling-window-ranges

(fixed-rolling-window-ranges n-elems window-size relative-window-position)

Return a reader of window-ranges of n-elems length.

Example:

tech.v3.datatype.rolling> (fixed-rolling-window-ranges 10 3 :left)
[[-2 -1 0] [-1 0 1] [0 1 2] [1 2 3] [2 3 4] [3 4 5] [4 5 6] [5 6 7] [6 7 8] [7 8 9]]
tech.v3.datatype.rolling> (fixed-rolling-window-ranges 10 3 :center)
[[-1 0 1] [0 1 2] [1 2 3] [2 3 4] [3 4 5] [4 5 6] [5 6 7] [6 7 8] [7 8 9] [8 9 10]]
tech.v3.datatype.rolling> (fixed-rolling-window-ranges 10 3 :right)
[[0 1 2] [1 2 3] [2 3 4] [3 4 5] [4 5 6] [5 6 7] [6 7 8] [7 8 9] [8 9 10] [9 10 11]]

variable-rolling-window-ranges

(variable-rolling-window-ranges src-data window-length {:keys [stepsize comp-fn relative-window-position], :or {stepsize 0.0}})(variable-rolling-window-ranges src-data window-length)

Given a reader of monotonically increasing source data, a double window length and a comparison function that takes two elements of the src-data and returns a double return an iterable of ranges that describe the windows in index space. Once a window is found start index will be incremented by stepsize amount as according to comp-fn or 1 if stepsize is not provided or 0.0.

  • src-data - convertible to reader.
  • window-length - double window length amount in the space of comp-fn.

Returns an iterable of clojure ranges. Note the last N ranges will not be window-size in length; you can filter these out if you require exactly window-size windows.

Options:

  • :stepsize - double stepsize amount in the space of comp-fn.
  • :comp-fn - defaults to (- rhs lhs), must return a double result that will be used to figure out the next window indexes and the amount to increment the start index in between windows.
  • :relative-window-position - defaults to :right, attempts to center the window relative to the input.

Examples:

tech.v3.datatype.rolling> (vec (variable-rolling-window-ranges
                                (range 20) 5))]
[[0 1 2 3 4]
 [1 2 3 4 5]
 [2 3 4 5 6]
 [3 4 5 6 7]
 [4 5 6 7 8]
 [5 6 7 8 9]
 [6 7 8 9 10]
 [7 8 9 10 11]
 [8 9 10 11 12]
 [9 10 11 12 13]
 [10 11 12 13 14]
 [11 12 13 14 15]
 [12 13 14 15 16]
 [13 14 15 16 17]
 [14 15 16 17 18]
 [15 16 17 18 19]
 [16 17 18 19]
 [17 18 19]
 [18 19]
 [19]]

tech.v3.datatype.rolling> (vec (variable-rolling-window-ranges
                           (range 20) 5 {:stepsize 2}))
[[0 1 2 3 4]
 [2 3 4 5 6]
 [4 5 6 7 8]
 [6 7 8 9 10]
 [8 9 10 11 12]
 [10 11 12 13 14]
 [12 13 14 15 16]
 [14 15 16 17 18]
 [16 17 18 19]
 [18 19]]

window-ranges->window-reader

(window-ranges->window-reader src-data win-ranges edge-mode)

Given src data, reader of window ranges and an edge mode return a reader that when read returns a reader of src-data appropriately padded depending on edge mode.

  • src-data - buffer of source data
  • win-ranges - reader of WindowRange.
  • edge-mode - one of :zero pad with 0/nil or :clamp in which case the padding is the first/last values in src data, respectively.

Example:

tech.v3.datatype.rolling> (window-ranges->window-reader
                           (range 10) (fixed-rolling-window-ranges 10 3 :center)
                           :clamp)
[[0 0 1] [0 1 2] [1 2 3] [2 3 4] [3 4 5] [4 5 6] [5 6 7] [6 7 8] [7 8 9] [8 9 9]]
tech.v3.datatype.rolling> (window-ranges->window-reader
                           (range 10) (fixed-rolling-window-ranges 10 3 :center)
                           :zero)
[[0 0 1] [0 1 2] [1 2 3] [2 3 4] [3 4 5] [4 5 6] [5 6 7] [6 7 8] [7 8 9] [8 9 0]]