Conclusion:
Here are four Equity I’ve invested since Jan 2015: EDU (The best eduation & training instituion in China), BABA(No need to introduce more, everyone knows Alibaba and Jack Ma), IQ (The best online video stream in China) and SOGO (the most famous on SOGO Pinyin typing). The reason I am investing into because I use literally their products everyday, so holding promising on their future. Cumulative returns is an good measure to evaluate whether certain stock worth holding, buying or selling. S&P 500 Index is selected as Beanchmark becasue all of them are listed equity in U.S.
We will see that EDU is not beating the S&P 500 Index in terms of cumulative return for the past decade (From 2009-01-02 to 2019-11-02).But if you could invest into right after the muddy water event for EDU, then you also can obtain pretty decent capital gain.
For BABA, without any doubt, it beats S&P 500 Index in terms of cumulative return since IPO (2014-09-19) up to 2019-11-02.
For IQ, if you could buy in right after IPO 2018-03-29 with large volume, then sold out within first three months, you would definitely already be millionaire.
SOGO, I kind of regret that I never sell it around 2018-7 where it was firt time marked high. Not quite sure why investor are shorting this stock, their typing products are really good. But right now, seems like it would take a long way to back to where it was on IPO.
Conclusion:
Beta is used in the Capital Asset Pricing model (CAPM), which calculates the expected return of an asset using beta and expected market returns.
We also call Beta is the sensitivity to benchmark performance, from regressoin chart we can see that EDU is not that sensitive (Normal beta range from -2 to 2 in the long term run, but for some lately IPO stock, we will see crazy high beta)
Beta for BABA and IQ are much higher, which also confirm with their performance on the first tab. IQ has the highest beta among these four stocks.
I am kind of surprised that beta for SOGO to S&P 500 can be a postive value, hopefully I didn’t plot regression chart wrong given its poor cumulative performance.
---
title: "Lab 1"
author: Jindong Zhao
date: "`r Sys.Date()`"
output:
flexdashboard::flex_dashboard:
orientation: columns
source_code: embed
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyquant)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(Quandl)
library(quantmod) # Include TTR, where ROC fuc can be used.
library(PerformanceAnalytics) # used for chart.Regression for Beta
library(gridExtra) # use grid.arrange() function to put multiple plots on one page
```
# Performance {.storyboard}
### Cumulative performance against Benchmark Index
```{r, fig.width=10, fig.height=6}
# Create a function cumu_return to handle dataframe pull from tidyquant
# This function is created to calculate log return and Cumulative return from daily close price
cumu_return <- function(df) {
# Since return of financial data subjective to log normal distribution
# So in practice we need to create log return=ln(St/St-1), since daily price series, so lag=1
# omit NA first if there is
df <- na.omit(df)
# add 0 as the first element
logret <- c(0, diff(log(df$close), lag = 1))
# add new Column logreturn into data frame
df <- data.frame(df, logreturn = logret)
# add new Column cumulative_return into data frame
# It is equal to cumulative product of gross return (logreturn + 1)
df <- data.frame(df, cumulative_return = cumprod(df$logreturn + 1))
}
EDU <- tq_get("EDU", get = "stock.prices", from = "2009-01-02", to = "2019-11-02")
EDU <- cumu_return(EDU)
# Prepare for S&P 500 index data
SP_EDU <- tq_get("^GSPC", get = "stock.prices", from = "2009-01-02", to = "2019-11-02")
SP_EDU <- cumu_return(SP_EDU)
# Build dataframe with Dates and price/cumulative return for both EDU and S&P 500
EDU_df <- data.frame(Date = EDU$date,
EDU_Price = EDU$close,
EDU_Cumulative_Ret = EDU$cumulative_return,
SP_Price = SP_EDU$close,
SP_Cumulative_Ret = SP_EDU$cumulative_return)
# Plot cumulative returns for both EDU and S&P 500 on the same plot, then assign to g_EDU
g_EDU <- EDU_df %>%
ggplot(aes(x = Date)) +
geom_line(aes(y = EDU_Cumulative_Ret, colour = "EDU")) +
geom_line(aes(y = SP_Cumulative_Ret, colour = "S&P 500")) +
labs(title = "Cumulative Returns: EDU vs S&P 500",
x = "Dates",
y = "Cumulative Returns") +
scale_colour_manual(name = "Equity/Benchmark Index", values = c("EDU" = "blue", "S&P 500" = "red")) +
theme_economist()
########################################################################################
# 2nd Stock/Equity: BABA
BABA <- tq_get("BABA", get = "stock.prices", from = "2014-09-19", to = "2019-11-02")
BABA <- cumu_return(BABA)
# Prepare for S&P 500 index data for BABA
SP_BABA <- tq_get("^GSPC", get = "stock.prices", from = "2014-09-19", to = "2019-11-02")
SP_BABA <- cumu_return(SP_BABA)
# Build dataframe with Dates and price/cumulative return for both BABA and S&P 500
BABA_df <- data.frame(Date = BABA$date,
BABA_Price = BABA$close,
BABA_Cumulative_Ret = BABA$cumulative_return,
SP_Price = SP_BABA$close,
SP_Cumulative_Ret = SP_BABA$cumulative_return)
# Plot cumulative returns for both BABA and S&P 500 on the same plot, then assign to g_BABA
g_BABA <- BABA_df %>%
ggplot(aes(x = Date)) +
geom_line(aes(y = BABA_Cumulative_Ret, colour = "BABA")) +
geom_line(aes(y = SP_Cumulative_Ret, colour = "S&P 500")) +
labs(title = "Cumulative Returns: BABA vs S&P 500",
x = "Dates",
y = "Cumulative Returns") +
scale_colour_manual(name = "Equity/Benchmark Index", values = c("BABA" = "blue", "S&P 500" = "red")) +
theme_economist()
########################################################################################
# 3rd Stock/Equity: IQ
IQ <- tq_get("IQ", get = "stock.prices", from = "2018-03-29", to = "2019-11-02")
IQ <- cumu_return(IQ)
# Prepare for S&P 500 index data for IQ
SP_IQ <- tq_get("^GSPC", get = "stock.prices", from = "2018-03-29", to = "2019-11-02")
SP_IQ <- cumu_return(SP_IQ)
# Build dataframe with Dates and price/cumulative return for both IQ and S&P 500
IQ_df <- data.frame(Date = IQ$date,
IQ_Price = IQ$close,
IQ_Cumulative_Ret = IQ$cumulative_return,
SP_Price = SP_IQ$close,
SP_Cumulative_Ret = SP_IQ$cumulative_return)
# Plot cumulative returns for both IQ and S&P 500 on the same plot, then assign to g_IQ
g_IQ <- IQ_df %>%
ggplot(aes(x = Date)) +
geom_line(aes(y = IQ_Cumulative_Ret, colour = "IQ")) +
geom_line(aes(y = SP_Cumulative_Ret, colour = "S&P 500")) +
labs(title = "Cumulative Returns: IQ vs S&P 500",
x = "Dates",
y = "Cumulative Returns") +
scale_colour_manual(name = "Equity/Benchmark Index", values = c("IQ" = "blue", "S&P 500" = "red")) +
theme_economist()
########################################################################################
# 4th Stock/Equity: SOGO
SOGO <- tq_get("SOGO", get = "stock.prices", from = "2017-10-26", to = "2019-11-02")
SOGO <- cumu_return(SOGO)
# Prepare for S&P 500 index data for SOGO
SP_SOGO <- tq_get("^GSPC", get = "stock.prices", from = "2017-10-26", to = "2019-11-02")
SP_SOGO <- cumu_return(SP_SOGO)
# Since SOGO price series have 9 NA, so use left_join instead
SOGO_df <- left_join(SOGO, SP_SOGO, by = "date")
# Plot cumulative returns for both SOGO and S&P 500 on the same plot, then assign to g_SOGO
g_SOGO <- SOGO_df %>%
ggplot(aes(x = date)) +
geom_line(aes(y = cumulative_return.x, colour = "SOGO")) +
geom_line(aes(y = cumulative_return.y, colour = "S&P 500")) +
labs(title = "Cumulative Returns: SOGO vs S&P 500",
x = "Dates",
y = "Cumulative Returns") +
scale_colour_manual(name = "Equity/Benchmark Index", values = c("SOGO" = "blue", "S&P 500" = "red")) +
theme_economist()
# use the grid.arrange() function from gridExtra to assemble multiple plots on one page
grid.arrange(g_EDU, g_BABA, g_IQ, g_SOGO, nrow = 2)
```
***
Conclusion:
- Here are four Equity I've invested since Jan 2015: EDU (The best eduation & training instituion in China), BABA(No need to introduce more, everyone knows Alibaba and Jack Ma), IQ (The best online video stream in China) and SOGO (the most famous on SOGO Pinyin typing). The reason I am investing into because I use literally their products everyday, so holding promising on their future. Cumulative returns is an good measure to evaluate whether certain stock worth holding, buying or selling. S&P 500 Index is selected as Beanchmark becasue all of them are listed equity in U.S.
- We will see that EDU is not beating the S&P 500 Index in terms of cumulative return for the past decade (From 2009-01-02 to 2019-11-02).But if you could invest into right after the muddy water event for EDU, then you also can obtain pretty decent capital gain.
- For BABA, without any doubt, it beats S&P 500 Index in terms of cumulative return since IPO (2014-09-19) up to 2019-11-02.
- For IQ, if you could buy in right after IPO 2018-03-29 with large volume, then sold out within first three months, you would definitely already be millionaire.
- SOGO, I kind of regret that I never sell it around 2018-7 where it was firt time marked high. Not quite sure why investor are shorting this stock, their typing products are really good. But right now, seems like it would take a long way to back to where it was on IPO.
# Sensitivity {.storyboard}
### EDU Beta Regression Chart
```{r, fig.width=10, fig.height=6}
# CAPM Beta for 1st stock: EDU
# Calculate Returns for Benchmark S&P 500: Daily RoC
EDU_bench_ret <- na.omit(ROC(EDU_df$SP_Price, type = "discrete"))
# Calculate Returns for EDU: Daily RoC
EDU_ret <- na.omit(ROC(EDU_df$EDU_Price, type = "discrete"))
# Get Risk Free rate, here we pull Treasury Yield 10 years, symbol is ^TNX
rf_data <- tq_get("^TNX",from = "2009-01-02", to = "2019-11-02")
# Calculate mean of Risk free rate
rf1 <- mean(rf_data$close, na.rm = TRUE)
# CAPM Beta for 2nd stock: BABA
# Calculate Returns for Benchmark S&P 500: Daily RoC
BABA_bench_ret <- na.omit(ROC(BABA_df$SP_Price, type = "discrete"))
# Calculate Returns for BABA: Daily RoC
BABA_ret <- na.omit(ROC(BABA_df$BABA_Price, type = "discrete"))
# Get Risk Free rate, here we pull Treasury Yield 10 years, symbol is ^TNX
rf_data <- tq_get("^TNX",from = "2014-09-19", to = "2019-11-02")
# Calculate mean of Risk free rate
rf2 <- mean(rf_data$close, na.rm = TRUE)
# CAPM Beta for 3rd stock: IQ
# Calculate Returns for Benchmark S&P 500: Daily RoC
IQ_bench_ret <- na.omit(ROC(IQ_df$SP_Price, type = "discrete"))
# Calculate Returns for IQ: Daily RoC
IQ_ret <- na.omit(ROC(IQ_df$IQ_Price, type = "discrete"))
# Get Risk Free rate, here we pull Treasury Yield 10 years, symbol is ^TNX
rf_data <- tq_get("^TNX",from = "2018-03-29", to = "2019-11-02")
# Calculate mean of Risk free rate
rf3 <- mean(rf_data$close, na.rm = TRUE)
# CAPM Beta for 4th stock: SOGO
# Calculate Returns for Benchmark S&P 500: Daily RoC
SOGO_bench_ret <- na.omit(ROC(SOGO_df$close.y, type = "discrete"))
# Calculate Returns for SOGO: Daily RoC
SOGO_ret <- na.omit(ROC(SOGO_df$close.x, type = "discrete"))
# Get Risk Free rate, here we pull Treasury Yield 10 years, symbol is ^TNX
rf_data <- tq_get("^TNX",from = "2017-10-26", to = "2019-11-02")
# Calculate mean of Risk free rate
rf4 <- mean(rf_data$close, na.rm = TRUE)
# Arrange four Beta Regression Chart into one plot
par(mfrow=c(2,2))
chart.Regression(EDU_ret, EDU_bench_ret, Rf = rf1,
excess.returns = FALSE, fit = c("loess", "linear"),
element.color = "blue", main = "EDU to S&P 500")
chart.Regression(BABA_ret, BABA_bench_ret, Rf = rf2,
excess.returns = FALSE, fit = c("loess", "linear"),
element.color = "blue", main = "BABA to S&P 500")
chart.Regression(IQ_ret, IQ_bench_ret, Rf = rf3,
excess.returns = FALSE, fit = c("loess", "linear"),
element.color = "blue", main = "IQ to S&P 500")
chart.Regression(SOGO_ret, SOGO_bench_ret, Rf = rf4,
excess.returns = FALSE, fit = c("loess", "linear"),
element.color = "blue", main = "SOGO to S&P 500")
```
***
Conclusion:
- Beta is used in the Capital Asset Pricing model (CAPM), which calculates the expected return of an asset using beta and expected market returns.
- We also call Beta is the sensitivity to benchmark performance, from regressoin chart we can see that EDU is not that sensitive (Normal beta range from -2 to 2 in the long term run, but for some lately IPO stock, we will see crazy high beta)
- Beta for BABA and IQ are much higher, which also confirm with their performance on the first tab. IQ has the highest beta among these four stocks.
- I am kind of surprised that beta for SOGO to S&P 500 can be a postive value, hopefully I didn't plot regression chart wrong given its poor cumulative performance.