-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_randomize_MRIO_ixi.R
179 lines (143 loc) · 5.24 KB
/
1_randomize_MRIO_ixi.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
#'
#'
#'
#' @author Simon Schulte
#' Date: 2019-10-31 16:55:52
#'
#' Content:
#'
#' NOTE: This script was run on the BWunicluster in parallel on 12 nodes with 62000 MB
#' per node
#'
############################################################################## #
##### load packages ############################################################
############################################################################## #
sessionInfo()
library(data.table)
library(tidyverse)
library(parallel)
library(tictoc)
library(Rfast)
############################################################################## #
##### settings #################################################################
############################################################################## #
# set to TRUE when running on cluster
if(FALSE) {
setwd("./trade_split")
}
source("./settings.R")
source("./functions.R")
#path2data <- '/home/simon/Documents/PhD_PROSET/code/R/Import_shares/data'
midpoints_selected = c(5,9,23,32)
path2current_results <- file.path(path2results,
paste0(Sys.time() %>% format(format = "%Y-%m-%d_%H:%M:%S"),
"_Trade_Split_MC_results_ixi"))
dir.create(path2current_results)
file.copy(from = list.files(".", pattern = ".R$",
full.names = TRUE),
to = path2current_results)
n_countries <- 49
n_products <- 163
n_dim <- n_countries * n_products
# Functions --------------------------------------------------------------------
randomize_flows2 <- function(x,y) {
new_flow_mat <- matrix(0, nrow = length(y),
ncol = length(x),
dimnames = list(names(y),
names(x)))
#y = supply, x = use
yleft <- y
j <- 1
for (i in sample(1:length(x))) {
#cat(i, "")
# go randomly through all industries
xleft <- x[i]
while (xleft > 0 & j <= length(y)) {
# as long as industry i still requires supply
if (yleft[j] <= xleft) {
# supply of country j is smaller (or equal) than use of industry i
new_flow_mat[j,i] <- yleft[j]
xleft <- xleft - yleft[j]
j <- j + 1 # go to next country
} else {
# supply of country j is larger than use of industry i
new_flow_mat[j,i] <- xleft
yleft[j] <- yleft[j] - xleft
xleft <- 0
}
}
}
return(new_flow_mat)
}
############################################################################## #
##### load data #############################################################
############################################################################## #
data <- readRDS(file.path(path2data, "IOT_2011_ixi.RData"))
# Z matrix as array
Z <- data$Z %>% as.matrix %>%
array(., dim = c(n_products,n_countries,n_products,n_countries))
# Y matrix as array
Y <- data$Y %>%
array(., dim = c(n_products,n_countries,n_fdcat,n_countries)) %>%
.[,,1:4,]
data$Z <- data$Y <- NULL
# Characterizisation factors
EB3_midpoints <- readRDS(file.path(path2data, "EB34_midpoints.RData")) %>%
.[["matrix"]] %>%
.[, midpoints_selected]
# Pre-calculate C * S (to only do it once + save RAM)
CS <- t(EB3_midpoints) %*% data$S
rm(EB3_midpoints)
data$S <- NULL
gc()
# 1. declare function ----------------------------------------------------------
.trade_split_fun <- function(i, Z, Y) {
for (ic in 1:n_countries) {
for (ip in 1:n_products) {
s <- rowSums(Z[ip,-ic,,ic]) + rowSums(Y[ip,-ic,,ic])
if (sum(s) > 0) {
u <- c(colSums(Z[ip,-ic,,ic]), colSums(Y[ip,-ic,,ic]))
T_new <- randomize_flows2(u, s)
Z[ip,-ic,,ic] <- T_new[,1:n_products]
Y[ip,-ic,,ic] <- T_new[,(n_products+1):ncol(T_new)]
}
}
}
Z <- matrix(Z, nrow = n_dim, ncol = n_dim) # reshape Z to matrix
A <- calculate_A(Z, data$x)# A = Z*(xhat^-1)
rm(Z)
gc()
L <- calculate_L(A) # L = (I-A)^-1
rm(A)
gc()
product_fp <- CS %*% (L) # fp = C * S * L
rm(L)
gc()
# national fp's
national_fp <- product_fp %*% matrix(Y, nrow = n_dim, ncol = 4*n_countries) # fp = CSLY
#tic("4. Save product_fp")
# save results
saveRDS(product_fp, file.path(path2current_results,
paste0("product_fp", i, ".RData")))
#toc()
#tic("5. Save national_fp")
saveRDS(national_fp, file.path(path2current_results,
paste0("national_fp", i, ".RData")))
rm(national_fp, product_fp)
gc()
#toc()
return(NULL)
}
# 2. run function -----------------------------------------------------------------
n.cores <- 12 #10 works, 12 definitly too much
N <- 5000 #n.cores*500
RNGkind("L'Ecuyer-CMRG") # choose RNG suitable for parallel computing (see: https://www.r-bloggers.com/%F0%9F%8C%B1-setting-a-seed-in-r-when-using-parallel-simulation/)
# _a) mclapply -----
results <- mclapply(X = 1:N,
FUN = .trade_split_fun,
Z = Z, Y = Y,
mc.cores = n.cores,
mc.set.seed = TRUE)
# 3. save results --------------------------------------------------------------
#saveRDS(results, file.path(path2current_results, "results.RData"))
# THE END ---------------------------------------------------------------------