Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9881 > unrolled thread

moving average function

Started byRobert McHugh <rtmphone09@gmail.com>
First post2012-04-30 08:42 +0000
Last post2012-05-01 09:25 +0000
Articles 6 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  moving average function Robert McHugh <rtmphone09@gmail.com> - 2012-04-30 08:42 +0000
    Re: moving average function Ray Koopman <koopman@sfu.ca> - 2012-05-01 09:24 +0000
      Re: moving average function rtmphone09@gmail.com - 2012-05-02 09:47 +0000
        Re: moving average function Ray Koopman <koopman@sfu.ca> - 2012-05-03 08:41 +0000
      Re: moving average function rtmphone09@gmail.com - 2012-05-03 08:34 +0000
    Re: moving average function "Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com> - 2012-05-01 09:25 +0000

#9881 — moving average function

FromRobert McHugh <rtmphone09@gmail.com>
Date2012-04-30 08:42 +0000
Subjectmoving average function
Message-ID<jnlj94$n3k$1@smc.vnet.net>
Below is a moving average function that has the following features:
1. returns a list with the same length as the original length
2. provides a reasonable estimate for averages on the "sides" of the
window.

Have failed to figure out how to do this with ListConvolve and
ListCorrelate, so I submit this with the hope that others can
recommend how it might be improved. Also searched this website for
alternatives but didn't find any that met the above criteria.

I was motivated to do this in order to keep my code free of handling
special cases related to the edges of the widow size.  Note that in
one particular case, I have data measured every minute and would like
to compare the results of using averaging the data over window sizes
of 61, 121, and 181.

Recommendations for how to improve the function or alternatives are
appreciated.
Bob.


