demoScript2.R

setwd("/home/thomas/BEWESO/data")
library(BEWESO)

## MAKE SURE TO DO THIS FIRST!
# put price file in your current directory
IFprices <- read.table("fund.prices2.csv",sep=";")
USETFprices <- read.table("USETFprices.csv",sep=";")
EUETFprices <- read.table("EUETFprices.csv")

# install packages
Pkg <- c("quadprog","devtools","corpcor")
inst <- Pkg %in% installed.packages()
if(length(Pkg[!inst]) > 0) install.packages(Pkg[!inst])
instpackages <- lapply(Pkg, library, character.only=TRUE)

install_github(repo="Bjerring/BEWESO",username = "Bjerring")
library(BEWESO)

## example 1

# Analyze a given portfolio
port.budget <- c(200,200) # current portfolio
port.names <- as.character(c("DK0060228633","DK0060010924")) # names of funds in current portfolio
data.length <- 220*3 # number of data points to be considered, 220*3 = 3 years of daily data

# calculate annualized standard deviation and mean
portforlio.stat <- PortStat(data=IFprices,port.budget=port.budget,port.names=port.names,data.length=data.length)
portforlio.stat # Standard deviation, mean

### example 2
# We have 417 (before 341) mutual funds and want to shrink the universe to 10 so we use feature selection
No.funds <- 10 # define number of clusters
OKlist <- port.names # list of mandatory funds in sub universe
prices <- IFprices
fees <-  rep(0.5,dim(prices)[2]) # Here, I am just creating some dummy annual cost. It should be AAOP for IF's and Expense ratio for ETFs
sub.uni <- FeatureSelection(prices=prices,Cno=No.funds,fees=fees,data.length=data.length,OKlist=OKlist)
sub.uni

### example 2.1
# We now concider all IF's, US ETF's and EU ETF's = 810
No.funds <- 10 # define number of clusters
OKlist <- port.names # list of mandatory funds in sub universe
prices <- data.frame(IFprices,USETFprices,EUETFprices)
fees <-  rep(0.5,dim(prices)[2]) # Here, I am just creating some dummy annual cost. It should be AAOP for IF's and Expense ratio for ETFs
names(fees) <- colnames(prices)
sub.uni2 <- FeatureSelection(prices=prices,Cno=No.funds,fees=fees,data.length=data.length,OKlist=OKlist)
sub.uni2


# example 3
# calculate the efficient frontier of the sub universe where mean expected return is substracted AAOP/expense ratio
sub.prices <- prices[,sub.uni] # Prices for sub universe
type <- fund.description[which(fund.description[,2] %in% sub.uni),5] # vector indicating if a fund is a bond or stock IF/etf
fees <- rep(0.5,dim(sub.prices)[2])  # Here, I am just creating some dummy annual cost 0.5 = 0.5%. It should be AAOP for IF's and Expense ratio for ETFs
bond.min <- 0.25 # minimum amount which should be allocated to bonds
equity.max <- 0.65 # maximum amount of money which can be allocated to equity
no.fp <- 15
EF <- EffFrontier(prices=sub.prices,type=type,bond.min,equity.max,no.fp,fees)
EF
# plot the efficient frontier
plot(EF[,dim(EF)[2]-1],EF[,dim(EF)[2]-2],type="l",xlim=c(0,0.14),ylim=c(0,0.20))
points(x=portforlio.stat[1],y=portforlio.stat[2]-0.5/100) # here we plot the given portfolio and substract the AAOP from the mean return

# example 3.1
# calculate the efficient frontier of the sub universe for the universe including IF, USETF and EUETF
sub.prices2 <- prices[,sub.uni2] # Prices for sub universe
type <- c("Equity","Bond","Bond","Equity","Bond","Bond","Equity","Equity","Bond","Equity","Equity","Equity") # vector indicating if fund is a bond or stock etf
bond.min <- 0.25 # minimum amount which should be allocated to bonds
equity.max <- 0.65 # maximum amount of money which can be allocated to equity
fees2 <- rep(0.5,dim(sub.prices2)[2])  # Here, I am just creating some dummy annual cost 0.5 = 0.5%. It should be AAOP for IF's and Expense ratio for ETFs
no.fp <- 15
EF2 <- EffFrontier(prices=sub.prices2,type=type,bond.min,equity.max,no.fp,fees2)
EF2

# plot the efficient frontier
plot(EF2[,dim(EF2)[2]-1],EF2[,dim(EF2)[2]-2],type="l",xlim=c(0,0.14),ylim=c(0,0.20))
points(x=portforlio.stat[1],y=portforlio.stat[2]-0.5/100)

# example 4.1
# calculate minimum variance portfolio for a target return
sub.prices <- prices[,sub.uni] # prices for the sub universe found using feature selection
type <- fund.description[which(fund.description[,2] %in% sub.uni),5] # Vector indicating if a mutual fund is a bond or stock fund. The info file "fund.description" is not complete for the newly included IFs. This infor should now been taken from "stamdata"
bond.min <- 0.25 # minimum which should be allocated to bonds
equity.max <- 0.65 # maximum amount of money which can be allocation to equity
maxexp <- portforlio.stat[2] # target return
fees <-  rep(0.5,dim(sub.prices)[2])

opt.port <- markowitzTargetMU(prices=sub.prices,lambda=0.000,maxexp=maxexp,type=type,bond.min=bond.min,equity.max=equity.max,fees = fees)
opt.port


# example 4.2
# calculate maximum return portfolio for a target standard deviation fofr the IF universe
sub.prices <- prices[,sub.uni] # prices for the sub universe found using feature selection
type <- fund.description[which(fund.description[,2] %in% sub.uni),5] # vector indicating if a mutual fund is a bond or stock fund
bond.min <- 0.25 # minimum which should be allocated to bonds
equity.max <- 0.65 # maximum amount of money which can be allocation to equity
Tsd <- portforlio.stat[1] # target standard deviation
fees <-  rep(0.5,dim(sub.prices)[2])

opt.port <- markowitzTargetSD(prices=sub.prices,type=type,bond.min=bond.min,equity.max=equity.max,Tsd=Tsd,fees = fees)
opt.port


# example 5
# let us make a forecast of a given portfolio

# run the next 4 lines to look at the initial portfolio
prices = prices[,port.names]
a.head = 5*220
port1 <- port.budget/sum(port.budget)
names(port1) <- port.names

# run these lines to look at optimal portfolio that you comuted for a target return
prices = sub.prices
port1 <- opt.port
names(port1) <- colnames(sub.prices)
data.length <- 3*220

# run prognose model (ÅOP and emission is not included)
prognose <- futuretest(prices=prices,a.head=a.head,port1=port1,data.length=data.length)
t(prognose)

# plot prognose
plot(prognose[1,],type="l",ylim=c(min(prognose),max(prognose)))
lines(prognose[2,])
lines(prognose[3,])
lines(prognose[4,])
lines(prognose[5,])
Bjerring/BEWESO documentation built on May 6, 2019, 7:56 a.m.