-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.R
203 lines (155 loc) · 6.81 KB
/
functions.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
## Define model performance query
#
custom_query_modelID <- function(username) {run_query(query = paste0('query{v3UserProfile(modelName: \"',username,'\") {id}}'), auth=FALSE)[[1]]$id}
if (!exists("mem_custom_query_modelID")) {
mem_custom_query_modelID <- memoise(custom_query_modelID)
}
custom_query <- function(userid) {paste0('query{v2RoundModelPerformances(modelId: \"',userid,'\") {
roundNumber
submissionScores {
date
day
displayName
value
}
}
}')}
acf1_pairwise <- function(series) {
if (!is.numeric(series) || length(series) < 2) {
stop("Series must be a numeric vector with at least two elements.")
}
# Create a lagged version of the series
lagged_series <- c(NA, series[-length(series)])
# Calculate the correlation with complete observations
acf1_value <- cor(series, lagged_series, use = "complete.obs")
return(acf1_value)
}
## Query model performance of all rounds since fromRound until last resolved round + 10 rounds
#
model_performance <- function(modelName, fromRound) {
output <- run_query(query = custom_query(mem_custom_query_modelID(tolower(modelName))), auth=FALSE)$v2RoundModelPerformances
output$nodata <- sapply(output$submissionScores,is.null)
output2 <- output %>% dplyr::filter(roundNumber >= fromRound & nodata == FALSE)
output3 <- data.frame()
for (i in 1:nrow(output2)) {
out <- output2[i,2][[1]]
out$roundNumber <- output2[i,1]
output3 <- rbind(output3,out)
}
# We include rounds that are within 10 rounds of resolving.
output <- pivot_wider(output3,names_from=displayName) %>% dplyr::filter(day > 9 & day <= 20) %>% dplyr::select(roundNumber,canon_corr,canon_mmc,bmc)
colnames(output) <- c("roundNumber","corr","mmc","bmc")
return(output)
}
## Memoize model performance query.
#
if (!exists("mem_model_performance")) {
mem_model_performance <- memoise(model_performance)
}
# Load in the performance data.
#
build_RAW <- function (model_df, MinfromRound = 1, corr_multiplier = 0.5, mmc_multiplier = 2, bmc_multiplier = 0) {
model_names <- model_df$name
model_starts <- model_df$start
RAW <- data.frame()
for (i in 1:length(model_names)) {
# Don't spam the API.
Sys.sleep(0.2)
print(model_names[i])
temp <- mem_model_performance(model_names[i],max(MinfromRound,model_starts[i]))
temp <- dplyr::select(temp,roundNumber,corr,mmc,bmc)
temp$score <- corr_multiplier * temp$corr + mmc_multiplier * temp$mmc + bmc_multiplier * temp$bmc
temp <- dplyr::select(temp,roundNumber,score)
temp$name <- model_names[i]
RAW <- rbind(RAW,temp)
}
data_ts <- unique(RAW) %>% group_by(name,roundNumber) %>% dplyr::ungroup() %>% tidyr::pivot_wider(names_from = name,values_from = score,values_fill = list(value = NA))
data_ts <- data.frame(dplyr::arrange(data_ts,roundNumber))
return(data_ts)
}
## Cumulative Plotting function.
#
cumulative_plot <- function(model_df,starting_era,good_models_all,daily,type) {
subset_daily <- dplyr::filter(daily,roundNumber >= starting_era)
subset_data <- dplyr::select(subset_daily,roundNumber,paste0(good_models_all,type)) %>%
mutate(across(-roundNumber, ~cumsum(replace_na(.x, 0))))
subset_data <- subset_data %>% pivot_longer(cols = -roundNumber, names_to = "model", values_to = "value")
ggplot(subset_data) + geom_line(aes(x=roundNumber,y=value,color=model))
}
tangency_portfolio <- function(daily_data) {
data <- daily_data
dates <- seq.Date(from = as.Date("1900-01-01"), by = "day", length.out = dim(data)[1])
spec <- portfolioSpec()
setType(spec) <- "CVAR"
setSolver(spec) <- "solveRglpk.CVAR"
TS <- timeSeries(data,dates)
colnames(TS) <- colnames(data)
optimized <- tangencyPortfolio(TS, spec = spec, constraints = "LongOnly")
return(optimized)
}
minvariance_portfolio <- function(daily_data) {
data <- daily_data
dates <- seq.Date(from = as.Date("1900-01-01"), by = "day", length.out = dim(data)[1])
spec <- portfolioSpec()
setType(spec) <- "CVAR"
setSolver(spec) <- "solveRglpk.CVAR"
TS <- timeSeries(data,dates)
colnames(TS) <- colnames(data)
optimized <- minvariancePortfolio(TS, spec = spec, constraints = "LongOnly")
return(optimized)
}
## Clean up the returned portfolio into something we can add to a dataframe.
#
cleanup_portfolio <- function(portfolio) {
weights <- data.frame(getWeights(portfolio))
colnames(weights) <- c("weight")
weights <- rownames_to_column(weights, var = "name")
return(weights[weights$weight > 0.001,])
}
## Calculate returns
#
virtual_returns <- function(daily,portfolio) {
daily <- dplyr::select(daily,portfolio$name) %>% na.omit()
dates <- seq.Date(from = as.Date("1900-01-01"), by = "day", length.out = dim(daily)[1])
ewSpec <- portfolioSpec()
setType(ewSpec) <- "CVAR"
setSolver(ewSpec) <- "solveRglpk.CVAR"
setWeights(ewSpec) <- portfolio$weight
port <- feasiblePortfolio(timeSeries(daily, dates), spec = ewSpec, constraints = "LongOnly")
output <- c(round(fPortfolio::getTargetReturn(port)[1],4),
round(fPortfolio::getTargetRisk(port)[1],4),
round(fPortfolio::getTargetRisk(port)[3],4),
round(fPortfolio::getTargetRisk(port)[4],4),
samplesize = dim(daily)[1])
return(t(output))
}
# Build portfolios.
#
build_portfolio <- function(daily,threshold) {
portfolio1 <- tangency_portfolio(daily[sample(nrow(daily),replace = TRUE),]) %>% cleanup_portfolio()
portfolio2 <- minvariance_portfolio(daily[sample(nrow(daily),replace = TRUE),]) %>% cleanup_portfolio()
for (i in 1:39) {
portfolio1 <- rbind(portfolio1,tangency_portfolio(daily[sample(nrow(daily),replace = TRUE),]) %>% cleanup_portfolio())
portfolio2 <- rbind(portfolio2,minvariance_portfolio(daily[sample(nrow(daily),replace = TRUE),]) %>% cleanup_portfolio())
}
portfolio <- rbind(portfolio1,portfolio2) %>% group_by(name) %>% summarise(weight = mean(weight))
# rebalance to remove small weights
portfolio <- portfolio[portfolio$weight > threshold,]
portfolio$weight <- round(portfolio$weight * 1/sum(portfolio$weight),3)
#
# Calculating stakesize for different multipliers
#
portfolio$stake <- round(portfolio$weight * NMR)
return(portfolio)
}
build_plot <- function(portfolio,starting_point) { # name weights starting round
plot_data <- dplyr::select(daily_data,c(roundNumber,portfolio$name))
plot_data <- plot_data[complete.cases(plot_data),]
weighted_data <- plot_data %>%
mutate(across(all_of(portfolio$name),
~ . * portfolio$weight[portfolio$name == cur_column()])) %>%
select(-roundNumber) %>%
rowSums()
result <- data.frame(roundNumber = plot_data$roundNumber, cumulative_portfolio_score = cumsum(weighted_data), starting_round = starting_point)
return(result)
}