6
$\begingroup$

Bug introduced in 10.0 and fixed in 10.1


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?

$\endgroup$
4
  • 1
    $\begingroup$ I recommend not Accepting an answer so soon. Someone else may have a different analysis. $\endgroup$
    – Mr.Wizard
    Commented Feb 8, 2015 at 7:42
  • $\begingroup$ @Mr.Wizard shouldn't we tag as a BUG? $\endgroup$
    – Murta
    Commented Feb 8, 2015 at 14:05
  • $\begingroup$ @Murta Let's wait for a third experienced user to declare it a bug. $\endgroup$
    – Mr.Wizard
    Commented Feb 8, 2015 at 15:22
  • $\begingroup$ Does anyone object to tagging this one as 'fixed in 10.1.0'. I realize Mr.Wizard is pointing out additional issues in the comments, but I think this is not the same issue that was originally pointed out. $\endgroup$ Commented Jul 27, 2015 at 22:00

2 Answers 2

6
$\begingroup$

Update

In version 10.1 the overhang problem described below has been removed. (In the default form; with Method -> "Legacy" the behavior below returns.) I am therefore tagging this as a bug.

Beware that MovingMap syntax has incompatibly changed in 10.1 as well.


This appears to be a bug in MovingMap. The function is applied beyond the left edge of the list and then the result is discarded:

echo[x_] := (Print[x]; x)

MovingMap[echo, Range@5, {2}]

{0,1}

{1,2}

{2,3}

{3,4}

{4,5}

{{1, 2}, {2, 3}, {3, 4}, {4, 5}}

I can see no reason to use the padding element 0 here. Interestingly when one specifies a different padding element that first result is not discarded:

MovingMap[Identity, Range@5, {2}, foo]
{{foo, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}}

The documentation for the padding parameter is:

enter image description here

So supposedly the default is no padding, but that is not true; the default padding is 0, and then the result is discarded. Again:

MovingMap[echo, Range@5, {5}]

{0,0,0,0,1}

{0,0,0,1,2}

{0,0,1,2,3}

{0,1,2,3,4}

{1,2,3,4,5}

{{1, 2, 3, 4, 5}}

There is no reason for the function (echo) to be applied the first four times, and in fact this directly contradicts the documentation which states that "no padding" is the default.

Also weird is this case:

MovingMap[echo, Range@5, {5, 2}]

{0,0,0,0,1}

{0,0,0,1,3}

{0,0,1,3,5}

MovingMap[echo, {1, 2, 3, 4, 5}, {5, 2}]

So even though it appears that the parameters are invalid and the entire MovingMap expression is returned unevaluated, the echo function is still applied to three times.

$\endgroup$
2
$\begingroup$

IMHO this is a Bug. You can prevent the error message in V10 using Quiet:

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

You can also use Internal`PartitionMap, without Quiet:

Developer`PartitionMap[#[[2]]/#[[1]]-1&, Range@5, 2, 1]

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

Post about PartitionMap

UPDATE

Fixed in 10.1

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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