movingAverageBalanced[list_List, nAvg_Integer?OddQ ] :=
 Module[{nHang, middle, left, right, all},
  nHang = (nAvg - 1)/2;

  middle = MovingAverage[list, nAvg];

  left = Total[ Take[list, 2 # - 1]] /(2 # - 1) & /@ Range[nHang];
  right =
   Reverse[Total[ Take[list, -( 2 # - 1)]] /(2 # - 1) & /@
     Range[nHang]];
  all = Join[left, middle, right] ;
  Return[all];
  ]

Example
listTest = {a, b, c, d, e, f, g, h, i, j, k};
r = movingAverageBalanced[listTest, 5];
r // TableForm

{
 {a},
 {1/3 (a + b + c)},
 {1/5 (a + b + c + d + e)},
 {1/5 (b + c + d + e + f)},
 {1/5 (c + d + e + f + g)},
 {1/5 (d + e + f + g + h)},
 {1/5 (e + f + g + h + i)},
 {1/5 (f + g + h + i + j)},
 {1/5 (g + h + i + j + k)},
 {1/3 (i + j + k)},
 {k}
}

[toc] | [next] | [standalone]


#9897

FromRay Koopman <koopman@sfu.ca>
Date2012-05-01 09:24 +0000
Message-ID<jnoa47$547$1@smc.vnet.net>
In reply to#9881
On Apr 30, 1:42 am, Robert McHugh <rtmphon...@gmail.com> wrote:
> Below is a moving average function that has the following features:
> 1. returns a list with the same length as the original length
> 2. provides a reasonable estimate for averages on the "sides" of the
> window.
>
> Have failed to figure out how to do this with ListConvolve and
> ListCorrelate, so I submit this with the hope that others can
> recommend how it might be improved. Also searched this website for
> alternatives but didn't find any that met the above criteria.
>
> I was motivated to do this in order to keep my code free of handling
> special cases related to the edges of the widow size.  Note that in
> one particular case, I have data measured every minute and would like
> to compare the results of using averaging the data over window sizes
> of 61, 121, and 181.
>
> Recommendations for how to improve the function or alternatives are
> appreciated.
> Bob.
>
> movingAverageBalanced[list_List, nAvg_Integer?OddQ ] :=
>  Module[{nHang, middle, left, right, all},
>   nHang = (nAvg - 1)/2;
>
>   middle = MovingAverage[list, nAvg];
>
>   left = Total[ Take[list, 2 # - 1]] /(2 # - 1) & /@ Range[nHang];
>   right =
>    Reverse[Total[ Take[list, -( 2 # - 1)]] /(2 # - 1) & /@
>      Range[nHang]];
>   all = Join[left, middle, right] ;
>   Return[all];
>   ]
>
> Example
> listTest = {a, b, c, d, e, f, g, h, i, j, k};
> r = movingAverageBalanced[listTest, 5];
> r // TableForm
>
> {
>  {a},
>  {1/3 (a + b + c)},
>  {1/5 (a + b + c + d + e)},
>  {1/5 (b + c + d + e + f)},
>  {1/5 (c + d + e + f + g)},
>  {1/5 (d + e + f + g + h)},
>  {1/5 (e + f + g + h + i)},
>  {1/5 (f + g + h + i + j)},
>  {1/5 (g + h + i + j + k)},
>  {1/3 (i + j + k)},
>  {k}
>
> }

bma[data_List, n_Integer] := bma[data, Table[1,{n}]]

bma[data_List, wts_List] :=
ListCorrelate[wts, data, {-1,1}, 0]/
ListCorrelate[wts, Table[1,{Length@data}], {-1,1}, 0]

bma[testList, 5]

a
(a + b)/2
(a + b + c)/3
(a + b + c + d)/4
(a + b + c + d + e)/5
(b + c + d + e + f)/5
(c + d + e + f + g)/5
(d + e + f + g + h)/5
(e + f + g + h + i)/5
(f + g + h + i + j)/5
(g + h + i + j + k)/5
(h + i + j + k)/4
(i + j + k)/3
(j + k)/2
k

bma[testList, {1,2,3,2,1}]

a
(2*a + b)/3
(3*a + 2*b + c)/6
(2*a + 3*b + 2*c + d)/8
(a + 2*b + 3*c + 2*d + e)/9
(b + 2*c + 3*d + 2*e + f)/9
(c + 2*d + 3*e + 2*f + g)/9
(d + 2*e + 3*f + 2*g + h)/9
(e + 2*f + 3*g + 2*h + i)/9
(f + 2*g + 3*h + 2*i + j)/9
(g + 2*h + 3*i + 2*j + k)/9
(h + 2*i + 3*j + 2*k)/8
(i + 2*j + 3*k)/6
(j + 2*k)/3
k

[toc] | [prev] | [next] | [standalone]


#9916

Fromrtmphone09@gmail.com
Date2012-05-02 09:47 +0000
Message-ID<jnqvr0$j73$1@smc.vnet.net>
In reply to#9897
This is intriguing, but the length of the returned list doesn't match the length of the original list. 

Length /@ {testList, bma[testList, 5]}

{11, 15}

[toc] | [prev] | [next] | [standalone]


#9930

FromRay Koopman <koopman@sfu.ca>
Date2012-05-03 08:41 +0000
Message-ID<jntgb3$2j$1@smc.vnet.net>
In reply to#9916
On May 2, 2:47 am, rtmphone09@gmail.com wrote:
> This is intriguing, but the length of the returned list doesn't match the length of the original list.
>
> Length /@ {testList, bma[testList, 5]}
>
> {11, 15}

I think this gives the output you want, but
it's just a cleaned-up version of your code.

mab[data_List, n_Integer?OddQ] := Join[
    Mean@Take[data, #]& /@ Range[1,n-2, 2],
    MovingAverage[data, n],
    Mean@Take[data,-#]& /@ Range[n-2,1,-2]]

mab[testList, 5]

a
(a + b + c)/3
(a + b + c + d + e)/5
(b + c + d + e + f)/5
(c + d + e + f + g)/5
(d + e + f + g + h)/5
(e + f + g + h + i)/5
(f + g + h + i + j)/5
(g + h + i + j + k)/5
(i + j + k)/3
k

An interesting error occurred in an earlier version.
I ran into this once before. See "Insulating data from code"
http://forums.wolfram.com/mathgroup/archive/2006/May/msg00368.html

bad[data_List, n_Integer?OddQ] := Join[
    Table[Mean@Take[data, k], {k,1,n-2, 2}],
    MovingAverage[data, n],
    Table[Mean@Take[data,-k], {k,n-2,1,-2}]]

bad[testList, 5]

a
(a + b + c)/3
(a + b + c + d + e)/5
(b + c + d + e + f)/5
(c + d + e + f + g)/5
(d + e + f + g + h)/5
(e + f + g + h + i)/5
(f + g + h + i + j)/5
(g + h + i + j + k)/5
(3 + i + j)/3
1

[toc] | [prev] | [next] | [standalone]


#9921

Fromrtmphone09@gmail.com
Date2012-05-03 08:34 +0000
Message-ID<jntfvj$t90$1@smc.vnet.net>
In reply to#9897
This is intriguing, but the length of the returned list doesn't match the length of the original list. 

Length /@ {testList, bma[testList, 5]}

{11, 15}

[toc] | [prev] | [next] | [standalone]


#9900

From"Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com>
Date2012-05-01 09:25 +0000
Message-ID<jnoa73$54u$1@smc.vnet.net>
In reply to#9881
On Mon, 30 Apr 2012 09:42:12 +0100, Robert McHugh <rtmphone09@gmail.com>
wrote:

> Below is a moving average function that has the following features:
> 1. returns a list with the same length as the original length
> 2. provides a reasonable estimate for averages on the "sides" of the
> window.
>
> Have failed to figure out how to do this with ListConvolve and
> ListCorrelate, so I submit this with the hope that others can
> recommend how it might be improved. Also searched this website for
> alternatives but didn't find any that met the above criteria.
>
> I was motivated to do this in order to keep my code free of handling
> special cases related to the edges of the widow size.  Note that in
> one particular case, I have data measured every minute and would like
> to compare the results of using averaging the data over window sizes
> of 61, 121, and 181.
>
> Recommendations for how to improve the function or alternatives are
> appreciated.
> Bob.
>
>
> movingAverageBalanced[list_List, nAvg_Integer?OddQ ] :=
>  Module[{nHang, middle, left, right, all},
>   nHang = (nAvg - 1)/2;
>
>   middle = MovingAverage[list, nAvg];
>
>   left = Total[ Take[list, 2 # - 1]] /(2 # - 1) & /@ Range[nHang];
>   right =
>    Reverse[Total[ Take[list, -( 2 # - 1)]] /(2 # - 1) & /@
>      Range[nHang]];
>   all = Join[left, middle, right] ;
>   Return[all];
>   ]
>
> Example
> listTest = {a, b, c, d, e, f, g, h, i, j, k};
> r = movingAverageBalanced[listTest, 5];
> r // TableForm
>
> {
>  {a},
>  {1/3 (a + b + c)},
>  {1/5 (a + b + c + d + e)},
>  {1/5 (b + c + d + e + f)},
>  {1/5 (c + d + e + f + g)},
>  {1/5 (d + e + f + g + h)},
>  {1/5 (e + f + g + h + i)},
>  {1/5 (f + g + h + i + j)},
>  {1/5 (g + h + i + j + k)},
>  {1/3 (i + j + k)},
>  {k}
> }
>

This is almost a special case of the Savitzky-Golay filter, code for which  
I posted here recently (see  
http://forums.wolfram.com/mathgroup/archive/2012/Feb/msg00036.html). The  
main difference is in the treatment of the endpoints; while you chose to  
use a shortened window in dealing with these, I used a skewed window, i.e.  
taking the endpoint values as given by the best-fitting n-th order  
polynomial. The latter will obviously give worse results for the simple  
moving average where n = 0 (i.e., the endpoints would be approximated by  
their mean value) but can provide better results for moving polynomial  
windows with larger n. For example, the effect of a 5-point quadratic  
smoothing kernel is given by:

SavitzkyGolayFilter[
  {a, b, c, d, e, f, g, h, i, j, k},
  2, 2, 0
] // Factor

i.e.,

{1/35 (31 a + 9  b - 3  c - 5  d + 3  e),
  1/35 (9  a + 13 b + 12 c + 6  d - 5  e),
  1/35 (-3 a + 12 b + 17 c + 12 d - 3  e),
  1/35 (-3 b + 12 c + 17 d + 12 e - 3  f),
  1/35 (-3 c + 12 d + 17 e + 12 f - 3  g),
  1/35 (-3 d + 12 e + 17 f + 12 g - 3  h),
  1/35 (-3 e + 12 f + 17 g + 12 h - 3  i),
  1/35 (-3 f + 12 g + 17 h + 12 i - 3  j),
  1/35 (-3 g + 12 h + 17 i + 12 j - 3  k),
  1/35 (-5 g + 6  h + 12 i + 13 j + 9  k),
  1/35 (3  g - 5  h - 3  i + 9  j + 31 k)}

One may also take derivatives in this way; again using a 5-point kernel, a  
piecewise linear approximation to the first derivative is:

SavitzkyGolayFilter[
  {a, b, c, d, e, f, g, h, i, j, k},
  2, 2, 1
] // Factor

or,

{1/70 (-34 a + 3  b + 20 c + 17 d - 6  e),
  1/35 (-12 a -    b + 5  c + 6  d + 2  e),
  1/10 (-2  a -    b +    d + 2  e),
  1/10 (-2  b -    c +    e + 2  f),
  1/10 (-2  c -    d +    f + 2  g),
  1/10 (-2  d -    e +    g + 2  h),
  1/10 (-2  e -    f +    h + 2  i),
  1/10 (-2  f -    g +    i + 2  j),
  1/10 (-2  g -    h +    j + 2  k),
  1/35 (-2  g - 6  h - 5  i +    j + 12 k),
  1/70 (6   g - 17 h - 20 i - 3  j + 34 k)}

and so on.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web