-
Notifications
You must be signed in to change notification settings - Fork 7
/
simulate_bankrupt.R
73 lines (63 loc) · 1.98 KB
/
simulate_bankrupt.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
library(igraph)
# source('~/Desktop/network/judge_bankrupt.R')
source('judge_bankrupt.R')
simulate_bankrupt <- function(network, method = 'random', type = 'banks',
target_policy = FALSE, print_out=FALSE) {
G <- network
network_size <- length(V(G))
if (method == 'biggest'){
node_degree = degree(G, mode='out') + degree(G, mode='in')
initial_point <- which(node_degree==max(node_degree))
} else{
initial_point <- as.numeric(sample(V(G), 1))
}
bankrupt_bank = c()
this_batch_bankrupt = c()
bankrupt_bank = append(bankrupt_bank, initial_point)
this_batch_bankrupt = append(this_batch_bankrupt, initial_point)
count_batch = 1
number_bankrupt = 1
simulate = TRUE
while (simulate == TRUE){
this_batch_neighbor = c()
if (print_out == TRUE){
cat('This batch bankrupt banks: ')
cat(this_batch_bankrupt)
cat('\n')
}
for (i in this_batch_bankrupt){
nodes_out <- as.numeric(neighbors(G, i, 'out'))
this_batch_neighbor = append(this_batch_neighbor, nodes_out)
}
this_batch_neighbor = unique(this_batch_neighbor)
if (print_out == TRUE){
cat('This batch neighbot: ')
cat(this_batch_neighbor)
cat('\n')
}
this_batch_bankrupt = judge_bankrupt(G, this_batch_neighbor, bankrupt_bank, target_policy = target_policy)
this_batch_bankrupt = unique(this_batch_bankrupt)
if (print_out == TRUE){
cat('This batch bankrupt (after): ')
cat(this_batch_bankrupt)
cat('\n')
}
bankrupt_bank = append(bankrupt_bank, this_batch_bankrupt)
bankrupt_bank = unique(bankrupt_bank)
if (print_out == TRUE){
cat('The bankrupt (total): ')
cat(bankrupt_bank)
cat('\n')
}
if (length(bankrupt_bank) == number_bankrupt){
simulate = FALSE
} else{
number_bankrupt = length(bankrupt_bank)
}
}
if (type == 'banks'){
return (bankrupt_bank)
} else{
return (length(bankrupt_bank))
}
}