Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
    
Is R.M's answer what you want? – Mr.Wizard May 4 '12 at 22:47
    
@Mr.Wizard, you truly are, after 30 min I realize it does not after 10 iterations for me in what I do :-( – 500 May 4 '12 at 23:30
    
@Mr Wizard, I guess Append[] is a bad example since it is part of the recursion I want to do ? – 500 May 4 '12 at 23:32
1  
@500 Could you explain how my answer doesn't do what you've described? I've edited my answer to make the steps clearer, but it still is the same approach... – R. M. May 5 '12 at 0:12
1  
@500 Seeing your image it's clear why the NestList didn't work for you — It's because you've defined your myFunction here as appending the new result to the previous two (i.e., returning a list of length 3), whereas you've defined maFonction in your file as returning only the updated value (i.e., a list of length 1)... – R. M. May 5 '12 at 3:24
up vote 5 down vote accepted

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}} *)
share|improve this answer

Taking R.M's lead and using the Fibonacci sequence, maybe this example is helpful?

f[{a_, b_}] := {b, a + b};

NestList[f, {1, 1}, 7]
{{1, 1}, {1, 2}, {2, 3}, {3, 5}, {5, 8}, {8, 13}, {13, 21}, {21, 34}}

Or as an anonymous function:

NestList[{#2, # + #2} & @@ # &, {1, 1}, 7]
share|improve this answer
    
Isn't this the same as my first NestList answer that you said was not what 500 wanted? – R. M. May 5 '12 at 0:24
    
@R.M I'm trying... (already voted for yours) – Mr.Wizard May 5 '12 at 0:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.