Provide major takeaways from Chapter 1 in 50 words:

Reproducible finance is described by the textbook as a philosophy about how to do quantitative analysis. It is essentially data that can be interpreted an understandable and efficient way. There are three universes of portfolio analysis with R: xts, tidyverse, and tidyquant. Data visualization is about turning numbers into shapes and colors, which supports the original theory.

Stock Index

tq_index_options()
## [1] "DOW"       "DOWGLOBAL" "SP400"     "SP500"     "SP600"
data <- tq_index("SP400")
data
## # A tibble: 401 × 8
##    symbol company     identifier sedol  weight sector shares_held local_currency
##    <chr>  <chr>       <chr>      <chr>   <dbl> <chr>        <int> <chr>         
##  1 IBKR   Interactiv… 45841N107  B1WT… 0.00794 Finan…      847624 USD           
##  2 EME    EMCOR Grou… 29084Q100  2474… 0.00754 Indus…      358132 USD           
##  3 OKTA   Okta Inc. … 679295105  BDFZ… 0.00712 Infor…     1273676 USD           
##  4 RBA    RB Global … 74935Q107  BMWG… 0.00697 Indus…     1435672 USD           
##  5 DUOL   Duolingo I… 26603R106  BMCM… 0.00691 Consu…      295030 USD           
##  6 DOCU   DocuSign I… 256163106  BFYT… 0.00629 Infor…     1572757 USD           
##  7 GWRE   Guidewire … 40171V100  B7JY… 0.00623 Infor…      650194 USD           
##  8 USFD   US Foods H… 912008109  BYVF… 0.00622 Consu…     1793506 USD           
##  9 CSL    Carlisle C… 142339100  2176… 0.00622 Indus…      346106 USD           
## 10 PSTG   Pure Stora… 74624M102  BYZ6… 0.00608 Infor…     2412778 USD           
## # ℹ 391 more rows

Stock Exchanges

tq_exchange_options() data <- tq_exchange(“NYSE”) data

tq_get

Stock prices from Yahoo Finance

stock <- tq_get("TSLA")
stock
## # A tibble: 2,611 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 TSLA   2015-01-02  14.9  14.9  14.2  14.6  71466000     14.6
##  2 TSLA   2015-01-05  14.3  14.4  13.8  14.0  80527500     14.0
##  3 TSLA   2015-01-06  14.0  14.3  13.6  14.1  93928500     14.1
##  4 TSLA   2015-01-07  14.2  14.3  14.0  14.1  44526000     14.1
##  5 TSLA   2015-01-08  14.2  14.3  14.0  14.0  51637500     14.0
##  6 TSLA   2015-01-09  13.9  14.0  13.7  13.8  70024500     13.8
##  7 TSLA   2015-01-12  13.5  13.6  13.3  13.5  89254500     13.5
##  8 TSLA   2015-01-13  13.6  13.8  13.4  13.6  67159500     13.6
##  9 TSLA   2015-01-14  12.4  13.0  12.3  12.8 173278500     12.8
## 10 TSLA   2015-01-15  13.0  13.1  12.7  12.8  78247500     12.8
## # ℹ 2,601 more rows

Economic Data From FRED

unemployment_nh <- tq_get ("NHUR", get = "economic.data")
unemployment_nh
## # A tibble: 123 × 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
## # ℹ 113 more rows

Import Data

AAPL <- tq_get("AAPL")
AAPL
## # A tibble: 2,611 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 AAPL   2015-01-02  27.8  27.9  26.8  27.3 212818400     24.3
##  2 AAPL   2015-01-05  27.1  27.2  26.4  26.6 257142000     23.6
##  3 AAPL   2015-01-06  26.6  26.9  26.2  26.6 263188400     23.6
##  4 AAPL   2015-01-07  26.8  27.0  26.7  26.9 160423600     23.9
##  5 AAPL   2015-01-08  27.3  28.0  27.2  28.0 237458000     24.9
##  6 AAPL   2015-01-09  28.2  28.3  27.6  28.0 214798000     24.9
##  7 AAPL   2015-01-12  28.1  28.2  27.2  27.3 198603200     24.3
##  8 AAPL   2015-01-13  27.9  28.2  27.2  27.6 268367600     24.5
##  9 AAPL   2015-01-14  27.3  27.6  27.1  27.5 195826400     24.4
## 10 AAPL   2015-01-15  27.5  27.5  26.7  26.7 240056000     23.7
## # ℹ 2,601 more rows

