forked from jeremyzechar/LoveThomas2013GRL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
molchanDiagram.R
43 lines (40 loc) · 1.81 KB
/
molchanDiagram.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
42
43
# read in catalog and decide in which size bin to group the eqks -- Marcus did
# this already w/ EQs-year, EQs-mon, EQs-day
# read in alarm function of choice, must use same binning as binned catalog --
# for sunspots, can use SSN-mon or SSN-year; for activity can use AA-day,
# AA-mon, & AA-year
#
# binned catalog and alarm function are now arrays of the same length
# from the alarm function array get a unique list of values; these go into a
# thresholds list
# instantiate arrays for tau and nu; they can be the same length as the number
# of unique thresholds + 1; the first point is nu = 1, tau = 0.
# now go through each threshold value (from the biggest to the smallest);
# tau = sum(which(alarmfunction > threshold)) / length(alarmfunction);
# nu = 1 - sum(eqks[which(alarmfunction > threshold)]) / N, where N = sum(eqks)
molchanDiagram <- function(catalog, alarmFunction) {
df.AlarmFunction <- read.table(alarmFunction)
# the alarm value is stored in the final column
alarmFunction <- df.AlarmFunction[, ncol(df.AlarmFunction)]
cells <- length(alarmFunction)
df.Catalog <- read.table(catalog)
# the number of eqks is stored in the final column
eqks <- df.Catalog[, ncol(df.Catalog)]
N <- sum(eqks)
# print(N)
thresholds <- sort(unique(alarmFunction), decreasing = TRUE)
tau <- rep(0, length(thresholds))
nu <- rep(0, length(thresholds))
counter <- 0
for (threshold in thresholds){
counter <- counter + 1
tau[counter] <- length(which(alarmFunction >= threshold)) / cells
nu[counter] <- 1 - sum(eqks[which(alarmFunction >= threshold)]) / N
if ((counter - 1) %% 100 == 0){
print(paste0('(threshold, tau, nu) = (', threshold, ', ', tau[counter],
', ', nu[counter], ')'))
}
}
plot(tau, nu, xlab = 'alarm fraction', ylab = 'miss rate')
abline(1, -1)
}