-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhi_li.R
595 lines (448 loc) · 25.3 KB
/
hi_li.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# Title : hi_li.R
# Description : This file contains the implementation of the Homogeneity index (HI) and Location Index (LI)
# Author : Ludovico Pinzari
# Usage : Run all the functions included in this file and use the function uni.hom and uni.loc (see notes)
# R version : 3.3 or later version
# Notes : see below
################################################################################################
# #
# HOMOGENEITY INDEX (HI) #
# #
################################################################################################
# The HI function [uni.hom], consists of two main functions: #
# #
# - [uni.concI] : computes the concentration index (CI) for a vector of observations #
# (e.g. the number of people in each quantile of the socioeconomic index). #
# #
# - [uni.div] : computes the divergence index (DI) for a probability distribution vector #
# (e.g. the proportion of people in each quantile of the socioeconomic index),#
# by calling two support functions: #
# #
# Divergence Index supporting functions #
# #
# - [uni.conv] : computes the convolution of two vectors #
# - [uni.corr] : computes the autocorrelation of a vector #
# #
# - [uni.divConst] : computes the DI of a bimodal distribution with maximum variance #
# #
################################################################################################
################################################################################################
# #
# LOCATION INDEX (LI) #
# #
################################################################################################
# The LI function [uni.loc],returns the position of the bins with the maximum concentration #
# by calling a single support function: #
# #
# - [uni.locVec] : returns the bin concentration vector for a probability distribution. #
# The bin concentration vector provides the concentration value for each #
# bin in the distribution. # #
# #
################################################################################################
#===============================================================================================#
#---------------------------------------------------------------------------------------------#
# #
# CONCENTRATION INDEX (CI) #
#---------------------------------------------------------------------------------------------#
#' @function uni.concI #
#' @description function to compute the Concentration Index for a vector #
#' @param p numeric vector with non-negative integer values. (1 x n) #
#' @return the Concentration Index (CI) of \code{p}: real number [0 1] #
#' @usage uni.concI(\code{p}) #
#' @examples #
#' uniform distribution #
#' p <- c(10,10,10,10,10,10,10,10,10,10) #
#' r <- uni.concI(p) r: 0 #
#' p <- c(50,0,0,0,0,0,0,0,0,0,50) #
#' r <- uni.concI(p) r: 0.88 #
#' p <- c(100,0,0,0,0,0,0,0,0,0) #
#' r <- uni.concI(p) r: 1 #
#' -------------------------------------------------------------------------------------------#
uni.concI <- function(p){
d <- length(p)
pop <- sum(p)
i <- 1
## compute the pdf
while(i <= d)
{
p[i] <- p[i]/pop
i <- i+1
}
p<-sort(p) ## sort the pdf vector in ascending order
## Compute the cumulative frequencies Lorenz Curve
lc <- rep(0,d+1)
i <- 1
while(i <= d)
{
if(i==1)
lc[2]<-lc[1]+p[1]
else
lc[i+1]<-lc[i]+p[i]
i<- i+1
}
## Compute the Area vector Under the Lorenz Curve
b <- 1/d ## base of the trapezoid
i <- 1
imp_area <- 1-b ## to normalize the result
la <- rep(0,d)
while( i <= d)
{
la[i] <- b*(lc[i]+lc[i+1])/(2*imp_area)
i <- i+1
}
## Compute the Lorenz Curve of the unifotm distr.
pu <- rep(1/d,d)
lcu <- rep(0,d+1)
i <- 1
while(i <= d)
{
if(i==1)
lcu[2] <- lcu[1]+pu[1]
else
lcu[i+1] <- lcu[i]+pu[i]
i<- i+1
}
## Compute the area under the Lorenz Curve of the unifoirm distribution
lau <- rep(0,d)
i<-1
while( i <= d)
{
lau[i] <- b*(lcu[i]+lcu[i+1])/(2*imp_area)
i <- i+1
}
## Compute the Zonoid Area (Area between the lorenz Curve and Uniform distribution)
Area <- 0
i<- 1
while( i <= d)
{
Area <- Area + lau[i]-la[i]
i <- i+1
}
## rounding the value
Area <-round(Area,4)
return(2*Area)
}
#---------------------------------------------------------------------------------------------#
# #
# DIVERGENCE INDEX SUPPORTING FUNCTIONS #
#---------------------------------------------------------------------------------------------#
#--------------------------------------------------------------------------------------------#
# #
# UNIDIMENSIONAL CONVOLUTION #
#--------------------------------------------------------------------------------------------#
#' @function uni.conv #
#' @description function to compute the convolution of two vectors #
#' @param x numeric vector representing polynomial coefficient (1 x m) #
#' @param y numeric vector representing polynomial coefficient (1 x n) #
#' @return the coefficient vector resulting from multiplying the polynomial represented #
#' by x by the polynomial represented by y (1 x m+n-1) #
#' @usage uni_concv(\code{x},\code{y}) #
#' @examples #
#' x <- c(0.25,0.25,0.25,0.25) #
#' y <- c(1,1,1,1) #
#' r <- uni.conv(x,y) r: num [1:7] 0.25 0.50 0.75 1.00 0.75 0.50 0.25 #
#' x <- c(1,0,0,0) #
#' y <- c(1,1,1,1) #
#' r <- uni.conv(x,y) r: num [1:7] 1 1 1 1 0 0 0 #
#' ------------------------------------------------------------------------------------------#
uni.conv <- function(x,y){
m <- length(x)
n <- length(y)
z <- numeric(m+n-1)
for(j in 1:m){
for(k in 1:n){
z[j+k-1] = z[j+k-1]+x[j]*y[k]
}
}
return(z)
}
#--------------------------------------------------------------------------------------------#
# #
# UNIDIMENSIONAL AUTOCORRELATION #
#--------------------------------------------------------------------------------------------#
#' @function uni.corr #
#' @description \code{uni.corr} uses \code{uni.conv} to compute the autocorrelation of #
#' a vector #
#' @param x numeric vector representing polynomial coefficient (1 x n) #
#' @return the coefficient vector resulting from the autocorrelation (1 x 2n-1) #
#' @usage uni.corr(\code{x}) #
#' @seealso \code{uni.conv} #
#' @examples #
#' x <- c(0.25,0.25,0.25,0.25) #
#' r <- uni.corr(x) r: num [1:7] 0.0625 0.1250 0.1875 0.2500 0.1875 0.1250 0.0625 #
#' ------------------------------------------------------------------------------------------#
uni.corr <- function(x){
R <- uni.conv(x,rev(x))
return(R)
}
#---------------------------------------------------------------------------------------------#
# #
# DIVERGENCE INDEX #
#---------------------------------------------------------------------------------------------#
#' @function uni.div #
#' @description \code{uni.div} uses \code{uni.conv} and \code{uni.corr} to compute the #
#' polarization divergence of a probability vector. #
#' @param x numeric vector representing polynomial coefficient distribution (pdf) #
#' @return the Divergence index. (1 x 1) real number [0 1) #
#' @usage uni.div(\code{x}) #
#' @seealso \code{uni.conv}, \code{uni.corr} #
#' @examples #
#' x <- c(0.5,0,0,0,0,0,0,0,0,0.5) #
#' r <- uni.div(x) r: 0.2973122 #
#' x <- c(0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1) Uniform distribution #
#' r <- uni.div(x) r: 0.1229451 #
#' x <- c(0,0,0,1,0,0,0,0,0,0) #
#' r <- uni.div(x) r: 0 #
#' -------------------------------------------------------------------------------------------#
uni.div <- function(x){
d <- length(x)
comb <- rep(1,d)
# compute the Bilateral Cumulative Distributive function and its autocorrelation (spectra)
bcdf <- uni.conv(x,comb)
sbcdf <- uni.corr(bcdf)
# compute the Singleton autocorrelation function
imp <- rep(0,d)
imp[1] <- 1
bcdfi <- uni.conv(imp,comb)
sbcdfi <- uni.corr(bcdfi)
# normalized energy (pdf of the signal)
E <- sum(sbcdfi)
sbcdf <- sbcdf/E
sbcdfi <- sbcdfi/E
# compute the binary logarithm of the signals
l <- length(sbcdf)
lgs1 <- rep(0,l)
lgsi <- rep(0,l)
i <- 1
while (i <= l){
if(sbcdf[i] > 0)
lgs1[i] <- log(sbcdf[i],2)
if(sbcdfi[i] > 0)
lgsi[i] <- log(sbcdfi[i],2)
i <- i+1
}
# compute the -log M(X)
M <- rep(0,l)
i <- 1
while (i <= l){
M[i] <- (sbcdf[i]+sbcdfi[i])/2
if (M[i] > 0)
M[i] <- -log(M[i],2)
i <- i+1
}
# compute D(I,M) D(I,S)
i <- 1
div <- 0
while (i <= l){
im <- sbcdfi[i]*(lgsi[i]+M[i])
is <- sbcdf[i]*(lgs1[i]+M[i])
div <- div + im + is
i <- i+1
}
return(div)
}
#---------------------------------------------------------------------------------------------#
# #
# DIVERGENCE INDEX CONSTANT #
#---------------------------------------------------------------------------------------------#
#' @function uni.divConst #
#' @description \code{uni.divConst} uses \code{uni.div} to compute the distribution #
#' divergence constant for the maximum variance. #
#' @param n the number of bins in the pdf (pdf) #
#' @return the Divergence index constant. (1 x 1) real number [0 1) #
#' @usage uni.divConst(\code{x}) #
#' @seealso \code{uni.div} #
#' @examples #
#' x <- c(0.5,0,0,0,0,0,0,0,0,0.5) #
#' r <- uni.div(x) r: 0.2973122 #
#' r <- uni.divConst(10) r: 0.2973122 #
#' -------------------------------------------------------------------------------------------#
uni.divConst <- function(n){
# create a bimodal distribution
p <- rep(0,n) # pdf
p[1] <- 0.5
p[n] <- 0.5
# compute the divergence index of the bimodal pdf
const <- uni.div(p)
return(const)
}
#---------------------------------------------------------------------------------------------#
# #
# HOMOGENEITY INDEX #
#---------------------------------------------------------------------------------------------#
#' @function uni.hom #
#' @description \code{uni.hom} uses \code{uni.concI} and \code{uni.div} to compute #
#' the Homogeneity Index for a vector of observations. #
#' @param p numeric vector with non-negative integer values. (1 x n) #
#' @return the Homogeneity Index (HI) of \code{p}: real number [0 1] #
#' @usage uni.hom(\code{p}) #
#' @seealso \code{uni.concI}, \code{uni.div} #
#' @examples #
#' p <- c(10,10,10,10,10) #
#' r <- uni.hom(p) r: 0 #
#' p <- c(0,0,50,0,0) #
#' r <- uni.hom(p) r: 1 #
#' -------------------------------------------------------------------------------------------#
uni.hom <- function(p){
# compute the concentration index
conc <- uni.concI(p)
# compute the pdf of p
d <- length(p)
pop <- sum(p)
i <- 1
## compute the pdf
while(i <= d)
{
p[i] <- p[i]/pop
i <- i+1
}
# compute the Divergence Index of the distribution
div <- uni.div(p)
# compute the Divergence Index for the Uniform distribution
uniform <- rep(1/d,d)
divUnif <- uni.div(uniform)
# compute the Homogeneity Index
H <- (conc + divUnif - div)/(1 + divUnif)
return(H)
}
#---------------------------------------------------------------------------------------------#
# #
# HOMOGENEITY INDEX - TRUE DIVERSITY #
#---------------------------------------------------------------------------------------------#
#' @function uni.hom_mn #
#' @description \code{uni.hom_mn} uses \code{uni.hom} to compute the Homogenity Index for a #
#' distribution of m bins and n equally abundant contiguous categories: pdf_mn #
#' @param m integer indicating the number of bins in the distribution. #
#' @param n integer indicating the number of contiguous bins with equal number of observations#
#' @return the Homogeneity Index (HI) of pdf_mn: real number [0 1] #
#' @usage uni.hom_mn(\code{m},\code{n}) #
#' @seealso \code{uni.hom} #
#' @examples #
#' r <- uni.hom_mn(5,5) r: 0 #
#' r <- uni.hom_mn(5,1) r: 1 #
#' p <- c(0.5,0.5,0,0,0) #
#' r <- uni.hom(p) r: 0.76 #
#' r <- uni.hom(5,2) r: 0.76 #
#' -------------------------------------------------------------------------------------------#
uni.hom_mn <- function(m,n) {
## create a pdf_mn
p <- rep(0,m)
for(j in 1:n){
p[j] <- 1/n
}
h <- uni.hom(p)
return (h)
}
#---------------------------------------------------------------------------------------------#
# #
# HOMOGENEITY INDEX - CLASSIFICATION #
#---------------------------------------------------------------------------------------------#
#' @function uni.hom_class #
#' @description \code{uni.hom_mn} uses \code{uni.hom} to compute the Homogenity class for a #
#' distribution #
#' @param a real number indicating the lower bound of the first class - A #
#' @param b real number indicating the lower bound of the second class - B #
#' @param c real number indicating the lower bound of the third class - C #
#' @return String: Homogenity Class #
#' @usage uni.hom_class(\code{a},\code{b},\code{c},\{p}) #
#' @seealso \code{uni.hom} #
#' -------------------------------------------------------------------------------------------#
uni.hom_class <- function(a,b,c,p) {
h <- uni.hom(p)
if (h >= a) {
class <- 'A'
} else if (h >= b) {
class <- 'B'
} else if (h >= c) {
class <- 'C'
} else {
class <- 'D'
}
return (class)
}
#---------------------------------------------------------------------------------------------#
# #
# LOCATION INDEX SUPPORTING FUNCTION #
#---------------------------------------------------------------------------------------------#
#' @function uni.locVec #
#' @description function to compute the Concentration Location Index vector of a pdf #
#' @param x probability density function vector. (1 x n) #
#' @return the Location Index vector score (LIS) of \code{x} #
#' @usage uni.locVec(\code{x}) #
#' @examples #
#' x <- c(0.5,0,0,0,0.5) #
#' v <- uni.locVec(x) r: num[1:5] 0.6 0.6 0.6 0.6 0.6 #
#' x <- c(1,0,0,0,0) #
#' x <- uni.locVec(x) r: num[1:5] 1 0.8 0.6 0.4 0.2 #
#---------------------------------------------------------------------------------------------#
uni.locVec <- function(x){
n <- length(x)
i <- 1 ## iterator for the bins
s <- 0 ## current interval score
vs <- rep(0,n) ## cumulative score
k <- rep(0,n) ## bins scores
while(i <= n){
j <- 0 ## iterator for the nested intervals (width)
s <- 0
vs <- rep(0,n)
while(j < n){
if(j == 0){
s <- x[i] ## interval width zero (initial point)
}else{
fw <- i+j ## interval border right
bk <- i-j ## interval border left
if (bk >= 1 && fw <= n){
s <- s+x[bk]+x[fw]
}else if (bk >= 1){
s <- s+x[bk]
}else if (fw <= n){
s <- s+x[fw]
}
}
vs[j+1] <- s
j <- j+1
}
k[i] <- sum(vs)
i<- i+1
}
## Normalized score (singleton is n)
k <- (1/n)*k
return(k)
}
#---------------------------------------------------------------------------------------------#
# #
# LOCATION INDEX #
#---------------------------------------------------------------------------------------------#
#' @function uni.loc #
#' @description function to compute the Location Index (LI) and Compactness(C). #
#' LI gives the minimum and maximum position of the bins with the maximum concentration #
#' @param x probability density function vector. (1 x n) #
#' @return the Location Index (LI) and Compactness of \code{x} (1x2) #
#' @usage uni.loc(\code{x}) #
#' @seealso \code{uni.locVec} #
#' @examples #
#' x <- c(0.5,0,0,0,0.5) #
#' v <- uni.loc(x) r: 1 5 #
#' x <- c(1,0,0,0,0) #
#' x <- uni.loc(x) r: 1 1 #
#' -------------------------------------------------------------------------------------------#
uni.loc <- function(x){
n <- length(x)
loc1 <- 0 ## minimum position of the bin with maximum score
loc2 <- 0 ## maximum position of the bin with maximum score
## compute the Location Index vector and the maximum score
v <- uni.locVec(x)
m <- max(v)
for (j in 1:n){
if (v[j] == m){
if (loc1 > 0)
loc2 <- j
else{
loc1 <- j
loc2 <- j
}
}
}
cl <- c(loc1,loc2)
return (cl)
}