Line Chart

AAPL %>%
    ggplot(aes(x = date, y = close)) +
    geom_line() +
    labs(title = "AAPL Line Chart", y = "Closing Price", x = "") +
    theme_tq()

Bar Chart

AAPL %>%
    ggplot(aes(x = date, y = close)) +
    geom_barchart(aes(open = open, high = high, low = low, close = close)) +
    labs(title = "AAPL Bar Chart", y = "Closing Price", x = "") +
    theme_tq()

Candlesticks

AAPL %>%
    tail(30) %>%
    ggplot(aes(x = date, y = close)) +
    geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
    labs(title = "AAPL Candlestick Chart", y = "Closing Price", x = "") +
    theme_tq()

1 Get Stock Prices And Convert To Returns

Ra <- c("AAPL", "GOOG", "NFLX") %>%
    tq_get(get = "stock.prices",
           from = "2010-01-01",
           to = "2015-12-31") %>%
    tq_transmute(select     = adjusted,
                 mutate_fun = periodReturn,
                 period     = "monthly",
                 col_rename = "Ra")
Ra
## # A tibble: 72 × 2
##    date            Ra
##    <date>       <dbl>
##  1 2010-01-29  0.383 
##  2 2010-02-26  0.0610
##  3 2010-03-31  0.116 
##  4 2010-04-30  0.341 
##  5 2010-05-28  0.124 
##  6 2010-06-30 -0.0225
##  7 2010-07-30 -0.0561
##  8 2010-08-31  0.224 
##  9 2010-09-30  0.292 
## 10 2010-10-29  0.0704
## # ℹ 62 more rows

2 Get baseline and convert to returns

Rb <- "XLK" %>%
    tq_get(get = "stock.prices",
           from = "2010-01-01",
           to = "2015-12-31") %>%
    tq_transmute(select     = adjusted,
                 mutate_fun = periodReturn,
                 period     = "monthly",
                 col_rename = "Rb")
Rb
## # A tibble: 72 × 2
##    date            Rb
##    <date>       <dbl>
##  1 2010-01-29 -0.0993
##  2 2010-02-26  0.0348
##  3 2010-03-31  0.0684
##  4 2010-04-30  0.0126
##  5 2010-05-28 -0.0748
##  6 2010-06-30 -0.0540
##  7 2010-07-30  0.0745
##  8 2010-08-31 -0.0561
##  9 2010-09-30  0.117 
## 10 2010-10-29  0.0578
## # ℹ 62 more rows

3 Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 72 × 3
##    date            Ra      Rb
##    <date>       <dbl>   <dbl>
##  1 2010-01-29  0.383  -0.0993
##  2 2010-02-26  0.0610  0.0348
##  3 2010-03-31  0.116   0.0684
##  4 2010-04-30  0.341   0.0126
##  5 2010-05-28  0.124  -0.0748
##  6 2010-06-30 -0.0225 -0.0540
##  7 2010-07-30 -0.0561  0.0745
##  8 2010-08-31  0.224  -0.0561
##  9 2010-09-30  0.292   0.117 
## 10 2010-10-29  0.0704  0.0578
## # ℹ 62 more rows

4 Calculate CAPM

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra,
                   Rb = Rb,
                   performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 1 × 17
##   ActivePremium  Alpha AlphaRobust AnnualizedAlpha  Beta `Beta-` `Beta-Robust`
##           <dbl>  <dbl>       <dbl>           <dbl> <dbl>   <dbl>         <dbl>
## 1         0.492 0.0581      0.0501           0.969 0.199   -2.42         -2.36
## # ℹ 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>