27

The MovingMap window spec has been changed between version 10.1 and 10.0.2.

Version 10.0.2

MovingMap[h, {a, b, c, d, e, f}, 2]
(* {h[{a, b}], h[{b, c}], h[{c, d}], h[{d, e}], h[{e, f}]} *)

Here we get a moving window of 2 items as expected.

Version 10.1

MovingMap[h, {a, b, c, d, e, f}, 2]
(* {h[{a, b, c}], h[{b, c, d}], h[{c, d, e}], h[{d, e, f}]} *)

In 10.1 the same statement returns a moving window of 3 items.

Is there a way to have a bug marked as urgent. I have a few functions in a package that I made in 10.0.2 that I am using in 10.1 that use MovingMap and direct uses scattered about in notebooks.

I think all the other "moving" functions should be testing as well.

Update on outcome

The short story is that the change was accidentally omitted from 10.1 release notes which lead to it being interpreted as a bug (it is a breaking change but not a bug). Below in there is a response (unofficial) in one of the comments from a WRI employee that states WRI is looking into how this happened in order to prevent this in future. And the beat goes on.


enter image description here

Edmund
  • 42,267
  • 3
  • 51
  • 143
  • 3
    There is no meaning on SE of marking a bug as "Urgent", this isn't a formal support forum for Mathematica. Bug logging/remediation requests need to be made directly to Wolfram. You might want to rephrase your question to a request for a workaround. – Gordon Coale Apr 15 '15 at 10:32
  • Yes, I just copied my text to support and formatted it for SE. Should have removed that bit. – Edmund Apr 15 '15 at 10:33
  • I added ss with docs. I have no idea what to think about 10.1 page. Details about windowsize are only describing usage for timeseries. – Kuba Apr 15 '15 at 10:42
  • Haven't had time to explore fully but the fact that Quantity seems to act as workaround seems to suggest that a plain "2" is being interpreted as something other than a plain integer. The help mentions Day a lot. – Gordon Coale Apr 15 '15 at 10:54
  • 1
    No matter how ridiculous the behavior appears it is best to not start with the bugs tag applied as this is conventionally applied after an issue has either been confirmed by WRI as a bug or there is at least community consensus. – Mr.Wizard Apr 15 '15 at 16:35
  • It is very strange. Perhaps one should prefer Developer\PartitionMap` instead. – Oleksandr R. Apr 16 '15 at 10:11

3 Answers3

15

This is not a bug. MovingMap underwent a significant design change in 10.1. Please consult the documentation for details on the new specification. For your particular example, you would now do

MovingMap[h, {a, b, c, d, e, f}, Quantity[2,"Events"]]
Stefan R
  • 2,136
  • 18
  • 17
  • 1
    This returns an error for me (v10.1.0, 64bit, Win7): Quantity::unkunit: Unable to interpret unit specification Events. >> – István Zachar Apr 15 '15 at 17:12
  • @IstvánZachar That is most likely an issue connecting to the WRI servers which perform unit interpretations. – Stefan R Apr 15 '15 at 17:55
  • 1
    @IstvánZachar Try Quantity[2, IndependentUnit["events"]] to prevent Mathematica from trying to interpret "events" as a built-in unit. For some reason, "Events" is not a recognized unit in Mathematica 10.1, even though it is used by MovingMap[]. – David Zhang Apr 15 '15 at 18:28
  • @DavidZhang Yes, there was an oversight here. The "Events" quantity should not be triggering server calls. We're looking into it. – Stefan R Apr 15 '15 at 20:10
  • 2
    Any other already documented functions underwent similar procedure?... – Kuba Apr 15 '15 at 23:02
  • 10
    @StefanR It is a severely poor practice to change the functionality of a feature and make no explicit mention of it to users. MovingMap has been developed into solutions with the existing 10.0.x functionality. WRI deliberately changing it without notice introduces significant logical errors into existing code. Errors that the developer has no reasonable means to identity or narrow as zero information has been provided. I strongly urge WRI to explicitly state what functions have been altered such that legal syntax produces different results. How is anyone to manage without this info? – Edmund Apr 15 '15 at 23:41
  • 2
    I thought I might have missed something so double-checked Summary of New Features in 10.1. As I remembered, no mention of changes to MovingMap. This just can't be done! You can't have changes in results for the same parameters on what should be deterministic functions and leave it to pure chance that the developers using your product will find them. It is ludicrous! I'm thinking: What else has changed in this manner? What other production code is broken from something like this? – Edmund Apr 15 '15 at 23:46
  • 4
    Apologies for what may seem like a flippant comment, but what is the value of having a quantity required in the third place? Nowhere else in Mathematica do we refer to raw numbers having units. And what would be the meaning of, for example, MovingMap[..., Quantity[2, "Kilograms"]]? – Oleksandr R. Apr 16 '15 at 10:07
  • 3
    @Edmund It should definitely have been mentioned in the release notes. We are looking into why it wasn't included, and how we can avoid such omissions in the future. We are sorry for any frustration due to this issue. This was the only major backwards-incompatible redesign in 10.1 in statistics, and I'm not personally aware of other WL functions that were similarly extensively changed. – Stefan R Apr 16 '15 at 18:25
  • 2
    @OleksandrR. Note that MovingMap[f,list,n] will give {f[list[[1;;1+n]]], f[list[[2;;2+n]]], ...}, so for lists the Quantity[_,"Events"] is not required. Also, the "Events" quantity has special meaning in MovingMap. Using "Kilograms" will probably not produce meaningful results, but for TimeSeries data with dates, quantities like Quantity[1, "Weeks"] can be used. – Stefan R Apr 16 '15 at 18:31
  • Thanks. That seems like a sensible definition, if not consistent with the last one. (I never used MovingMap before, so it didn't affect me anyway.) – Oleksandr R. Apr 16 '15 at 19:16
  • 1
    @StefanR Thank you for clarifying that the omission of the changes from the release notes was accidental. It is also encouraging to hear that the process will be reviewed to reduce the chance of this sort of thing happening in future. Mathematica is a great tool but this is my first upgrade experience while using it professionally. Therefore, my first experience closely tracking version changes as it is no longer a hobby tool for me. This was a scary one. – Edmund Apr 16 '15 at 22:29
13

MovingMap as of 10.1 specifies windows by their absolute length. Since lists are treated as implicit time-series with (zero-based) integer index as time, window size of 2 means that the windowing function sees $\{x_{n-2}, x_{n-1}, x_n\}$. This is the only window specification that admits a clean generalization to irregular time series.

Using Quantity[2,"Events"] specifies possibly variable size window that always includes 2 events (points).

In[26]:= tvPairs = 
 Table[{t, x[t]}, {t, Accumulate[PadLeft[{}, 6, {1, 3}]]}]

Out[26]= {{1, x[1]}, {4, x[4]}, {5, x[5]}, {8, x[8]}, {9, x[9]}, {12, 
  x[12]}}

In[27]:= MovingMap[f, tvPairs, 2]

Out[27]= {{4, f[{x[4]}]}, {5, f[{x[4], x[5]}]}, {8, f[{x[8]}]}, {9, 
  f[{x[8], x[9]}]}, {12, f[{x[12]}]}}

In[28]:= MovingMap[f, tvPairs, Quantity[2, "Events"]]

Out[28]= {{4, f[{x[1], x[4]}]}, {5, f[{x[4], x[5]}]}, {8, 
  f[{x[5], x[8]}]}, {9, f[{x[8], x[9]}]}, {12, f[{x[9], x[12]}]}}

[Added]

The legacy behavior, can also be recovered using Method->"Legacy" option.

In[29]:= MovingMap[f, tvPairs, 2, Method -> "Legacy"]

Out[29]= {{4, f[{x[1], x[4]}]}, {5, f[{x[4], x[5]}]}, {8, 
  f[{x[5], x[8]}]}, {9, f[{x[8], x[9]}]}, {12, f[{x[9], x[12]}]}}

[/Added]


Notice that MovingMap has become much more powerful in 10.1. It is now possible to align windows at specific positions. For instance, consider a daily closing values of the Starbucks stocks from the beginning of 2002 and through the end of 2014.

sb = TimeSeries[FinancialData["SBUX", {{2002}, {2014}}]];

The following computes past monthly median weekly starting from Fri, Feb 1 of 2012:

MovingMap[Median, sb, {"Month", 
  Right, {"Feb 1 2002", Automatic, "Week"}}, None]

This was not easy to do in v9.

enter image description here

Sasha
  • 7,373
  • 36
  • 47
4

I would echo's @Kuba comments about most of the examples being for Time related windows, I would also note that there is an explicit example in the help that returns a window size of 3, so it could be an indication that its deliberate.

MovingMap[Mean, {x1, x2, x3, x4, x5}, 2]

(* {1/3 (x1 + x2 + x3), 1/3 (x2 + x3 + x4), 1/3 (x3 + x4 + x5)} *)

There is a workaround that seems to have no ill effects though.

MovingMap[h, {a, b, c, d, e, f}, Quantity[2, "Events"]]

(* {h[{a, b}], h[{b, c}], h[{c, d}], h[{d, e}], h[{e, f}]} *)
Gordon Coale
  • 2,341
  • 15
  • 20