sarovasta
sarovasta

Reputation: 89

Forcing discrete time series to be monotonous decreasing

I've an evaluations series. Each evaluation could have discrete values ranging from 0 to 4. Series should decrease in time. However, since values are inserted manually, errors could happen. Therefore, I would like to modify my series to be monotonous decreasing. Moreover, I would minimize the number of evaluations modified. Finally, if two or more series would satisfy these criteria, would choose the one with the higher overall values sum.

E.g.

Recorded evaluation

4332422111

Ideal evaluation

4332222111

Recorded evaluation

4332322111

Ideal evaluation

4333322111 (in this case, 4332222111 would have satisfied criteria too, but I chose with the higher values)

I tried with brutal force approach by generating all possible combinations, selecting those monotonous decreasing and finally comparing each one of these with that recorded. However, series could be even 20-evaluations long and combinations would too many.

Upvotes: 0

Views: 64

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226192

x1 <- c(4,3,3,2,4,2,2,1,1,1)
x2 <- c(4,3,3,2,3,2,2,1,1,1)

You could almost certainly break this algorithm, but here's a first try: replace locations with increased values by NA, then fill them in with the previous location.

dfun <- function(x) {
    r <- replace(x,which(c(0,diff(x))>0),NA)
    zoo::na.locf(r)
}

dfun(x1)
dfun(x2)

This gives the "less-ideal" answer in the second case.

For the record, I also tried

dfun2 <- function(x) {
    s <- as.stepfun(isoreg(-x))
    -s(seq_along(x))
}

but this doesn't handle the first example as desired.

You could also try to do this with discrete programming (about which I know almost nothing), or a slightly more sophisticated form of brute force -- use a stochastic algorithm that strongly penalizes non-monotonicity and weakly penalizes the distance from the initial sequence ... (e.g. optim(..., method="SANN") with a candidate function that adds or subtracts 1 from an element at random)

Upvotes: 1

Related Questions