Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#'Compute Brier score and its decomposition and Brier skill score
#'
#'Compute the Brier score (BS) and the components of its standard decompostion
#'as well with the two within-bin components described in Stephenson et al.,
#'(2008). It also returns the bias-corrected decomposition of the BS (Ferro and
#'Fricker, 2012). BSS has the climatology as the reference forecast.
#'.BrierScore provides the same functionality, but taking a matrix of ensemble
#'members (exp) as input.
#'
#'@param obs Vector of binary observations (1 or 0).
#'@param pred Vector of probablistic predictions with values in the range [0,1].
#'@param thresholds Values used to bin the forecasts. By default the bins are
#' {[0,0.1), [0.1, 0.2), ... [0.9, 1]}.
#'@param exp Matrix of predictions with values in the range [0,1] for the
#' .BrierScore function
#'
#'@return Both BrierScore and .Brier score provide the same outputs:
#'\itemize{
#' \item{$rel}{standard reliability}
#' \item{$res}{standard resolution}
#' \item{$unc}{standard uncertainty}
#' \item{$bs}{Brier score}
#' \item{$bs_check_res}{rel-res+unc}
#' \item{$bss_res}{res-rel/unc}
#' \item{$gres}{generalized resolution}
#' \item{$bs_check_gres}{rel-gres+unc}
#' \item{$bss_gres}{gres-rel/unc}
#' \item{$rel_bias_corrected}{bias-corrected rel}
#' \item{$gres_bias_corrected}{bias-corrected gres}
#' \item{$unc_bias_corrected}{bias-corrected unc}
#' \item{$bss_bias_corrected}{gres_bias_corrected-rel_bias_corrected/unc_bias_corrected}
#' \item{$nk}{number of forecast in each bin}
#' \item{$fkbar}{average probability of each bin}
#' \item{$okbar}{relative frequency that the observed event occurred}
#' \item{$bins}{bins used}
#' \item{$pred}{values with which the forecasts are verified}
#' \item{$obs}{probability forecasts of the event}
#'}
#'
#'@references
#'Wilks (2006) Statistical Methods in the Atmospheric Sciences.\cr
#'Stephenson et al. (2008). Two extra components in the Brier score decomposition.
#' Weather and Forecasting, 23: 752-757.\cr
#'Ferro and Fricker (2012). A bias-corrected decomposition of the BS.
#' Quarterly Journal of the Royal Meteorological Society, DOI: 10.1002/qj.1924.
#'
#'@examples
#'# Minimalist examples with BrierScore
#'a <- runif(10)
#'b <- round(a)
#'x <- BrierScore(b, a)
#'x$bs - x$bs_check_res
#'x$bs - x$bs_check_gres
#'x$rel_bias_corrected - x$gres_bias_corrected + x$unc_bias_corrected
#' \dontrun{
#'a <- runif(10)
#'b <- cbind(round(a),round(a)) # matrix containing 2 identical ensemble members...
#'x2 <- BrierScore(a, b)
#' }
#'
#'# Example of BrierScore using UltimateBrier
#'# See ?UltimateBrier for more information
#'example(Load)
#'clim <- Clim(sampleData$mod, sampleData$obs)
#'ano_exp <- Ano(sampleData$mod, clim$clim_exp)
#'ano_obs <- Ano(sampleData$obs, clim$clim_obs)
#'bs <- UltimateBrier(ano_exp, ano_obs, thr = c(1/3, 2/3))
#'
#' \dontrun{
#'# Example of .BrierScore with veriApply
#'require(easyVerification)
#'BrierScore2 <- s2dverification:::.BrierScore
#'bins_ano_exp <- ProbBins(ano_exp, thr = c(1/3, 2/3), posdates = 3, posdim = 2)
#'bins_ano_obs <- ProbBins(ano_obs, thr = c(1/3, 2/3), posdates = 3, posdim = 2)
#'bs2 <- veriApply("BrierScore2", bins_ano_exp, Mean1Dim(bins_ano_ob,s 3),
#' tdim = 2, ensdim = 3)
#' }
#'@import multiApply
#'@export
BrierScore <- function(obs, pred, thresholds = seq(0, 1, 0.1)) {
if (max(pred) > 1 | min(pred) < 0) {
stop("Predictions outside [0,1] range. Are you certain this is a probability forecast? \n")
} else if (max(obs) != 1 & min(obs) != 0) {
.message("Binary events must be either 0 or 1. Are you certain this is a binary event? ")
} else {
nbins <- length(thresholds) - 1 # Number of bins
n <- length(pred)
bins <- as.list(paste("bin", 1:nbins,sep = ""))
for (i in 1:nbins) {
if (i == nbins) {
bins[[i]] <- list(which(pred >= thresholds[i] & pred <= thresholds[i + 1]))
} else {
bins[[i]] <- list(which(pred >= thresholds[i] & pred < thresholds[i + 1]))
}
}
fkbar <- okbar <- nk <- array(0, dim = nbins)
for (i in 1:nbins) {
nk[i] <- length(bins[[i]][[1]])
fkbar[i] <- sum(pred[bins[[i]][[1]]]) / nk[i]
okbar[i] <- sum(obs[bins[[i]][[1]]]) / nk[i]
}
obar <- sum(obs) / length(obs)
relsum <- ressum <- term1 <- term2 <- 0
for (i in 1:nbins) {
if (nk[i] > 0) {
relsum <- relsum + nk[i] * (fkbar[i] - okbar[i])^2
ressum <- ressum + nk[i] * (okbar[i] - obar)^2
for (j in 1:nk[i]) {
term1 <- term1 + (pred[bins[[i]][[1]][j]] - fkbar[i])^2
term2 <- term2 + (pred[bins[[i]][[1]][j]] - fkbar[i]) * (obs[bins[[i]][[1]][j]] - okbar[i])
}
}
}
rel <- relsum / n
res <- ressum / n
unc <- obar * (1 - obar)
bs <- sum((pred - obs)^2) / n
bs_check_res <- rel - res + unc
bss_res <- (res - rel) / unc
gres <- res - term1 * (1 / n) + term2 * (2 / n) # Generalized resolution
bs_check_gres <- rel - gres + unc # BS using GRES
bss_gres <- (gres - rel) / unc # BSS using GRES
#
# Estimating the bias-corrected components of the BS
#
term3 <- array(0, nbins)
for (i in 1:nbins) {
term3[i] <- (nk[i] / (nk[i] - 1)) * okbar[i] * (1 - okbar[i])
}
term_a <- sum(term3, na.rm = T) / n
term_b <- (obar * (1 - obar)) / (n - 1)
rel_bias_corrected <- rel - term_a
gres_bias_corrected <- gres - term_a + term_b
if (rel_bias_corrected < 0 || gres_bias_corrected < 0) {
rel_bias_corrected2 <- max(rel_bias_corrected, rel_bias_corrected - gres_bias_corrected, 0)
gres_bias_corrected2 <- max(gres_bias_corrected, gres_bias_corrected - rel_bias_corrected, 0)
rel_bias_corrected <- rel_bias_corrected2
gres_bias_corrected <- gres_bias_corrected2
}
unc_bias_corrected <- unc + term_b
bss_bias_corrected <- (gres_bias_corrected - rel_bias_corrected) / unc_bias_corrected
#if (round(bs, 8) == round(bs_check_gres, 8) & round(bs_check_gres, 8) == round((rel_bias_corrected - gres_bias_corrected + unc_bias_corrected), 8)) {
# cat("No error found \ n")
# cat("BS = REL - GRES + UNC = REL_lessbias - GRES_lessbias + UNC_lessbias \ n")
#}
invisible(list(rel = rel, res = res, unc = unc, bs = bs, bs_check_res = bs_check_res, bss_res = bss_res, gres = gres, bs_check_gres = bs_check_gres, bss_gres = bss_gres, rel_bias_corrected = rel_bias_corrected, gres_bias_corrected = gres_bias_corrected, unc_bias_corrected = unc_bias_corrected, bss_bias_corrected = bss_bias_corrected, nk = nk, fkbar = fkbar, okbar = okbar, bins = bins, pred = pred, obs = obs))
}
}
#'@rdname BrierScore
#'@export
.BrierScore <- function(exp, obs, thresholds = seq(0, 1, 0.1)) {
if (max(exp) > 1 || min(exp) < 0) {
stop("Parameter 'exp' contains predictions outside [0,1] range. Are you certain this is a probability forecast?")
} else if (max(obs) != 1 && min(obs) != 0) {
.message("Binary events in 'obs' must be either 0 or 1. Are you certain this is a binary event?")
} else {
nbins <- length(thresholds) - 1 # Number of bins
n <- dim(exp)[1] # Number of observations
ens_mean <- rowMeans(exp, na.rm = TRUE)
n.ens <- seq(1, dim(exp)[2], 1) # Number of ensemble members
bins <- as.list(paste("bin", 1:nbins, sep = ""))
for (i in 1:nbins) {
if (i == nbins) {
bins[[i]] <- list(which(ens_mean >= thresholds[i] & ens_mean <= thresholds[i + 1]))
} else {
bins[[i]] <- list(which(ens_mean >= thresholds[i] & ens_mean < thresholds[i + 1]))
}
}
fkbar <- okbar <- nk <- array(0, dim = nbins)
for (i in 1:nbins) {
nk[i] <- length(bins[[i]][[1]])
fkbar[i] <- sum(ens_mean[bins[[i]][[1]]]) / nk[i]
okbar[i] <- sum(obs[bins[[i]][[1]]]) / nk[i]
}
fkbar[fkbar == Inf] <- 0
okbar[is.nan(okbar)] <- 0
obar <- sum(obs) / length(obs)
relsum <- ressum <- relsum1 <- ressum1 <- term1 <- term1a <- term2 <- term2a <- 0
for (i in 1:nbins) {
if (nk[i] > 0) {
relsum <- relsum + nk[i] * (fkbar[i] - okbar[i]) ^ 2
ressum <- ressum + nk[i] * (okbar[i] - obar) ^ 2
for (j in 1:nk[i]) {
term1 <- term1 + (ens_mean[bins[[i]][[1]][j]] - fkbar[i]) ^ 2
term2 <- term2 + (ens_mean[bins[[i]][[1]][j]] - fkbar[i]) * (obs[bins[[i]][[1]][j]] - okbar[i])
}
}
}
}
rel <- relsum / n
res <- ressum / n
unc <- obar * (1 - obar)
#bs <- apply(ens, MARGIN = 2, FUN = function(x) sum((x - obs)^2) / n)
bs <- sum((rowMeans(exp, na.rm = T) - obs) ^ 2) / n
bs_check_res <- rel - res + unc
bss_res <- (res - rel) / unc
gres <- res - term1 * (1 / n) + term2 * (2 / n) # Generalized resolution
bs_check_gres <- rel - gres + unc # BS using GRES
bss_gres <- (gres - rel) / unc # BSS using GRES
#
# Estimating the bias-corrected components of the BS
#
term3 <- array(0, nbins)
for (i in 1:nbins) {
term3[i] <- (nk[i] / (nk[i] - 1)) * okbar[i] * (1 - okbar[i])
}
term_a <- sum(term3, na.rm = T) / n
term_b <- (obar * (1 - obar)) / (n - 1)
rel_bias_corrected <- rel - term_a
gres_bias_corrected <- gres - term_a + term_b
if (rel_bias_corrected < 0 || gres_bias_corrected < 0) {
rel_bias_corrected2 <- max(rel_bias_corrected, rel_bias_corrected - gres_bias_corrected, 0)
gres_bias_corrected2 <- max(gres_bias_corrected, gres_bias_corrected - rel_bias_corrected, 0)
rel_bias_corrected <- rel_bias_corrected2
gres_bias_corrected <- gres_bias_corrected2
}
unc_bias_corrected <- unc + term_b
bss_bias_corrected <- (gres_bias_corrected - rel_bias_corrected) / unc_bias_corrected
#if (round(bs, 8) == round(bs_check_gres, 8) & round(bs_check_gres, 8) == round((rel_bias_corrected - gres_bias_corrected + unc_bias_corrected), 8)) {
# cat("No error found \ n")
# cat("BS = REL - GRES + UNC = REL_lessbias - GRES_lessbias + UNC_lessbias \ n")
#}
invisible(list(rel = rel, res = res, unc = unc, bs = bs,
bs_check_res = bs_check_res, bss_res = bss_res, gres = gres,
bs_check_gres = bs_check_gres, bss_gres = bss_gres,
rel_bias_corrected = rel_bias_corrected,
gres_bias_corrected = gres_bias_corrected,
unc_bias_corrected = unc_bias_corrected,
bss_bias_corrected = bss_bias_corrected))
}