-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_functions.R
More file actions
151 lines (128 loc) · 6.81 KB
/
code_functions.R
File metadata and controls
151 lines (128 loc) · 6.81 KB
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
#######################################################
# 1. FORMULAE FOR COEFFS/CONSTANTS
E<-function(m,r) 1-ifelse((length(r)-m)<1,0,sum(sapply(1:(length(r)-m),function(i) r[i+m]*ifelse((i-1)<1,1,prod(sapply(1:(i-1),function(j) (1-r[j+m])))))))
J<-function(m,r,gam,w) sum(sapply(1:(length(r)-m+1),function(i) (gam[i+m-1]-w)*ifelse((i-1)<1,1,prod(sapply(1:(i-1),function(j) (1-r[j+m]))))))
G<-function(m,r,rho) rho+(1-rho)*ifelse((length(r)-m)<1,0,sum(sapply(1:(length(r)-m),function(i) r[i+m]*ifelse((i-1)<1,1,prod(sapply(1:(i-1),function(j) (1-r[j+m])))))))
Fx<-function(r,l) l-l*sum(sapply(1:length(r),function(m) r[m]*ifelse((m-1)<1,1,prod(sapply(1:(m-1),function(i) (1-r[i]))))))
I<-function(r,l,gam,w) l*sum(sapply(1:length(r),function(m) (gam[m]-w)*prod(sapply(1:m,function(i) (1-r[i])))))
H<-function(r,l,rho) (1-rho)*l*sum(sapply(1:length(r),function(m) r[m]*ifelse((m-1)<1,1,prod(sapply(1:(m-1),function(i) (1-r[i]))))))
A<-function(r,l,gam,w,rho) t(as.matrix(data.frame(E=sapply(1:length(r),function(m) E(m,r)),
J=sapply(1:length(r),function(m) J(m,r,gam,w)),
G=sapply(1:length(r),function(m) G(m,r,rho)))))
b<-function(r,l,gam,w,rho) c(Fx(r,l),I(r,l,gam,w),H(r,l,rho))
n<-function(m,Tx,r,l) l*ifelse(m<1,1,prod(sapply(1:m,function(i) (1-r[i]))))-ifelse(m<1,0,sum(sapply(1:m,function(i) Tx[i]*ifelse((i+1)>m,1,prod(sapply((i+1):m,function(j) (1-r[j])))))))
n_vectorised_m<-function(m_vector,Tx,r,l) sapply(m_vector,function(m) n(m,Tx,r,l))
print_coeffs<-function(r,l,gam,w,rho) {
print(paste("E:",paste0(round(sapply(1:length(r),function(m) E(m,r)),5),collapse=", ")),quote=FALSE)
print(paste("J:",paste0(round(sapply(1:length(r),function(m) J(m,r,gam,w)),5),collapse=", ")),quote=FALSE)
print(paste("G:",paste0(round(sapply(1:length(r),function(m) G(m,r,rho)),5),collapse=", ")),quote=FALSE)
print(paste("F:",paste0(round(Fx(r,l),5))),quote=FALSE)
print(paste("I:",paste0(round(I(r,l,gam,w),5))),quote=FALSE)
print(paste("H:",paste0(round(H(r,l,rho),5))),quote=FALSE)
print(paste("A:"),quote=FALSE)
print(round(A(r,l,gam,w,rho),5),quote=FALSE)
print(paste("b:"),quote=FALSE)
print(round(b(r,l,gam,w,rho),5),quote=FALSE)
}
#######################################################
# 2. SOLUTIONS
################
# solution 1: no optimisation - just solving the three objectives
solution_lp_standard<-function(r,l,gam,w,rho) {
tmp_A<-A(r,l,gam,w,rho)
tmp_b<-b(r,l,gam,w,rho)
# zero objective to find any feasible solution
f.obj<-rep(0,ncol(tmp_A))
# constraint: Ax=b (equality), noting x>=0 is default in lpSolve
f.con<-tmp_A
f.dir<-rep("=",nrow(tmp_A))
f.rhs<-tmp_b
# solution
lp("min",f.obj,f.con,f.dir,f.rhs)
}
Tx_solution_lp_standard<-function(r,l,gam,w,rho) solution_lp_standard(r,l,gam,w,rho)$solution
################
# solution 2: optimisation - minimise positive differences between Tx
solution_lp_optimised_rolling_diffs<-function(r,l,gam,w,rho) {
tmp_A<-A(r,l,gam,w,rho)
tmp_b<-b(r,l,gam,w,rho)
tmp_M<-length(r)
# objective: minimize sum d_m, so coeffs: 0 for x, 1 for each d
f.obj<-c(rep(0,tmp_M),rep(1,tmp_M-1))
# constraint 1: Ax=b equality constraints
con_Ax<-cbind(tmp_A,matrix(0,nrow=nrow(tmp_A),ncol=ncol(tmp_A)-1))
dir_Ax<-rep("=",nrow(tmp_A))
rhs_Ax<-tmp_b
# constraint 2: d_m >= x_{m+1} - x_m
con_d_diff<-matrix(0,nrow=ncol(tmp_A)-1,ncol=2*ncol(tmp_A)-1)
for (m in 1:(tmp_M-1)) {
con_d_diff[m,m]<-1 # x_m
con_d_diff[m,m+1]<- -1 # -x_{m+1}
con_d_diff[m,tmp_M+m]<-1 # d_m
}
dir_diff<-rep(">=",tmp_M-1)
rhs_diff<-rep(0,tmp_M-1)
# combine
f.con<-rbind(con_Ax,con_d_diff)
f.dir<-c(dir_Ax,dir_diff)
f.rhs<-c(rhs_Ax,rhs_diff)
# solution
lp("min",f.obj,f.con,f.dir,f.rhs,all.int=FALSE)
}
Tx_solution_lp_optimised_rolling_diffs<-function(r,l,gam,w,rho) solution_lp_optimised_rolling_diffs(r,l,gam,w,rho)$solution[1:length(r)]
################
# solution 3: optimisation - minimise difference to provided Tx (presumably based on historical data)
solution_lp_optimised_diffs_to_given_s<-function(r,l,gam,w,rho,s_given) {
tmp_Tx_given<-s_given*l*(1-rho)
tmp_A<-A(r,l,gam,w,rho)
tmp_b<-b(r,l,gam,w,rho)
tmp_M<-length(r)
# objective: minimize sum d_m, so coeffs: 0 for x, 1 for each d
f.obj<-c(rep(0,tmp_M),rep(1,tmp_M))
# constraint 1: Ax=b equality constraints
con_Ax<-cbind(tmp_A,matrix(0,nrow=nrow(tmp_A),ncol=ncol(tmp_A)))
dir_Ax<-rep("=",nrow(tmp_A))
rhs_Ax<-tmp_b
# constraint 2: d_m >= x_m - tmp_Tx_given_m: d_m - x_m >= -tmp_Tx_given_m
con_d_pos<-matrix(0,nrow=ncol(tmp_A),ncol=2*ncol(tmp_A))
for (m in 1:tmp_M) {
con_d_pos[m,m]<- -1 # -x_m
con_d_pos[m,tmp_M+m]<- 1 # d_m
}
dir_d_pos<-rep(">=",tmp_M)
rhs_d_pos<- -tmp_Tx_given
# constraint 3: d_m >= tmp_Tx_given_m - x_m: d_m + x_m >= tmp_Tx_given_m
con_d_neg<-matrix(0,nrow=ncol(tmp_A),ncol=2*ncol(tmp_A))
for (m in 1:tmp_M) {
con_d_neg[m,m]<-1 # x_m
con_d_neg[m,tmp_M+m]<-1 # d_m
}
dir_d_neg<-rep(">=",tmp_M)
rhs_d_neg<-tmp_Tx_given
# combine
f.con<-rbind(con_Ax,con_d_pos,con_d_neg)
f.dir<-c(dir_Ax,dir_d_pos,dir_d_neg)
f.rhs<-c(rhs_Ax,rhs_d_pos,rhs_d_neg)
# solution
lp("min",f.obj,f.con,f.dir,f.rhs,all.int=FALSE)
}
Tx_solution_lp_optimised_diffs_to_given_s<-function(r,l,gam,w,rho,s_given) solution_lp_optimised_diffs_to_given_s(r,l,gam,w,rho,s_given)$solution[1:length(r)]
#######################################################
# 3. VALIDATION CHECKS
print_objectives_alignment<-function(Tx,r,l,gam,w,rho) {
n<-function(m) l*ifelse(m<1,1,prod(sapply(1:m,function(i) (1-r[i]))))-ifelse(m<1,0,sum(sapply(1:m,function(i) Tx[i]*ifelse((i+1)>m,1,prod(sapply((i+1):m,function(j) (1-r[j])))))))
# obj1
print(paste0("Obj 1, steady state reneging + treatments (5dp): ",round(sum(Tx)+sum(sapply(1:length(Tx),function(m) r[m]*n(m-1))),5),
" (should equal ",round(l,5)," - referrals)"),quote=FALSE)
# obj2
print(paste0("Obj 2, performance (5dp): ",round(sum(sapply(1:length(Tx),function(m) gam[m]*n(m)))/sum(sapply(1:length(Tx),function(m) n(m))),5)," (should equal ",w,")"),quote=FALSE)
# obj3
print(paste0("Obj 3, renege rate (5dp): ",
round((sum(sapply(1:length(Tx),function(m) r[m]*n(m-1))))/(sum(Tx)+sum(sapply(1:length(Tx),function(m) r[m]*n(m-1)))),5),
" (should equal ",round(rho,5),")"),quote=FALSE)
# matrix check
print(paste0("A·Tx (5dp): ",paste0(round(A(r,l,gam,w,rho) %*% Tx,5),collapse=", ")),quote=FALSE)
print(paste0("b (5dp): ",paste0(round(b(r,l,gam,w,rho),5),collapse=", ")),quote=FALSE)
#print(paste("Note: A·Tx should equal b for all three objectives to be satisfied"),quote=FALSE)
}
#######################################################