-
Notifications
You must be signed in to change notification settings - Fork 9
/
master.R
201 lines (188 loc) · 7.55 KB
/
master.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
# master script for conducting ABM experiments
# rm(list=ls()) # make sure to always run this line of code and see that the next two
# lines of code work without error, ensuring that the working of the model is not
# dependent on anything in your global workspace, if it is, then you need to create
# whatever is in your global workpace in the code
args <- commandArgs(trailingOnly = TRUE)
cores <- as.integer(args[1])
if (length(args) < 2) {
plotting = FALSE
} else {
plotting <- as.logical(args[2])
}
message(paste("Number of cores", cores))
source("main.R")
# install.packages("devtools")
# devtools::install_github("JohnNay/sa")
library(sa)
test <- FALSE
estimate_replicates <- FALSE
if(test){
main(c(runif(3, min = 0.0001, max = 0.9999), 1, runif(2, min = 0.0001, max = 0.9999)), visu =T)
main(c(runif(3, min = 0.0001, max = 0.9999), 0, runif(2, min = 0.0001, max = 0.9999)))
}
##############################################################################
## Param distributions to draw from
##############################################################################
input_values <- lapply(list(seg = NA, ideo = NA, risk.tak = NA),
function(x) list(random_function = "qunif",
ARGS = list(min = 0.0001, max = 0.9999)))
input_values$true.model <- list(random_function = "qbinom",
ARGS = list(size = 1, prob = 0.5))
input_values$n.edg <- list(random_function = "qunif",
ARGS = list(min = 0.0001, max = 0.9999))
input_values$n.traders <- list(random_function = "qunif",
ARGS = list(min = 0.0001, max = 0.9999))
if(estimate_replicates){
##############################################################################
## Estimate number of replicates needed for each param set
##############################################################################
iters_res <- compute_iters(main, input_values, "converg",
initial_iters = 1,
max_iters = 20,
sample_count = 60, parallel = TRUE, cores = 30,
measure = "sd", thresh = 0.05, repeats = 100)
save(iters_res, file = "output/iters_res.Rda")
plot(iters_res, ylab = "100 Standard Deviations",
outcome = "Samples of 60 Convergence of Beliefs Outcomes")
summary(iters_res)
}
##############################################################################
## Main sens analysis # Standardized Regression Coefficient
##############################################################################
future <- TRUE
past <- TRUE
sample_count <- 80
if(past){
main2 <- function(parameters,
iterations = 10,
burn.in = 51,
n.seq = 14,
horizon = 6,
out){
main(parameters = parameters,
out = out,
iterations = iterations,
burn.in = burn.in,
n.seq = n.seq,
horizon = horizon,
nyears = burn.in + n.seq * horizon)
}
if(file.exists("output/src.Rda")){
load("output/src.Rda")
src <- pc_sa(abm = main2,
input_values = input_values,
previous_pc_sa = list(src),
out = "converge",
iterations = 5,
sample_count = sample_count,
nboot = 1000,
parallel = TRUE,
cores = cores,
rank = TRUE, method = "src")
} else{
src <- pc_sa(abm = main2,
input_values = input_values,
out = "converge",
iterations = 5,
sample_count = sample_count,
nboot = 1000,
parallel = TRUE,
cores = cores,
rank = TRUE, method = "src")
}
save(src, file = "output/src.Rda")
if (plotting){
pdf("output/sa_past.pdf", width=10, height=10)
plot(src, outcome_var = paste0("Convergence of Beliefs in Past Scenario \n (R^2= ",
round(src@r_squared, 2), " , n=", length(src@sims),")"))
dev.off()
}
}
if(future){
main2 <- function(parameters,
iterations = 10,
burn.in = 135,
n.seq = 14,
horizon = 6,
out){
main(parameters = parameters,
iterations = iterations,
out = out,
burn.in = burn.in,
n.seq = n.seq,
horizon = horizon,
nyears = burn.in + n.seq * horizon)
}
if(file.exists("output/src_future.Rda")){
load("output/src_future.Rda")
src_future <- pc_sa(abm = main2,
input_values = input_values,
previous_pc_sa = list(src_future),
out = "converge",
iterations = 5,
sample_count = sample_count,
nboot = 1000,
parallel = TRUE,
cores = cores,
rank = TRUE, method = "src")
} else {
src_future <- pc_sa(abm = main2,
input_values = input_values,
out = "converge",
iterations = 5,
sample_count = sample_count,
nboot = 1000,
parallel = TRUE,
cores = cores,
rank = TRUE, method = "src")
}
save(src_future, file = "output/src_future.Rda")
if (plotting){
pdf("output/sa_future.pdf", width=10, height=10)
plot(src_future, outcome_var = paste0("Convergence of Beliefs in Future Scenario \n (R^2= ",
round(src_future@r_squared, 2), " , n=",
length(src_future@sims),")"))
dev.off()
}
}
# ##############################################################################
# ## collecting the value of outcome.converge for the same value of the parameters
# # we test on the sa, and plot them as an histogram
# ##############################################################################
#
#
# cores <- parallel::detectCores() - 1
# sample_count <- 7000
#
# input_set <- create_set(input_values, input_names, sample_count = sample_count,
# constraints = "none")
# # Simulation runs with generated input factor sets:
# doParallel::registerDoParallel(cores = cores)
# source("main.R")
# sim <- foreach::`%dopar%`(foreach::foreach(i=seq(nrow(input_set)), .combine='c'), {
# main(as.numeric(input_set[i, ]), out = "converg")
# })
# d <- data.frame(convergence = sim, input_set)
# save(d, file = "output/d.Rda")
#
# library(ggplot2)
# ggplot2::ggplot(d, aes(x = convergence)) + geom_histogram(binwidth = 0.04) +
# xlim(c(-0.6, 0.6)) +
# ylab("Count") + xlab("Convergence of Beliefs") +
# ggtitle(paste(sample_count,
# "Convergence of Belief Outcomes with LHS Input Parameters")) +
# theme_bw()
#
# d$true.model <- factor(d$true.model)
# library(plyr)
# cdat <- plyr::ddply(d, "true.model", summarize, outcome_mean = mean(convergence))
# ggplot2::ggplot(d, aes(x = convergence, fill = true.model)) +
# geom_density(alpha = 0.3) +
# # xlim(c(-0.6, 0.6)) +
# ylab("Density") + xlab("Convergence of Beliefs") +
# ggtitle(paste(sample_count,
# "Convergence of Belief Outcomes with LHS Input Parameters")) +
# geom_vline(data=cdat, aes(xintercept= outcome_mean, color = true.model),
# linetype="dashed", size=1) +
# theme_bw()