Skip to main content
3 of 4
edited tags
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

Using MovingMap to calculate percentage changes

MovingMap (new in version 10) applies a function to discrete data over a moving window. This is very convenient for calculating the percentage change of a series.

But the error returned by the following code bothers me:

series = Range[5];
MovingMap[#[[2]]/#[[1]] - 1 &, series, {2}]

(* Power::infy: Infinite expression 1/0. encountered. >>\*)
(* {1, 1/2, 1/3, 1/4} *)

However, this code returns the result with no error:

MovingMap[f, series, {2}] /. f -> Function[#[[2]]/#[[1]] - 1]

(* {1, 1/2, 1/3, 1/4} *)

What causes this problem?

Y. Kwon
  • 565
  • 2
  • 8