-
Notifications
You must be signed in to change notification settings - Fork 0
/
wbpip_gd_doc.qmd
1916 lines (1467 loc) · 57.4 KB
/
wbpip_gd_doc.qmd
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Group Data Documentation (wbpip)"
author: "Diana Garcia"
format:
html:
number-sections: true
theme: sandstone
code-fold: true
code-summary: "Show the code"
toc: true
link-external-newwindow: true
---
## Objective {.unnumbered}
This document aims to describe the functions used in `wbpip` to calculated the poverty and inequality statistics using group data.
## Structure {.unnumbered}
```{mermaid}
%%| fig-width: 6.5
flowchart RL
lz(select_lorenz) --> stats[pip_stats]
lq(pip_stats_lq) --> lz
lb(pip_stats_lb) --> lz
form_lq(functional_form_lq) --> lq
est_lq(estimate_lq) --> lq
fit_lq(fit_lq) --> lq
derive_lq(derive_lq) --> lq
regres(regres) --> lq
regres(regres) --> lb
derive_lq(derive_lq) --> lq
regres(regres) --> lq
regres(regres) --> lb
form_lb(functional_form_lb) --> lb
est_lb(estimate_lb) --> lb
fit_lb(fit_lb) --> lb
derive_lb(derive_lb) ---> lb
check_val(check_curve_validity_lq) --> est_lq
comp_dist_lq(compute_dist_stats_lq) --> est_lq
comp_pov_lq(compute_pov_stats_lq) --> est_lq
check_val_lb(check_curve_validity_lb) --> est_lb
derive_lb(derive_lb) ---> check_val_lb
ddlk(ddlk) --> check_val_lb
comp_dist_lb(compute_dist_stats_lb) --> est_lb
derive_lb --> comp_dist_lb
value_at_lb --> comp_dist_lb
gini_lb --> comp_dist_lb
value_at_lb --> gini_lb
polarization_lb --> comp_dist_lb
derive_lb --> polarization_lb
value_at_lb --> polarization_lb
quantile_lb --> comp_dist_lb
gini_lb --> comp_dist_lb
value_at_lb --> gini_lb
polarization_lb --> comp_dist_lb
derive_lb --> polarization_lb
value_at_lb --> polarization_lb
quantile_lb --> comp_dist_lb
comp_pov_lb(compute_pov_stats_lb) --> est_lb
headcount_lb --> comp_pov_lb
rtSafe --> headcount_lb
headcount_lb --> comp_pov_lb
rtSafe --> headcount_lb
funcD --> rtSafe
rtNewt --> rtSafe
BETAI --> headcount_lb
BETAI --> headcount_lb
BETAICF --> BETAI
GAMMLN --> BETAI
pov_gap_lb --> comp_pov_lb
pov_severity_lb --> comp_pov_lb
pov_gap_lb --> comp_pov_lb
pov_severity_lb --> comp_pov_lb
value_at_lb --> fit_lb
polarization_lq(<b>polarization_lq</b>) --> comp_dist_lq
gini_lq(<b>gini_lq</b>) --> comp_dist_lq
value_at_lq --> polarization_lq
value_at_lq(value_at_lq) --> comp_dist_lq
mld_lq(<b>mld_lq</b>) --> comp_dist_lq
quantile_lq(<b>quantile_lq</b>) --> comp_dist_lq
headcount_lq --> comp_pov_lq
pov_gap_lq --> comp_pov_lq
pov_severity_lq --> comp_pov_lq
watts_lq --> comp_pov_lq
headcount_lq --> comp_pov_lq
pov_gap_lq --> comp_pov_lq
pov_severity_lq --> comp_pov_lq
watts_lq --> comp_pov_lq
derive_lq --> comp_dist_lq
derive_lq --> mld_lq
derive_lq --> polarization_lq
value_at_lq --> quantile_lq
value_at_lq --> pov_gap_lq
value_at_lq --> pov_severity_lq
value_at_lq --> fit_lq
style derive_lq fill: #50C878
style value_at_lq fill: #50C878
style check_val fill: #50C878
style form_lq fill: #50C878
style regres fill: #50C878
style gini_lq fill: #FFA500
style polarization_lq fill: #FFA500
style quantile_lq fill: #FFA500
style mld_lq fill: #FFA500
style comp_dist_lq fill: #FFF1A2
style comp_pov_lq fill: #FFF1A2
style headcount_lq fill: #FFF1A2
style pov_gap_lq fill: #FFF1A2
style pov_severity_lq fill: #FFF1A2
style watts_lq fill: #FFF1A2
value_at_lq --> pov_gap_lq
value_at_lq --> pov_severity_lq
value_at_lq --> fit_lq
style derive_lq fill: #50C878
style value_at_lq fill: #50C878
style check_val fill: #50C878
style form_lq fill: #50C878
style regres fill: #50C878
style gini_lq fill: #FFF1A2
style polarization_lq fill: #FFF1A2
style quantile_lq fill: #FFA500
style mld_lq fill: #FFA500
style comp_pov_lq fill: #FFF1A2
style headcount_lq fill: #FFF1A2
style pov_gap_lq fill: #FFF1A2
style pov_severity_lq fill: #FFF1A2
style watts_lq fill: #FFF1A2
style form_lb fill: #50C878
style derive_lb fill: #FFF1A2
style est_lb fill: #50C878
style check_val_lb fill: #FFF1A2
style comp_pov_lb fill: #FFF1A2
style ddlk fill: #FFF1A2
style headcount_lb fill: #FFF1A2
style BETAI fill: #FFA500
style BETAICF fill: #50C878
style GAMMLN fill: #FFA500
style rtSafe fill: #50C878
style funcD fill: #50C878
style rtNewt fill: #50C878
```
## Functions
### pip_stats {#sec-pip_stats}
*Description*: Compute poverty statistics for grouped data by selecting the best functional fit for the Lorenz curve (either beta or quadratic)
```{r}
gd_compute_pip_stats <- function(welfare,
povline,
population,
requested_mean,
popshare = NULL,
default_ppp = 1,
ppp = NULL,
p0 = 0.5) {
# Apply Lorenz quadratic fit ----------------------------------------------
results_lq <- gd_compute_pip_stats_lq(
welfare = welfare,
population = population,
requested_mean = requested_mean,
povline = povline,
popshare = popshare,
default_ppp = default_ppp,
ppp = ppp,
p0 = p0
)
# Apply Lorenz beta fit ----------------------------------------------
results_lb <- gd_compute_pip_stats_lb(
welfare = welfare,
population = population,
requested_mean = requested_mean,
povline = povline,
popshare = popshare,
default_ppp = default_ppp,
ppp = ppp,
p0 = p0
)
# Apply selection rules ---------------------------------------------------
out <- gd_select_lorenz(
lq = results_lq,
lb = results_lb
)
# Return only subset of variables
out <- out[c(
"poverty_line",
"mean",
"median",
"headcount",
"poverty_gap",
"poverty_severity",
"watts",
"gini",
"mld",
"polarization",
"deciles"
)]
return(out)
}
```
::: {#nte-boundary-issue .callout-note}
#### Issue: Boundary conditions
At the moment, this is calculated using $(\mu L'(0.001) + 4,\mu L'(0.98) - 4)$ (*Note: Not sure what is the reason of adding and subtracting 4*).
:::
### select_lorenz {#sec-select_lorenz}
*Description*: Select best Lorenz fit and adjust the returned statistics if needed.
```{r}
gd_select_lorenz <- function(lq, lb) {
# Set default value
datamean <- lq[["mean"]]
is_valid <- lq[["is_valid"]] | lb[["is_valid"]]
is_normal <- lq[["is_normal"]] | lb[["is_normal"]]
# Selection of Lorenz fit for poverty statistics
use_lq_for_pov <- use_lq_for_poverty(
lq = lq,
lb = lb
)
# Selection of Lorenz fit for distributional statistics
use_lq_for_dist <- use_lq_for_distributional(
lq = lq,
lb = lb
)
# Retrieve distributional statistics
dist <- retrieve_distributional(
lq = lq,
lb = lb,
is_valid = is_valid,
use_lq_for_dist = use_lq_for_dist
)
# Retrieve poverty statistics
pov <- retrieve_poverty(
lq = lq,
lb = lb,
is_normal = is_normal,
use_lq_for_pov = use_lq_for_pov
)
return(list(
mean = datamean,
poverty_line = pov[["poverty_line"]],
z_min = dist[["z_min"]],
z_max = dist[["z_max"]],
# ppp = lq[["ppp"]],
gini = dist[["gini"]],
median = dist[["median"]],
# rmed = rmed,
rmhalf = dist[["rmhalf"]],
polarization = dist[["polarization"]],
ris = dist[["ris"]],
mld = dist[["mld"]],
dcm = lq[["dcm"]],
deciles = dist[["deciles"]],
headcount = pov[["headcount"]],
poverty_gap = pov[["poverty_gap"]],
poverty_severity = pov[["poverty_severity"]],
eh = pov[["eh"]],
epg = pov[["epg"]],
ep = pov[["ep"]],
gh = pov[["gh"]],
gpg = pov[["gpg"]],
gp = pov[["gp"]],
watts = pov[["watts"]],
sse = dist[["sse"]]
))
}
```
### pip_stats_lq
*Description*: Compute poverty statistics for grouped data using the quadratic functional form of the Lorenz qurve.
```{r}
gd_compute_pip_stats_lq <- function(welfare,
povline,
population,
requested_mean,
popshare = NULL,
default_ppp,
ppp = NULL,
p0 = 0.5) {
# Adjust mean if different PPP value is provided
if (!is.null(ppp)) {
requested_mean <- requested_mean * default_ppp / ppp
} else {
ppp <- default_ppp
}
# STEP 1: Prep data to fit functional form
prepped_data <- create_functional_form_lq(
welfare = welfare,
population = population
)
# STEP 2: Estimate regression coefficients using LQ parameterization
reg_results <- regres(prepped_data, is_lq = TRUE)
reg_coef <- reg_results$coef
A <- reg_coef[1]
B <- reg_coef[2]
C <- reg_coef[3]
# Step 2.1: pre-calculate key values
kv <- gd_lq_key_values(A, B, C)
# OPTIONAL: Only when popshare is supplied
# return poverty line if share of population living in poverty is supplied
# intead of a poverty line
if (!is.null(popshare)) {
povline <- derive_lq(popshare,
A, B, C,
key_values = kv) * requested_mean
}
# Boundary conditions (Why 4?)
z_min <- requested_mean * derive_lq(0.001,
A, B, C,
key_values = kv) + 4
z_max <- requested_mean * derive_lq(0.980,
A, B, C,
key_values = kv) - 4
z_min <- if (z_min < 0) 0L else z_min
results1 <- list(requested_mean, povline, z_min, z_max, ppp)
names(results1) <- list("mean", "poverty_line", "z_min", "z_max", "ppp")
# STEP 3: Estimate poverty measures based on identified parameters
results2 <- gd_estimate_lq(requested_mean, povline, p0,
A, B, C, key_values = kv)
# STEP 4: Compute measure of regression fit
results_fit <- gd_compute_fit_lq(welfare,
population,
results2$headcount,
A, B, C,
key_values = kv)
res <- c(results1,
results2,
results_fit,
reg_results)
return(res)
}
```
### functional_form_lq
*Description*: Prepares data for regression of $y(1-y)$ on $(x^2-y)$, $y(x-1)$ and $(x-y)$.
```{r}
create_functional_form_lq <- function(welfare,
population) {
# CHECK inputs
# assertthat::assert_that(is.numeric(population))
# assertthat::assert_that(is.numeric(welfare))
# assertthat::assert_that(length(population) == length(welfare))
# assertthat::assert_that(length(population) > 1)
# Remove last observation (the functional form for the Lorenz curve already forces
# it to pass through the point (1, 1)
nobs <- length(population) - 1
population <- population[1:nobs]
welfare <- welfare[1:nobs]
# L(1-L)
y <- welfare * (1 - welfare)
# (P^2-L)
x1 <- population^2 - welfare
# L(P-1)
x2 <- welfare * (population - 1)
# P-L
x3 <- population - welfare
return(list(y = y, X = cbind(x1, x2, x3)))
}
```
*Note*: The last observation of (x,y), which by construction has the value (1, 1), is excluded since the functional form for the Lorenz curve already forces it to pass through the point (1, 1).
*References*: \[1\]
*Relevant equations*:
The general Quadratic Lorenz curve form is:
$$ax^2 + bxy + cy^2 + dx + ey + f = 0$$ {#eq-gen-equation}
where $y$ is the vector of cumulative proportion of consumption/income (L) and $x$ is the cumulative proportions of population (P). Using the conditions $f=0$ and $e = -(a+b+d+1)$ the previous equation is rewritten in a linear form as follows (Equation 15 in Villasenor et al, 1989):
$$y(1-y) = a(x^2-y) + by(x-1) + d(x-y)$$ {#eq-linear-form}
This function prepares data to estimate $a$, $b$, and $d$ (*Note*: $d$ is named $C$ in `wbpip`).
### value_at_lq
*Description*: Solves for the Quadratic Lorenz curves.
```{r}
value_at_lq <- function(x, A, B, C, key_values) {
# Check for NA, Inf and negative values in x
check_NA_Inf_values(x)
check_neg_values(x)
# Calculations
# e <- -(A + B + C + 1)
# m <- (B^2) - (4 * A)
# n <- (2 * B * e) - (4 * C)
temp <- (key_values$m * x^2) + (key_values$n * x) + (key_values$e^2)
temp[temp < 0] <- 0
# Solving the equation of the Lorenz curve
estle <- -0.5 * ((B * x) + key_values$e + sqrt(temp))
return(estle)
}
```
*References*: \[1\]
*Relevant equations*:
This function calculates the value at the Quadratic Lorenz Curve. Solving @eq-gen-equation for $y$, and assuming $f=0$ and $e = -(a+b+d+1)$ the density function that better fits income distributions will be (Equation 6b in Villasenor et al, 1989):
$$ y= \Bigl\{-(bx+e) - (\alpha x^2 + \beta x + e^2)^\frac{1}{2}\Bigl\}/2$$ {#eq-solve-y}
where $\alpha = b^2 -4a$ and $\beta = 2be - 4d$.
### derive_lq
*Description*: returns the first derivative of the quadratic Lorenz curves with $c = 1$.
```{r}
derive_lq <- function(x, A, B, C, key_values) {
if (is.null(key_values)) {
key_values <- gd_lq_key_values(A, B, C)
# e <- key_values$e
# m <- key_values$m
# n <- key_values$n
}
if (anyNA(x) == TRUE) {
cli::cli_abort("`x' must be a numeric or integer vector")
}
# note:
# alpha --> m
# beta --> n
# e <- -(A + B + C + 1)
# m <- (B^2) - (4 * A)
# n <- (2 * B * e) - (4 * C) # C is called D in original paper, but C in Datt paper
tmp <- (key_values$m * x^2) + (key_values$n * x) + (key_values$e^2)
tmp[(!is.na(tmp) & tmp < 0)] <- 0 # If tmp == 0, val = Inf.
# Formula for first derivative of GQ Lorenz Curve
val <- -(B / 2) - ((2 * key_values$m * x + key_values$n) / (4 * sqrt(tmp)))
return(val)
}
```
*References*: \[1\]
*Relevant equations*:
This function computes the first derivative of @eq-solve-y:
$$-(b / 2) - (\beta + 2 \alpha x) / (4\sqrt(\alpha x^2 + \beta x + e^2)$$ {#eq-first-der}
### estimate_lq
*Description*: Estimates poverty and inequality stats from Quadratic Lorenz fit
```{r}
gd_estimate_lq <- function(mean, povline, p0, A, B, C, key_values) {
if (is.null(key_values)) {
key_values <- gd_lq_key_values(A, B, C)
}
e <- key_values$e
m <- key_values$m
n <- key_values$n
r <- key_values$r
s1 <- key_values$s1
s2 <- key_values$s2
validity <- check_curve_validity_lq(A, B, C, key_values = key_values)
#e, m, n, r^2)
if (validity$is_valid == FALSE & validity$is_normal == FALSE) {
return(empty_gd_compute_pip_stats_response)
}
# Compute distributional measures -----------------------------------------
dist_stats <- gd_compute_dist_stats_lq(mean, p0, A, B, C, key_values = key_values)
# Compute poverty stats ---------------------------------------------------
pov_stats <- gd_compute_poverty_stats_lq(mean, povline, A, B, C, key_values = key_values)
out <- list(
gini = dist_stats$gini,
median = dist_stats$median,
rmhalf = dist_stats$rmhalf,
polarization = dist_stats$polarization,
ris = dist_stats$ris,
mld = dist_stats$mld,
dcm = dist_stats$dcm,
deciles = dist_stats$deciles,
headcount = pov_stats$headcount,
poverty_gap = pov_stats$pg,
poverty_severity = pov_stats$p2,
eh = pov_stats$eh,
epg = pov_stats$epg,
ep = pov_stats$ep,
gh = pov_stats$gh,
gpg = pov_stats$gpg,
gp = pov_stats$gp,
watts = pov_stats$watts,
dl = pov_stats$dl,
ddl = pov_stats$ddl,
is_normal = validity$is_normal,
is_valid = validity$is_valid
)
return(out)
}
```
### check_curve_validity_lq
*Description*: Check validity of Lorenz Quadratic fit
```{r}
check_curve_validity_lq <- function(A, B, C, key_values) {
is_normal <- FALSE
is_valid <- FALSE
r <- (key_values$r)^2 # formerly, the input to the func was r^2
# r needs to be > 0 because need to extract sq root
if (r < 0) { # now that r is squared, this will never be TRUE
return(list( # but r^2 was used as input before `key_values` so
is_normal = is_normal, # this was already never executed
is_valid = is_valid
))
}
if (key_values$e > 0 || C < 0) {
return(list(
is_normal = is_normal,
is_valid = is_valid
))
}
# Failure conditions for checking theoretically valid Lorenz curve
# Found in section 4 of Datt computational tools paper
cn1 <- key_values$n^2
cn3 <- cn1 / (4 * key_values$e^2)
if (!((key_values$m < 0) |
((key_values$m > 0) & (key_values$m < cn3) & (key_values$n >= 0)) |
((key_values$m > 0) & (key_values$m < -key_values$n / 2) & (key_values$m < cn3)))) {
return(list(
is_normal = is_normal,
is_valid = is_valid
))
}
is_normal <- TRUE
is_valid <- (A + C) >= 0.9
return(list(
is_normal = is_normal,
is_valid = is_valid
))
}
```
*References*: \[1\], \[2\], \[3\]
*Relevant equations*:
The function tests for specific assumptions for the Lorenz Quadratic and relies on the formulas from Table 2 in Datt, G (1998) \[3\]. The nomenclature for some of these formulas differ from Villasenor et al (1989), so this is how they match:
$m = \alpha = b^2 -4a$
$n = \beta = 2be -4c$ ($c=d$ for Villasenor et al, 1989)
$r = K*2\alpha = (n^2 - 4me^2)^\frac{1}{2}$
::: {#nte-cond-r-issue .callout-note}
#### Issue: Rename $r$
At the moment, $r$ refers to $(n^2 - 4me^2)$ in `wbpip`. This is already fixed on commit `47852c` in branch `fix_key_values` by Zander (Waiting for merge). We should maybe rename this within this function.
:::
The conditions this function tests are presented in Section 4 of Datt (1998):
Normality and Validity
- $(n^2 - 4me^2)>0$ so the square root in $r$ to be positive. (Normality and validity)
- $e<0$ or $c>0$ so $L(0,y) = 0$ and $L'(0^{+},y) \geq 0$ (Normality and validity)
- $a+d \geq 0.9$ so $L(1,y) = 1$ (Validity)
::: {#imp-condad .callout-important}
#### Issue: The inequality for $a+d$
At the moment, most test are design for the old validation $a+d \leq 1$. However, the Corrigendum of the original paper \[2\] indicates we should use $a+d \geq 1$
:::
And so $L''(x,y) \geq 0$ for $x$ within $(0,1)$:
- $m < 0$ (condition on Villasenor et al., 1989)
- if $m > 0$ then $m < n^2/4 e^2$ and $n \leq 0$ (last condition from Datt,1998)
- if $m > 0$ then $m < n^2/4 e^2$ and $m < -n/2$ (last condition from Datt,1998)
$L''(x,y)$ can be calculated using @eq-first-der:
$$ \frac{\beta^2 -4\alpha e^2}{8(\alpha x^2 + \beta x + e^2)^\frac{3}{2}} = \frac{n^2 - 4me^2}{8(mx^2 +nx +e^2)^\frac{3}{2}}=\frac{r^2}{8(mx^2 +nx +e^2)^\frac{3}{2}} $$
### compute_dist_stats_lq
*Description*: Computes distributional stats from Lorenz Quadratic fit
```{r}
gd_compute_dist_stats_lq <- function(mean, p0, A, B, C, key_values = key_values) {
gini <- gd_compute_gini_lq(A, B, C,
key_values = key_values)
median <- mean * derive_lq(0.5, A, B, C,
key_values = key_values)
rmhalf <- value_at_lq(p0, A, B, C,
key_values = key_values) * mean / p0 # What is this??
dcm <- (1 - gini) * mean
pol <- gd_compute_polarization_lq(mean, p0, dcm, A, B, C,
key_values = key_values)
ris <- value_at_lq(0.5, A, B, C,
key_values = key_values)
mld <- gd_compute_mld_lq(A, B, C,
key_values = key_values)
deciles <- gd_compute_quantile_lq(A, B, C,
key_values = key_values)
return(list(
gini = gini,
median = median,
rmhalf = rmhalf,
dcm = dcm,
polarization = pol,
ris = ris,
mld = mld,
deciles = deciles
))
}
```
::: {#nte-check1 .callout-note}
#### Check:
What is the difference with `gd_estimate_dist_stats_lq`?
:::
### quantile_lq
*Description*: Compute quantiles from Lorenz Quandratic fit
```{r}
old_gd_compute_quantile_lq <- function(A, B, C, n_quantile = 10) {
vec <- vector(mode = "numeric", length = n_quantile)
x1 <- 1 / n_quantile
q <- 0L
lastq <- 0L
for (i in seq_len(n_quantile - 1)) {
q <- value_at_lq(x1, A, B, C)
v <- q - lastq
vec[i] <- v
lastq <- q
x1 <- x1 + 1 / n_quantile
}
vec[n_quantile] <- 1 - lastq
return(vec)
}
```
*Note*: This function calculates the quantiles for a Lorenz Quadratic with specific values for $a$, $b$ and $d$.
::: {#imp-quant-gd-issue .callout-important}
#### Issues:
The description of this function indicates that it calculates the quantiles (deciles) for the density for some specific values of $a$, $b$ and $d$, but it calculates instead the "share" or the value between the deciles. If I understand correctly, the density is the lorenz curve and the share will refer to the `deciles` of the welfare vector.
Regarding the last decile, they manually calculated it by subtracting 1 to the second-to-last decile:
`vec[n_quantile] <- 1 - value_at_lq(x[n_quantile-1], A, B, C)` where `n_quantile = 10`
However, my hypothesis is that this is related to *Issue: The inequality for* $a+d$ above. If $A+C \geq 1$ then `value_at_lq(1, A, B, C) = 1`. We can see the case in this [Desmos graph](https://www.desmos.com/calculator/g164gdepzk){.external target="_blank"}.
:::
*My version of the code*:
```{r}
gd_compute_quantile_lq <- function(A, B, C, n_quantile = 10) {
x <- seq(from = 1/n_quantile, to = 1, by = 1/n_quantile)
vec <- diff(c(0,value_at_lq(x, A, B, C)))
vec[n_quantile] <- 1- value_at_lq(x[n_quantile-1], A, B, C) # Is this correct?
return(vec)
}
```
### mld_lq
*Description*: Computes Mean Log Deviation from Lorenz Quadratic fit
```{r}
old_gd_compute_mld_lq <- function(A, B, C) {
x1 <- derive_lq(0.0005, A, B, C)
gap <- 0L
mld <- 0L
if (x1 == 0) {
gap <- 0.0005
} else {
mld <- suppressWarnings(log(x1) * 0.001)
}
x1 <- derive_lq(0, A, B, C)
for (xstep in seq(0, 0.998, 0.001)) {
x2 <- derive_lq(xstep + 0.001, A, B, C)
if ((x1 <= 0) || (x2 <= 0)) {
gap <- gap + 0.001
if (gap > 0.5) {
return(-1)
}
} else {
gap <- 0L
mld <- mld + (log(x1) + log(x2)) * 0.0005
}
x1 <- x2
}
return(-mld)
}
```
*References*: None
*Note*: In this function, they do not describe how the function is calculated. The following is my hypothesis of what they were trying to accomplish:
*Relevant equations*:
The mean log deviation:
$$-\frac{1}{N} \sum_{i=1}^N ln(\frac{y_i}{\mu})=-\frac{1}{N} \sum_{i=1}^N ln(\frac{y_i}{\frac{1}{N}\sum_{i=1}^N y_i}) $$
Using the derivation from Rohde (2008) (Equation 15.20), we know that:
$$L'(\pi) = \frac{N y_k}{\sum_{k=1}^N y_k}$$
where $y_k$ is the income accruing to the $k_{th}$ individual if ordered such that $y_1<y_2<...<y_k$ and $\pi= \frac{k}{j}$. Then the mean log deviation can be rewritten as (Equation 15.21):
$$-\int_0^1 ln(L'(\pi))d\pi= \lim_{n \rightarrow \infty} -\sum_{i=1}^{N}\frac{1}{N}ln(\frac{N y_k}{\sum_{k=1}^N y_k})$$
::: {#imp-mld-gd-issue .callout-important}
#### Issue:
My hypothesis is that they used the last formula to calculate the Mean Log Deviation. I am still unsure why they used some rules at the lower end, more specifically why they return $1$ if for the left tail of $y$ we encounter negative values.
:::
*My version of the code*:
```{r}
gd_compute_mld_lq <- function(A, B, C) {
x1 <- derive_lq(0.0005, A, B, C)
mld <- 0L
if (x1 != 0) {
mld <- suppressWarnings(log(x1) * 0.001) # Needed to match test
}
xstep <- seq(0, 0.999, 0.001)
x <- derive_lq(xstep, A, B, C)
if (any(x[1:33]<=0)){ # To account for negative values
return(-1)
}else{
mld <- mld + fsum( (log(x[1:999])+log(x[2:1000])) *0.0005)
return(-mld)
}
}
```
### gini_lq
*Description*: Compute Gini index from Lorenz Quadratic fit.
```{r}
gd_compute_gini_lq <- function(A, B, C, key_values) {
# For the GQ Lorenz curve, the Gini formula are valid under the condition A+C>=1
# P.isValid <- (A + C) >= 0.9
# P.isNormal <- TRUE
e1 <- abs(A + C - 1)
e2 <- 1 + (B / 2) + key_values$e
tmp1 <- key_values$n * (B + 2) / (4 * key_values$m)
tmp2 <- (key_values$r^2) / (8 * key_values$m)
tmp3 <- (2 * key_values$m) + key_values$n
if (key_values$m > 0) {
# tmpnum <- tmp3 + 2 * sqrt(m) * abs(e)
# tmpden <- n - 2 * abs(e) * sqrt(m)
# Formula from Datt paper
# CHECK that code matches formulas in paper
gini <- e2 + (tmp3 / (4 * key_values$m)) * e1 - (key_values$n * abs(key_values$e) / (4 * key_values$m)) - ((key_values$r^2) / (8 * sqrt(key_values$m)^3)) *
log(abs(((tmp3 + (2 * sqrt(key_values$m) * e1))) / (key_values$n + (2 * sqrt(key_values$m) * abs(key_values$e)))))
# P.gi <- (e/2) - tmp1 - (tmp2 * log(abs(tmpnum/tmpden)) / sqrt(m))
} else {
tmp4 <- ((2 * key_values$m) + key_values$n) / key_values$r
tmp4 <- if (tmp4 < -1) -1 else tmp4
tmp4 <- if (tmp4 > 1) 1 else tmp4
# Formula does not match with paper
gini <- e2 +
(tmp3 / (4 * key_values$m)) *
e1 - (key_values$n * abs(key_values$e) / (4 * key_values$m)) +
(tmp2 * (asin(tmp4) - asin(key_values$n / key_values$r)) / sqrt(-key_values$m))
# P.gi <- (e/2) - tmp1 + ((tmp2 * (asin(tmp4) - asin(n/r))) / sqrt(-m))
}
return(gini)
}
```
*References*: \[3\]
*Relevant equations*:
- if $m<0$:
$$ \frac{e}{2} - \frac{n (b+2)}{4m} + \frac{r^2}{8m\sqrt{-m}}\left[ \sin^{-1} \frac{2 + n}{r} - \sin^{-1} \frac{2 + n}{r} \right]$$
- if $m>0$:
$$ \frac{e}{2} - \frac{n (b+2)}{4m} - \frac{r^2}{8m\sqrt{m}} \ln \left[\left|{\frac{2m+n+2\sqrt{m} (a+c-1)}{n-2e\sqrt{m}}}\right| \right] $$
::: {#imp-gini-gd-issue .callout-important}
#### Issue:
The formula does not match the Datt paper Gini formula as mentioned previously by Tony.
:::
### polarization_lq
*Description*: Computes polarization index from parametric Lorenz fit
```{r}
gd_compute_polarization_lq <- function(mean,
p0,
dcm,
A, B, C,
key_values) {
pol <- 2 - (1 / p0) +
(dcm - (2 * value_at_lq(p0, A, B, C, key_values) * mean)) /
(p0 * mean * derive_lq(p0, A, B, C, key_values))
return(pol)
}
```
*References*: None
*Relevant equations*: None
*Questions*: What is p0?
### pip_stats_lb
*Description*: Compute poverty statistics for grouped data using the beta functional form of the Lorenz curve.
```{r}
gd_compute_pip_stats_lb <- function(welfare,
povline,
population,
requested_mean,
popshare = NULL,
default_ppp,
ppp = NULL,
p0 = 0.5) {
# Adjust mean if different PPP value is provided
if (!is.null(ppp)) {
requested_mean <- requested_mean * default_ppp / ppp
} else {
ppp <- default_ppp
}
# STEP 1: Prep data to fit functional form
prepped_data <- create_functional_form_lb(
welfare = welfare,
population = population
)
# STEP 2: Estimate regression coefficients using LB parameterization
reg_results <- regres(prepped_data, is_lq = FALSE)
reg_coef <- reg_results$coef
A <- reg_coef[1]
B <- reg_coef[2]
C <- reg_coef[3]
# OPTIONAL: Only when popshare is supplied
# return poverty line if share of population living in poverty is supplied
# instead of a poverty line
if (!is.null(popshare)) {
povline <- derive_lb(popshare, A, B, C) * requested_mean
}
# Boundary conditions (Why 4?)
z_min <- requested_mean * derive_lb(0.001, A, B, C) + 4
z_max <- requested_mean * derive_lb(0.980, A, B, C) - 4
z_min <- if (z_min < 0) 0L else z_min
results1 <- list(requested_mean, povline, z_min, z_max, ppp)
names(results1) <- list("mean", "poverty_line", "z_min", "z_max", "ppp")
# STEP 3: Estimate poverty measures based on identified parameters
results2 <- gd_estimate_lb(requested_mean, povline, p0, A, B, C)
# STEP 4: Compute measure of regression fit
results_fit <- gd_compute_fit_lb(welfare, population, results2$headcount, A, B, C)
res <- c(results1, results2, results_fit, reg_results)
return(res)
}
```
### functional_form_lb
*Description*: Prepare data for Lorenz beta regression: $Log(L(p) - p) = \log(a) + \alpha \log(p) + \beta \log(1 - p)$.
```{r}
create_functional_form_lb <- function(welfare, population) {
# CHECK inputs
# assertthat::assert_that(is.numeric(population))
# assertthat::assert_that(is.numeric(welfare))
# assertthat::assert_that(length(population) == length(welfare))