-
Notifications
You must be signed in to change notification settings - Fork 6
/
Functions_Measures.R
212 lines (140 loc) · 7.14 KB
/
Functions_Measures.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
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
#### measure of contribution volatility ####
#**********************************************************
# What presures the political system?
# 1. ERC rate rises unexpectedly in a relatively short period of time. This threats budget planning.
# 2. ERC rate rises to a very high level, even through a relatively long period of time, that is unaffordable to the plan sponsor.
# This threats the affordability and may cause benefit cuts, tax increase, and crowding out expenditure on other public services.
# Questions we may want to ask plan sponsor/policymakers:
# How big is the rise in ERC rate in a short period time that can threat budget planning?
# How big is the ERC rate that can threat the affordability?
# General measure of volatility
# - standard devation of year-to-year change in
# Measures of sharp rise of ERC rate in a short time period
# - max 5-year change in ERC rate in each simulation, median over all simulations
# - max 10-year change in ERC rate in each simulation, median over all simulations
# - max deviation from 5/10 year moving average. For ERC rate and ERC
# - probability(over all simulations) of ERC rate rising by a% in 5/10 years, through year x
# Measure of high ERC/ERC rate level
# - probability of ERC exceeding 2*NC in any of the years through year x
# - probability of ERC rate exceeding a% in any of the years through year x
calc_FR.pctless <- function(df_results, FR.pct = c(40, 60)){
# df_results <- results.stch
# FR.pct = c(40, 60)
runname <- df_results$runname[1]
fn <- function(df_results, FR.pct.single){
df_results %>%
group_by(sim) %>%
mutate(FR.pctless = cumany(FR_MA <= FR.pct.single)) %>%
select(sim, year, FR.pctless) %>%
group_by(year) %>%
summarize_each(funs(100 * sum(., na.rm = T)/n()), -sim) %>%
plyr::rename(c("FR.pctless" = paste0("FR", FR.pct.single,"less")))
}
sapply(FR.pct, fn, df_results = df_results, simplify = F) %>% join_all %>%
mutate(runname = runname) %>%
select(runname, everything())
}
calc_FR.pctmore <- function(df_results, FR.pct = c(80, 95), rolling = FALSE){
# df_results <- results.stch
# FR.pct = c(40, 60)
runname <- df_results$runname[1]
fn <- function(df_results, FR.pct.single){
df_results %>%
group_by(sim) %>%
mutate(rolling = rolling,
FR.pctmore = ifelse(rolling, cumany(FR_MA >= FR.pct.single), (FR_MA >= FR.pct.single))) %>%
select(sim, year, FR.pctmore) %>%
group_by(year) %>%
summarize_each(funs(100 * sum(., na.rm = T)/n()), -sim) %>%
plyr::rename(c("FR.pctmore" = paste0("FR", FR.pct.single,"more")))
}
sapply(FR.pct, fn, df_results = df_results, simplify = F) %>% join_all %>%
mutate(runname = runname) %>%
select(runname, everything())
}
calc_ERCsharpRise <- function(df_results, ERC.pct = c(5, 10)){
# df_results <- results.stch
# FR.pct = c(40, 60)
runname <- df_results$runname[1]
fn <- function(df_results, ERC.pct.single){
df_results %>%
group_by(sim) %>%
mutate(ERC.ChgPts5y = ERC_PR - lag(ERC_PR, 5), # year1-5 change in pct points
ERC.ChgPts5y = na2zero(ERC.ChgPts5y),
ERC.ChgPts5y.Xmore = cumany(ERC.ChgPts5y >= ERC.pct.single)) %>%
select(sim, year, ERC.ChgPts5y.Xmore) %>%
group_by(year) %>%
summarize_each(funs(100 * sum(., na.rm = T)/n()), -sim) %>%
plyr::rename(c("ERC.ChgPts5y.Xmore" = paste0("ERC.ChgPts5y.", ERC.pct.single,"more")))
}
sapply(ERC.pct, fn, df_results = df_results, simplify = F) %>% join_all %>%
mutate(runname = runname) %>%
select(runname, everything())
}
calc_highERC <- function(df_results, ERC.pct = c(30, 50)){
# df_results <- results.stch
# FR.pct = c(40, 60)
runname <- df_results$runname[1]
fn <- function(df_results, ERC.pct.single){
df_results %>%
group_by(sim) %>%
mutate(ERC_PR.Xmore = cumany(ERC_PR >= ERC.pct.single)) %>%
select(sim, year, ERC_PR.Xmore) %>%
group_by(year) %>%
summarize_each(funs(100 * sum(., na.rm = T)/n()), -sim) %>%
plyr::rename(c("ERC_PR.Xmore" = paste0("ERC_PR.", ERC.pct.single,"more")))
}
sapply(ERC.pct, fn, df_results = df_results, simplify = F) %>% join_all %>%
mutate(runname = runname) %>%
select(runname, everything())
}
calc_qts <- function(df_results, varname, qts = c(0.1, 0.25, 0.5, 0.75, 0.9)){
# df_results <- results.stch
# qts = c(0.1, 0.25, 0.5, 0.75, 0.9)
# varname = "FR"
# qts.single = 0.1
runname <- df_results$runname[1]
df_results %<>% select(runname, sim, year, one_of(varname)) %>%
dplyr::rename_("var" = varname)
fn <- function(df_results, qts.single){
df_results %>%
group_by(year) %>%
summarize(var = quantile(var, qts.single)) %>%
plyr::rename(c("var" = paste0(varname, ".q", qts.single)))
}
sapply(qts, fn, df_results = df_results, simplify = F) %>% join_all %>%
mutate(runname = runname) %>%
select(runname, everything())
}
get_df_results <- function(folderName, fileName){
fileName <- paste0(folderName, fileName)
load(fileName)
return(outputs_list$results)
}
get_measureList <- function(df_results){
results.stch <- df_results %>% filter(sim > 0)
runname <- results.stch$runname[1]
prob.FR.pctless <- results.stch %>% calc_FR.pctless
prob.FR.pctmore <- results.stch %>% calc_FR.pctmore(rolling = T)
prob.ERCsharpRise <- results.stch %>% calc_ERCsharpRise
prob.ERChighERC <- results.stch %>% calc_highERC
FR.qts <- results.stch %>% calc_qts("FR")
ERC_PR.qts <- results.stch %>% calc_qts("ERC_PR")
assign(paste0("RiskMeasures_", runname),
list(
prob.FR.pctless = prob.FR.pctless,
prob.FR.pctmore = prob.FR.pctmore,
prob.ERCsharpRise = prob.ERCsharpRise,
prob.ERChighERC = prob.ERChighERC,
FR.qts = FR.qts ,
ERC_PR.qts = ERC_PR.qts))
# return(get(paste0("RiskMeasures_", runname)))
# do.call(save, list(paste0("RiskMeasures_", runname), file=paste0(folderName, "RiskMeasures_", runname, ".RData")))
#do.call( return, list(get(paste0("RiskMeasures_", runname))))
return(get(paste0("RiskMeasures_", runname)))
}
# RiskMeasures_F50mature1_gn1 <- get_df_results("IO_M2.1_new/", "Outputs_D1F050-mature1_gn1.RData") %>% get_measureList()
# RiskMeasures_F50mature2_gn1 <- get_df_results("IO_M2.1_new/", "Outputs_D1F050-mature2_gn1.RData") %>% get_measureList()
#
# RiskMeasures_F75mature1_gn1 <- get_df_results("IO_M2.1_new/", "Outputs_D1F075-mature1_gn1.RData") %>% get_measureList()
# RiskMeasures_F75mature2_gn1 <- get_df_results("IO_M2.1_new/", "Outputs_D1F075-mature2_gn1.RData") %>% get_measureList()