-
Notifications
You must be signed in to change notification settings - Fork 1
/
ffv_opt_sobin_rev.Rmd
188 lines (155 loc) · 5.92 KB
/
ffv_opt_sobin_rev.Rmd
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
---
title: Welfare Test Resource Equivalent Variation (REV) Computation (Line by Line)
description: |
Line by line, calculate welfare distances, Resource Equivalent Variation (REV),
for the Binary Allocation problem based on Theorem 1. Hand-input A and alpha values.
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Welfare Test Resource Equivalent Variation (REV) Computation (Line by Line)}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
urlcolor: blue
---
Line by line, calculate welfare distances, Resource Equivalent Variation (REV), for the Binary Allocation problem based on Theorem 1. Hand-input A and alpha values.
## Load Dependencies
```{r GlobalOptions, echo = T, results = 'hide', message=F, warning=F}
rm(list = ls(all.names = TRUE))
options(knitr.duplicate.label = 'allow')
```
```{r loadlib, echo = T, results = 'hide', message=F, warning=F}
library(dplyr)
library(tidyr)
library(tibble)
library(PrjOptiAlloc)
library(knitr)
library(kableExtra)
```
## Implement Algorithm Line by Line
### Generate Testing Rank Data
Generate data needed.
```{r rank generate}
# 1. Generate A and alpha Testing Vectors, random list no meanings
ar_alpha <- c(0.1,1.5,2.5,4 ,9,1.2,3,2,8)
ar_A <- c(0.5,1.5,3.5,6.5,1.9,3,4,6,4)
ar_beta <- rep(0, length(ar_A)) + 1/length(ar_A)
mt_alpha_A <- cbind(ar_alpha, ar_A, ar_beta)
ar_st_varnames <- c('alpha', 'A', 'beta')
tb_alpha_A <- as_tibble(mt_alpha_A) %>% rename_all(~c(ar_st_varnames))
tb_alpha_A
ar_rho <- c(0.99)
for (it_rho_ctr in seq(1,length(ar_rho))) {
fl_rho <- ar_rho[it_rho_ctr]
ls_ranks <- ffp_opt_sobin_target_row(tb_alpha_A[1,], fl_rho, ar_A, ar_alpha, ar_beta)
it_rank <- ls_ranks$it_rank
ar_it_rank <- ls_ranks$ar_it_rank
ar_fl_rank_val <- ls_ranks$ar_fl_rank_val
cat('fl_rho:', fl_rho, 'it_rank:', it_rank, '\n')
cat('ar_it_rank:', ar_it_rank, '\n')
cat('ar_fl_rank_val:', ar_fl_rank_val, '\n')
}
```
### Compute REV
Compute REV giving observable and ranking
```{r }
# Use the just generated optimal ranking above
ar_it_obs <- c(0,1,0,1,0,0,1,1,0)
ar_opti_r <- ar_it_rank
mt_rev <- cbind(seq(1,length(ar_opti_r)), ar_it_obs, ar_opti_r, ar_A, ar_alpha, ar_beta)
ar_st_varnames <- c('id', 'observed', 'optimal', 'A', 'alpha', 'beta')
tb_onevar <- as_tibble(mt_rev) %>% rename_all(~c(ar_st_varnames)) %>%
arrange(optimal)
it_w_res <- sum(ar_it_obs)
fl_rho <- ar_rho
# Generate util observed and util unobserved columns
tb_onevar <- tb_onevar %>% mutate(utility = beta*((A+alpha)^fl_rho - A^fl_rho)) %>%
mutate(util_ob = case_when(observed == 1 ~ utility,
TRUE ~ 0 )) %>%
mutate(util_notob = case_when(observed == 0 ~ utility,
TRUE ~ 0 ))
# Display
kable(tb_onevar) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
```
### Generate Util observed um and not observed Reverse Sum
```{r}
# Generate directional cumulative sums
tb_onevar <- tb_onevar %>%
arrange(-optimal) %>%
mutate(util_ob_sum = cumsum(util_ob)) %>%
arrange(optimal) %>%
mutate(util_notob_sum = cumsum(util_notob)) %>%
select(id, observed, optimal, alpha, A, beta,
utility,
util_ob, util_ob_sum,
util_notob, util_notob_sum)
# Display
kable(tb_onevar) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
```
```{r}
# Following Theorem 1, simply compare util_ob_sum vs util_notob_sum
tb_onevar <- tb_onevar %>%
mutate(opti_better_obs =
case_when(util_notob_sum/util_ob_sum > 1 ~ 1,
TRUE ~ 0)
) %>%
select(id, observed, optimal,
utility,
util_ob, util_ob_sum,
util_notob, util_notob_sum,
opti_better_obs,
alpha, A, beta)
# Display
kable(tb_onevar) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
```
```{r}
# Find the Rank number tha tmatches the First 1 in opti_better_obs
tb_onevar <- tb_onevar %>%
mutate(opti_better_obs_cumu = cumsum(opti_better_obs)) %>%
mutate(min_w_hat =
case_when(opti_better_obs_cumu == 1 ~ optimal,
TRUE ~ 0)) %>%
select(id, observed, optimal,
utility,
util_ob, util_ob_sum,
util_notob, util_notob_sum,
opti_better_obs,
min_w_hat,
alpha, A, beta)
# Display
kable(tb_onevar) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
# Compute REV
it_min_w_hat <- max(tb_onevar %>% pull(min_w_hat))
it_w_hat_obs <- sum(ar_it_obs)
delta_rev <- 1-(it_min_w_hat/it_w_hat_obs)
cat('it_min_w_hat:', it_min_w_hat, '\n')
cat('it_w_hat_obs:', it_w_hat_obs, '\n')
cat('delta_rev:', delta_rev, '\n')
```
## Function test
Test the function written based on the details above.
```{r function test}
# ar_alpha <- c(0.1,1.5,2.5,4 ,9,1.2,3,2,8)
# ar_A <- c(0.5,1.5,3.5,6.5,1.9,3,4,6,4)
# ar_beta <- rep(0, length(ar_A)) + 1/length(ar_A)
# mt_alpha_A <- cbind(ar_alpha, ar_A, ar_beta)
# ar_st_varnames <- c('alpha', 'A', 'beta')
# tb_alpha_A <- as_tibble(mt_alpha_A) %>% rename_all(~c(ar_st_varnames))
# tb_alpha_A
# ar_rho <- c(-1)
for (it_rho_ctr in seq(1,length(ar_rho))) {
fl_rho <- ar_rho[it_rho_ctr]
ls_ranks <- ffp_opt_sobin_target_row(tb_alpha_A[1,], fl_rho, ar_A, ar_alpha, ar_beta)
it_rank <- ls_ranks$it_rank
ar_it_rank <- ls_ranks$ar_it_rank
ar_fl_rank_val <- ls_ranks$ar_fl_rank_val
cat('fl_rho:', fl_rho, 'it_rank:', it_rank, '\n')
cat('ar_it_rank:', ar_it_rank, '\n')
cat('ar_fl_rank_val:', ar_fl_rank_val, '\n')
}
ar_bin_observed <- ar_it_obs
ar_queue_optimal <- ar_it_rank
ffp_opt_sobin_rev(ar_queue_optimal, ar_bin_observed, ar_A, ar_alpha, ar_beta, fl_rho)
```