Reproducible Finance, the book! And a discount for our readers

[This article was first published on R Views, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I’m thrilled to announce the release of my new book Reproducible Finance with R: Code Flows and Shiny Apps for Portfolio Analysis, which originated as a series of R Views posts in this space. The first post was written way back in November of 2016 – thanks to all the readers who have supported us along the way!

If you are familiar with the R Views posts, then you probably have a pretty good sense for the book’s style, prose, and code approach, but I’d like to add a few quick words of background.

The book’s practical motivations are: (1) to introduce R to finance professionals, or aspiring finance professionals, who wish to move beyond Excel for their quantitative work, and (2) to introduce various finance coding paradigms to R coders.

The softer motivation is to demonstrate and emphasize readable, reusable, and reproducible R code, data visualizations, and Shiny dashboards. It will be very helpful to have some background in the R programming language or in finance, but the most important thing is a desire to learn about the landscape of R code and finance packages.

An overarching goal of the book is to introduce the three major R paradigms for portfolio analysis: xts, the tidyverse, and tidyquant. As a result, we will frequently run the same analysis using three different code flows.

If that ‘three-universe’ structure seems a bit unclear, have a quick look back at this post on skewness and this post on kurtosis, and you’ll notice that we solve the same task and get the same result with different code paths.

For example, if we had portfolio returns saved in a tibble object called portfolio_returns_tq_rebalanced_monthly, and an xts object called portfolio_returns_xts_rebalanced_monthly, and our goal was to find the Sharpe Ratio of portfolio returns, we would start in the xts world with SharpeRatio() from the PerformanceAnalytics package.

# define risk free rate variable
rfr <- .0003

sharpe_xts <- 
  SharpeRatio(portfolio_returns_xts_rebalanced_monthly, 
              Rf = rfr,
              FUN = "StdDev") %>% 
  `colnames<-`("sharpe_xts")

sharpe_xts
##                               sharpe_xts
## StdDev Sharpe (Rf=0%, p=95%):  0.2748752

We next would use the tidyverse and run our calculations in a piped flow:

sharpe_tidyverse_byhand <- 
  portfolio_returns_tq_rebalanced_monthly %>% 
  summarise(sharpe_dplyr = mean(returns - rfr)/
              sd(returns - rfr))

sharpe_tidyverse_byhand
## # A tibble: 1 x 1
##   sharpe_dplyr
##          <dbl>
## 1        0.275

And then head to the tidyquant paradigm where we can apply the SharpeRatio() function to a tidy tibble.

sharpe_tq <- 
  portfolio_returns_tq_rebalanced_monthly %>%
  tq_performance(Ra = returns,
                 performance_fun = SharpeRatio,
                 Rf = rfr,
                 FUN = "StdDev") %>%
  `colnames<-`("sharpe_tq")

We can compare our three Sharpe objects and confirm consistent results.

sharpe_tq %>% 
  mutate(tidy_sharpe = sharpe_tidyverse_byhand$sharpe_dplyr,
         xts_sharpe = sharpe_xts)
## # A tibble: 1 x 3
##   sharpe_tq tidy_sharpe xts_sharpe
##       <dbl>       <dbl>      <dbl>
## 1     0.275       0.275      0.275

We might be curious how the Sharpe-Ratio-to-standard-deviation ratio of our portfolio compares to those of the component ETFs and a ggplot scatter is a nice way to visualize that.

asset_returns_long %>%
  na.omit() %>% 
  summarise(stand_dev = sd(returns),
            sharpe = mean(returns - rfr)/
              sd(returns - rfr)) %>% 
  add_row(asset = "Portfolio",
    stand_dev =
     portfolio_sd_xts_builtin[1],
    sharpe =
      sharpe_tq$sharpe_tq) %>%
  ggplot(aes(x = stand_dev,
             y = sharpe,
             color = asset)) +
  geom_point(size = 2) +
  geom_text(
   aes(x =
    sd(portfolio_returns_tq_rebalanced_monthly$returns),
     y =
    sharpe_tq$sharpe_tq + .02,
         label = "Portfolio")) +
  ylab("Sharpe Ratio") +
  xlab("standard deviation") +
  ggtitle("Sharpe Ratio versus Standard Deviation") +
  # The next line centers the title
  theme_update(plot.title = element_text(hjust = 0.5))
Sharpe versus Standard Deviation

Figure 1: Sharpe versus Standard Deviation

Finally, we are ready to calculate and visualize the Sharpe Ratio of a custom portfolio with Shiny and a flexdashboard, like the one found here.

As in the above example, most tasks in the book end with data visualization and then Shiny (a few early readers have commented with happy surprise that all the charts and code are in full color in the book - thanks to CRC press for making that happen!). Data visualization and Shiny are heavily emphasized - more so than in other finance books - and that might seem unusual. After all, every day we hear about how the financial world is becoming more quantitatively driven as firms race towards faster, more powerful algorithms. Why emphasize good ol’ data visualization? I believe, and have observed first-hand, that the ability to communicate and tell the story of data in a compelling way is only going to become more crucial as the financial world becomes more complex. Investors, limited partners, portfolio managers, clients, risk managers - they might not want to read our code or see our data, but we still need to communicate to them the value of our work. Data visualization and Shiny dashboards are a great way to do that. By book’s end, a reader will have built a collection of live, functioning flexdashboards that can be the foundation for more complex apps in the future.

If you’ve read this far, good news! Between now and December 31, 2018, there’s a 20% discount on the book being run at CRC, and if you don’t see it applied, readers can use discount code SS120 on the CRC website. The book is also available on Amazon as Kindle or paperback (but there’s only than 10 copies left as of right now).

Thanks so much for reading, and happy coding!

To leave a comment for the author, please follow the link and comment on their blog: R Views.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)