-
Notifications
You must be signed in to change notification settings - Fork 20
/
f.atleastone.R
42 lines (29 loc) · 938 Bytes
/
f.atleastone.R
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
# a function to compute the probability of at least one event occurring in some period of time, given
# a vector representing a sequence of probabilities that (at least one) event will occur in each of
# a series of time steps of equal width.
#
# For example, if the hourly probabilities of rain for some 3-hour window are 88%, 76%, and %50, we
# could use:
#
# rain <- c(0.88, 0.76, 0.50)
#
# f.atleastone(rain)
#
# > f.atleastone(rain)
# [1] 0.9856
#
# If you've got a vector of probabilities of independent events that aren't sequenced, you can just use:
#
# 1 - (prod(1 - vector))
f.atleastone <- function(vector) {
p.conditional <- vector()
for (i in seq_along(vector)) {
if (i == 1) {
p.conditional[i] <- vector[i]
} else {
p.conditional[i] <- vector[i] * (1 - sum(p.conditional[1:i - 1]))
}
}
sigma <- sum(p.conditional)
return(sigma)
}