[prev in list] [next in list] [prev in thread] [next in thread] 

List:       r-sig-finance
Subject:    Re: [R-SIG-Finance] Range intersections
From:       Matthew Clegg <matthewcleggphd () gmail ! com>
Date:       2013-07-28 18:41:33
Message-ID: CABLRk0GA-p9iJ0mSi0npcp61-dTQUhd20=AdYdE6-=aOSaQGDg () mail ! gmail ! com
[Download RAW message or body]

This reminds me of my grad school days.  Many years ago, in a galaxy far,
far away, I wrote a paper on a variation of this problem.

Here is a function that I think will do what you have asked for:

max_supported_interval <- function (L, H) {
# On input, L and H are equal length vectors representing
# a set of intervals, e.g., [L[1], H[1]], [L[2], H[2]], ...
# If the intersection of these intervals is non-empty,
# returns the intersection.  Otherwise, returns an interval
# [Lm,Hm] which is contained within a maximal number of
# the input intervals, and such that any larger interval
# containing [Lm,Hm] will not be supported by as many of the
# input intervals.
Ldf <- data.frame(index=L, value=1)
Hdf <- data.frame(index=H, value=-1)
LH <- merge(Ldf, Hdf, all=TRUE)
LH$support <- cumsum(LH$value)
N <- nrow(LH)
ileft <- which.max(LH$support)
iright <- ileft+1
# If the left and right endpoints of all of the input intervals
# are distinct, then the body of the following loop will never
# be executed.
while ((iright < N) &&
  ((LH$index[iright] == LH$index[iright+1]) ||
   (LH$support[iright] >= LH$support[ileft])))
iright <- iright + 1
c(LH$index[ileft], LH$index[iright])
}

> max_supported_interval(Pred1[,1],Pred1[,2])
[1] 100 105
> max_supported_interval(Pred2[,1],Pred2[,2])
[1] 100 110
>


Matthew Clegg



On Sat, Jul 27, 2013 at 3:24 PM, Enrico Schumann <es@enricoschumann.net>wrote:

> On Sat, 27 Jul 2013, Mark Knecht <markknecht@gmail.com> writes:
>
> > For the sake of asking here assume I have 5 models that attempt to
> > predict a pair of future values like tomorrow's high & low. The
> > predictions are placed in a matrix with the low prediction in column 1
> > and the high prediction in column 2. _IF_ there is an intersection of
> > all 5 predictions then I can test for that using the simple equation
> > in Range1. However if there is a prediction that's a complete outlier
> > as is Pred2[4,] then the simple equation fails and says the high is
> > less than the low.
> >
> > Are there any R packages that automatically handle simple intersection
> > problems like this? My real goals include things like probability
> > distributions across the ranges, but for now I'm just looking for
> > what's out there?
> >
> > Thanks,
> > Mark
> >
> >
> > Pred1 = matrix(data=NA, nrow=5, ncol=2)
> > Pred1[1,] = c(100,110)
> > Pred1[2,] = c(88,125)
> > Pred1[3,] = c(92,120)
> > Pred1[4,] = c(25,105)
> > Pred1[5,] = c(91,121)
> >
> > Range1 = c(max(Pred1[,1]), min(Pred1[,2]))
> > Range1
> >
> > Pred2 = matrix(data=NA, nrow=5, ncol=2)
> > Pred2[1,] = c(100,110)
> > Pred2[2,] = c(88,125)
> > Pred2[3,] = c(92,120)
> > Pred2[4,] = c(25,70) #outlier - the high is too low
> > Pred2[5,] = c(91,121)
> >
> > Range2 = c(max(Pred2[,1]), min(Pred2[,2]))
> > Range2
> >
>
> Yes, taking the intersection "fails" because in the second case the
> intersection is empty.  So /you/ will have to decide how you want to
> handle such cases.  Perhaps remove the outlier, or use some majority
> rule to find a "most likely" outcome (according to your preditions).
> For instance, you could count how often a given price falls in the range
> of any of the models.
>
>
> ind <- function(x, min, max) {
>     s <- numeric(length(x))
>     for (i in seq_along(min))
>         s <- s + as.integer(x >= min[i] & x <= max[i])
>     s
> }
>
> x <- seq(0, 130, by = 0.5)
> s <- ind(x, Pred2[ ,1], Pred2[ ,2])
> plot(x, s, type = "S")
>
> range(ca <- x[s == max(s)])            ## "consensus area"
> all.equal(max(diff(ca)), diff(x)[1L])  ## single area?
>
>
> Regards,
>         Enrico
>
> --
> Enrico Schumann
> Lucerne, Switzerland
> http://enricoschumann.net
>
> _______________________________________________
> R-SIG-Finance@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
>



-- 
Matthew Clegg
matthewcleggphd@gmail.com

	[[alternative HTML version deleted]]

_______________________________________________
R-SIG-Finance@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic