-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptII_quan_Bayes_lossfun_startagain.r
287 lines (238 loc) · 10.5 KB
/
ptII_quan_Bayes_lossfun_startagain.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
### (C) 2005-2023 by Leo Guertler
### R-code supplement
### to the book
###
### "Subjektive Ansichten und objektive Betrachtungen"
###
### written by Gürtler & Huber (2023)
###
### All R-code is published under the GPL v3 license:
###
### https://www.gnu.org/licenses/gpl-3.0.en.html
###
### except for 'borrowed' code - see links and references.
### For this R-code the original license of the respective
### authors is valid.
###
### R-code published on
###
### https://osdn.net/projects/mixedmethod-rcode
### https://github.com/abcnorio/mixedmethod-rcode
# file:
# ptII_quan_Bayes_lossfun_startagain.r
# location:
# chap. 6 [6.7.1.2]
# Verlustfunktionen
# load necessary libs
library(LaplacesDemon)
# load necessary helper functions
source("ptall_generalfuncs_Bayes_binomial-prop-test.r")
# R package
# library(LaplacesDemon)
# LossMatrix
# Point-estimated loss and state probabilities
L <- matrix(c(-500,1000,-300,-300), 2, 2)
rownames(L) <- c("s[1]: !Defaults","s[2]: Defaults")
colnames(L) <- c("a[1]: Buy ZZZ", "a[2]: Buy XXX")
L
p.theta <- matrix(c(0.9, 0.1, 1, 0), 2, 2)
fit <- LossMatrix(L, p.theta)
fit
# see notes from the package
# success rates start again 1992-2017
# colnames "year","org","IN","OUT","s","f","indiff","mpyear"
sa <- read.table(file="startagain_statistics_1992-2017.tab", header=TRUE, sep="\t")
sa
# set directory
sa.3 <- read.table("startagain_statistics_1992-2017_all-out.tab", header=TRUE, sep="\t")
head(sa.3)
steps <- 1000
theta <- seq(0,1,length.out=steps)
sa.3.d <- dim(sa.3)
si.3 <- sa.3[sa.3.d[1],"s.cs"]
Ni.3 <- sa.3[sa.3.d[1],"N.cs"]
pbl.res <- exp(pbl(theta=theta, si=si.3, Ni=Ni.3, loga=TRUE))
pjc.res <- exp(pjc(theta=theta, si=si.3, Ni=Ni.3, loga=TRUE))
# plot
plot(theta, pbl.res, pre.plot=grid(), col="darkred", type="l", bty="n")
lines(theta,pjc.res, col="green")
# better plots
sN.ME.res <- data.frame(pbl.res, pjc.res)
head(sN.ME.res)
tail(sN.ME.res)
plot.bl.jc(theta, sN.ME.res=sN.ME.res, si=si.3, Ni=Ni.3, filling=TRUE)
plot.bl.jc(theta, sN.ME.res=sN.ME.res, si=si.3, Ni=Ni.3, filling=FALSE)
plot.bl.jc(theta, sN.ME.res=sN.ME.res, si=si.3, Ni=Ni.3, filling=TRUE, sele=c(0.4,0.6))
# create dataframe
theta.mat <- data.frame(theta, pbl.res)
# we work with the pbl and not pjc, difference between both is not really much if at all
theta.mat.pjc <- data.frame(theta, pjc.res)
colnames(theta.mat) <- c("theta","post")
# MAP = maximum aposteriori
theta.map <- theta.mat[theta.mat[,"post"] == max(theta.mat[,"post"]),"theta"]
# mean
theta.mean <- sum(theta.mat[,"theta"] * theta.mat[,"post"] / steps)
# alternative from McElreath p.60f.
# median = min linear loss function
# infids <- apply(res.Xct,2, function(i) which(is.infinite(i),arr.ind=TRUE))
# theta.mat <- res.Xct[-infids$pbl.res,]
theta.linear <- sapply(theta.mat[,"theta"], function(x) sum(theta.mat[,"post"]*abs(x - theta.mat[,"theta"])))
theta.median <- theta.mat[which.min(sapply(theta.mat[,"theta"], function(x) sum(theta.mat[,"post"]*abs(x - theta.mat[,"theta"])))) ,1]
# mean = min quadratic loss function
# MW <- res[which.min(sapply(res[,1], function(x) sum(res[,2]*abs(x - res[,1])^2 ))) ,1]
theta.quad <- sapply(theta.mat[,"theta"], function(x) sum(theta.mat[,"post"]*abs(x - theta.mat[,"theta"])^2 ))
theta.mean <- theta.mat[which.min(sapply(theta.mat[,"theta"], function(x) sum(theta.mat[,"post"]*abs(x - theta.mat[,"theta"])^2 ))) ,1]
# theta mean = a/(a+b)
theta.map
theta.median
theta.mean
# actual loss function calculation
# define reference
ref <- 0.43
# calculate deviations for the loss function
# linear
theta.linear <- sapply(theta.mat[,"theta"], function(x) sum(theta.mat[,"post"]*abs(x - ref)))
head(theta.linear)
# quadratic
theta.quad <- sapply(theta.mat[,"theta"], function(x) sum(theta.mat[,"post"]*(x - ref)^2 ))
head(theta.quad)
tail(theta.quad)
# where to invest
# specific context
theta.linear1 <- sapply(theta.mat[,"theta"], function(x) mean(theta.mat[,"post"]*(abs(x - ref) > 0 & abs(x-ref) < .17)))
head(theta.linear1)
tail(theta.linear1)
# plot loss functions
# ref and associated values
ref
p.max <- 0.6
fac <- 1.2
maxdist <- p.max - ref
# plot single loss functions
par(oma=c(2,1,1,1), "cex.axis"=1, bty="l", cex.axis=1, cex.lab=1.2, cex.main=1, cex.sub=1, ask=TRUE)
# linear difference
theta.linear <- sapply(theta.mat[,"theta"], function(x) mean(theta.mat[,"post"]*abs(x - ref)))
plot(theta.mat[,"theta"], theta.linear, type="l", bty="n", xlab=expression(theta), col="darkred", ylab="linear loss", pre.plot=grid())
abline(v=theta.median, lty=2, lwd=2, col="blue")
abline(v=ref, lty=2, lwd=2, col="green")
text(theta.median,max(theta.linear)/2, "median", col="blue", pos=4)
text(ref,max(theta.linear)/2, "ref", col="green", pos=2)
mtext("Linear loss functions", outer=TRUE, line=-2, cex=1.5, side=3)
# quadratic difference
theta.quad <- sapply(theta.mat[,"theta"], function(x) mean(theta.mat[,"post"]*(x - ref)^2 ))
plot(theta.mat[,"theta"], theta.quad, type="l", bty="n", xlab=expression(theta), col="darkred", ylab="quadratic loss", pre.plot=grid())
abline(v=theta.mean, lty=2, lwd=2, col="blue")
abline(v=ref, lty=2, lwd=2, col="green")
text(theta.mean,max(theta.quad)/2, "mean", col="blue", pos=4)
text(ref,max(theta.quad)/2, "ref", col="green", pos=2)
mtext("Quadratic loss functions", outer=TRUE, line=-2, cex=1.5, side=3)
# limited area where to invest
# use theta.linear1 - see above
ylim <- c(0,1.2)
plot(theta.mat[,"theta"], theta.linear1, ylim=ylim, type="l", bty="n", xlab=expression(theta), col="darkred", ylab="limited area for investment", pre.plot=grid())
abline(v=theta.median, lty=2, lwd=1, col="blue")
abline(v=theta.mean, lty=2, lwd=1, col="red")
abline(v=ref, lty=2, lwd=1, col="green")
text(theta.median,max(theta.linear1)/2, "median", col="blue", pos=4)
text(theta.mean,max(theta.quad)/2, "mean", col="red", pos=2)
text(ref,max(theta.linear1)/2, "ref", col="green", pos=2)
mtext("Linear loss functions with limited area", outer=TRUE, line=-2, cex=1.5, side=3)
# limited area where to invest + bonus
theta.linear2 <- sapply(theta.mat[,"theta"], function(x) {
DIFF <- x-ref
BENEFIT <- 0
if(abs(DIFF) > 0 & abs(DIFF) < maxdist) BENEFIT <- 1
if(DIFF > 0 & abs(DIFF) < .17) BENEFIT <- 1 + DIFF
mean(theta.mat[,"post"] * BENEFIT)
} )
plot(theta.mat[,"theta"], theta.linear2, ylim=ylim, type="l", bty="n", xlab=expression(theta), col="darkred", ylab="limited area for investment + bonus", pre.plot=grid())
abline(v=theta.median, lty=2, lwd=2, col="blue")
abline(v=theta.mean, lty=2, lwd=2, col="red")
abline(v=ref, lty=2, lwd=2, col="green")
text(theta.median,max(theta.linear)/2, "median", col="blue", pos=4)
text(theta.mean,max(theta.quad)/2, "mean", col="red", pos=2)
text(ref,max(theta.linear2)/2, "ref", col="green", pos=2)
mtext("Linear loss functions (with area to invest)", outer=TRUE, line=-2, cex=1.5, side=3)
# bonus with upper limit
uplimit <- 0.6
theta.b <- seq(ref,uplimit,0.01)
theta.b
satz <- 450
fac <- 1.05
length(theta.b)
bonus <- satz * seq(1,1.05,length.out=18)
bonus
theta.l <- length(theta.b[theta.b >= 0.43 & theta.b < 0.6])
theta.b <- theta
theta.b[theta.b < 0.2] <- 0
theta.b[theta.b >= 0.2 & theta.b < 0.43] <- 450
theta.b[theta.b >= 0.43 & theta.b < 0.6] <- satz * seq(1,1.05,length.out=theta.l)
theta.b[theta.b >= 0.6 & theta.b <= 1] <- satz*fac
# everything on one plot...
par(oma=c(2,1,4,1), "cex.axis"=1, bty="l", mfrow=c(3,2))
#
# linear loss
plot(theta.mat[,"theta"], theta.linear, main="linear loss", type="l", bty="n", xlab=expression(theta), col="darkred", ylab="linear loss", pre.plot=grid(), cex.lab=1.2)
abline(v=ref, lty=2, lwd=2, col="blue")
text(ref,max(theta.linear)/2, "ref", col="blue", pos=2)
#
# quadratic loss
plot(theta.mat[,"theta"], theta.quad, main="quadratic loss", type="l", bty="n", xlab=expression(theta), col="darkred", ylab="quadratic loss", pre.plot=grid(), cex.lab=1.2)
abline(v=ref, lty=2, lwd=2, col="blue")
text(ref,max(theta.quad)/2, "ref", col="blue", pos=2)
#
# linear1 limited area to invest
ylim <- c(0,1.2)
plot(theta.mat[,"theta"], theta.linear1, ylim=ylim, main="linear loss functions (with limited area to invest)", type="l", bty="n", xlab=expression(theta), col="darkred", ylab="linear loss + investment area", pre.plot=grid())
abline(v=theta.median, lty=2, lwd=1, col="blue")
abline(v=theta.mean, lty=2, lwd=1, col="red")
abline(v=ref, lty=2, lwd=1, col="green")
text(theta.median,max(theta.linear1)/2, "median", col="blue", pos=4)
text(theta.mean,max(theta.quad)/2, "mean", col="red", pos=2)
text(ref,max(theta.linear1)/2, "ref", col="green", pos=2)
#
# linear2 limited area to invest + bonus
plot(theta.mat[,"theta"], theta.linear2, main="linear loss (with limited area to invest + bonus)", ylim=ylim, type="l", bty="n", xlab=expression(theta), col="darkred", ylab="linear loss + area to invest + bonus", pre.plot=grid(), cex.lab=1.2)
abline(v=ref, lty=2, lwd=2, col="blue")
text(ref,max(theta.linear2)/2, "ref", col="blue", pos=2)
#
# bonus with upper limit
plot(theta, theta.b, main="linear loss (with bonus and upper limit)", ylim=c(0,600), type="l", bty="n", xlab=expression(theta), col="darkred", ylab="linear loss + bonus + upper limit", pre.plot=grid(), cex.lab=1.2)
abline(v=ref, lty=2, lwd=2, col="blue")
text(ref,mean(theta.b)/2, "ref", col="blue", pos=2)
# main title
mtext("Loss functions", outer=TRUE, line=0.5, cex=1.8, side=3)
# end of all plots
# UMS example (Studer, 1996)
si <- 23
Ni <- 27
pbl.res <- exp(pbl(theta=theta, si=si, Ni=Ni, loga=TRUE))
pjc.res <- exp(pjc(theta=theta, si=si, Ni=Ni, loga=TRUE))
# plot
plot(theta, pbl.res, pre.plot=grid(), col="darkred", type="l", bty="n")
sN.ME.res <- data.frame(pbl.res, pjc.res)
head(sN.ME.res)
tail(sN.ME.res)
plot.bl.jc(theta, sN.ME.res=sN.ME.res, si=si, Ni=Ni, filling=TRUE)
plot.bl.jc(theta, sN.ME.res=sN.ME.res, si=si, Ni=Ni, filling=FALSE)
plot.bl.jc(theta, sN.ME.res=sN.ME.res, si=si, Ni=Ni, filling=TRUE, sele=c(0.5,1))
# difference between both posterior distributions
par(mfrow=c(2,2))
plot((with(sN.ME.res, pbl.res-pjc.res)), type="l", bty="n", pre.plod=grid(), main="BL - JC", ylab="BL-JC")
plot((with(sN.ME.res, abs(pbl.res-pjc.res))), type="l", bty="n", pre.plod=grid(), main="BL - JC", ylab="abs(BL-JC)")
plot((with(sN.ME.res, log(abs(pbl.res-pjc.res)))), type="l", bty="n", pre.plod=grid(), main="BL - JC", ylab="log(abs(BL-JC))")
sN.ME.post.summary <- sN.post.su(Ni=Ni, si=si)
ab <- bino.ab.lik(si=si, Ni=Ni)
a <- as.numeric(ab[["a"]])
b <- as.numeric(ab[["b"]])
a
b
a/(a+b)
1-(theta.mean/(a/(a+b)))
theta.var <- a*b / ( (a+b)^2 * (a+b+1) )
theta.sd <- sqrt(theta.var)
theta.var
theta.sd
theta.summary <- c(theta.map, theta.median, theta.mean, theta.sd, theta.var)
names(theta.summary) <- c("mode (MAP)","median","mean","sd","var")
theta.summary