 
 
 
 
 
 
Re: time based moving average (and other newbie mathematica questions)
- To: mathgroup at smc.vnet.net
- Subject: [mg92746] Re: time based moving average (and other newbie mathematica questions)
- From: Peter Pein <petsie at dordos.net>
- Date: Sun, 12 Oct 2008 04:31:59 -0400 (EDT)
- References: <gcn496$751$1@smc.vnet.net>
Hi Falcon,
if I understood you correctly, try the following code:
data = Sort[({#1, Sqrt[#1] + Sin[#1]} & ) /@ RandomReal[{0, 100},
           {100}]];
ListPlot[data, Joined -> True]
IntervalMovingAverage[dat_List, width_, index_: 1] :=
   If[index =!= 1, Sort, Identity][
     Mean /@
   Function[pos, Select[dat, pos[[index]] <= #1[[index]] <=
                 pos[[index]] + width & ]] /@ dat]
ListPlot[IntervalMovingAverage[data, 5], Joined -> True]
ListPlot[IntervalMovingAverage[data, 20], Joined -> True]
ListPlot[IntervalMovingAverage[data, 3, 2], Joined -> True]
hope this helps,
Peter
falcon schrieb:
> Hi,
> I see that Mathematica provides a couple of moving average functions.
> As far as I can tell, they are based on the number of elements in an
> array.  Is there a function for doing moving average based on time?
> In other words, if I pass in intra-day data containing prices, times
> (up to a millisecond) and some other fields, can I get Mathematica to
> to give me a 5 minute moving average rather than moving average of the
> last 100 trades?  Obviously this five minute window may contain any
> number of elements.
> 
> Secondly, along the same idea, I have a file with a large number of
> stocks.  They are all mixed in (the file is in chronological order).
> Is there an sql type 'group by' command that lets me see moving
> averages for each stock?
> 
> Third, if I'm able to get moving average for each stock in the list,
> can I plot all of them in one command (I guess this technique is
> called "small multiples" in charting vernacular). Obviously I will
> only have 20 or 30 stocks.
> 
> Thanks
> 

