```
library(tidyverse)
library(tidyquant)
```

# Introduction to Tidy Finance

The main aim of this chapter is to familiarize yourself with the tidyverse. We start by downloading and visualizing stock data from Yahoo!Finance. Then we move to a simple portfolio choice problem and construct the efficient frontier. These examples introduce you to our approach of *Tidy Finance*.

## Working with Stock Market Data

At the start of each session, we load the required packages. Throughout the entire book, we always use the `tidyverse`

(Wickham et al. 2019). In this chapter, we also load the convenient `tidyquant`

package (Dancho and Vaughan 2022) to download price data. This package provides a convenient wrapper for various quantitative functions compatible with the `tidyverse`

.

You typically have to install a package once before you can load it. In case you have not done this yet, call `install.packages("tidyquant")`

. If you have trouble using `tidyquant`

, check out the corresponding documentation.

We first download daily prices for one stock market ticker, e.g., the Apple stock, *AAPL*, directly from the data provider Yahoo!Finance. To download the data, you can use the command `tq_get`

. If you do not know how to use it, make sure you read the help file by calling `?tq_get`

. We especially recommend taking a look at the examples section of the documentation. We request daily data for a period of more than 20 years.

```
<- tq_get("AAPL",
prices get = "stock.prices",
from = "2000-01-01",
to = "2021-12-31"
) prices
```

```
# A tibble: 5,535 × 8
symbol date open high low close volume adjusted
<chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 AAPL 2000-01-03 0.936 1.00 0.908 0.999 535796800 0.851
2 AAPL 2000-01-04 0.967 0.988 0.903 0.915 512377600 0.779
3 AAPL 2000-01-05 0.926 0.987 0.920 0.929 778321600 0.790
4 AAPL 2000-01-06 0.948 0.955 0.848 0.848 767972800 0.722
5 AAPL 2000-01-07 0.862 0.902 0.853 0.888 460734400 0.756
# … with 5,530 more rows
```

`tq_get`

downloads stock market data from Yahoo!Finance if you do not specify another data source. The function returns a tibble with eight quite self-explanatory columns: `symbol`

, `date`

, the market prices at the `open`

, `high`

, `low`

, and `close`

, the daily `volume`

(in the number of traded shares), and the `adjusted`

price in USD. The adjusted prices are corrected for anything that might affect the stock price after the market closes, e.g., stock splits and dividends. These actions affect the quoted prices, but they have no direct impact on the investors who hold the stock. Therefore, we often rely on adjusted prices when it comes to analyzing the returns an investor would have earned by holding the stock continuously.

Next, we use the `ggplot2`

package (Wickham 2016) to visualize the time series of adjusted prices in Figure 1 . This package takes care of visualization tasks based on the principles of the grammar of graphics (Wilkinson 2012).

```
|>
prices ggplot(aes(x = date, y = adjusted)) +
geom_line() +
labs(
x = NULL,
y = NULL,
title = "Apple stock prices between beginning of 2000 and end of 2021"
)
```

Instead of analyzing prices, we compute daily net returns defined as \(r_t = p_t / p_{t-1} - 1\), where \(p_t\) is the adjusted day \(t\) price. In that context, the function `lag()`

is helpful, which returns the previous value in a vector.

```
<- prices |>
returns arrange(date) |>
mutate(ret = adjusted / lag(adjusted) - 1) |>
select(symbol, date, ret)
returns
```

```
# A tibble: 5,535 × 3
symbol date ret
<chr> <date> <dbl>
1 AAPL 2000-01-03 NA
2 AAPL 2000-01-04 -0.0843
3 AAPL 2000-01-05 0.0146
4 AAPL 2000-01-06 -0.0865
5 AAPL 2000-01-07 0.0474
# … with 5,530 more rows
```

The resulting tibble contains three columns, where the last contains the daily returns (`ret`

). Note that the first entry naturally contains a missing value (`NA`

) because there is no previous price. Obviously, the use of `lag()`

