Reproducible Finance, the book!

Reproducible Finance, the book!

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 blog posts. 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 blog 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 at this post on portfolio returns, 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

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 8 copies left as of right now) .

Thanks so much for reading, and happy coding!

Skot Kortje

Equity market Analyst and Content creator, Portfolio analyst, Systematic trading publisher

6 年

Congratulations, Jonathan! I've appreciated and benefited greatly from your Reproducible Finance posts over the years. This is great news. All the best!

回复
John Edward Gore, CFA

Better reckoning, with machines

6 年

looking forward to it JR your work is always great and kudos too to Rstudio

回复
Nima Safaian

Commercial Strategy | Commodities Trading & Risk | Advanced Analytics

6 年

Congrats Jonathan

回复

要查看或添加评论,请登录

Jonathan Regenstein的更多文章

  • If you want to understand how to succeed in the hedge fund world, read these two books

    If you want to understand how to succeed in the hedge fund world, read these two books

    Happy holidays to all! I wanted to share brief thoughts on two excellent books that found their way to my book stack…

    10 条评论
  • Momentum investing with R

    Momentum investing with R

    After an extended hiatus, Reproducible Finance is back! We’ll celebrate by changing focus a bit and coding up an…

  • Some Sector Analysis with R

    Some Sector Analysis with R

    Welcome to the second installment of Reproducible Finance 2019! In the previous post, we looked back on the daily…

    2 条评论
  • Looking back on 2018 Reproducible Finance with R part 1

    Looking back on 2018 Reproducible Finance with R part 1

    Welcome to Reproducible Finance 2019! It’s a new year, a new beginning, the Earth has completed one more trip around…

  • Fund Flows via the FT

    Fund Flows via the FT

    There’s a great piece on the FT today about algos and fund flows. It’s author, Robin Wigglesworth, has become one of my…

    3 条评论
  • Rolling Origin Sampling

    Rolling Origin Sampling

    Today, we continue our work on sampling so that we can run models on subsets of our data and then test the accuracy of…

    2 条评论
  • Rsampling Fama French + Quant Finance Contest

    Rsampling Fama French + Quant Finance Contest

    Today we will continue our work on Fama French factor models, but more as a vehicle to explore some of the awesome…

  • Fama French with R: Managing Multiple Models + Cyber Monday Book Discount

    Fama French with R: Managing Multiple Models + Cyber Monday Book Discount

    Today, we will return to the Fama French (FF) model of asset returns and use it as a proxy for fitting and evaluating…

    1 条评论
  • Visualizing GDP with R

    Visualizing GDP with R

    Today we will take a look at the GDP data that is released every quarter or so by the Bureau of Economic Analysis BEA…

  • Highcharting Jobs Friday

    Highcharting Jobs Friday

    Today, in honor of last week’s jobs report from the Bureau of Labor Statistics (BLS), we will visualize jobs data with…

社区洞察

其他会员也浏览了