A periodic sequence is a sequence that repeats itself after n terms, for example, the following is a periodic sequence:
1, 2, 3, 1, 2, 3, 1, 2, 3, ...
And we define the period of that sequence to be the number of terms in each subsequence (the subsequence above is 1, 2, 3). So the period for the above sequence is 3.
In R, I can define the above sequence (albeit not to infinity), using:
sequence <- rep(c(1,2,3),n) #n is a predefined variable
So if n = 50
, sequence
will be the sequence 1, 2, 3, 1, 2, 3, ... , 1, 2, 3, where each number has appeared 50 times, in the obvious way.
I am looking to build a function that calculates the periodicity of sequence
. Pseudocode is as follows:
period <- function(sequence){
subsequence <- subsequence(sequence) #identify the subsequence
len.subsequence <- length(subsequence) #calculate its length
return(len.subsequence) #return it
}
How would I identify the subsequence? This is sort of a reversing of the rep
function, such that I pass in a sequence and it passes out the length of the initial vector.
In particular, for a periodic sequence {an}, there exists a positive integer constant p such that for all n in thhe natural numbers, an=an+p. The constant p is said to be the period of the sequence. The lowest positive constant p possible is called the fundamental period of the sequence.
The order (or period) of a periodic sequence is the number of terms in each repeating cycle.
Each term in the sequence is got by doubling the previous term. So to define the recurrence relation, we give the first term, written U1 = 2. Then we write: Un = 2(Un-1). This just means that the nth term, Un is equal to 2 × the (n-1)th term, Un-1.
If the period is always the same, i.e. the sequence never changes, then you could use a loop over lag
to see when a match occurs.
With total bias, I would also recommend using seqle
(guess who wrote that function :-) ), which is like rle
but finds sequences. detect intervals of the consequent integer sequences
I'm not the only person to edit the source for "rle" that way.
It's fairly easy with that sequence, although I would avoid using the name 'sequence' since it is an R function name. This would identify the periodicity of any monotonic sequence so it's a bit more general but it would not identify a sequence like: 1.2.3.4.2.3.4,1,2,3,4,2,3,4, ....
> which(diff(seQ) < 0)
[1] 3 6 9 12 15 18 21 24 27
> diff(which(diff(seQ) < 0) )
[1] 3 3 3 3 3 3 3 3
You could test the equality of intervals or use either of those results to index the original vector. You should test your answers with c(1, 2, 3, 4, 2, 3, 4, 1, 2, 3, 4, 2, 3, 4) to see if they pass the test of identifying an non-monotonic repetition. So far none of them do so; since none report a period of 7.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With