I need to apply some computations to a moving window of $N$ items in a time series and I am struggling with doing recursion and shifting the considered window.
To illustrate, please consider the simple function below.
myFunction[state_] := Append[state[[2 ;;]], RandomInteger[10]]
initialState = {1, 2, 3};
RandomInteger[10];
state1 = myFunction[initialState]
state2 = myFunction[state1]
In reality I am doing some time series analysis.
I am predicting the t4 based on t1, t2 and t3. Then I want to predict t5 based on t2, t3 and my predicted t4 and so on.
So after 3 iterations, I will be predicting based on my 3 first predictions
Answer
From your question, it looks like you need to implement some form of a linear predictor and step forward in time starting with an initial state. The solution is still the same as my previous version — i.e., using Nest, but it's now written in a clearer form:
predict[samples_] := Total[samples] (* Replace Total with your function *)
step[state_, n_: 2] := state ~Join~ {predict[state[[-n ;;]]]}
Nest[step[#, 3] &, initialState, 10] (* enter your lag (here 3), initialState, iterations *)
An example to generate the Fibonacci series with the above:
Nest[step, {0, 1, 1}, 10]
(* {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144} *)
Original answer:
You can do it easily with NestList:
NestList[myFunction, initialState, 5]
(* {{1, 2, 3}, {2, 3, 1}, {3, 1, 3}, {1, 3, 3}, {3, 3, 5}, {3, 5, 5}} *)
Comments
Post a Comment