s-news
[Top] [All Lists]

Re: a movjng average median

To: "Leeds, Mark" <mleeds@mlp.com>, <s-news@wubios.wustl.edu>
Subject: Re: a movjng average median
From: "Eric Zivot" <ezivot@u.washington.edu>
Date: Mon, 20 Oct 2003 13:58:48 -0700
Importance: Normal
In-reply-to: <37DA35FDF352A24AB931404B856180FA02FB0B@MAIL002.AD.MLP.COM>
Something like this is particularly easy using the S-PLUS command aggregateSeries. To illustrate, suppose you have a time series object ret.ts containing daily returns on an asset. To compute the median over a window of length say 24 days incremented by one day use
 
roll.median = aggregateSeries(ret.ts,moving=24,adj=1,FUN=median)
 
The arguments moving=24, adj=1 and FUN=median tell the splus function aggregateSeries to evaluate the median function on a moving window of length 24 and to adjust the output positions to the end of each window.
 
I discuss several extensions of calculations like this in chapter 9 of my book Modeling Financial Time Series with Splus. ez
-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu [mailto:s-news-owner@lists.biostat.wustl.edu]On Behalf Of Leeds, Mark
Sent: Monday, October 20, 2003 1:24 PM
To: s-news@wubios.wustl.edu
Subject: [S] a movjng average median

does anyone have or know of an easy way to calculate
the "windowLength" day moving median given a vector of numbers.
 
one can use filter to calculate the "windowLength" day moving average
as shown below but i can't think of a good way to
do the median.
 
"backwardsmovingaverage<-function(series,windowLength)
{
return(  c(filter( series, sides=1, filter=rep(1/windownlength,windowlength))))
}
 
thanks.
 
                                            mark
<Prev in Thread] Current Thread [Next in Thread>