would be meaningless if the time series is not ordered by ascending dates. The command `arrange()`

provides a convenient way to order observations in the correct way for our application. In case you want to order observations by descending dates, you can use `arrange(desc(date))`

.

For the upcoming examples, we remove missing values as these would require separate treatment when computing, e.g., sample averages. In general, however, make sure you understand why `NA`

values occur and carefully examine if you can simply get rid of these observations.

```
<- returns |>
returns drop_na(ret)
```

Next, we visualize the distribution of daily returns in a histogram in Figure 2. For convenience, we multiply the returns by 100 to get returns in percent for the visualizations. Additionally, we add a dashed line that indicates the 5 percent quantile of the daily returns to the histogram, which is a (crude) proxy for the worst return of the stock with a probability of at most 5 percent. The 5 percent quantile is closely connected to the (historical) value-at-risk, a risk measure commonly monitored by regulators. We refer to Tsay (2010) for a more thorough introduction to stylized facts of returns.

```
<- quantile(returns |> pull(ret) * 100, probs = 0.05)
quantile_05 |>
returns ggplot(aes(x = ret * 100)) +
geom_histogram(bins = 100) +
geom_vline(aes(xintercept = quantile_05),
linetype = "dashed"
+
) labs(
x = NULL,
y = NULL,
title = "Distribution of daily Apple stock returns in percent"
)
```

Here, `bins = 100`

determines the number of bins used in the illustration and hence implicitly the width of the bins. Before proceeding, make sure you understand how to use the geom `geom_vline()`

to add a dashed line that indicates the 5 percent quantile of the daily returns. A typical task before proceeding with *any* data is to compute summary statistics for the main variables of interest.

```
|>
returns mutate(ret = ret * 100) |>
summarize(across(
ret,list(
daily_mean = mean,
daily_sd = sd,
daily_min = min,
daily_max = max
) ))
```

```
# A tibble: 1 × 4
ret_daily_mean ret_daily_sd ret_daily_min ret_daily_max
<dbl> <dbl> <dbl> <dbl>
1 0.130 2.52 -51.9 13.9
```

We see that the maximum *daily* return was 13.905 percent. Perhaps not surprisingly, the average daily return is close to but slightly above 0. In line with the illustration above, the large losses on the day with the minimum returns indicate a strong asymmetry in the distribution of returns.

You can also compute these summary statistics for each year individually by imposing `group_by(year = year(date))`

, where the call `year(date)`

returns the year. More specifically, the few lines of code below compute the summary statistics from above for individual groups of data defined by year. The summary statistics, therefore, allow an eyeball analysis of the time-series dynamics of the return distribution.

```
|>
returns mutate(ret = ret * 100) |>
group_by(year = year(date)) |>
summarize(across(
ret,list(
daily_mean = mean,
daily_sd = sd,
daily_min = min,
daily_max = max
),.names = "{.fn}"
|>
)) print(n = Inf)
```

```
# A tibble: 22 × 5
year daily_mean daily_sd daily_min daily_max
<dbl> <dbl> <dbl> <dbl> <dbl>
1 2000 -0.346 5.49 -51.9 13.7
2 2001 0.233 3.93 -17.2 12.9
3 2002 -0.121 3.05 -15.0 8.46
4 2003 0.186 2.34 -8.14 11.3
5 2004 0.470 2.55 -5.58 13.2
6 2005 0.349 2.45 -9.21 9.12
7 2006 0.0949 2.43 -6.33 11.8
8 2007 0.366 2.38 -7.02 10.5
9 2008 -0.265 3.67 -17.9 13.9
10 2009 0.382 2.14 -5.02 6.76
11 2010 0.183 1.69 -4.96 7.69
12 2011 0.104 1.65 -5.59 5.89
13 2012 0.130 1.86 -6.44 8.87
14 2013 0.0472 1.80 -12.4 5.14
15 2014 0.145 1.36 -7.99 8.20
16 2015 0.00199 1.68 -6.12 5.74
17 2016 0.0575 1.47 -6.57 6.50
18 2017 0.164 1.11 -3.88 6.10
19 2018 -0.00573 1.81 -6.63 7.04
20 2019 0.266 1.65 -9.96 6.83
21 2020 0.281 2.94 -12.9 12.0
22 2021 0.133 1.58 -4.17 5.39
```

