-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.R
226 lines (163 loc) · 8.59 KB
/
Test.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
rm(list = ls())
source("formulas.r")
source("hyperparametersfunctions.r")
source("BetSizingFormulas.r")
source("bestsubsetsformulas.r")
library(leaps)
library(bestglm)
library(glmnet)
library(boot)
library(MASS)
train_modelwithRatings = read.table("Data/datasets/train_modelWithRatings.csv"
, header = T, sep = ",", quote = "\"", fill = TRUE)
cv_withRatings = read.table("Data/datasets/testWithRatings.csv"
, header = T, sep = ",", quote = "\"", fill = TRUE)
cv_withRatings = cv_withRatings[!is.na(cv_withRatings$Best.of), ]
xtrain = rbind(train_modelwithRatings, cv_withRatings)
rm(train_modelwithRatings, cv_withRatings)
test = read.table("Data/datasets/testWithRatings.csv"
, header = T, sep = ",", quote = "\"", fill = TRUE)
test = test[!is.na(test$Best.of), ]
Ntrain = nrow(xtrain)
Ntest = nrow(test)
set.seed(42)
ytrain = as.numeric(runif(Ntrain, 0, 1) > 0.5)
ytest = as.numeric(runif(Ntest, 0, 1) > 0.5)
xtrain = regressorvariables(ytrain, xtrain)
q = 27
quantile = quantile(xtrain$Uncertainty, q / 100)
xtrain = removeUncertainMatches(xtrain, quantile, "peter")
xtrainHard = getXThisSurface(xtrain, "Hard")
xtrainGrass = getXThisSurface(xtrain, "Grass")
#xtrainHard = relevantVariables(xtrainHard)
regHard = glm(y ~ 0 + ratingdiff + ratingHarddiff
#+ DummyBo5TimesAvgRatingdiff
# + RetiredDiff + FatigueDiff#+ HeadtoHeadPercentageWeightedsqN #this one even better
#+ HeadtoHead #appears to improve the loglikelihood, so maybe interesting, also improve
#BR a lot but doesn't work for grass
, data = xtrainHard, family = binomial)
#regGrass = glm(y ~ 0 + ratingdiff + ratingGrassdiff + DummyBo5TimesAvgRatingdiff
# +FatigueDiff
# , data = xtrainGrass, family = binomial)
xtest = regressorvariables(ytest, test)
xtest = removeUncertainMatches(xtest, quantile, "Peter")
xtestHard = getXThisSurface(xtest, "Hard")
#xtestHard = relevantVariables(xtestHard)
xtestGrass = getXThisSurface(xtest, "Grass")
testpredHard = predict(regHard, xtestHard, type = "response")
LogLoss(testpredHard, xtestHard$y)
testpredGrass = predict(regGrass, xtestGrass, type = "response")
#predictionsHard = 0.4314 * testpredHard + 0.5790 * xtestHard$ImpProb
#predictionsGrass = 0.8582 * testpredGrass + 0.1607 * xtestGrass$ImpProb
#This is kind a random for Hard, but it seems to go to infinity so whatever?
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#predictionsHard = 0.85 * testpredHard + 0.15 * xtestHard$ImpProb
#predictionsGrass = 0.85 * testpredGrass + 0.15 * xtestGrass$ImpProb
predictionsHard = 0.2 * testpredHard + 0.8 * xtestHard$PSWImpr
predictionsGrass = 0.2 *testpredGrass + 0.8 * xtestGrass$PSWImpr
combinedResultsHard = result(predictionsHard, xtestHard, xtestHard$y)
combinedResultsGrass = result(predictionsGrass, xtestGrass, xtestGrass$y)
resultsHard = combinedResultsHard$results
resultsGrass = combinedResultsGrass$results
betsHard = combinedResultsHard$bets
betsGrass = combinedResultsGrass$bets
betsHard$bet = replace(betsHard$bet, betsHard$bet == 0, -500)
betsHard$result = replace(betsHard$result, betsHard$result == 0, -500)
plot(1 : length(betsHard$br), betsHard$br, ylim = c(-30, 60), "l")
points(betsHard$bet, col = "red")
points(betsHard$result, col = "green")
betsGrass$bet = replace(betsGrass$bet, betsGrass$bet == 0, -500)
betsGrass$result = replace(betsGrass$result, betsGrass$result == 0, -500)
plot(1 : length(betsGrass$br), betsGrass$br, ylim = c(-20, 20), "l")
points(betsGrass$bet, col = "red")
points(betsGrass$result, col = "green")
getLogLoss = function(data, index){
predictionsB = data[index, 1]
ytestB = data[index, 2]
return(LogLoss(predictionsB, ytestB))
}
dataHard = as.data.frame(cbind(predictionsHard, ytestHard))
bootHard = boot(data = dataHard, statistic = getLogLoss, R = 1000)
upperBoundHard = 0.5486395 + 1.96 * 0.01533176
lowerBoundHard = 0.5486395 - 1.96 * 0.01533176
dataGrass = as.data.frame(cbind(predictionsGrass, ytestGrass))
bootGrass = boot(data = dataGrass, statistic = getLogLoss, R = 1000)
upperBoundGrass = 0.5541796 + 1.96 * 0.05029078
lowerBoundGrass = 0.5541796 - 1.96 * 0.05029078
getLogLoss2Hard = function(data, index){
xtrainB = data[index, ]
ytrainB = data$ytrainHard[index]
regB = glm(ytrainB ~ 0 + ratingdiff + ratingHarddiff + DummyBo5TimesAvgRatingdiff +
RetiredDiff + FatigueDiff, data = xtrainB, family = binomial)
testpredB = predict(regB, xtestHard, type = "response")
predictionsB = 0.85 * testpredB + 0.15 * xtestHard$ImpProb
return(LogLoss(predictionsB, ytestHard))
}
getLogLoss2Grass = function(data, index){
xtrainB = data[index, ]
ytrainB = data$ytrainGrass[index]
regB = glm(ytrainB ~ 0 + ratingdiff + ratingGrassdiff + DummyBo5TimesAvgRatingdiff +
FatigueDiff, data = xtrainB, family = binomial)
testpredB = predict(regB, xtestGrass, type = "response")
predictionsB = 0.85 * testpredB + 0.15 * xtestGrass$ImpProb
return(LogLoss(predictionsB, ytestGrass))
}
dataHard2 = as.data.frame(cbind(xtrainHard, ytrainHard))
bootHard2 = boot(data = dataHard2, statistic = getLogLoss2Hard, R = 1000)
upperBoundHard = 0.5486395 + 1.96 * 0.0008083271
lowerBoundHard = 0.5486395 - 1.96 * 0.0008083271
dataGrass2 = as.data.frame(cbind(xtrainGrass, ytrainGrass))
bootGrass2 = boot(data = dataGrass2, statistic = getLogLoss2Grass, R = 1000)
upperBoundGrass = 5541796 + 1.96 * 0.003812915
lowerBoundGrass = 5541796 - 1.96 * 0.003812915
#only the most recent half data
dataHard3 = dataHard2[floor(nrow(dataHard2)/2):nrow(dataHard2), ]
dataGrass3 = dataGrass2[floor(nrow(dataGrass2)/2):nrow(dataGrass2), ]
bootHard3 = boot(data = dataHard3, statistic = getLogLoss2Hard, R = 1000)
bootGrass3 = boot(data = dataGrass3, statistic = getLogLoss2Grass, R = 1000)
#time for a good old chowbreaktest
#CONCLUSIONS PROBABLY SUCK BECAUSE CHOW BREAK TEST IS FOR LINEAR REGRESSION YO
dataHardFirst = dataHard2[1 : floor(nrow(dataHard2)/2), ]
dataHardLast = dataHard2[floor(nrow(dataHard2)/2):nrow(dataHard2), ]
regHardFirst = glm(ytrainHard ~ 0 + ratingdiff + ratingHarddiff + DummyBo5TimesAvgRatingdiff +
RetiredDiff + FatigueDiff, data = dataHardFirst, family = binomial)
regHardLast = glm(ytrainHard ~ 0 + ratingdiff + ratingHarddiff + DummyBo5TimesAvgRatingdiff +
RetiredDiff + FatigueDiff, data = dataHardLast, family = binomial)
diffB = (regHardFirst$coefficients-regHardLast$coefficients)
inversesumvar = ginv(vcov(regHardFirst) + vcov(regHardLast))
WaldT = t(diffB) %*% inversesumvar %*% diffB
qchisq(0.95, df=5)
#WaldT is a lot smaller than the criticial value so probably no break in hard
dataGrassFirst = dataGrass2[1 : floor(nrow(dataGrass2)/2), ]
dataGrassLast = dataGrass2[floor(nrow(dataGrass2)/2):nrow(dataGrass2), ]
regGrassFirst = glm(ytrainGrass ~ 0 + ratingdiff + ratingGrassdiff + DummyBo5TimesAvgRatingdiff +
FatigueDiff, data = dataGrassFirst, family = binomial)
regGrassLast = glm(ytrainGrass ~ 0 + ratingdiff + ratingGrassdiff + DummyBo5TimesAvgRatingdiff +
FatigueDiff, data = dataGrassLast, family = binomial)
diffB = (regGrassFirst$coefficients-regGrassLast$coefficients)
inversesumvar = ginv(vcov(regGrassFirst) + vcov(regGrassLast))
WaldT = t(diffB) %*% inversesumvar %*% diffB
qchisq(0.95, df=5)
#WaldT is a lot smaller than the criticial value so probably no break in grass
#lets do a breaktest between hard and grass for fun/checking whether it works
regHard = glm(ytrainHard ~ 0 + ratingdiff + ratingHarddiff + DummyBo5TimesAvgRatingdiff +
RetiredDiff + FatigueDiff, data = dataHard2, family = binomial)
regGrass = glm(ytrainGrass ~ 0 + ratingdiff + ratingHarddiff + DummyBo5TimesAvgRatingdiff +
RetiredDiff + FatigueDiff, data = dataGrass2, family = binomial)
diffB = (regHard$coefficients-regGrass$coefficients)
inversesumvar = ginv(vcov(regHard) + vcov(regGrass))
WaldT = t(diffB) %*% inversesumvar %*% diffB
qchisq(0.95, df=5)
#Bootstrap roi
getROI = function(data, index){
xtestB = data[index, 1 : (length(data) - 2)]
ytestB = data[index, (length(data) - 1)]
predictionsB = data[index,length(data)]
combinedResultsB = result(predictionsB, xtestB, ytestB)
resultsB = combinedResultsB$results
return(resultsB$ROI)
}
dataHardForROI = cbind(xtestHard, ytestHard, predictionsHard)
bootROIHard = boot(data = dataHardForROI, statistic = getROI, R = 1000)
dataGrassForROI = cbind(xtestGrass, ytestGrass, predictionsGrass)
bootROIGrass = boot(data = dataGrassForROI, statistic = getROI, R = 1000)