Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: list manipulation, mean value

  • To: mathgroup at smc.vnet.net
  • Subject: [mg23002] Re: list manipulation, mean value
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Mon, 10 Apr 2000 02:22:30 -0400 (EDT)
  • References: <8cp61k$cu5@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Deborah:

We can proceed as follows

lis = { 1, 0, 2, 1, 3, 4} ;

w[n_] := Partition[lis, n, 1]

w[2]

{{1, 0}, {0, 2}, {2, 1}, {1, 3}, {3, 4}}

and go on to build up some custom code.

But using the package

<< Statistics`DescriptiveStatistics`

We can go directly to the moving averages.

ma = MovingAverage[N[lis], 2]

{0.5, 1., 1.5, 2., 3.5}

And then get the standard deviations that you want.

ReplaceList[{0.5`, 1.`, 1.5`, 2.`, 3.5`}, {x__, y___} :>
    StandardDeviation[{x}]]

{StandardDeviation[{0.5}], 0.353553, 0.5, 0.645497, 1.15109}

Well! However we can extend the definition of StandardDeviation to single
element lists.

Unprotect[StandardDeviation];
StandardDeviation[{x_}] := 0;
Protect[StandardDeviation];

Now we have

ReplaceList[{0.5`, 1.`, 1.5`, 2.`, 3.5`}, {x__, y___} :>
    StandardDeviation[{x}]]

{0, 0.353553, 0.5, 0.645497, 1.15109}

Build a function to do the work:

final[lis_] :=
  Table[ReplaceList[
      MovingAverage[N[lis], k], {x__, y___} :> StandardDeviation[{x}]],
    {k, 1, Length[lis]}]

final[lis]

{0.06 Second, {{0, 0.707107, 1., 0.816497, 1.14018, 1.47196}, {0, 0.353553,
      0.5, 0.645497, 1.15109}, {0, 0., 0.57735, 0.816497}, {0, 0.353553,
      0.763763}, {0, 0.424264}, {0}}}

If you want to get just one list then

flatfinal[lis_] := Flatten[final[lis]]

flatfinal[lis]

{0, 0.707107, 1., 0.816497, 1.14018, 1.47196, 0, 0.353553, 0.5, 0.645497, \
1.15109, 0, 0., 0.57735, 0.816497, 0, 0.353553, 0.763763, 0, 0.424264, 0}

It may be faster to avoid the use of ReplaceList:

final2[lis_] :=
Table[
    StandardDeviation[
      Take[MovingAverage[N[lis], k], n]],
    {k, 1, Length[lis]}, {n, 1, Length[lis] - k + 1}
    ]

final2[lis]

{{0, 0.707107, 1., 0.816497, 1.14018, 1.47196}, {0, 0.353553, 0.5, 0.645497,
    1.15109}, {0, 0., 0.57735, 0.816497}, {0, 0.353553, 0.763763}, {0,
    0.424264}, {0}}

And custom coding instead of using the package might be quicker.

"Deborah Leddon " <Dleddon at students.cas.unt.edu> wrote in message
news:8cp61k$cu5 at smc.vnet.net...
> Hello,
>
> I have a rather involved question on how to partition a data list into
> sliding windowed components and then calculate a mean value
> followed by a running standard deviation.
> For example, for the following data list:
>  lis ={ 1,0,2,1,3,4}
>
>  with a length of  n=6, I want to take groups of data points of
> lengths running from 1 to n= 6 (length of the data list), perform a
> mean value and then take a running standard deviation (STD) as
> shown below:
>
> For a group of length = 1, take from lis:
> w(1) = {1,0,2,1,3,4}
> perform mean value of each number (sublist;group length =1) to get
> {1,0,2,1,3,4},
> perform running standard deviations (STD) to get:
> {STD[1], STD[{1,0}], STD[{1,0,2}], STD[{1,0,2,1}], STD[{1,0,2,1,3}],
> STD[{1,0,2,1,3,4}]}
> then put into a list called lets say "final".
>
> For the next group of length = 2, take from lis:
> w(2) = { {1,0}, {0,2}, {2,1},{1,3}, {3,4} }, *(note this is not just a
> partition of the list, but a sliding one)*
> get the mean value of each sublist;
> {1, 1, 1.5, 2, 3.5};
> perform running standard dev.;
> STD[{1}], STD[{1,1}], STD[{1,1,1.5}], STD[{1,1,1.5,2}],
> STD[{1,1,1.5,2,3.5}]
> then append these STD values to the list 'final'.
>
> then so on for w(3),.....,w(n=6).
>
> Eventually the goal is to get a 'final' list of running standard
> deviations in order to plot.
>
> If anyone can help, I'd really appreciate it. Thanks for your time.
>
> Debbie
>
>
>




  • Prev by Date: Re: Re: Command to get a notebook's directory?
  • Next by Date: Re: InequalitySolve with algebraic numbers and Simplify
  • Previous by thread: Re: list manipulation, mean value
  • Next by thread: Re: list manipulation, mean value