In case you wonder: the additional argument `.names = "{.fn}"`

in `across()`

determines how to name the output columns. The specification is rather flexible and allows almost arbitrary column names, which can be useful for reporting. The `print()`

function simply controls the output options for the R console.

## Scaling Up the Analysis

As a next step, we generalize the code from before such that all the computations can handle an arbitrary vector of tickers (e.g., all constituents of an index). Following tidy principles, it is quite easy to download the data, plot the price time series, and tabulate the summary statistics for an arbitrary number of assets.

This is where the `tidyverse`

magic starts: tidy data makes it extremely easy to generalize the computations from before to as many assets as you like. The following code takes any vector of tickers, e.g., `ticker <- c("AAPL", "MMM", "BA")`

, and automates the download as well as the plot of the price time series. In the end, we create the table of summary statistics for an arbitrary number of assets. We perform the analysis with data from all current constituents of the Dow Jones Industrial Average index.

```
<- tq_index("DOW")
ticker ticker
```

```
# A tibble: 30 × 8
symbol company ident…¹ sedol weight sector share…² local…³
<chr> <chr> <chr> <chr> <dbl> <chr> <dbl> <chr>
1 UNH UnitedHealth Gr… 91324P… 2917… 0.0951 Healt… 5530524 USD
2 GS Goldman Sachs G… 38141G… 2407… 0.0677 Finan… 5530524 USD
3 HD Home Depot Inc. 437076… 2434… 0.0591 Consu… 5530524 USD
4 MCD McDonald's Corp… 580135… 2550… 0.0541 Consu… 5530524 USD
5 MSFT Microsoft Corpo… 594918… 2588… 0.0513 Infor… 5530524 USD
# … with 25 more rows, and abbreviated variable names ¹identifier,
# ²shares_held, ³local_currency
```

Conveniently, `tidyquant`

provides a function to get all stocks in a stock index with a single call (similarly, `tq_exchange("NASDAQ")`

delivers all stocks currently listed on the NASDAQ exchange).

```
<- tq_get(ticker,
index_prices get = "stock.prices",
from = "2000-01-01",
to = "2021-12-31"
)
```

