-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForecastMetric.R
More file actions
19 lines (17 loc) · 842 Bytes
/
ForecastMetric.R
File metadata and controls
19 lines (17 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# An interval forecast metric for forecast objects.
# Originally introduced in https://www.stat.washington.edu/raftery/Research/PDF/Gneiting2007jasa.pdf
# Sums the calculated metric for each pair of prediction interval boundaries for all the
# predicted points.
interval.score <- function(lb.95, ub.95, actual.vals, alpha){
testing.data <- data.frame(lb.95 = lb.95, ub.95 = ub.95, actual.val = actual.vals)
scores <- apply(testing.data, 1, estimate.interval.score, alpha)
return(sum(scores))
}
# Computes the metric for the single prediction interval and actual value
estimate.interval.score <- function(row.to.estimate, alpha) {
l <- row.to.estimate[1]
u <- row.to.estimate[2]
x <- row.to.estimate[3]
estimate <- (u - l) + (2 / alpha) * (l - x) * identity(x < l) + (2 / alpha) * (x - u) * identity(x > u)
return(estimate)
}