-
Notifications
You must be signed in to change notification settings - Fork 7
/
ER_tp.R
70 lines (61 loc) · 1.97 KB
/
ER_tp.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
#source('~/Desktop/network/create_network.R')
#source('~/Desktop/network/judge_bankrupt.R')
#source('~/Desktop/network/simulate_bankrupt.R')
source('create_network.R')
source('judge_bankrupt.R')
source('simulate_bankrupt.R')
network_size <- 1000
simulation_times <- 100 # 100
x_average_dgree <- seq(0, 10.1, 1) # 0.2
prob <- x_average_dgree/(network_size -1)
contagion_threshould <- 0.05
threshould <- network_size * contagion_threshould
main <- function(){
y_prob <- c()
y_exte <- c()
for (j in prob) {
count_contagion <- 0
sum_percentages <- 0
cat('Doing simulation on Average Degree: ')
cat(j*(network_size-1))
cat('\n')
for (i in 1:simulation_times){
if (i==100){
cat('No.')
cat(i)
cat('\n')
} else if (i %% 10 == 0) {
cat('No.')
cat(i)
cat('...')
}
G <- create_network(network_size, j, type = 'er')
r <- simulate_bankrupt(G, method = 'biggest', type = 'num', target_policy = TRUE)
r <- as.numeric(r)
if (r > threshould){
count_contagion <- count_contagion +1
percentage_cont <- r/network_size
sum_percentages <- sum_percentages + percentage_cont
}
}
proba_contagion <- count_contagion / simulation_times
if (count_contagion != 0){
exten_contagion <- sum_percentages / count_contagion
} else{
exten_contagion <- 0
}
y_prob <- cbind(y_prob, proba_contagion)
y_exte <- cbind(y_exte, exten_contagion)
}
return(do.call(rbind, Map(data.frame, y_prob=y_prob, y_exte=y_exte)))
}
system.time({
results <- main()
cat('Saving the results ... ')
cat('\n')
write.table(results,file="results_er_target_0.1.csv", sep=',', quote=F,col.name=F,row.names=F)
plot_the_figure(x_average_dgree, results$y_prob, results$y_exte,
network_name = 'ER Network',
xlab = 'Average Degree (Connectivity)',
notes = 'Random Bankrupt with Target Policy')
})