In this tutorial, we will learn how to compute annualized return of stock using tidyverse from scratch. Annualized return of an investment is the average returns of the investment that is held over many years. In comparison to simple mean of yearly returns, annualized return is a geometric mean and it accounts for the compounding over time.
Let us compute the annualized return of Nvidia stock, a hottest stock in the recent years. We will use tidyquant package to download stock price data for Nvidia over time.
library(tidyquant) library(tidyverse)
Fetch Stock Price Data
We can use tidyquant’s tq_get() function to obtain stock price data over a specific interval for the Nvidia stock. In this example we get the data for four years from 2020 to 2023.
stock_prices <- c("NVDA") %>% tq_get(get = "stock.prices", from = "2020-01-01", to = "2023-12-31") stock_prices # A tibble: 1,006 × 8 symbol date open high low close volume adjusted <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 1 NVDA 2020-01-02 5.97 6.00 5.92 6.00 237536000 5.97 2 NVDA 2020-01-03 5.88 5.95 5.85 5.90 205384000 5.88 3 NVDA 2020-01-06 5.81 5.93 5.78 5.93 262636000 5.90 4 NVDA 2020-01-07 5.95 6.04 5.91 6.00 314856000 5.97 5 NVDA 2020-01-08 5.99 6.05 5.95 6.01 277108000 5.99 6 NVDA 2020-01-09 6.10 6.15 6.02 6.08 255112000 6.05 7 NVDA 2020-01-10 6.18 6.21 6.09 6.11 316296000 6.08 8 NVDA 2020-01-13 6.19 6.32 6.17 6.30 319840000 6.27 9 NVDA 2020-01-14 6.26 6.28 6.17 6.18 359088000 6.16 10 NVDA 2020-01-15 6.19 6.22 6.11 6.14 263104000 6.11 # ℹ 996 more rows
Compute yearly returns of an investment
Let us compute yearly return, that uses stock price at the begining and the end of each year. Again we use tidyquant’s tq_transmute() function to compute the yearly returns of Nvidia stock.
stock_returns_yearly <- stock_prices %>% tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "yearly", col_rename = "yearly_returns") stock_returns_yearly # A tibble: 4 × 2 date yearly_returns <date> <dbl> 1 2020-12-31 1.18 2 2021-12-31 1.25 3 2022-12-30 -0.503 4 2023-12-29 2.39
We can see that we have yearly returns for the years 2020,2021,2022, and 2023. Among the four years, Nvida had negative return during 2022. And we can quickly visualize the returns as a bar graph.
stock_returns_yearly %>% mutate(return=ifelse(yearly_returns>0, "+ve", "-ve")) %>% ggplot(aes(x=date, y=yearly_returns, fill=return)) + geom_col()+ scale_y_continuous(labels=scales::percent, breaks=scales::breaks_pretty(n=5))+ theme(legend.position = "none")+ labs(title="NVIDIA: Yearly Returns") ggsave("NVDA_absoulte_return_yearly.png")
Compute Annualized Return
As mentioned before, to compute annualized return we need the number of years an investment is held and its return for each year. We can use the formula for annualized return shown above and compute using prod() function as shown below.
stock_returns_yearly %>% summarise(annualized_return = prod((1+yearly_returns))^(1/n()) - 1) # A tibble: 1 × 1 annualized_return <dbl> 1 0.697
We get the the annualized return for Nvidia stock help from the beginning of 2020 to the end of 2023 is 69.7%.
Here we show each intermediate step of computing annualized return by adding the intermediate results as a column. And we get the same answer as expected.
stock_returns_yearly %>% mutate(s1 = (1+yearly_returns)) %>% mutate(s2 = prod(s1)) %>% mutate(s3 = s2^(1/n())) %>% mutate(ann_return = s3 - 1) # A tibble: 4 × 6 date yearly_returns s1 s2 s3 ann_return <date> <dbl> <dbl> <dbl> <dbl> <dbl> 1 2020-12-31 1.18 2.18 8.29 1.70 0.697 2 2021-12-31 1.25 2.25 8.29 1.70 0.697 3 2022-12-30 -0.503 0.497 8.29 1.70 0.697 4 2023-12-29 2.39 3.39 8.29 1.70 0.697