Skip to main content
1 of 4
Y. Kwon
  • 565
  • 2
  • 8

Using MovingMap to calculate percentage changes

MovingMap is a very convenient tool to calculate the percentage changes of a series.

But one thing bothers me is this error.

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

The output is

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

However,

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

gives

{1, 1/2, 1/3, 1/4}

without an error.

I don't understand what causes that problem.

Y. Kwon
  • 565
  • 2
  • 8