The resulting tibble contains 158033 daily observations for 30 different corporations. Figure 3 illustrates the time series of downloaded *adjusted* prices for each of the constituents of the Dow Jones index. Make sure you understand every single line of code! (What are the arguments of `aes()`

? Which alternative `geoms`

could you use to visualize the time series? Hint: if you do not know the answers try to change the code to see what difference your intervention causes.

```
|>
index_prices ggplot(aes(
x = date,
y = adjusted,
color = symbol
+
)) geom_line() +
labs(
x = NULL,
y = NULL,
color = NULL,
title = "Stock prices of DOW index constituents"
+
) theme(legend.position = "none")
```

Do you notice the small differences relative to the code we used before? `tq_get(ticker)`

returns a tibble for several symbols as well. All we need to do to illustrate all tickers simultaneously is to include `color = symbol`

in the `ggplot2`

aesthetics. In this way, we generate a separate line for each ticker. Of course, there are simply too many lines on this graph to identify the individual stocks properly, but it illustrates the point well.

The same holds for stock returns. Before computing the returns, we use `group_by(symbol)`

such that the `mutate()`

command is performed for each symbol individually. The same logic also applies to the computation of summary statistics: `group_by(symbol)`

is the key to aggregating the time series into ticker-specific variables of interest.

```
<- index_prices |>
all_returns group_by(symbol) |>
mutate(ret = adjusted / lag(adjusted) - 1) |>
select(symbol, date, ret) |>
drop_na(ret)
|>
all_returns mutate(ret = ret * 100) |>
group_by(symbol) |>
summarize(across(
ret,list(
daily_mean = mean,
daily_sd = sd,
daily_min = min,
daily_max = max
),.names = "{.fn}"
|>
)) print(n = Inf)
```

```
# A tibble: 30 × 5
symbol daily_mean daily_sd daily_min daily_max
<chr> <dbl> <dbl> <dbl> <dbl>
1 AAPL 0.130 2.52 -51.9 13.9
2 AMGN 0.0475 1.99 -13.4 15.1
3 AXP 0.0547 2.30 -17.6 21.9
4 BA 0.0614 2.20 -23.8 24.3
5 CAT 0.0699 2.04 -14.5 14.7
6 CRM 0.128 2.68 -27.1 26.0
7 CSCO 0.0370 2.39 -16.2 24.4
8 CVX 0.0486 1.75 -22.1 22.7
9 DIS 0.0531 1.93 -18.4 16.0
10 DOW 0.0799 2.81 -21.7 20.9
11 GS 0.0584 2.33 -19.0 26.5
12 HD 0.0601 1.94 -28.7 14.1
13 HON 0.0523 1.95 -17.4 28.2
14 IBM 0.0262 1.66 -15.5 12.0
15 INTC 0.0400 2.36 -22.0 20.1
16 JNJ 0.0415 1.23 -15.8 12.2
17 JPM 0.0625 2.44 -20.7 25.1
18 KO 0.0329 1.33 -10.1 13.9
19 MCD 0.0553 1.48 -15.9 18.1
20 MMM 0.0452 1.49 -12.9 12.6
21 MRK 0.0325 1.70 -26.8 13.0
22 MSFT 0.0587 1.93 -15.6 19.6
23 NKE 0.0824 1.90 -19.8 15.5
24 PG 0.0398 1.34 -30.2 12.0
25 TRV 0.0554 1.85 -20.8 25.6
26 UNH 0.101 2.00 -18.6 34.8
27 V 0.0995 1.89 -13.6 15.0
28 VZ 0.0287 1.51 -11.8 14.6
29 WBA 0.0340 1.81 -15.0 16.6
30 WMT 0.0321 1.49 -10.2 11.7
```

Note that you are now also equipped with all tools to download price data for *each* ticker listed in the S&P 500 index with the same number of lines of code. Just use `ticker <- tq_index("SP500")`

, which provides you with a tibble that contains each symbol that is (currently) part of the S&P 500. However, don’t try this if you are not prepared to wait for a couple of minutes because this is quite some data to download!

## Other Forms of Data Aggregation

Of course, aggregation across variables other than `symbol`

can also make sense. For instance, suppose you are interested in answering the question: are days with high aggregate trading volume likely followed by days with high aggregate trading volume? To provide some initial analysis on this question, we take the downloaded data and compute aggregate daily trading volume for all Dow Jones constituents in USD. Recall that the column `volume`

is denoted in the number of traded shares. Thus, we multiply the trading volume with the daily closing price to get a proxy for the aggregate trading volume in USD. Scaling by `1e9`

(R can handle scientific notation) denotes daily trading volume in billion USD.

```
<- index_prices |>
volume group_by(date) |>
summarize(volume = sum(volume * close / 1e9))
|>
volume ggplot(aes(x = date, y = volume)) +
geom_line() +
labs(
x = NULL, y = NULL,
title = "Aggregate daily trading volume of DOW index constitutens"
)
```

Figure 4 indicates a clear upward trend in aggregated daily trading volume. In particular, since the outbreak of the COVID-19 pandemic, markets have processed substantial trading volumes, as analyzed, for instance, by Goldstein, Koijen, and Mueller (2021). One way to illustrate the persistence of trading volume would be to plot volume on day \(t\) against volume on day \(t-1\) as in the example below. In Figure 5, we add a dotted 45°-line to indicate a hypothetical one-to-one relation by `geom_abline()`

, addressing potential differences in the axes’ scales.

```
|>
volume ggplot(aes(x = lag(volume), y = volume)) +
geom_point() +
geom_abline(aes(intercept = 0, slope = 1),
linetype = "dashed"
+
) labs(
x = "Previous day aggregate trading volume",
y = "Aggregate trading volume",
title = "Persistence in daily trading volume of DOW index constituents"
)
```

`Warning: Removed 1 rows containing missing values (`geom_point()`).`

Do you understand where the warning `## Warning: Removed 1 rows containing missing values (geom_point).`

comes from and what it means? Purely eye-balling reveals that days with high trading volume are often followed by similarly high trading volume days.

## Portfolio Choice Problems

In the previous part, we show how to download stock market data and inspect it with graphs and summary statistics. Now, we move to a typical question in Finance: how to allocate wealth across different assets optimally. The standard framework for optimal portfolio selection considers investors that prefer higher future returns but dislike future return volatility (defined as the square root of the return variance): the *mean-variance investor* (Markowitz 1952).

An essential tool to evaluate portfolios in the mean-variance context is the *efficient frontier*, the set of portfolios which satisfies the condition that no other portfolio exists with a higher expected return but with the same volatility (the square root of the variance, i.e., the risk), see, e.g., Merton (1972). We compute and visualize the efficient frontier for several stocks. First, we extract each asset’s *monthly* returns. In order to keep things simple, we work with a balanced panel and exclude DOW constituents for which we do not observe a price on every single trading day since the year 2000.

```
<- index_prices |>
index_prices group_by(symbol) |>
mutate(n = n()) |>
ungroup() |>
filter(n == max(n)) |>
select(-n)
<- index_prices |>
returns mutate(month = floor_date(date, "month")) |>
group_by(symbol, month) |>
summarize(price = last(adjusted), .groups = "drop_last") |>
mutate(ret = price / lag(price) - 1) |>
drop_na(ret) |>
select(-price)
```

Here, `floor_date()`

is a function from the `lubridate`

package (Grolemund and Wickham 2011), which provides useful functions to work with dates and times.

Next, we transform the returns from a tidy tibble into a \((T \times N)\) matrix with one column for each of the \(N\) tickers and one row for each of the \(T\) trading days to compute the sample average return vector \[\hat\mu = \frac{1}{T}\sum\limits_{t=1}^T r_t\] where \(r_t\) is the \(N\) vector of returns on date \(t\) and the sample covariance matrix \[\hat\Sigma = \frac{1}{T-1}\sum\limits_{t=1}^T (r_t - \hat\mu)(r_t - \hat\mu)'.\] We achieve this by using `pivot_wider()`

with the new column names from the column `symbol`

and setting the values to `ret`

. We compute the vector of sample average returns and the sample variance-covariance matrix, which we consider as proxies for the parameters of the distribution of future stock returns. Thus, for simplicity, we refer to \(\Sigma\) and \(\mu\) instead of explicitly highlighting that the sample moments are estimates. In later chapters, we discuss the issues that arise once we take estimation uncertainty into account.

```
<- returns |>
returns_matrix pivot_wider(
names_from = symbol,
values_from = ret
|>
) select(-month)
<- cov(returns_matrix)
Sigma <- colMeans(returns_matrix) mu
```

Then, we compute the minimum variance portfolio weights \(\omega_\text{mvp}\) as well as the expected portfolio return \(\omega_\text{mvp}'\mu\) and volatility \(\sqrt{\omega_\text{mvp}'\Sigma\omega_\text{mvp}}\) of this portfolio. Recall that the minimum variance portfolio is the vector of portfolio weights that are the solution to \[\omega_\text{mvp} = \arg\min w'\Sigma w \text{ s.t. } \sum\limits_{i=1}^Nw_i = 1.\] The constraint that weights sum up to one simply implies that all funds are distributed across the available asset universe, i.e., there is no possibility to retain cash. It is easy to show analytically that \(\omega_\text{mvp} = \frac{\Sigma^{-1}\iota}{\iota'\Sigma^{-1}\iota}\), where \(\iota\) is a vector of ones and \(\Sigma^{-1}\) is the inverse of \(\Sigma\).

