Tuesday, July 14, 2015

[11c] Portfolio Optimization and Efficient Frontier: Import CSV files

Create csv files:

er.csv
SPY,EFA,LQD,EEM
0.14473728,0.06458429,0.05929884,0.07109433



covmat.csv
,SPY,EFA,LQD,HYG
SPY,0.02141010,0.02351144,-0.00049749,0.00915216
EFA,0.02351144,0.03227444,-0.00011169,0.01105802
LQD,-0.00049749,-0.00011169,0.00323208,0.00146939
HYG,0.00915216,0.01105802,0.00146939,0.00689213




Original sample codes in [11a] or [11b] are on the left hand side. For this section, please try the sample codes of read.csv on the right hand side, and then run sample codes in [11a] and/or [11b].

sample code(s)
r.free <- 0.00973449


er <- c(
 "SPY" = 0.14473728,
 "EFA" = 0.06458429,
 "LQD" = 0.05929884,
 "EEM" = 0.07109433
)


covmat <- matrix(c(0.02141010, 0.02351144, -0.00049749, 0.00915216, 0.02351144, 0.03227444, -0.00011169, 0.01105802, -0.00049749, -0.00011169, 0.00323208, 0.00146939, 0.00915216, 0.01105802, 0.00146939, 0.00689213), nrow = 4, ncol = 4, byrow = TRUE)

rownames(covmat) <- c("SPY", "EFA", "LQD", "HYG")
colnames(covmat) <- c("SPY", "EFA", "LQD", "HYG")

r.free <- 0.00973449




sample code(s)
er
       SPY        EFA        LQD        EEM
0.14473728 0.06458429 0.05929884 0.07109433

class(er)
[1] "numeric"

names(er)
[1] "SPY" "EFA" "LQD" "EEM"

rownames(er)
NULL

colnames(er)
NULL

dim(er)
NULL

erdf <- read.csv("H:/Quantitative Research/R/Optimization/er.csv", sep=",")


erdf
        SPY        EFA        LQD        EEM
1 0.1447373 0.06458429 0.05929884 0.07109433

class(erdf)
[1] "data.frame"

names(erdf)
[1] "SPY" "EFA" "LQD" "EEM"

rownames(erdf)
[1] "1"

colnames(erdf)
[1] "SPY" "EFA" "LQD" "EEM"

dim(erdf)
[1] 1 4


ermt <- as.matrix(erdf)

ermt
           SPY        EFA        LQD        EEM
[1,] 0.1447373 0.06458429 0.05929884 0.07109433

class(ermt)
[1] "matrix"

names(ermt)
NULL

rownames(ermt)
NULL

colnames(ermt)
[1] "SPY" "EFA" "LQD" "EEM"

dim(ermt)
[1] 1 4


names(ermt) <- colnames(ermt)
names(ermt) 
[1] "SPY" "EFA" "LQD" "EEM"


er <- ermt


er
           SPY        EFA        LQD        EEM
[1,] 0.1447373 0.06458429 0.05929884 0.07109433
attr(,"names")
[1] "SPY" "EFA" "LQD" "EEM"

class(er)
[1] "matrix"

names(er)
[1] "SPY" "EFA" "LQD" "EEM"

rownames(er)
NULL

colnames(er)
[1] "SPY" "EFA" "LQD" "EEM"

dim(er)
[1] 1 4





sample code(s)
covmat
            SPY         EFA         LQD        HYG
SPY  0.02141010  0.02351144 -0.00049749 0.00915216
EFA  0.02351144  0.03227444 -0.00011169 0.01105802
LQD -0.00049749 -0.00011169  0.00323208 0.00146939
HYG  0.00915216  0.01105802  0.00146939 0.00689213

names(covmat)
NULL

rownames(covmat)
[1] "SPY" "EFA" "LQD" "HYG"

colnames(covmat)
[1] "SPY" "EFA" "LQD" "HYG"

class(covmat)
[1] "matrix"

dim(covmat)
[1] 4 4

covmatdf <- read.csv("H:/Quantitative Research/R/Optimization/covmat.csv", sep=",", row.names=1)

covmatdf
            SPY         EFA         LQD        HYG
SPY  0.02141010  0.02351144 -0.00049749 0.00915216
EFA  0.02351144  0.03227444 -0.00011169 0.01105802
LQD -0.00049749 -0.00011169  0.00323208 0.00146939
HYG  0.00915216  0.01105802  0.00146939 0.00689213

names(covmatdf)
[1] "SPY" "EFA" "LQD" "HYG"

class(covmatdf)
[1] "data.frame"

dim(covmatdf)
[1] 4 4



covmatmt <- as.matrix(covmatdf)

covmatmt
            SPY         EFA         LQD        HYG
SPY  0.02141010  0.02351144 -0.00049749 0.00915216
EFA  0.02351144  0.03227444 -0.00011169 0.01105802
LQD -0.00049749 -0.00011169  0.00323208 0.00146939
HYG  0.00915216  0.01105802  0.00146939 0.00689213

names(covmatmt)
NULL

rownames(covmatmt)
[1] "SPY" "EFA" "LQD" "HYG"

colnames(covmatmt)
[1] "SPY" "EFA" "LQD" "HYG"

class(covmatmt)
[1] "data.frame"

dim(covmatmt)
[1] 4 4



covmat <- covmatmt


covmat
            SPY         EFA         LQD        HYG
SPY  0.02141010  0.02351144 -0.00049749 0.00915216
EFA  0.02351144  0.03227444 -0.00011169 0.01105802
LQD -0.00049749 -0.00011169  0.00323208 0.00146939
HYG  0.00915216  0.01105802  0.00146939 0.00689213

class(covmat)
[1] "matrix"

names(covmat)
[1] "SPY" "EFA" "LQD" "EEM"

rownames(covmat)
[1] "SPY" "EFA" "LQD" "HYG"

colnames(covmat)
[1] "SPY" "EFA" "LQD" "EEM"

dim(covmat)
[1] 1 4




Try [11a] and/or [11b] after running the sample codes of read.csv on the right hand side.

No comments:

Post a Comment