Provide major takeaways from Chapter 1 in 50 words # Reproducible Finance is a philosiphy about how to do quantitative, data science driven financial analysis. Data visualization translates numbers into shapes and colors so others can derive value from the information. One of R’s most powerful traits is its collection of packages that allows us to use smart R codes.
library(tidyverse)
library(tidyquant)
tq_index_options()
## [1] "DOW" "DOWGLOBAL" "SP400" "SP500" "SP600"
data <- tq_index("SP400")
tq_exchange_options()
## [1] "AMEX" "NASDAQ" "NYSE"
data <- tq_exchange("NYSE")
stock prices from yahoo finance
stock <- tq_get("TSLA")
unemployment_nh <- tq_get("NHUR", get = "economic.data")
unemployment_nh
## # A tibble: 127 × 3
## symbol date price
## <chr> <date> <dbl>
## 1 NHUR 2015-01-01 3.8
## 2 NHUR 2015-02-01 3.8
## 3 NHUR 2015-03-01 3.7
## 4 NHUR 2015-04-01 3.6
## 5 NHUR 2015-05-01 3.5
## 6 NHUR 2015-06-01 3.4
## 7 NHUR 2015-07-01 3.3
## 8 NHUR 2015-08-01 3.3
## 9 NHUR 2015-09-01 3.2
## 10 NHUR 2015-10-01 3.1
## # ℹ 117 more rows
AAPL <- tq_get("AAPL")
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_line() +
labs(title = "APPL Line Chart", y = "Closing Price", x = "") +
theme_tq()
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_barchart(aes(open = open, high = high, low = low, close = close)) +
labs(title = "APPL Bar Chart", y = "Closing Price", x = "") +
theme_tq()
AAPL %>%
tail(30) %>%
ggplot(aes(x = date, y = close)) +
geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
labs(title = "APPL Candlestick Chart", y = "Closing Price", x = "") +
theme_tq()
Ra <- c("AAPL", "GOOG", "NFLX") %>%
tq_get(get = "stock.price",
from = "2010-01-01",
to = "2015-12-31") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Ra")
Ra
## # A tibble: 216 × 3
## # Groups: symbol [3]
## symbol date Ra
## <chr> <date> <dbl>
## 1 AAPL 2010-01-29 -0.103
## 2 AAPL 2010-02-26 0.0654
## 3 AAPL 2010-03-31 0.148
## 4 AAPL 2010-04-30 0.111
## 5 AAPL 2010-05-28 -0.0161
## 6 AAPL 2010-06-30 -0.0208
## 7 AAPL 2010-07-30 0.0227
## 8 AAPL 2010-08-31 -0.0550
## 9 AAPL 2010-09-30 0.167
## 10 AAPL 2010-10-29 0.0607
## # ℹ 206 more rows
Rb <- ("XLK") %>%
tq_get(get = "stock.price",
from = "2010-01-01",
to = "2015-12-31") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Rb")
Rb
## # A tibble: 72 × 3
## # Groups: symbol [1]
## symbol date Rb
## <chr> <date> <dbl>
## 1 XLK 2010-01-29 -0.0993
## 2 XLK 2010-02-26 0.0348
## 3 XLK 2010-03-31 0.0684
## 4 XLK 2010-04-30 0.0126
## 5 XLK 2010-05-28 -0.0748
## 6 XLK 2010-06-30 -0.0540
## 7 XLK 2010-07-30 0.0745
## 8 XLK 2010-08-31 -0.0561
## 9 XLK 2010-09-30 0.117
## 10 XLK 2010-10-29 0.0578
## # ℹ 62 more rows
RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 216 × 5
## symbol.x date Ra symbol.y Rb
## <chr> <date> <dbl> <chr> <dbl>
## 1 AAPL 2010-01-29 -0.103 XLK -0.0993
## 2 AAPL 2010-02-26 0.0654 XLK 0.0348
## 3 AAPL 2010-03-31 0.148 XLK 0.0684
## 4 AAPL 2010-04-30 0.111 XLK 0.0126
## 5 AAPL 2010-05-28 -0.0161 XLK -0.0748
## 6 AAPL 2010-06-30 -0.0208 XLK -0.0540
## 7 AAPL 2010-07-30 0.0227 XLK 0.0745
## 8 AAPL 2010-08-31 -0.0550 XLK -0.0561
## 9 AAPL 2010-09-30 0.167 XLK 0.117
## 10 AAPL 2010-10-29 0.0607 XLK 0.0578
## # ℹ 206 more rows
RaRb_capm <- RaRb %>%
tq_performance(Ra = Ra,
Rb = Rb,
performance_fun = table.CAPM,
scale = 1)
RaRb_capm
## # A tibble: 1 × 17
## ActivePremium Alpha AlphaRobust AnnualizedAlpha Beta `Beta-` `Beta-Robust`
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.0131 0.0216 0.0097 0.0216 0.880 0.150 1.31
## # ℹ 10 more variables: `Beta+` <dbl>, `Beta+Robust` <dbl>, BetaRobust <dbl>,
## # Correlation <dbl>, `Correlationp-value` <dbl>, InformationRatio <dbl>,
## # `R-squared` <dbl>, `R-squaredRobust` <dbl>, TrackingError <dbl>,
## # TreynorRatio <dbl>