```
<- ncol(returns_matrix)
N <- rep(1, N)
iota <- solve(Sigma) %*% iota
mvp_weights <- mvp_weights / sum(mvp_weights)
mvp_weights tibble(
average_ret = as.numeric(t(mvp_weights) %*% mu),
volatility = as.numeric(sqrt(t(mvp_weights) %*% Sigma %*% mvp_weights))
)
```

```
# A tibble: 1 × 2
average_ret volatility
<dbl> <dbl>
1 0.00857 0.0314
```

The command `solve(A, b)`

returns the solution of a system of equations \(Ax = b\). If `b`

is not provided, as in the example above, it defaults to the identity matrix such that `solve(Sigma)`

delivers \(\Sigma^{-1}\) (if a unique solution exists).

Note that the *monthly* volatility of the minimum variance portfolio is of the same order of magnitude as the *daily* standard deviation of the individual components. Thus, the diversification benefits in terms of risk reduction are tremendous!

Next, we set out to find the weights for a portfolio that achieves, as an example, three times the expected return of the minimum variance portfolio. However, mean-variance investors are not interested in any portfolio that achieves the required return but rather in the efficient portfolio, i.e., the portfolio with the lowest standard deviation. If you wonder where the solution \(\omega_\text{eff}\) comes from: The efficient portfolio is chosen by an investor who aims to achieve minimum variance *given a minimum acceptable expected return* \(\bar{\mu}\). Hence, their objective function is to choose \(\omega_\text{eff}\) as the solution to \[\omega_\text{eff}(\bar{\mu}) = \arg\min w'\Sigma w \text{ s.t. } w'\iota = 1 \text{ and } \omega'\mu \geq \bar{\mu}.\]

The code below implements the analytic solution to this optimization problem for a benchmark return \(\bar\mu\), which we set to 3 times the expected return of the minimum variance portfolio. We encourage you to verify that it is correct.

```
<- 3 * t(mvp_weights) %*% mu
mu_bar <- as.numeric(t(iota) %*% solve(Sigma) %*% iota)
C <- as.numeric(t(iota) %*% solve(Sigma) %*% mu)
D <- as.numeric(t(mu) %*% solve(Sigma) %*% mu)
E <- as.numeric(2 * (mu_bar - D / C) / (E - D^2 / C))
lambda_tilde <- mvp_weights +
efp_weights / 2 * (solve(Sigma) %*% mu - D * mvp_weights) lambda_tilde
```

## The Efficient Frontier

The mutual fund separation theorem states that as soon as we have two efficient portfolios (such as the minimum variance portfolio \(w_{mvp}\) and the efficient portfolio for a higher required level of expected returns \(\omega_\text{eff}(\bar{\mu})\), we can characterize the entire efficient frontier by combining these two portfolios. That is, any linear combination of the two portfolio weights will again represent an efficient portfolio. The code below implements the construction of the *efficient frontier*, which characterizes the highest expected return achievable at each level of risk. To understand the code better, make sure to familiarize yourself with the inner workings of the `for`

loop.

```
<- seq(from = -0.4, to = 1.9, by = 0.01)
c <- tibble(
res c = c,
mu = NA,
sd = NA
)for (i in seq_along(c)) {
<- (1 - c[i]) * mvp_weights + (c[i]) * efp_weights
w $mu[i] <- 12 * 100 * t(w) %*% mu
res$sd[i] <- 100 * sqrt(12) * sqrt(t(w) %*% Sigma %*% w)
res }
```

The code above proceeds in two steps: First, we compute a vector of combination weights \(c\) and then we evaluate the resulting linear combination with \(c\in\mathbb{R}\):

\[w^* = cw_\text{eff}(\bar\mu) + (1-c)w_{mvp} = \omega_\text{mvp} + \frac{\lambda^*}{2}\left(\Sigma^{-1}\mu -\frac{D}{C}\Sigma^{-1}\iota \right)\] with \(\lambda^* = 2\frac{c\bar\mu + (1-c)\tilde\mu - D/C}{E-D^2/C}\) where \(C = \iota'\Sigma^{-1}\iota\), \(D=\iota'\Sigma^{-1}\mu\), and \(E=\mu'\Sigma^{-1}\mu\). Finally, it is simple to visualize the efficient frontier alongside the two efficient portfolios within one powerful figure using `ggplot2`

(see Figure 6). We also add the individual stocks in the same call. We compute annualized returns based on the simple assumption that monthly returns are independent and identically distributed. Thus, the average annualized return is just 12 times the expected monthly return.

```
|>
res ggplot(aes(x = sd, y = mu)) +
geom_point() +
geom_point(
data = res |> filter(c %in% c(0, 1)),
size = 4
+
) geom_point(
data = tibble(
mu = 12 * 100 * mu,
sd = 100 * sqrt(12) * sqrt(diag(Sigma))
),aes(y = mu, x = sd), size = 1
+
) labs(
x = "Annualized standard deviation (in percent)",
y = "Annualized expected return (in percent)",
title = "Efficient frontier for DOW index constituents"
)
```

The line in Figure 6 indicates the efficient frontier: the set of portfolios a mean-variance efficient investor would choose from. Compare the performance relative to the individual assets (the dots) - it should become clear that diversifying yields massive performance gains (at least as long as we take the parameters \(\Sigma\) and \(\mu\) as given).

## Exercises

- Download daily prices for another stock market ticker of your choice from Yahoo!Finance with
`tq_get()`

from the`tidyquant`

package. Plot two time series of the ticker’s un-adjusted and adjusted closing prices. Explain the differences. - Compute daily net returns for the asset and visualize the distribution of daily returns in a histogram. Also, use
`geom_vline()`

to add a dashed line that indicates the 5 percent quantile of the daily returns within the histogram. Compute summary statistics (mean, standard deviation, minimum and maximum) for the daily returns - Take your code from before and generalize it such that you can perform all the computations for an arbitrary vector of tickers (e.g.,
`ticker <- c("AAPL", "MMM", "BA")`

). Automate the download, the plot of the price time series, and create a table of return summary statistics for this arbitrary number of assets. - Consider the research question: Are days with high aggregate trading volume often also days with large absolute price changes? Find an appropriate visualization to analyze the question.
- Compute monthly returns from the downloaded stock market prices. Compute the vector of historical average returns and the sample variance-covariance matrix. Compute the minimum variance portfolio weights and the portfolio volatility and average returns. Visualize the mean-variance efficient frontier. Choose one of your assets and identify the portfolio which yields the same historical volatility but achieves the highest possible average return.
- In the portfolio choice analysis, we restricted our sample to all assets trading every day since 2000. How is such a decision a problem when you want to infer future expected portfolio performance from the results?
- The efficient frontier characterizes the portfolios with the highest expected return for different levels of risk, i.e., standard deviation. Identify the portfolio with the highest expected return per standard deviation. Hint: the ratio of expected return to standard deviation is an important concept in Finance.

## References

*tidyquant: Tidy quantitative financial analysis*. https://CRAN.R-project.org/package=tidyquant.

*Review of Financial Studies*34 (11): 5135–48. https://doi.org/10.1093/rfs/hhab085.

*Journal of Statistical Software*40 (3): 1–25. https://www.jstatsoft.org/v40/i03/.

*The Journal of Finance*7 (1): 77–91. https://doi.org/10.1111/j.1540-6261.1952.tb01525.x.

*Journal of Financial and Quantitative Analysis*7 (4): 1851–72. https://doi.org/10.2307/2329621.

*Analysis of financial time series*. John Wiley & Sons.

*ggplot2: Elegant graphics for data analysis*. Springer-Verlag New York. https://ggplot2.tidyverse.org.

*Journal of Open Source Software*4 (43): 1686. https://doi.org/10.21105/joss.01686.

*The grammar of graphics*. Springer.