demoScript.R

# 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="BEWESO",username = "Bjerring")
library(BEWESO)

### note
# remeber that you can always see what a function do by writing
# ?function, e.g. ?FeatureSelection
?FeatureSelection

# we are using the embended mutual fund data set that I have put in this package
## here is the two data files that we will be using
fund.description
fund.prices

## example 1

# Analyze a given portfolio
prices <- fund.prices # calculate prices from return
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=prices,port.budget=port.budget,port.names=port.names,data.length=data.length)
portforlio.stat # Standard deviation, mean

### example 2
# We have 341 mutual funds and want to shrink the universe to 10 so we use feature selection
No.funds <- 10 # define number of clusters
fees <- fund.description[,4]/100 # annual fees
OKlist <- port.names # list of mandatory funds in sub universe
sub.uni <- FeatureSelection(prices=prices,Cno=No.funds,fees=fees,data.length=data.length,OKlist=OKlist)
sub.uni

# example 3
# calculate the efficient frontier of the sub universe
sub.prices <- prices[,sub.uni] # Prices for sub universe
type <- fund.description[which(fund.description[,2] %in% sub.uni),5] # 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
no.fp <- 15
EF <- EffFrontier(prices=sub.prices,type=type,bond.min,equity.max,no.fp)
EF

# plot the efficient frontier
plot(EF[,15],EF[,14],type="l",xlim=c(0,0.14),ylim=c(0,0.14))
plot(EF[,dim(EF)[2]-1],EF[,dim(EF)[2]-2],type="l")
points(x=portforlio.stat[1],y=portforlio.stat[2])

# 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
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

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


# example 4.2
# calculate maximum return portfolio for a target standard deviation
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

opt.port <- markowitzTargetSD(prices=sub.prices,type=type,bond.min=bond.min,equity.max=equity.max,Tsd=Tsd)
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.