[R-SIG-Finance] rollapply and cummin

Gabor Grothendieck ggrothendieck at gmail.com
Thu Aug 23 14:55:36 CEST 2007


In that case you might prefer:

# test data
set.seed(1)
z <- zoo(rnorm(25))

rollapply(z, 5, function(x) min(cumsum(x)), align = "left")

which (1) works with the old rollapply too since the function
returns a single number and (2) has the advantage of returning
a zoo object rather than a numeric vector so the time information
is not lost.

On 8/23/07, Voss, Kent <VOSSK at kochind.com> wrote:
> Thank you so much, the new rollapply worked like a charm.  I retrospect
> what I was really trying to do (which the new rollapply works perfectly
> for), is calculate the maximum cumulative loss over some period.
>
> For the benefit of anyone else who has this problem...
> So with the new rollapply, the following returns an n x m matrix where m
> in the width specified in rollapply and is a cumulative sum over the
> width
> zl <- rollapply(z, 5, align='left',cumsum)
>
> Then doing an apply gives the minimum for each row, and is the maximum
> cumulative loss over the window.
> zm <- apply(zl, 1, min)
>
> Thank you so much Gabor and Z!!
>
>
>
>
>
> -----Original Message-----
> From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com]
> Sent: Wednesday, August 22, 2007 11:47 PM
> To: Voss, Kent
> Cc: r-sig-finance at stat.math.ethz.ch
> Subject: Re: [R-SIG-Finance] rollapply and cummin
>
> The usual case is that the function used in rollapply returns a single
> number; however, if you really want to return a vector of 5 numbers try
> the latest version of rollapply which was fixed about 2 weeks ago and is
> not yet on CRAN but can be downloaded from the development repository.
> From within R:
>
> library(zoo)
> source("http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/*checkou
> t*/pkg/R/rollapply.R?rev=363&root=zoo")
> # ... now run your commands ...
>
>
>
> On 8/22/07, Voss, Kent <VOSSK at kochind.com> wrote:
> > I am getting some unexpected results using rollapply and the cummin
> function that I am hoping someone can help me understand.  I'm just
> trying to get a rolling cumulative minimum of a time series.  Think of
> it as the maximum loss over a rolling window.
> >
> > Here's an example
> > # Create a dummy sequence to use with a set of dates used to create a
> > zoo object x <- c(0,rep(c(seq(1,5,by=1),seq(-1,-3, by=-1)),2))
> > DateList <- as.Date(seq(ISOdate(1990,1,1), length.out=length(x), by="1
>
> > day"), '%Y-%m-%d')
> >
> > z <- zoo(x,DateList)
> >
> > # Doing the following gets expected results, a rolling 5 day sum.  So
> > far so good zl <- rollapply(z, 5, align='left',sum)
> >
> > # The following however creates an n x n matrix where n = the length
> > of z, that I can't quite figure out zl <- rollapply(z, 5,
> > align='left',cummin)
> >
> >
> > Now I'm pretty new at this stuff, so I'm sure there's something I'm
> missing, but I can't make heads or tails around the results of the
> cummin.  Any help would be greatly appreciated.  Thanks in advance.
> >
> > Kent
> >
> >
> >
> >
> >
> > "EMF <kochind.com>" made the following annotations.
> > ----------------------------------------------------------------------
> > -------- The information in this e-mail and any attachments is
> > confidential and intended solely for the attention and use of the
> named addressee(s). It must not be disclosed to any person without
> proper authority. If you are not the intended recipient, or a person
> responsible for delivering it to the intended recipient, you are not
> authorized to and must not disclose, copy, distribute, or retain this
> message or any part of it.
> >
> > ======================================================================
> > ========
> >
> >        [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-SIG-Finance at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > -- Subscriber-posting only.
> > -- If you want to post, subscribe first.
> >
>
>
> "EMF <kochind.com>" made the following annotations.
> ------------------------------------------------------------------------------
> The information in this e-mail and any attachments is confidential and intended solely for the attention and use of the named addressee(s). It must not be disclosed to any person without proper authority. If you are not the intended recipient, or a person responsible for delivering it to the intended recipient, you are not authorized to and must not disclose, copy, distribute, or retain this message or any part of it.
>
> ==============================================================================
>
>



More information about the R-SIG-Finance mailing list