-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 31.1 KB
/
.Rhistory
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
df <- as.data.frame(lapply(X,as.numeric)
output <- matrix(lapply(unlist(df),as.numeric), ncol = 7, byrow = FALSE)
output <- matrix(lapply(unlist(df),as.numeric), ncol = 7, byrow = FALSE)
output <- matrix(unlist(df), ncol = 7, byrow = FALSE)
#' Simulating test data that Qlik will send to openCPU
QlikJson <-
'{"data":{
"Date":["41950","41949","41948","41947","41946","41943","41942","41941","41940","41939","41936","41935","41934","41933","41932","41929","41928","41927"],
"SPY":["0.000935269505291547","0.0040031629929822","0.00631620828567181","-0.00346929672399277","0.000545472577605954","0.0114354498946736","0.00641057998081873","-0.00151202056347954","0.0114702283849919","-0.00137453545792399","0.00769507002513725","0.0116248897192381","-0.00711083629618181","0.019810825013137","0.00970976813285951","0.0118108122617704","-0.000858230971410134","-0.00676611614278089"],
"EFA":["0.000477554918815626","-0.0053831538948701","0.00190355329949243","-0.00567823343848584","-0.00860046911649737","0.0184742793438446","0.00721847930702602","-0.00764087870105057","0.0151906916612798","-0.00562429696287958","0.00549361770883827","0.0096247960848288","-0.00728744939271264","0.0119632907243525","0.0062664907651715","0.0186460608096757","-0.00931935430188058","-0.00595533498759304"],
"IWM":["0.00154466660945674","0.00430923037145559","0.00181315834916251","-0.00464077002406327","-0.00171585449553879","0.0147122834508575","0.00851624231782266","-0.00236489445563626","0.0284659039726149","-0.000540199873953262","0.00171356421356417","0.0177145479577787","-0.0142947616031847","0.0164612837962113","0.0117231112765164","-0.00296846011131724","0.0112570356472796","0.0100435853704754"],
"VWO":["0.00649975926817525","-0.0142382534409112","-0.00589761736258554","0.000944510035419066","-0.00680112570356473","0.00803782505910178","0.0114777618364419","-0.00190930787589494","0.0209551656920077","-0.0103689414034241","0.009493670886076","0.000974658869395739","-0.00797679477882518","0.00461389023797953","0.000729040097205313","0.0127984248092541","-0.00999025341130599","-0.0139356078808265"],
"LQD":["0.00633660020277116","-0.00303234501347704","-0.00184967210358167","0.00143133787993599","-0.00477626948215193","-0.000167560321715832","0.000670690811535923","-0.00184100418410038","-0.00333611342785656","0.00125260960334028","0.00041771094402665","-0.00191778537480203","-0.00166486306501279","-0.000582362728785424","0.00183363893982325","-0.000416562526035169","-0.0022443890274314","0.000748689792862578"],
"HYG":["0.000975609756097562","0.00260841212911633","-0.000217320438987256","-0.00314124783362213","-0.00226953420512277","0.000865332612222769","-0.000432479186939005","-0.000756266205704459","0.00162320095227786","-0.00409526888673362","0.00280989949205668","-0.000432105433725738","-0.00654646919939916","0.00811424862057764","0.00719189277541687","0.00846153846153852","0.00563598187645042","0.000221067757267601"]
},
"type":{
"Date":"dimension",
"SPY":"expression",
"EFA":"expression",
"IWM":"expression",
"VWO":"expression",
"LQD":"expression",
"HYG":"expression"
},
"param":{
"short":"no",
"max_allocation": "NULL",
"risk_premium_up": ".5",
"risk_increment": ".005"
}
}'
OpenCpuData <- fromJSON(QlikJson)
str(OpenCpuData)
OpenCpuData$data
data.matrix(OpenCpuData$data)
data <- data.matrix(OpenCpuData$data)
dat <- as.data.frame(sapply(data, as.numeric))
is.numeric(dat)
data <- data.matrix(dat)
View(data)
is.numeric(data)
colnames(data)
data <- data.matrix(OpenCpuData$data)
colnames(data)
OpenCpuData$data
colnames(OpenCpuData$data)
View(data)
data <- data.matrix(OpenCpuData$data, dimnames = c("tom", "dick","bob")))
data <- data.matrix(OpenCpuData$data, dimnames = c("tom", "dick","bob"))
data <- data.matrix(OpenCpuData$data)
data <- data.matrix(OpenCpuData$data,rownames.force=TRUE)
dat <- as.data.frame(sapply(data, as.numeric)) #<- sapply is here
data$dimnames
str(data)
data$attr
data
attr(dim)
data$attr(dim)
data$attr
rownames(data)
dat <- as.data.frame(sapply(data, as.numeric), row.names=rownames(data))
rownames(data)
rowNames <- rownames(data)
dat <- as.data.frame(sapply(data, as.numeric), row.names = rowNames) #<- sapply is here
dat <- as.data.frame(sapply(data, as.numeric), row.names = rowNames, optional = FALSE) #<- sapply is here
dat <- as.data.frame(sapply(data, as.numeric)) #<- sapply is here
is.numeric(dat)
str(datt)
str(dat)
names(dat)<-rowNames
data <- data.matrix(dat)
View(data)
data <- data.matrix(OpenCpuData$data,rownames.force=TRUE)
#' Simulating test data that Qlik will send to openCPU
QlikJson <-
'{"data":{
"Date":["41950","41949","41948","41947","41946","41943","41942","41941","41940","41939","41936","41935","41934","41933","41932","41929","41928","41927"],
"SPY":["0.000935269505291547","0.0040031629929822","0.00631620828567181","-0.00346929672399277","0.000545472577605954","0.0114354498946736","0.00641057998081873","-0.00151202056347954","0.0114702283849919","-0.00137453545792399","0.00769507002513725","0.0116248897192381","-0.00711083629618181","0.019810825013137","0.00970976813285951","0.0118108122617704","-0.000858230971410134","-0.00676611614278089"],
"EFA":["0.000477554918815626","-0.0053831538948701","0.00190355329949243","-0.00567823343848584","-0.00860046911649737","0.0184742793438446","0.00721847930702602","-0.00764087870105057","0.0151906916612798","-0.00562429696287958","0.00549361770883827","0.0096247960848288","-0.00728744939271264","0.0119632907243525","0.0062664907651715","0.0186460608096757","-0.00931935430188058","-0.00595533498759304"],
"IWM":["0.00154466660945674","0.00430923037145559","0.00181315834916251","-0.00464077002406327","-0.00171585449553879","0.0147122834508575","0.00851624231782266","-0.00236489445563626","0.0284659039726149","-0.000540199873953262","0.00171356421356417","0.0177145479577787","-0.0142947616031847","0.0164612837962113","0.0117231112765164","-0.00296846011131724","0.0112570356472796","0.0100435853704754"],
"VWO":["0.00649975926817525","-0.0142382534409112","-0.00589761736258554","0.000944510035419066","-0.00680112570356473","0.00803782505910178","0.0114777618364419","-0.00190930787589494","0.0209551656920077","-0.0103689414034241","0.009493670886076","0.000974658869395739","-0.00797679477882518","0.00461389023797953","0.000729040097205313","0.0127984248092541","-0.00999025341130599","-0.0139356078808265"],
"LQD":["0.00633660020277116","-0.00303234501347704","-0.00184967210358167","0.00143133787993599","-0.00477626948215193","-0.000167560321715832","0.000670690811535923","-0.00184100418410038","-0.00333611342785656","0.00125260960334028","0.00041771094402665","-0.00191778537480203","-0.00166486306501279","-0.000582362728785424","0.00183363893982325","-0.000416562526035169","-0.0022443890274314","0.000748689792862578"],
"HYG":["0.000975609756097562","0.00260841212911633","-0.000217320438987256","-0.00314124783362213","-0.00226953420512277","0.000865332612222769","-0.000432479186939005","-0.000756266205704459","0.00162320095227786","-0.00409526888673362","0.00280989949205668","-0.000432105433725738","-0.00654646919939916","0.00811424862057764","0.00719189277541687","0.00846153846153852","0.00563598187645042","0.000221067757267601"]
},
"type":{
"Date":"dimension",
"SPY":"expression",
"EFA":"expression",
"IWM":"expression",
"VWO":"expression",
"LQD":"expression",
"HYG":"expression"
},
"param":{
"short":"no",
"max_allocation": "NULL",
"risk_premium_up": ".5",
"risk_increment": ".005"
}
}'
#' OpenCPU will automatically convert it to a list using fromJSON
OpenCpuData <- fromJSON(QlikJson)
#FUCKING working
data <- data.matrix(OpenCpuData$data,rownames.force=TRUE)
rowNames <- rownames(data)
colNames <- rownames(data)
data <- as.data.frame(sapply(data, as.numeric)) #<- sapply is here
names(data)<-colNames
data <- data.matrix(data)
View(data)
dimensionCount<-length(which(OpenCpuData$type == 'dimension'))
returns<-data[dimensionCount*-1]
covariance <- cov(returns)
View(data)
View(data)
data
data[1]
data[1,]
data[,-1]
cov(data[,-1])
returns<-data[,dimensionCount*-1]
covariance <- cov(returns)
#Dependcies
library(stockPortfolio) # Base package for retrieving returns
library(reshape2) # Used to melt the data
library(quadprog) #Needed for solve.QP
#' Simulating test data that Qlik will send to openCPU
QlikJson <-
'{"data":{
"Date":["41950","41949","41948","41947","41946","41943","41942","41941","41940","41939","41936","41935","41934","41933","41932","41929","41928","41927"],
"SPY":["0.000935269505291547","0.0040031629929822","0.00631620828567181","-0.00346929672399277","0.000545472577605954","0.0114354498946736","0.00641057998081873","-0.00151202056347954","0.0114702283849919","-0.00137453545792399","0.00769507002513725","0.0116248897192381","-0.00711083629618181","0.019810825013137","0.00970976813285951","0.0118108122617704","-0.000858230971410134","-0.00676611614278089"],
"EFA":["0.000477554918815626","-0.0053831538948701","0.00190355329949243","-0.00567823343848584","-0.00860046911649737","0.0184742793438446","0.00721847930702602","-0.00764087870105057","0.0151906916612798","-0.00562429696287958","0.00549361770883827","0.0096247960848288","-0.00728744939271264","0.0119632907243525","0.0062664907651715","0.0186460608096757","-0.00931935430188058","-0.00595533498759304"],
"IWM":["0.00154466660945674","0.00430923037145559","0.00181315834916251","-0.00464077002406327","-0.00171585449553879","0.0147122834508575","0.00851624231782266","-0.00236489445563626","0.0284659039726149","-0.000540199873953262","0.00171356421356417","0.0177145479577787","-0.0142947616031847","0.0164612837962113","0.0117231112765164","-0.00296846011131724","0.0112570356472796","0.0100435853704754"],
"VWO":["0.00649975926817525","-0.0142382534409112","-0.00589761736258554","0.000944510035419066","-0.00680112570356473","0.00803782505910178","0.0114777618364419","-0.00190930787589494","0.0209551656920077","-0.0103689414034241","0.009493670886076","0.000974658869395739","-0.00797679477882518","0.00461389023797953","0.000729040097205313","0.0127984248092541","-0.00999025341130599","-0.0139356078808265"],
"LQD":["0.00633660020277116","-0.00303234501347704","-0.00184967210358167","0.00143133787993599","-0.00477626948215193","-0.000167560321715832","0.000670690811535923","-0.00184100418410038","-0.00333611342785656","0.00125260960334028","0.00041771094402665","-0.00191778537480203","-0.00166486306501279","-0.000582362728785424","0.00183363893982325","-0.000416562526035169","-0.0022443890274314","0.000748689792862578"],
"HYG":["0.000975609756097562","0.00260841212911633","-0.000217320438987256","-0.00314124783362213","-0.00226953420512277","0.000865332612222769","-0.000432479186939005","-0.000756266205704459","0.00162320095227786","-0.00409526888673362","0.00280989949205668","-0.000432105433725738","-0.00654646919939916","0.00811424862057764","0.00719189277541687","0.00846153846153852","0.00563598187645042","0.000221067757267601"]
},
"type":{
"Date":"dimension",
"SPY":"expression",
"EFA":"expression",
"IWM":"expression",
"VWO":"expression",
"LQD":"expression",
"HYG":"expression"
},
"param":{
"short":"no",
"max_allocation": "NULL",
"risk_premium_up": ".5",
"risk_increment": ".005"
}
}'
#' OpenCPU will automatically convert it to a list using fromJSON
OpenCpuData <- fromJSON(QlikJson)
eff_frontier(OpenCpuData)
eff_frontier <- function (Data){
# return argument should be a m x n matrix with one column per security
# short argument is whether short-selling is allowed; default is no (short
# selling prohibited)max_allocation is the maximum % allowed for any one
# security (reduces concentration) risk_premium_up is the upper limit of the
# risk premium modeled (see for loop below) and risk_increment is the
# increment (by) value used in the for loop
data <- data.matrix(Data$data,rownames.force=TRUE)
colNames <- rownames(data)
#Need to add the row.names to work correct
data <- as.data.frame(sapply(data, as.numeric)) #<- sapply is here
names(data)<-colNames
data <- data.matrix(data)
#find and remove all the dimensions
dimensionCount<-length(which(OpenCpuData$type == 'dimension'))
returns<-data[,dimensionCount*-1]
#extract and save all parameters
short<-Data$param$short
max_allocation<-Data$param$max_allocation
risk_premium_up<-Data$param$risk_premium_up
risk_increment<-Data$param$risk_increment
covariance <- cov(returns)
# print(covariance)
n <- ncol(covariance)
# Create initial Amat and bvec assuming only equality constraint
# (short-selling is allowed, no allocation constraints)
Amat <- matrix (1, nrow=n)
bvec <- 1
meq <- 1
# Then modify the Amat and bvec if short-selling is prohibited
if(short=="no"){
Amat <- cbind(1, diag(n))
bvec <- c(bvec, rep(0, n))
}
# And modify Amat and bvec if a max allocation (concentration) is specified
if(!is.null(max_allocation)){
if(max_allocation > 1 | max_allocation <0){
stop("max_allocation must be greater than 0 and less than 1")
}
if(max_allocation * n < 1){
stop("Need to set max_allocation higher; not enough assets to add to 1")
}
Amat <- cbind(Amat, -diag(n))
bvec <- c(bvec, rep(-max_allocation, n))
}
# Calculate the number of loops
loops <- risk_premium_up / risk_increment + 1
loop <- 1
# Initialize a matrix to contain allocation and statistics
# This is not necessary, but speeds up processing and uses less memory
eff <- matrix(nrow=loops, ncol=n+3)
# Now I need to give the matrix column names
colnames(eff) <- c(colnames(returns), "Std.Dev", "Exp.Return", "sharpe")
# Loop through the quadratic program solver
for (i in seq(from=0, to=risk_premium_up, by=risk_increment)){
dvec <- colMeans(returns) * i # This moves the solution along the EF
sol <- solve.QP(covariance, dvec=dvec, Amat=Amat, bvec=bvec, meq=meq)
eff[loop,"Std.Dev"] <- sqrt(sum(sol$solution*colSums((covariance*sol$solution))))
eff[loop,"Exp.Return"] <- as.numeric(sol$solution %*% colMeans(returns))
eff[loop,"sharpe"] <- eff[loop,"Exp.Return"] / eff[loop,"Std.Dev"]
eff[loop,1:n] <- sol$solution
loop <- loop+1
}
eff <-as.data.frame(eff)
#Asuumption: check if the more then one result is max for Sharpe then take the first one
if (nrow(eff[eff$sharpe==max(eff$sharpe),])>1) {
optimized<-eff[eff$sharpe==max(eff$sharpe),!(names(eff) %in% c("Std.Dev", "Exp.Return", "sharpe"))]
optimized<-optimized[1,]
} else {
optimized<-eff[eff$sharpe==max(eff$sharpe),!(names(eff) %in% c("Std.Dev", "Exp.Return", "sharpe"))]
}
#!(names(eff) %in% c("Std.Dev", "Exp.Return", "sharpe"))
#return(eff[eff$sharpe==max(eff$sharpe),])
return(optimized)
}
eff_frontier(OpenCpuData)
#' Simulating test data that Qlik will send to openCPU
QlikJson <-
'{"data":{
"Date":["41950","41949","41948","41947","41946","41943","41942","41941","41940","41939","41936","41935","41934","41933","41932","41929","41928","41927"],
"SPY":["0.000935269505291547","0.0040031629929822","0.00631620828567181","-0.00346929672399277","0.000545472577605954","0.0114354498946736","0.00641057998081873","-0.00151202056347954","0.0114702283849919","-0.00137453545792399","0.00769507002513725","0.0116248897192381","-0.00711083629618181","0.019810825013137","0.00970976813285951","0.0118108122617704","-0.000858230971410134","-0.00676611614278089"],
"EFA":["0.000477554918815626","-0.0053831538948701","0.00190355329949243","-0.00567823343848584","-0.00860046911649737","0.0184742793438446","0.00721847930702602","-0.00764087870105057","0.0151906916612798","-0.00562429696287958","0.00549361770883827","0.0096247960848288","-0.00728744939271264","0.0119632907243525","0.0062664907651715","0.0186460608096757","-0.00931935430188058","-0.00595533498759304"],
"IWM":["0.00154466660945674","0.00430923037145559","0.00181315834916251","-0.00464077002406327","-0.00171585449553879","0.0147122834508575","0.00851624231782266","-0.00236489445563626","0.0284659039726149","-0.000540199873953262","0.00171356421356417","0.0177145479577787","-0.0142947616031847","0.0164612837962113","0.0117231112765164","-0.00296846011131724","0.0112570356472796","0.0100435853704754"],
"VWO":["0.00649975926817525","-0.0142382534409112","-0.00589761736258554","0.000944510035419066","-0.00680112570356473","0.00803782505910178","0.0114777618364419","-0.00190930787589494","0.0209551656920077","-0.0103689414034241","0.009493670886076","0.000974658869395739","-0.00797679477882518","0.00461389023797953","0.000729040097205313","0.0127984248092541","-0.00999025341130599","-0.0139356078808265"],
"LQD":["0.00633660020277116","-0.00303234501347704","-0.00184967210358167","0.00143133787993599","-0.00477626948215193","-0.000167560321715832","0.000670690811535923","-0.00184100418410038","-0.00333611342785656","0.00125260960334028","0.00041771094402665","-0.00191778537480203","-0.00166486306501279","-0.000582362728785424","0.00183363893982325","-0.000416562526035169","-0.0022443890274314","0.000748689792862578"],
"HYG":["0.000975609756097562","0.00260841212911633","-0.000217320438987256","-0.00314124783362213","-0.00226953420512277","0.000865332612222769","-0.000432479186939005","-0.000756266205704459","0.00162320095227786","-0.00409526888673362","0.00280989949205668","-0.000432105433725738","-0.00654646919939916","0.00811424862057764","0.00719189277541687","0.00846153846153852","0.00563598187645042","0.000221067757267601"]
},
"type":{
"Date":"dimension",
"SPY":"expression",
"EFA":"expression",
"IWM":"expression",
"VWO":"expression",
"LQD":"expression",
"HYG":"expression"
},
"param":{
"short":"no",
"max_allocation": "0.5",
"risk_premium_up": ".5",
"risk_increment": ".005"
}
}'
#' OpenCPU will automatically convert it to a list using fromJSON
OpenCpuData <- fromJSON(QlikJson)
#' List is pass to the function and return back to Qlik in JSON format
eff_frontier(OpenCpuData)
OpenCpuData$param$short
OpenCpuData$param$max_allocation
is.numeric(OpenCpuData$param$max_allocation)
is.character(OpenCpuData$param$max_allocation)
as.numeric(OpenCpuData$param$max_allocation)
is.numeric(as.numeric(OpenCpuData$param$max_allocation))
eff_frontier <- function (Data){
# return argument should be a m x n matrix with one column per security
# short argument is whether short-selling is allowed; default is no (short
# selling prohibited)max_allocation is the maximum % allowed for any one
# security (reduces concentration) risk_premium_up is the upper limit of the
# risk premium modeled (see for loop below) and risk_increment is the
# increment (by) value used in the for loop
data <- data.matrix(Data$data,rownames.force=TRUE)
colNames <- rownames(data)
#Need to add the row.names to work correct
data <- as.data.frame(sapply(data, as.numeric)) #<- sapply is here
names(data)<-colNames
data <- data.matrix(data)
#find and remove all the dimensions
dimensionCount<-length(which(OpenCpuData$type == 'dimension'))
returns<-data[,dimensionCount*-1]
#extract, convert and save all parameters
short<-Data$param$short
max_allocation<-as.numeric(Data$param$max_allocation)
risk_premium_up<-as.numeric(Data$param$risk_premium_up)
risk_increment<-as.numeric(Data$param$risk_increment)
covariance <- cov(returns)
# print(covariance)
n <- ncol(covariance)
# Create initial Amat and bvec assuming only equality constraint
# (short-selling is allowed, no allocation constraints)
Amat <- matrix (1, nrow=n)
bvec <- 1
meq <- 1
# Then modify the Amat and bvec if short-selling is prohibited
if(short=="no"){
Amat <- cbind(1, diag(n))
bvec <- c(bvec, rep(0, n))
}
# And modify Amat and bvec if a max allocation (concentration) is specified
if(!is.null(max_allocation)){
if(max_allocation > 1 | max_allocation <0){
stop("max_allocation must be greater than 0 and less than 1")
}
if(max_allocation * n < 1){
stop("Need to set max_allocation higher; not enough assets to add to 1")
}
Amat <- cbind(Amat, -diag(n))
bvec <- c(bvec, rep(-max_allocation, n))
}
# Calculate the number of loops
loops <- risk_premium_up / risk_increment + 1
loop <- 1
# Initialize a matrix to contain allocation and statistics
# This is not necessary, but speeds up processing and uses less memory
eff <- matrix(nrow=loops, ncol=n+3)
# Now I need to give the matrix column names
colnames(eff) <- c(colnames(returns), "Std.Dev", "Exp.Return", "sharpe")
# Loop through the quadratic program solver
for (i in seq(from=0, to=risk_premium_up, by=risk_increment)){
dvec <- colMeans(returns) * i # This moves the solution along the EF
sol <- solve.QP(covariance, dvec=dvec, Amat=Amat, bvec=bvec, meq=meq)
eff[loop,"Std.Dev"] <- sqrt(sum(sol$solution*colSums((covariance*sol$solution))))
eff[loop,"Exp.Return"] <- as.numeric(sol$solution %*% colMeans(returns))
eff[loop,"sharpe"] <- eff[loop,"Exp.Return"] / eff[loop,"Std.Dev"]
eff[loop,1:n] <- sol$solution
loop <- loop+1
}
eff <-as.data.frame(eff)
#Asuumption: check if the more then one result is max for Sharpe then take the first one
if (nrow(eff[eff$sharpe==max(eff$sharpe),])>1) {
optimized<-eff[eff$sharpe==max(eff$sharpe),!(names(eff) %in% c("Std.Dev", "Exp.Return", "sharpe"))]
optimized<-optimized[1,]
} else {
optimized<-eff[eff$sharpe==max(eff$sharpe),!(names(eff) %in% c("Std.Dev", "Exp.Return", "sharpe"))]
}
#!(names(eff) %in% c("Std.Dev", "Exp.Return", "sharpe"))
#return(eff[eff$sharpe==max(eff$sharpe),])
return(optimized)
}
#' List is pass to the function and return back to Qlik in JSON format
eff_frontier(OpenCpuData)
#### Efficient Frontier function ####
#eff_frontier <- function (returns, short="no", max_allocation=NULL,
# risk_premium_up=.5, risk_increment=.005)
eff_frontier <- function (Data){
# return argument should be a m x n matrix with one column per security
# short argument is whether short-selling is allowed; default is no (short
# selling prohibited)max_allocation is the maximum % allowed for any one
# security (reduces concentration) risk_premium_up is the upper limit of the
# risk premium modeled (see for loop below) and risk_increment is the
# increment (by) value used in the for loop
data <- data.matrix(Data$data,rownames.force=TRUE)
colNames <- rownames(data)
#Need to add the row.names to work correct
data <- as.data.frame(sapply(data, as.numeric)) #<- sapply is here
names(data)<-colNames
data <- data.matrix(data)
#find and remove all the dimensions
dimensionCount<-length(which(OpenCpuData$type == 'dimension'))
returns<-data[,dimensionCount*-1]
#extract, convert and save all parameters
short<-as.numeric(Data$param$short)
max_allocation<-as.numeric(Data$param$max_allocation)
risk_premium_up<-as.numeric(Data$param$risk_premium_up)
risk_increment<-as.numeric(Data$param$risk_increment)
covariance <- cov(returns)
# print(covariance)
n <- ncol(covariance)
# Create initial Amat and bvec assuming only equality constraint
# (short-selling is allowed, no allocation constraints)
Amat <- matrix (1, nrow=n)
bvec <- 1
meq <- 1
# Then modify the Amat and bvec if short-selling is prohibited
# 0 means No
# 1 means yes
if(short==0){
Amat <- cbind(1, diag(n))
bvec <- c(bvec, rep(0, n))
}
# And modify Amat and bvec if a max allocation (concentration) is specified
if(!is.null(max_allocation)){
if(max_allocation > 1 | max_allocation <0){
stop("max_allocation must be greater than 0 and less than 1")
}
if(max_allocation * n < 1){
stop("Need to set max_allocation higher; not enough assets to add to 1")
}
Amat <- cbind(Amat, -diag(n))
bvec <- c(bvec, rep(-max_allocation, n))
}
# Calculate the number of loops
loops <- risk_premium_up / risk_increment + 1
loop <- 1
# Initialize a matrix to contain allocation and statistics
# This is not necessary, but speeds up processing and uses less memory
eff <- matrix(nrow=loops, ncol=n+3)
# Now I need to give the matrix column names
colnames(eff) <- c(colnames(returns), "Std.Dev", "Exp.Return", "sharpe")
# Loop through the quadratic program solver
for (i in seq(from=0, to=risk_premium_up, by=risk_increment)){
dvec <- colMeans(returns) * i # This moves the solution along the EF
sol <- solve.QP(covariance, dvec=dvec, Amat=Amat, bvec=bvec, meq=meq)
eff[loop,"Std.Dev"] <- sqrt(sum(sol$solution*colSums((covariance*sol$solution))))
eff[loop,"Exp.Return"] <- as.numeric(sol$solution %*% colMeans(returns))
eff[loop,"sharpe"] <- eff[loop,"Exp.Return"] / eff[loop,"Std.Dev"]
eff[loop,1:n] <- sol$solution
loop <- loop+1
}
eff <-as.data.frame(eff)
#Asuumption: check if the more then one result is max for Sharpe then take the first one
if (nrow(eff[eff$sharpe==max(eff$sharpe),])>1) {
optimized<-eff[eff$sharpe==max(eff$sharpe),!(names(eff) %in% c("Std.Dev", "Exp.Return", "sharpe"))]
optimized<-optimized[1,]
} else {
optimized<-eff[eff$sharpe==max(eff$sharpe),!(names(eff) %in% c("Std.Dev", "Exp.Return", "sharpe"))]
}
#!(names(eff) %in% c("Std.Dev", "Exp.Return", "sharpe"))
#return(eff[eff$sharpe==max(eff$sharpe),])
return(optimized)
}
#-----------------------------------------------
#Dependcies
library(reshape2) # Used to melt the data
library(quadprog) #Needed for solve.QP
#' Simulating test data that Qlik will send to openCPU
QlikJson <-
'{"data":{
"Date":["41950","41949","41948","41947","41946","41943","41942","41941","41940","41939","41936","41935","41934","41933","41932","41929","41928","41927"],
"SPY":["0.000935269505291547","0.0040031629929822","0.00631620828567181","-0.00346929672399277","0.000545472577605954","0.0114354498946736","0.00641057998081873","-0.00151202056347954","0.0114702283849919","-0.00137453545792399","0.00769507002513725","0.0116248897192381","-0.00711083629618181","0.019810825013137","0.00970976813285951","0.0118108122617704","-0.000858230971410134","-0.00676611614278089"],
"EFA":["0.000477554918815626","-0.0053831538948701","0.00190355329949243","-0.00567823343848584","-0.00860046911649737","0.0184742793438446","0.00721847930702602","-0.00764087870105057","0.0151906916612798","-0.00562429696287958","0.00549361770883827","0.0096247960848288","-0.00728744939271264","0.0119632907243525","0.0062664907651715","0.0186460608096757","-0.00931935430188058","-0.00595533498759304"],
"IWM":["0.00154466660945674","0.00430923037145559","0.00181315834916251","-0.00464077002406327","-0.00171585449553879","0.0147122834508575","0.00851624231782266","-0.00236489445563626","0.0284659039726149","-0.000540199873953262","0.00171356421356417","0.0177145479577787","-0.0142947616031847","0.0164612837962113","0.0117231112765164","-0.00296846011131724","0.0112570356472796","0.0100435853704754"],
"VWO":["0.00649975926817525","-0.0142382534409112","-0.00589761736258554","0.000944510035419066","-0.00680112570356473","0.00803782505910178","0.0114777618364419","-0.00190930787589494","0.0209551656920077","-0.0103689414034241","0.009493670886076","0.000974658869395739","-0.00797679477882518","0.00461389023797953","0.000729040097205313","0.0127984248092541","-0.00999025341130599","-0.0139356078808265"],
"LQD":["0.00633660020277116","-0.00303234501347704","-0.00184967210358167","0.00143133787993599","-0.00477626948215193","-0.000167560321715832","0.000670690811535923","-0.00184100418410038","-0.00333611342785656","0.00125260960334028","0.00041771094402665","-0.00191778537480203","-0.00166486306501279","-0.000582362728785424","0.00183363893982325","-0.000416562526035169","-0.0022443890274314","0.000748689792862578"],
"HYG":["0.000975609756097562","0.00260841212911633","-0.000217320438987256","-0.00314124783362213","-0.00226953420512277","0.000865332612222769","-0.000432479186939005","-0.000756266205704459","0.00162320095227786","-0.00409526888673362","0.00280989949205668","-0.000432105433725738","-0.00654646919939916","0.00811424862057764","0.00719189277541687","0.00846153846153852","0.00563598187645042","0.000221067757267601"]
},
"type":{
"Date":"dimension",
"SPY":"expression",
"EFA":"expression",
"IWM":"expression",
"VWO":"expression",
"LQD":"expression",
"HYG":"expression"
},
"param":{
"short":"0",
"max_allocation": "0.5",
"risk_premium_up": ".5",
"risk_increment": ".005"
}
}'
#' OpenCPU will automatically convert it to a list using fromJSON
OpenCpuData <- fromJSON(QlikJson)
#' List is pass to the function and return back to Qlik in JSON format
eff_frontier(OpenCpuData)
detach("package:stockPortfolio", unload=TRUE)
detach("package:quadprog", unload=TRUE)
detach("package:reshape2", unload=TRUE)
eff_frontier(OpenCpuData)
library("quadprog", lib.loc="~/R/win-library/3.1")
eff_frontier(OpenCpuData)
library(qEfficientFrontier)
library("roxygen2", lib.loc="~/R/win-library/3.1")
library("roxygen2", lib.loc="~/R/win-library/3.1")
library(qEfficientFrontier)
library(qEfficientFrontier)
#' K Means Unit Test
#' Simulating test data that Qlik will send to opencpu to validate the R code is working
#'
#' Load in the necessary Libraries
library("quadprog", lib.loc="~/R/win-library/3.1")
library("rjson", lib.loc="~/R/win-library/3.1")
library("qEfficientFrontier", lib.loc="~/R/win-library/3.1")
#' Simulating test data that Qlik will send to openCPU
QlikJson <-
'{"data":{
"Date":["41950","41949","41948","41947","41946","41943","41942","41941","41940","41939","41936","41935","41934","41933","41932","41929","41928","41927"],
"SPY":["0.000935269505291547","0.0040031629929822","0.00631620828567181","-0.00346929672399277","0.000545472577605954","0.0114354498946736","0.00641057998081873","-0.00151202056347954","0.0114702283849919","-0.00137453545792399","0.00769507002513725","0.0116248897192381","-0.00711083629618181","0.019810825013137","0.00970976813285951","0.0118108122617704","-0.000858230971410134","-0.00676611614278089"],
"EFA":["0.000477554918815626","-0.0053831538948701","0.00190355329949243","-0.00567823343848584","-0.00860046911649737","0.0184742793438446","0.00721847930702602","-0.00764087870105057","0.0151906916612798","-0.00562429696287958","0.00549361770883827","0.0096247960848288","-0.00728744939271264","0.0119632907243525","0.0062664907651715","0.0186460608096757","-0.00931935430188058","-0.00595533498759304"],
"IWM":["0.00154466660945674","0.00430923037145559","0.00181315834916251","-0.00464077002406327","-0.00171585449553879","0.0147122834508575","0.00851624231782266","-0.00236489445563626","0.0284659039726149","-0.000540199873953262","0.00171356421356417","0.0177145479577787","-0.0142947616031847","0.0164612837962113","0.0117231112765164","-0.00296846011131724","0.0112570356472796","0.0100435853704754"],
"VWO":["0.00649975926817525","-0.0142382534409112","-0.00589761736258554","0.000944510035419066","-0.00680112570356473","0.00803782505910178","0.0114777618364419","-0.00190930787589494","0.0209551656920077","-0.0103689414034241","0.009493670886076","0.000974658869395739","-0.00797679477882518","0.00461389023797953","0.000729040097205313","0.0127984248092541","-0.00999025341130599","-0.0139356078808265"],
"LQD":["0.00633660020277116","-0.00303234501347704","-0.00184967210358167","0.00143133787993599","-0.00477626948215193","-0.000167560321715832","0.000670690811535923","-0.00184100418410038","-0.00333611342785656","0.00125260960334028","0.00041771094402665","-0.00191778537480203","-0.00166486306501279","-0.000582362728785424","0.00183363893982325","-0.000416562526035169","-0.0022443890274314","0.000748689792862578"],
"HYG":["0.000975609756097562","0.00260841212911633","-0.000217320438987256","-0.00314124783362213","-0.00226953420512277","0.000865332612222769","-0.000432479186939005","-0.000756266205704459","0.00162320095227786","-0.00409526888673362","0.00280989949205668","-0.000432105433725738","-0.00654646919939916","0.00811424862057764","0.00719189277541687","0.00846153846153852","0.00563598187645042","0.000221067757267601"]
},
"type":{
"Date":"dimension",
"SPY":"expression",
"EFA":"expression",
"IWM":"expression",
"VWO":"expression",
"LQD":"expression",
"HYG":"expression"
},
"param":{
"short":"0",
"max_allocation": "0.4",
"risk_premium_up": ".5",
"risk_increment": ".005"
}
}'
#' OpenCPU will automatically convert it to a list using fromJSON
OpenCpuData <- fromJSON(QlikJson)
#' List is pass to the function and return back to Qlik in JSON format
eff_frontier(OpenCpuData)
library(qEfficientFrontier)
library(qEfficientFrontier)