Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Christina Kontostavlos #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ckontostavlos
Copy link

No description provided.

@ckontostavlos
Copy link
Author


title: "Portfoliomanagement and Financial Analysis - Assignment 1"
author: "Kontostavlos, Christina"
subtitle: Submit until Monday 2020-09-21, 13:00
output:
html_notebook: default
word_document: default
pdf_document: default

pacman::p_load(tidyverse,tidyquant,FFdownload)

For all exercises please refer to my book Tidy Portfoliomanagement in R. Unfortunately some functions (e.g. get_exchange()) are not working on rstudio.cloud, so please try to solve the exercises without. Also remember, that everything you do below must be explained to me (why? what does this mean? what is the interpretation?). If you have any questions or problems, please refer to the assignment forum on moodle.

Exercise 1: xts

Create a daily time series for r lubridate::year(Sys.time()):

lubridate::year(Sys.time()) # to find out the year

seq(as.Date("2020-01-01"), length=366, by="day")

timeSequence(from = "2020-01-01", to = "2020-12-31", by = "day")

  1. Find the subset of first and last days per month/quarter (uniquely)

#month

seq(as.Date("2020-01-01), length=12, by="month") # Version 1

timeSequence(from = "2020-01-01", to="2020-12-31", by = "month") #Version2

dates1 <- seq(as.Date("2020-01-01), length=12, by="month"); dates 1
dates2 <- timeSequence(from = "2020-01-01", to="2020-12-31", by = "month"); dates2

timeFirstDayInMonth (dates1 -7)
timeFirstDayInMonth (dates2 -7)

timeLastDayInMonth(dates1)
timeLastDayInMonth (dates2)

#quarter

timeFirstDayInQuarter(dates1)
timeFirstDayInQuarter (dates2)

timeLastDayInQuarter(dates1)
timeLastDayInQuarter (dates2)

  1. Take December r lubridate::year(Sys.time())-1 and remove all weekends and holidays in Zurich (Tokyo)

lubridate::year(Sys.time())-1 # 2019

dateSeq <- timeSequence(from = "2019-12-01", to = "2019-12-31", by = "day")

holidayZURICH(2019)

dateSeq2 <- dateSeq[isWeekday(dateSeq)]; dateSeq2 # only weekdays

dayOfWeek(dateSeq2)

dateSeq3 <- dateSeq[isBizday(dateSeq, holidayZURICH(2019))];
dateSeq3 # select only BusinessDays of Zurich

dayOfWeek(dateSeq3)

  1. create a series of five dates & times in New York. Show them for New York, London and Belgrade

Dates <- c("1998-04-17", "2003-09-02", "2016-12-05", "2000-01-01", "2001-09-11")
Times <- c("01:00:00", "15:00:00", "18:00:00", "08:00:00", "22:00:00")

DatesTimes <- paste(Dates,Times)

as.Date(DatesTimes)

NY <- timeDate(DatesTimes, zone="NewYork", FinCenter="NewYork")
LD <- timeDate(DatesTimes, zone="London", FinCenter="London")
BG <- timeDate(DatesTimes, zone="Belgrade", FinCenter="Belgrade")

c(LD,NY,BG) ##London

c(NY,LD,BG) ##New York

c(BG,LD,NY) ##Belgrade

Exercise 2: Download Financial Data and Merge

Download information on all stocks currently part of the S&P500. Select 10 that have data back to 2000. Additionally download the S&P 500 index and the four Fama-French factors including the risk-free rate (using my package ffdownload). Merge all data together and calculate returns (simple or continuous returns?).
Split your data in two files: One holding daily returns and one with monthly returns. Additionally show how to we could get from long to wide format to transfer between tibble (tsibble) and xts.

tq_exchange_options() # find all exchanges available

tq_index_options() # find all indices available

tq_get_options() # find all data sources available

#10 S&P 500 stocks with IPO<2000

SP <- tq_index("SP500")
NASDAQ <- tq_exchange("NASDAQ")
NYSE <- tq_exchange("NYSE")

stocks.selection <- SP %>%
inner_join(rbind(NYSE,NASDAQ) %>% select(symbol,last.sale.price,market.cap,ipo.year),by=c("symbol")) %>% # join datasets
filter(ipo.year<2000&!is.na(market.cap)) %>% # filter years with ipo<2000 or ipo=NA
arrange(desc(weight)) %>% # sort in descending order
slice(1:10)

Additional Data for the S&P500-index

stocks.prices <- stocks.selection$symbol %>% # daily prices
tq_get(get = "stock.prices",from = "2000-01-01",to = "2019-12-31") %>%
group_by(symbol)

index.prices <- "^GSPC" %>%

tq_get(get = "stock.prices",from = "2000-01-01",to = "2019-12-31")
stocks.prices %>% slice(1:2) # show the first two entries of each group

Dividends and Stock Splits

stocks.dividends <- stocks.selection$symbol %>%
tq_get(get = "dividends",from = "2000-01-01",to = "2019-12-31") %>%
group_by(symbol)

stocks.splits <- stocks.selection$symbol %>%
tq_get(get = "splits",from = "2000-01-01",to = "2019-12-31") %>%
group_by(symbol)

#Stock Ratios does not work
stocks.ratios <- stocks.selection$symbol %>%
tq_get(get = "key.ratios",from = "2000-01-01",to = "2019-12-31") %>%
group_by(symbol)

Fama-French Data

factors <- FFdownload$x_F-F_Research_Data_Factors$monthly$Temp2 %>%
tk_tbl(rename_index="date") %>% # make tibble
mutate(date=as.Date(date, frac=1)) %>% # make proper month-end date format
gather(key=FFvar,value = price,-date) # gather into tidy format
factors %>% group_by(FFvar) %>% slice(1:2)

if error: library(timetk)

#Stock prices monthly
stocks.prices.monthly <- stocks.prices %>%
tq_transmute(select = c(adjusted,volume), # which column to choose
mutate_fun = to.monthly, # funtion: make monthly series
indexAt = "lastof") %>%
ungroup() %>% mutate(date=as.yearmon(date))

Stock Returns monthly

stocks.returns <- stocks.prices %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn, # create monthly returns
period="monthly",
type="arithmetic") %>%
ungroup() %>% mutate(date=as.yearmon(date))

save(stocks.returns,file="stocksreturn_monthly.RData")

Stocks Return Daily

stocks.returns.daily <- stocks.prices %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn, # create daily returns
period="daily",
type="arithmetic") %>%
ungroup() %>% mutate(date=as.yearmon(date))

save(stocks.returns.daily,file="stocksreturns_daily.RData")

Index Returns monthly

index.returns <- index.prices %>%
tq_transmute(select = adjusted,mutate_fun = periodReturn,
period="monthly", type="arithmetic") %>%
mutate(date=as.yearmon(date))

Factor Returns monthly (already)

factors.returns <- factors %>% mutate(price=price/100) %>%
mutate(date=as.yearmon(date))

Show first 5 enteries

stocks.prices.monthly %>% ungroup() %>% slice(1:5)

stocks.returns %>% ungroup() %>% slice(1:5)

index.returns %>% ungroup() %>% slice(1:5)

factors.returns %>% ungroup() %>% slice(1:5)

Merging data together

first.merge <- merge(stocks.prices.monthly,stocks.returns, by = c("symbol","date"))

stocks.final <- merge(merge(first.merge,index.returns, by = "date"),factors.returns, by= "date")

Long to Wide Format

library(tidyr)

stocks.returns.daily

stocks.returns

`
?FFdownload()
tempf <- tempfile(fileext = ".RData"); tempd <- tempdir(); temptxt <- tempfile(fileext = ".txt")
inputlist <- c("F-F_Research_Data_Factors","F-F_Momentum_Factor","F-F_ST_Reversal_Factor","F-F_LT_Reversal_Factor")
# Now process only these files if they can be matched (download only)
FFdownload(exclude_daily=TRUE,tempdir=tempd,download=TRUE,download_only=FALSE,inputlist=inputlist)

tempf2 <- tempfile(fileext = ".RData"); tempd2 <- tempdir()
FFdownload(output_file = tempf2,tempdir = tempd2,exclude_daily = TRUE, download = TRUE, download_only=FALSE, listsave=temptxt)
load(tempf2)
FFdownload$x_25_Portfolios_5x5$monthly$average_value_weighted_returns

Exercise 3: Plotting Data

Carefully work through vignette("TQ04-charting-with-tidyquant") and create plots using ggplot from the tidyverse. Select four assets to highlight all functions that are important for you!

#Get AAPL, AMZN, NFLX, FB STock Prices

AAPL <- tq_get("AAPL", get = "stock.prices", from = "2015-01-01", to = "2019-12-31")

AMZN <- tq_get("AMZN", get = "stock.prices", from = "2015-01-01", to = "2019-12-31")

NFLX <- tq_get("NFLX", get = "stock.prices", from = "2015-01-01", to = "2019-12-31")

FB <- tq_get("FB", get = "stock.prices", from = "2015-01-01", to = "2019-12-31")

Histogram (incl. normal density)

hist(AAPL$adjusted, # histogram APPLE
col="violet", # column color
prob = TRUE, # show densities instead of frequencies
xlab = "Adjusted",
main = "APPLE")
lines(density(AAPL$adjusted), # density plot
lwd = 2, # thickness of line
col = "pink")

hist(AMZN$adjusted, # histogram AMAZON
col="peachpuff", # column color
prob = TRUE, # show densities instead of frequencies
xlab = "Adjusted",
main = "AMAZON")
lines(density(AMZN$adjusted), # density plot
lwd = 2, # thickness of line
col = "chocolate3")

hist(NFLX$adjusted, # histogram NETFLIX
col="lightpink", # column color
prob = TRUE, # show densities instead of frequencies
xlab = "Adjusted",
main = "NETFLIX")
lines(density(NFLX$adjusted), # density plot
lwd = 2, # thickness of line
col = "cyan4")

hist(FB$adjusted, # histogram FACEBOOK
col="rosybrown1", # column color
prob = TRUE, # show densities instead of frequencies
xlab = "Adjusted",
main = "FACEBOOK")
lines(density(FB$adjusted), # density plot
lwd = 2, # thickness of line
col = "royalblue1")

#Box-Plot
boxplot(AAPL$adjusted, main="APPLE", xlab="Adjusted",col="lightsteelblue1", horizontal = TRUE)

boxplot(AMZN$adjusted, main="AMAZON", xlab="Adjusted",col="yellow", horizontal = TRUE)

boxplot(NFLX$adjusted, main="NETFLIX", xlab="Adjusted",col="red", horizontal = TRUE)

boxplot(FB$adjusted, main="FACEBOOK", xlab="Adjusted",col="royalblue3", horizontal = TRUE)

#gg-Plot

qqnorm(AAPL$adjusted)
qqline(AAPL$adjusted,col="royalblue",lwd=3) #to see if normal distribution

qqnorm(AMZN$adjusted)
qqline(AMZN$adjusted,col="yellow",lwd=3)

qqnorm(NFLX$adjusted)
qqline(NFLX$adjusted,col="red",lwd=3)

qqnorm(FB$adjusted)
qqline(FB$adjusted,col="blue",lwd=3)

ggplot(aes()) + ... + facet_wrap()

Exercise 4: Statistics

Here you might use all tidyquant-vignettes! My shiny-script here might also help you to get an idea: www.riskindex.eu:3838/mci.

Exercise 4.1 Within securities

For the four securities calculate all interesting statistics (measures of location, dispersion, skewness and kurtosis). Check returns for univariate (and multivariate) normality.

table.Stats()
table.DownsideRisk
table.HigherMoments

options(xts_check_TZ=FALSE)

Stocks.raw <- c("MSFT","AAPL","AMZN","GOOG") %>%
    tq_get(get  = "stock.prices", from = "2000-01-01", to   = Sys.Date()) %>%
    group_by(symbol)
Stocks_ret_daily <- Stocks.raw %>%
    tq_transmute(select = adjusted, mutate_fun = periodReturn,
                 period = "daily", col_rename = "return", type="log")
Stocks_ret_monthly <- Stocks.raw %>%
    tq_transmute(select = adjusted, mutate_fun = periodReturn,
                 period = "monthly", col_rename = "return", type="log")

#geometric and arithmetic mean

#monthly

Stocks_ret_monthly %>% summarize(geometric_mean=mean.geometric(return), arithmetic_mean=mean(return)) %>%
mutate_at(-1, round, 5) %>% t()

#daily
Stocks_ret_daily %>% summarize(geometric_mean=mean.geometric(return), arithmetic_mean=mean(return)) %>%
mutate_at(-1, round, 5) %>% t()

#Skewness and kurtosis

Stocks_ret_daily %>% summarize(Skewness=skewness(return), "Ex. Kurtosis"=kurtosis(return)) %>%
          mutate_at(-1, round, 3) %>% t()

#measures of spread
#monthly
Stocks_ret_monthly %>% summarize(Volatility=sd(return), Range=max(return)-min(return), IQR=IQR(return)) %>%
mutate_at(-1, round, 3) %>% t()

#daily
Stocks_ret_daily %>% summarize(Volatility=sd(return), Range=max(return)-min(return), IQR=IQR(return)) %>%
mutate_at(-1, round, 3) %>% t()

#Standard deviation - measures of spread financial risk

#monthly
Stocks_ret_monthly %>% summarize(Semideviation=SemiDeviation(return),
"VaR(0.05)"=VaR(return,p=0.05), "CVaR(0.05)"=CVaR(return,p=0.05)) %>%
mutate_at(-1, round, 3) %>% t()

#daily
Stocks_ret_daily %>% summarize(Semideviation=SemiDeviation(return),
"VaR(0.05)"=VaR(return,p=0.05), "CVaR(0.05)"=CVaR(return,p=0.05)) %>%
mutate_at(-1, round, 3) %>% t()

Exercise 4.2 Between securities and with index

Plot all securities vis-a-vis the index. Include a regression line and regression information. Can you plot rolling correlations between assets (and/or the index)?

#grouping the 4 securities, getting monthly returns
Ra<-c("AAPL","AMZN","MSFT","GOOG")%>%
tq_get(get="stock.prices",from="2016-12-31",to="2019-12-31")%>%
group_by(symbol)%>%
tq_transmute(select = adjusted, mutate_fun = periodReturn,period = "monthly",col_rename = "Ra")

#monthly Returns for the Dow Jones
Rb <- c("DJI") %>%
tq_get(get = "stock.prices",from="2016-12-31",to="2019-12-31") %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Rb")
RaRb <- left_join(Ra, Rb, by = c("date" = "date"))

RaRb %>%
tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM)

#AAPL vs Dow Jones
stock_prices <- c("DJI", "AAPL") %>%
tq_get(get = "stock.prices",
from="2016-12-31",to="2019-12-31") %>%
group_by(symbol)

stock_pairs <- stock_prices %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "daily",
# type = "log",
col_rename = "returns") %>%
spread(key = symbol, value = returns)

stock_pairs %>%
ggplot(aes(x = DJI, y = AAPL)) +
geom_point(color = palette_light()[[1]], alpha = 0.5) +
geom_smooth(method = "lm") +
labs(title = "Returns Relationship of Apple and Dow Jones 2016/12/31 to 2019/12/31 - Method: lm") +
theme_tq()

#AMZN vs Dow Jones
stock_prices <- c("DJI", "AMZN") %>%
tq_get(get = "stock.prices",
from="2016-12-31",to="2019-12-31") %>%
group_by(symbol)

stock_pairs <- stock_prices %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "daily",
# type = "log",
col_rename = "returns") %>%
spread(key = symbol, value = returns)

stock_pairs %>%
ggplot(aes(x = DJI, y = AMZN)) +
geom_point(color = palette_light()[[1]], alpha = 0.5) +
geom_smooth(method = "lm") +
labs(title = "Returns Relationship of Amazon and Dow Jones 2016/12/31 to 2019/12/31 - Method: lm") +
theme_tq()

#MSFT vs Dow Jones
stock_prices <- c("DJI", "MSFT") %>%
tq_get(get = "stock.prices",
from="2016-12-31",to="2019-12-31") %>%
group_by(symbol)

stock_pairs <- stock_prices %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "daily",
# type = "log",
col_rename = "returns") %>%
spread(key = symbol, value = returns)

stock_pairs %>%
ggplot(aes(x = DJI, y = MSFT)) +
geom_point(color = palette_light()[[1]], alpha = 0.5) +
geom_smooth(method = "lm") +
labs(title = "Returns Relationship of Microsoft and Dow Jones 2016/12/31 to 2019/12/31 - Method: lm") +
theme_tq()

#Google vs Dow Jones

stock_prices <- c("DJI", "GOOG") %>%
tq_get(get = "stock.prices",
from="2016-12-31",to="2019-12-31") %>%
group_by(symbol)

stock_pairs <- stock_prices %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "daily",
# type = "log",
col_rename = "returns") %>%
spread(key = symbol, value = returns)

stock_pairs %>%
ggplot(aes(x = DJI, y = GOOG)) +
geom_point(color = palette_light()[[1]], alpha = 0.5) +
geom_smooth(method = "lm") +
labs(title = "Returns Relationship of Google and Dow Jones 2016/12/31 to 2019/12/31 - Method: lm") +
theme_tq()

#Can you plot rolling correlations between assets (and/or the index)?
Ra <- c("AAPL", "GOOG", "MSFT","AMZN") %>%
tq_get(get = "stock.prices",
from = "2013-12-31",
to = "2018-12-31") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log",
col_rename = "Ra")

Rb <- c("DJI") %>%
tq_get(get = "stock.prices",
from = "2013-12-31",
to = "2018-12-31") %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log",
col_rename = "Rb")

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))

rolling_corr <- RaRb %>%
tq_transmute_xy(x = Ra,
y = Rb,
mutate_fun = runCor,
n = 6,
col_rename = "rolling.corr.6")

rolling_corr %>%
ggplot(aes(x = date, y = rolling.corr.6, color = symbol)) +
geom_hline(yintercept = 0, color = palette_light()[[1]]) +
geom_line(size = 1) +
labs(title = "Six Month Rolling Correlations to Dow Jones (2013/12/31 to 2018/12/31)",
x = "", y = "Correlation", color = "") +
facet_wrap(~ symbol, ncol = 2) +theme_tq() +
scale_color_tq()

table.CAPM

Exercise 5: Performance Analysis

Create multiple portfolios using tq_portfolio() (e.g. an equally weighted one, a market-value weighted one, and one where you create a trading strategy according to your liking). DO a performance analysis a la vignette("TQ05-performance-analysis-with-tidyquant").

To make a portfolio I have to choose assets. Therefore I took the monthly stock prices from Apple, Google, Netflix and Amazon from 31.12.2013 to 31.12.2019.

stock_returns_monthly <- c("AAPL", "GOOG", "NFLX", "AMZN") %>%
  tq_get(get  = "stock.prices",
         from = "2013-12-31",
         to   = "2019-12-31") %>%
  group_by(symbol) %>%
  tq_transmute(select     = adjusted, 
               mutate_fun = periodReturn, 
               period     = "monthly", 
               col_rename = "Ra")

Creating a repetition for three portfolios

stock_returns_monthly_multi <- stock_returns_monthly %>%
  tq_repeat_df(n = 3)

Market-value weighted Portfolio, Using the market values as of 13th September 2019


MV_AAPL<-988.57
MV_GOOG<-859.51
MV_NFLX<-128.79
MV_AMZN<-909.84

MV_sum<-MV_NFLX+MV_AAPL+MV_AMZN+MV_GOOG

#Now I create the vector for the equally weighted one (25% for each stock).
One portfolio is equally weighted with 25% and the 3rd portfolio is weighted after my preferences

weights <- c(
  0.25, 0.25, 0.25, 0.25, 
  MV_AAPL/MV_sum, MV_GOOG/MV_sum, MV_NFLX/MV_sum, MV_AMZN/MV_sum, 
  0.2, 0.4, 0.2, 0.2)

#To measure the performance of the 3 portfolios I choose the SP500 as baseline returns

baseline_returns_monthly <- "^GSPC" %>%
  tq_get(get  = "stock.prices",
         from = "2013-12-31",
         to   = "2019-12-31") %>%
  tq_transmute(select     = adjusted, 
               mutate_fun = periodReturn, 
               period     = "monthly", 
               col_rename = "Rb")

#The next step is to create the portfolios according to the weight table

stocks <- c("AAPL", "GOOG", "NFLX", "AMZN")
weights_table <-  tibble(stocks) %>%
  tq_repeat_df(n = 3) %>%
  bind_cols(tibble(weights)) %>%
  group_by(portfolio)
weights_table

#Creating the returns for the portfolios

portfolio_returns_monthly_multi <- stock_returns_monthly_multi %>%
  tq_portfolio(assets_col  = symbol, 
               returns_col = Ra, 
               weights     = weights_table, 
               col_rename  = "Ra")
portfolio_returns_monthly_multi

#Combining the portfolios with the baseline

RaRb_multiple_portfolio <- left_join(portfolio_returns_monthly_multi, 
                                     baseline_returns_monthly,
                                     by = "date")

#A performance analysis can be done now --> CAPM table is to be created

RaRb_multiple_portfolio %>%
  tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM)

Sharpe Ratio Table

RaRb_multiple_portfolio %>%
  tq_performance(Ra = Ra, Rb = NULL, performance_fun = SharpeRatio)

Table Stats


RaRb_multiple_portfolio %>%
  tq_performance(Ra = Ra, Rb = NULL, performance_fun = table.Stats)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant