/
diversity.R
295 lines (228 loc) · 8.76 KB
/
diversity.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#Diversity
#apply simulation
set.seed(13)
name<-sample(1:500,replace=FALSE) #use larger name space to avoid from comp being overflown also sometimes TRUE or FALSE needed because
#single vertex graph produced. we deal with this by making sure the name space is much larger than the number of trees(not ideal)
master_list<-create_list(name,100)
graph_union<-create_union(master_list)
#plot(graph_union,curved=0.1,vertex.size=1*V(graph_union)$wealth_vector_trickle,layout=layout.davidson.harel)
graph_union<-delete_edge_attr(graph_union, "weight" )
score<-page_rank(graph_union,directed=TRUE)
#notes the root_wealth is 100 for all
# the branch attribution is even
#compute fairness
fair(V(graph_union)$wealth_vector_trickle,1/score$vector) #fairness is bad
#barplots
barplot(V(graph_union)$wealth_vector_trickle,names.arg=V(graph_union)$name)
barplot(score$vector)
#plothist
hist(V(graph_union)$wealth_vector_trickle)
hist(score$vector)
#quantiles
par(xpd=F)
plot(Lc(V(graph_union)$wealth_vector_trickle),xlab="percent of people",ylab="percent of wealth")
length(V(graph_union)$wealth_vector_trickle) #200 vertices
sum(Lc(V(graph_union)$wealth_vector_trickle)$L>0.5) #28 owns more than 50% of wealth
sum(Lc(V(graph_union)$wealth_vector_trickle)$L>0.6) #22 owns more than 40% of wealth
Lc(V(graph_union)$wealth_vector_trickle)$p[which(Lc(V(graph_union)$wealth_vector_trickle)$L>0.5)[1]] #0.865 percent of ppl hold 0.5 percent of wealth
Lc(V(graph_union)$wealth_vector_trickle)$p[which(Lc(V(graph_union)$wealth_vector_trickle)$L>0.7)[1]] #0.925 percent of ppl hold 0.7 percent of wealth
o<-order(V(graph_union)$wealth_vector_trickle)
#V(graph_union)[o][which(Lc(V(graph_union)$wealth_vector_trickle)$L>0.5)[1]]
q3<-V(graph_union)[o]$wealth_vector_trickle[which(Lc(V(graph_union)$wealth_vector_trickle)$L>0.9)[1]]
q2<-V(graph_union)[o]$wealth_vector_trickle[which(Lc(V(graph_union)$wealth_vector_trickle)$L>0.6)[1]]
q1<-V(graph_union)[o]$wealth_vector_trickle[which(Lc(V(graph_union)$wealth_vector_trickle)$L>0.3)[1]]
quant<-c(q1,q2,q3)
quant
#prepare quantiles
#quant<-quantile(V(graph_union)$wealth_vector_trickle,probs=c(0.6,0.9,1))
#visualise
hist(V(graph_union)$wealth_vector_trickle)
abline(v=quant)
WC_1<-V(graph_union)[V(graph_union)$wealth_vector_trickle<=quant[1]]$wealth_vector_trickle
names(WC_1)<-V(graph_union)[V(graph_union)$wealth_vector_trickle<=quant[1]]$name
WC_2<-V(graph_union)[((V(graph_union)$wealth_vector_trickle<=quant[2]) +(V(graph_union)$wealth_vector_trickle>quant[1]))==2 ]$wealth_vector_trickle
names(WC_2)<-V(graph_union)[((V(graph_union)$wealth_vector_trickle<=quant[2]) +(V(graph_union)$wealth_vector_trickle>quant[1]))==2 ]$name
WC_3<-V(graph_union)[((V(graph_union)$wealth_vector_trickle<=quant[3]) +(V(graph_union)$wealth_vector_trickle>quant[2]))==2 ]$wealth_vector_trickle
names(WC_3)<-V(graph_union)[((V(graph_union)$wealth_vector_trickle<=quant[3]) +(V(graph_union)$wealth_vector_trickle>quant[2]))==2 ]$name
#set up
rank_before<-rank(c(WC_1,WC_2,WC_3),ties.method="first") #before
hist(c(WC_1,WC_2,WC_3)) #before
sigma1<-min(WC_2)-max(WC_1)
sigma2<-min(WC_3)-max(WC_2)
tau1<-mean(WC_2)-mean(WC_1)
tau2<-mean(WC_3)-mean(WC_2)
tau1<-quantile(WC_2)[2]-quantile(WC_1)[4]
tau2<-quantile(WC_3)[2]-quantile(WC_2)[4]
sigma1
sigma2
tau1
tau2
sum(V(graph_union)$wealth_vector_trickle)#1383.947
#hist(WC_1)
#mean(WC_1)
#hist(WC_2)
#mean(WC_2)
#hist(WC_3)
#mean(WC_3)
#WC_2<-V(graph_union)[((V(graph_union)$wealth_vector_trickle<=quant[2]) +(V(graph_union)$wealth_vector_trickle>quant[1]))==2 ]
#WC_3<-V(graph_union)[((V(graph_union)$wealth_vector_trickle<=quant[3]) +(V(graph_union)$wealth_vector_trickle>quant[2]))==2 ]
reward_pool<-0.1*sum(V(graph_union)$wealth_vector_trickle)
reward_pool
#if(0.6*reward_pool<sigma1){
# WC_1<-WC_1+0.6*reward_pool
# print("good sigma1")
#}else{
# WC_1<-WC_1+tau1
#}
#if(0.3*reward_pool<sigma2){
# WC_2<-WC_2+0.3*reward_pool
# print("good sigma2")
#}else{
# WC_2<-WC_2+tau2
#}
#WC_3<-WC_3+0.1*reward_pool
#choose between using tau1 or using some percentage obviously if the percentage is less than tau 1 then it is better
tau1
tau2
0.6*reward_pool/length(WC_1)
0.3*reward_pool/length(WC_2)
0.1*reward_pool/length(WC_3)
1/(sum(WC_1)/sum(c(WC_1,WC_2,WC_3)) ) *reward_pool
1/(sum(WC_2)/sum(c(WC_1,WC_2,WC_3)) )*reward_pool
1/(sum(WC_3)/sum(c(WC_1,WC_2,WC_3)) ) *reward_pool
reward_pool
#if we use tau1
if(tau1*length(WC_1)>reward_pool){
print("tau1*length(WC_1)>reward_pool")
WC_1<-WC_1+reward_pool/length(WC_1)
reward_pool<-0
}else{
WC_1<-WC_1+tau1
reward_pool<-reward_pool-tau1*length(WC_1)
}
if(reward_pool!=0){
if(tau2*length(WC_2)>reward_pool){
WC_2<-WC_2+reward_pool/length(WC_1)
}else{
WC_2<-WC_2+tau2
reward_pool<-reward_pool-tau1*length(WC_2)
}
}
if(reward_pool!=0){
if(tau2<reward_pool/length(WC_3)){
WC_3<-WC_3+tau2/2
reward_pool<-reward_pool-length(WC_3)*(tau2/2)
WC_1<-WC_1+reward_pool/length(c(WC_1,WC_2,WC_3))
WC_2<-WC_2+reward_pool/length(c(WC_1,WC_2,WC_3))
WC_3<-WC_3+reward_pool/length(c(WC_1,WC_2,WC_3))
}else{
WC_3<-reward_pool/length(WC_3)
reward_pool<-0
}
}
reward_pool
sigma1*length(WC_1) #amount paid if bad sigma1
sigma2*length(WC_2) #amount paid if bad sigma2
tau1*length(WC_1)
tau2*length(WC_2)
ineq(V(graph_union)$wealth_vector_trickle,type="Gini")
ineq(c(WC_1,WC_2,WC_3),type="Gini")
plot(Lc(V(graph_union)$wealth_vector_trickle))
plot(Lc(c(WC_1,WC_2,WC_3)))
#investigation as to whether the ranking has changed(keep in mind that it should be a function of lorenz)
rank_after<-rank(c(WC_1,WC_2,WC_3),ties.method="first") #after
rank_before==rank_after
sum(rank_before==rank_after)/length(V(graph_union)$wealth_vector_trickle)
hist(c(WC_1,WC_2,WC_3))
#try with missing values
#this function uses Lc
#x: wealth vector
#prob: proportion of wealth
#percent: percent of reward pool
diversity<-function(x,prob=c(0.3,0.9),percent=0.1){
#set parameters
if(prob[2]<=prob[1]){
stop("prob2<prob1")
}
#ensure x is all positive
if(sum(x<0)!=0){
stop("Not every element is positive")
}
#make sure there are names
if(is.null(names(x))){
names(x)<-seq(1,length(x))
}
#set order
o<-order(x)
q1<-tail(x[o][which(Lc(x)$L<prob[1])],1)
q2<-tail(x[o][which(Lc(x)$L<prob[2])],1)
quant<-c(q1,q2)
#define wealth_classes
WC_1<-x[x<=quant[1]]
WC_2<-x[((x<=quant[2]) +(x>quant[1]))==2]
WC_3<-x[ (x>quant[2]) ]
#change ranking
#-rank is the scoreboard, order is the index
rank_before<-rank(c(WC_1,WC_2,WC_3)) #before
#make taus(how much to add to a particular class)
tau1<-quantile(WC_2)[2]-quantile(WC_1)[4]
tau2<-quantile(WC_3)[2]-quantile(WC_2)[4]
#tau2 should be defined such that it is less than tau1
#make reward pool
reward_pool<-percent*sum(x)
#if we use tau1
if(tau1*length(WC_1)>reward_pool){
print(paste("giving all of reward pool to WC_1","value per unit=",reward_pool/length(WC_1),"total=",reward_pool) )
WC_1<-WC_1+reward_pool/length(WC_1)
reward_pool<-0
}else{
print(paste("giving tau1","value per unit=",tau1,"total=",tau1*length(WC_1)) )
WC_1<-WC_1+tau1
reward_pool<-reward_pool-tau1*length(WC_1)
}
if(reward_pool!=0){
if(tau2*length(WC_2)>reward_pool){
if(reward_pool/length(WC_2)>tau1/length(WC_1)){
print(paste("giving all of reward pool to WC_2","value per unit=",reward_pool/length(WC_2),"total=",reward_pool) )
#WC_2<-WC_2+0.8*tau1
WC_2<-WC_2+
reward_pool<-0
}
}else{
#if(tau1/length(WC_1)<tau2/length(WC_2)){
print(paste("giving tau2","value per unit=",tau2,"total=",tau2*length(WC_2)) )
WC_2<-WC_2+tau2
reward_pool<-reward_pool-tau2*length(WC_2)
#}else{
# print(paste("giving tau1 because tau2>tau1","value per unit=",tau1,"total=",tau1*length(WC_2)) )
# WC_2<-WC_2+tau1
# reward_pool<-reward_pool-tau1*length(WC_2)
#}
}
}
#add this tau1>tau2 thing to 3rd
if(reward_pool!=0){
if(tau2<reward_pool/length(WC_3)){
WC_3<-WC_3+tau2/2
reward_pool<-reward_pool-length(WC_3)*(tau2/2)
WC_1<-WC_1+reward_pool/length(c(WC_1,WC_2,WC_3))
WC_2<-WC_2+reward_pool/length(c(WC_1,WC_2,WC_3))
WC_3<-WC_3+reward_pool/length(c(WC_1,WC_2,WC_3))
}else{
WC_3<-WC_3+reward_pool/length(WC_3)
reward_pool<-0
}
}
#measure changes in ranking
rank_after<-rank(c(WC_1,WC_2,WC_3)) #after
#ties first still doesnt make things as tho they dont change
#set parameters back
#par(xpd=T)
return(list(x=x,gini_before=ineq(x,type="Gini"),WC=c(WC_1,WC_2,WC_3),gini_after=ineq(c(WC_1,WC_2,WC_3),type="Gini"),rank_change=sum(rank_before==rank_after)/length(x),quant=quant ))
}
#reward pool just doesn
debug(diversity)
debug(diversity)
diversity(rgamma(100,1),prob=c(0.6,0.2))
diversity(rgamma(100,1,2))
diversity(rpareto(100000,2,2))