Skip to content
Commits on Source (2)
......@@ -2,3 +2,4 @@ Functions
^CLA-ex.*\.R?out
.*\+M$
tests/SP500-ex\.Rout\.
inst/xtraR/CLA_Data.csv.txt
\ No newline at end of file
......@@ -24,10 +24,10 @@ initAlgo <- function(mu, lB, uB ) {
## getMatrices -----------------------------------------------------------------
getMatrices <- function(mu, covar, w, f) {
## Slice covarF,covarFB,covarB,muF,muB,wF,wB
covarF <- covar[f,f]
covarF <- covar[f,f , drop=FALSE]
muF <- mu[f]
b <- seq_along(mu)[-f]
covarFB <- covar[f,b]
covarFB <- covar[f,b , drop=FALSE]
wB <- w[b]
list(covarF = covarF, covarFB = covarFB, muF = muF, wB = wB)
}
......
X1,X2,X3,X4,X5,X6,X7,X8,X9,X10
1.175,1.19,0.396,1.12,0.346,0.679,0.089,0.73,0.481,1.08
0,0,0,0,0,0,0,0,0,0
1,1,1,1,1,1,1,1,1,1
0.40755159,0.03175842,0.05183923,0.05663904,0.0330226,0.00827775,0.02165938,0.01332419,0.0343476,0.02249903
0.03175842,0.9063047,0.03136385,0.02687256,0.01917172,0.00934384,0.02495043,0.00761036,0.02874874,0.01336866
0.05183923,0.03136385,0.19490901,0.04408485,0.03006772,0.01322738,0.03525971,0.0115493,0.0427563,0.02057303
0.05663904,0.02687256,0.04408485,0.19528471,0.02777345,0.00526665,0.01375808,0.00780878,0.02914176,0.01640377
0.0330226,0.01917172,0.03006772,0.02777345,0.34059105,0.00777055,0.02067844,0.00736409,0.02542657,0.01284075
0.00827775,0.00934384,0.01322738,0.00526665,0.00777055,0.15983874,0.02105575,0.00518686,0.01723737,0.00723779
0.02165938,0.02495043,0.03525971,0.01375808,0.02067844,0.02105575,0.68056711,0.01377882,0.04627027,0.01926088
0.01332419,0.00761036,0.0115493,0.00780878,0.00736409,0.00518686,0.01377882,0.95526918,0.0106553,0.00760955
0.0343476,0.02874874,0.0427563,0.02914176,0.02542657,0.01723737,0.04627027,0.0106553,0.31681584,0.01854318
0.02249903,0.01336866,0.02057303,0.01640377,0.01284075,0.00723779,0.01926088,0.00760955,0.01854318,0.11079287
### ---> ../../R/
if(FALSE) { ## ==> in another file -- TODO
data(SP500, package="FRAPO")
system.time(muS.sp500 <- muSigma(SP500)) # 26 sec. (lynne, 2017)
data(NASDAQ, package="FRAPO")
system.time(muS.nasdaq <- muSigma(NASDAQ)) # 122.3 sec. (lynne, 2017)
## Prove they are the same as Yanhao's version in her thesis:
if(exists("trans.sp")) {# from her (*rds file)
cat("comparing Yanhao Shi's 'trans.sp' with our muSigma() result:\n")
stopifnot(all.equal(as.vector(muS.sp500$mu),
as.vector(trans.sp$mu), tol = 1e-15))
stopifnot(all.equal(muS.sp500$covmat,
trans.sp $covmat, tol = 1e-15))
}
if(exists("trans.nasdaq")) {# from her (*rds file)
cat("comparing Yanhao Shi's 'trans.nasdaq' with our muSigma() result:\n")
stopifnot(all.equal(as.vector( muS.nasdaq$mu),
as.vector(trans.nasdaq$mu), tol = 1e-15))
stopifnot(all.equal( muS.nasdaq $covmat,
trans.nasdaq $covmat, tol = 1e-15))
}
object.size(muS.sp500 ) ## 1843784 bytes : 1.8 MB ; ok for CRAN pkg
object.size(muS.nasdaq) ## 38720584 bytes : 39 Mega too large for CRAN package
##
if(FALSE) { ## as MM: do once
save (muS.sp500, file = "~/R/Pkgs/CLA/data/muSig_sp500.rda", compress = "xz")
saveRDS(muS.sp500, file = "~/R/Pkgs/CLA/data/muSig_sp500.rds", compress = "xz")
}
}
## Bottom of page 16 "5. A Numerical Example"
## of the (2013) paper about the Python implementation
## Edited into R code by Martin Maechler
LBound <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
UBound <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
Mean <- c(1.175, 1.19, 0.396, 1.12, 0.346, 0.679, 0.089, 0.73, 0.481, 1.08)
Cov <- c(0.4075516, 0.0317584, 0.9063047, 0.0518392, 0.0313639, 0.194909, 0.056639, 0.0268726, 0.0440849, 0.1952847, 0.0330226, 0.0191717, 0.0300677, 0.0277735, 0.3405911, 0.0082778, 0.0093438, 0.0132274, 0.0052667, 0.0077706, 0.1598387, 0.0216594, 0.0249504, 0.0352597, 0.0137581, 0.0206784, 0.0210558, 0.6805671, 0.0133242, 0.0076104, 0.0115493, 0.0078088, 0.0073641, 0.0051869, 0.0137788, 0.9552692, 0.0343476, 0.0287487, 0.0427563, 0.0291418, 0.0254266, 0.0172374, 0.0462703, 0.0106553, 0.3168158, 0.022499, 0.0133687, 0.020573, 0.0164038, 0.0128408, 0.0072378, 0.0192609, 0.0076096, 0.0185432, 0.1107929)
(p <- length(Mean))# 10
stopifnot(p == length(LBound), p == length(UBound),
p*(p+1)/2 == length(Cov))
require(Matrix)
options(width = 110)# so printing shows the matrix nicely
C <- matrix(0, p,p); C[ lower.tri(C, diag=TRUE) ] <- Cov
(C <- as(C, "Matrix")) # wrong
## try again
C <- matrix(0, p,p); C[ upper.tri(C, diag=TRUE) ] <- Cov
(C <- t(as(C, "Matrix")))# yes
##====== get data from
if(FALSE) # << maybe offline
str(a10 <- read.csv("http://www.quantresearch.info/CLA_Data.csv.txt"))
## MM: local copy in package source:
str(a10 <- read.csv("CLA_Data.csv.txt")
a10 <- as.matrix(a10)
str(mu.10 <- a10[1,])
stopifnot(a10[2,] == 0)# == Lower_bound
stopifnot(a10[3,] == 1)# == Upper_bound
str(cc <- a10[-(1:3),]) # 10 x 10
isSymmetric(cc, check.attributes=FALSE)
LT <- lower.tri(cc, diag=TRUE)
all.equal(cc[LT], C[LT])) # ==> mean relative difference: 3.59e-7
muS.10ex <- list(mu = mu.10, # including names "X1" .. "X10"
covar = unname(cc))
if(FALSE) ## as maintainer, did once
save(muS.10ex, file = "~/R/Pkgs/CLA/data/muS.10ex.rda", compress = "xz")
CLA.10ex <- with(muS.10ex, CLA(mu, covar, lB=0, uB=1)) # works after 'drop = FALSE' fix in getMatrices() !!
drop0(zapsmall(CLA.10ex$weights_set))
## or to look similar as in Alexander Norrington's M.thesis, p.33
## (but has 1st row doubled, *and* last row [lambda=0] missing (!?)
t(round(CLA.10ex$weights_set, 3))
## X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
## [1,] 0.000 1.000 0.000 0.000 0.000 0.000 0 0.000 0.000 0.000
## [2,] 0.000 1.000 0.000 0.000 0.000 0.000 0 0.000 0.000 0.000
## [3,] 0.649 0.351 0.000 0.000 0.000 0.000 0 0.000 0.000 0.000
## [4,] 0.434 0.231 0.000 0.335 0.000 0.000 0 0.000 0.000 0.000
## [5,] 0.127 0.072 0.000 0.281 0.000 0.000 0 0.000 0.000 0.520
## [6,] 0.123 0.070 0.000 0.279 0.000 0.000 0 0.006 0.000 0.521
## [7,] 0.087 0.050 0.000 0.224 0.000 0.174 0 0.030 0.000 0.435
## [8,] 0.085 0.049 0.000 0.220 0.000 0.180 0 0.031 0.006 0.429
## [9,] 0.074 0.044 0.000 0.199 0.026 0.198 0 0.033 0.028 0.398
## [10,] 0.068 0.041 0.015 0.188 0.034 0.202 0 0.034 0.034 0.383
\name{muS.10ex}
\alias{muS.10ex}
\docType{data}
\title{10 Assets Example Data from Markowitz & Todd}
\description{
The simple example Data of Markowitz and Todd (2000); used for
illustrating the CLA; reused in Bailey and López de Prado (2013).
}
\usage{
data("muS.10ex")
}
\format{
A list with two components,
\describe{
\item{mu}{Named num [1:10] 1.175 1.19 0.396 1.12 0.346 ...\cr
names : chr [1:10] "X1" "X2" "X3" "X4" ...}
\item{covar}{num [1:10, 1:10] 0.4076 0.0318 0.0518 0.0566 0.033 ...}
}
}
\source{
From \url{http://www.quantresearch.info/CLA_Data.csv.txt},
by López de Prado.
}
\references{
Markowitz, H. M. (1987, 1st ed.) and
Markowitz, H. M. and Todd, P. G. (2000)
\emph{Mean-Variance Analysis in Portfolio Choice and Capital Markets},
page 335.
Bailey, D. H. and López de Prado, M. (2013)
An open-source implementation of the critical-line algorithm for portfolio
optimization, \emph{Algorithms} \bold{6}(1), 169--196;
\doi{10.3390/a6010169}, p. 16f.
}
\examples{
data(muS.10ex)
str(muS.10ex)
CLA.10ex <- with(muS.10ex, CLA(mu, covar, lB=0, uB=1)) # works after 'drop = FALSE' fix in getMatrices() !!
if(require("Matrix"))
drop0(zapsmall(CLA.10ex$weights_set))
CLA.10ex.1c <- with(muS.10ex, CLA(mu, covar, lB=1/100, uB=1))
round(CLA.10ex.1c$weights_set, 3)
}
\keyword{datasets}