calc_endpoints: Calculate a vector of end points that divides an integer time...

View source: R/RcppExports.R

calc_endpointsR Documentation

Calculate a vector of end points that divides an integer time sequence of time periods into equal time intervals.

Description

Calculate a vector of end points that divides an integer time sequence of time periods into equal time intervals.

Usage

calc_endpoints(length, step = 1L, stub = 0L, stubs = TRUE)

Arguments

length

An integer equal to the length of the time sequence to be divided into equal intervals.

step

The number of time periods in each interval between neighboring end points (the default is step = 1).

stub

An integer equal to the first non-zero end point (the default is stub = 0).

stubs

A Boolean specifying whether to include stub intervals (the default is stubs = TRUE).

Details

The end points are a vector of integers which divide the sequence of time periods of length equal to length into equally spaced time intervals. The number of time periods between neighboring end points is equal to the argument step. If a whole number of intervals doesn't fit over the whole sequence, then calc_endpoints() adds a stub interval at the end. A stub interval is one where the number of periods between neighboring end points is less than the argument step.

If stubs = TRUE (the default) then the first end point is equal to 0 (since indexing in C++ code starts at 0). The first non-zero end point is equal to step or stub (if it's not zero). If stub = 0 (the default) then the first end point is equal to 0 (even if stubs = FALSE). If stubs = TRUE (the default) then the last end point is always equal to length-1. The argument stub should be less than the step: stub < step.

If step = 1 and stub = 0 (the default), then the vector of end points is simply equal to:

\{ 0, 1, 2, ..., length - 1 \}

If stub = 0 (the default) and stubs = TRUE (the default) then the vector of end points is equal to:

\{ 0, step, 2*step, ..., length - 1 \}

If stub = 0 (the default) and stubs = FALSE then the vector of end points is equal to:

\{ 0, step, 2*step, ..., n*step \}

If stub > 0 and stubs = TRUE (the default), then the vector of end points is equal to:

\{ 0, stub, stub + step, ..., length - 1 \}

For example, the end points for length = 20, divided into intervals of step = 5 are equal to: 0, 5, 10, 15, 19.

If stub = 1 then the first non-zero end point is equal to 1 and the end points are equal to: 0, 1, 6, 11, 16, 19. The stub interval at the beginning is equal to 2 (including 0 and 1). The stub interval at the end is equal to 3 = 19 - 16.

The end points for length = 21 divided into intervals of length step = 5, with stub = 0, are equal to: 0, 5, 10, 15, 20. The beginning interval is equal to 5. The end interval is equal to 5 = 20 - 15.

If stub = 1 then the first non-zero end point is equal to 1 and the end points are equal to: 0, 1, 6, 11, 16, 20. The beginning stub interval is equal to 2. The end stub interval is equal to 4 = 20 - 16.

The function calc_endpoints() is similar to the function rutils::calc_endpoints() from package rutils.

But the end points are shifted by -1 compared to R code because indexing starts at 0 in C++ code, while it starts at 1 in R code. So if calc_endpoints() is used in R code then 1 should be added to it.

Value

A vector of equally spaced integers representing the end points.

Examples

# Calculate the end points without a stub interval
HighFreq::calc_endpoints(length=20, step=5)
# Calculate the end points with a final stub interval
HighFreq::calc_endpoints(length=23, step=5)
# Calculate the end points with initial and final stub intervals
HighFreq::calc_endpoints(length=20, step=5, stub=2)


algoquant/HighFreq documentation built on Feb. 9, 2024, 8:15 p.m.