<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Finance Book on R Views</title>
    <link>https://rviews.rstudio.com/tags/finance-book/</link>
    <description>Recent content in Finance Book on R Views</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 29 Oct 2018 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://rviews.rstudio.com/tags/finance-book/" rel="self" type="application/rss+xml" />
    
    
    
    
    <item>
      <title>Reproducible Finance, the book! And a discount for our readers</title>
      <link>https://rviews.rstudio.com/2018/10/29/reproducible-finance-the-book/</link>
      <pubDate>Mon, 29 Oct 2018 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2018/10/29/reproducible-finance-the-book/</guid>
      <description>
        


&lt;p&gt;I’m thrilled to announce the release of my new book &lt;a href=&#34;https://www.crcpress.com/Reproducible-Finance-with-R-Code-Flows-and-Shiny-Apps-for-Portfolio-Analysis/Regenstein-Jr/p/book/9781138484030&#34;&gt;Reproducible Finance with R: Code Flows and Shiny Apps for Portfolio Analysis&lt;/a&gt;, which originated as a series of R Views posts in this space. The &lt;a href=&#34;https://rviews.rstudio.com/2016/11/09/reproducible-finance-with-r-the-sharpe-ratio/&#34;&gt;first post&lt;/a&gt; was written way back in November of 2016 - thanks to all the readers who have supported us along the way!&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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 &lt;em&gt;or&lt;/em&gt; in finance, but the most important thing is a desire to learn about the landscape of R code and finance packages.&lt;/p&gt;
&lt;p&gt;An overarching goal of the book is to introduce the three major R paradigms for portfolio analysis: &lt;code&gt;xts&lt;/code&gt;, the &lt;code&gt;tidyverse&lt;/code&gt;, and &lt;code&gt;tidyquant&lt;/code&gt;. As a result, we will frequently run the same analysis using three different code flows.&lt;/p&gt;
&lt;p&gt;If that ‘three-universe’ structure seems a bit unclear, have a quick look back at &lt;a href=&#34;https://rviews.rstudio.com/2017/12/13/introduction-to-skewness/&#34;&gt;this post on skewness&lt;/a&gt; and &lt;a href=&#34;https://rviews.rstudio.com/2018/01/04/introduction-to-kurtosis/&#34;&gt;this post on kurtosis&lt;/a&gt;, and you’ll notice that we solve the same task and get the same result with different code paths.&lt;/p&gt;
&lt;p&gt;For example, if we had portfolio returns saved in a tibble object called &lt;code&gt;portfolio_returns_tq_rebalanced_monthly&lt;/code&gt;, and an &lt;code&gt;xts&lt;/code&gt; object called &lt;code&gt;portfolio_returns_xts_rebalanced_monthly&lt;/code&gt;, and our goal was to find the Sharpe Ratio of portfolio returns, we would start in the &lt;code&gt;xts&lt;/code&gt; world with &lt;code&gt;SharpeRatio()&lt;/code&gt; from the &lt;code&gt;PerformanceAnalytics&lt;/code&gt; package.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# define risk free rate variable
rfr &amp;lt;- .0003

sharpe_xts &amp;lt;- 
  SharpeRatio(portfolio_returns_xts_rebalanced_monthly, 
              Rf = rfr,
              FUN = &amp;quot;StdDev&amp;quot;) %&amp;gt;% 
  `colnames&amp;lt;-`(&amp;quot;sharpe_xts&amp;quot;)

sharpe_xts&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##                               sharpe_xts
## StdDev Sharpe (Rf=0%, p=95%):  0.2748752&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We next would use the tidyverse and run our calculations in a piped flow:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;sharpe_tidyverse_byhand &amp;lt;- 
  portfolio_returns_tq_rebalanced_monthly %&amp;gt;% 
  summarise(sharpe_dplyr = mean(returns - rfr)/
              sd(returns - rfr))

sharpe_tidyverse_byhand&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # A tibble: 1 x 1
##   sharpe_dplyr
##          &amp;lt;dbl&amp;gt;
## 1        0.275&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then head to the &lt;code&gt;tidyquant&lt;/code&gt; paradigm where we can apply the &lt;code&gt;SharpeRatio()&lt;/code&gt; function to a tidy tibble.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;sharpe_tq &amp;lt;- 
  portfolio_returns_tq_rebalanced_monthly %&amp;gt;%
  tq_performance(Ra = returns,
                 performance_fun = SharpeRatio,
                 Rf = rfr,
                 FUN = &amp;quot;StdDev&amp;quot;) %&amp;gt;%
  `colnames&amp;lt;-`(&amp;quot;sharpe_tq&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can compare our three Sharpe objects and confirm consistent results.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;sharpe_tq %&amp;gt;% 
  mutate(tidy_sharpe = sharpe_tidyverse_byhand$sharpe_dplyr,
         xts_sharpe = sharpe_xts)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # A tibble: 1 x 3
##   sharpe_tq tidy_sharpe xts_sharpe
##       &amp;lt;dbl&amp;gt;       &amp;lt;dbl&amp;gt;      &amp;lt;dbl&amp;gt;
## 1     0.275       0.275      0.275&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We might be curious how the Sharpe-Ratio-to-standard-deviation ratio of our portfolio compares to those of the component ETFs and a &lt;code&gt;ggplot&lt;/code&gt; scatter is a nice way to visualize that.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;asset_returns_long %&amp;gt;%
  na.omit() %&amp;gt;% 
  summarise(stand_dev = sd(returns),
            sharpe = mean(returns - rfr)/
              sd(returns - rfr)) %&amp;gt;% 
  add_row(asset = &amp;quot;Portfolio&amp;quot;,
    stand_dev =
     portfolio_sd_xts_builtin[1],
    sharpe =
      sharpe_tq$sharpe_tq) %&amp;gt;%
  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 = &amp;quot;Portfolio&amp;quot;)) +
  ylab(&amp;quot;Sharpe Ratio&amp;quot;) +
  xlab(&amp;quot;standard deviation&amp;quot;) +
  ggtitle(&amp;quot;Sharpe Ratio versus Standard Deviation&amp;quot;) +
  # The next line centers the title
  theme_update(plot.title = element_text(hjust = 0.5))&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&#34;figure&#34;&gt;&lt;span id=&#34;fig:unnamed-chunk-5&#34;&gt;&lt;/span&gt;
&lt;img src=&#34;/post/2018-10-22-reproducible-finance-the-book_files/figure-html/unnamed-chunk-5-1.png&#34; alt=&#34;Sharpe versus Standard Deviation&#34; width=&#34;672&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;
Figure 1: Sharpe versus Standard Deviation
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Finally, we are ready to calculate and visualize the Sharpe Ratio of a custom portfolio with Shiny and a flexdashboard, like the one found &lt;a href=&#34;http://www.reproduciblefinance.com/shiny/sharpe-ratio/&#34;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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 &lt;a href=&#34;https://crcpress.com/Reproducible-Finance-with-R-Code-Flows-and-Shiny-Apps-for-Portfolio-Analysis/Jr/p/book/9781138484030&#34;&gt;CRC&lt;/a&gt;, and if you don’t see it applied, readers can use discount code SS120 on the &lt;a href=&#34;https://crcpress.com/Reproducible-Finance-with-R-Code-Flows-and-Shiny-Apps-for-Portfolio-Analysis/Jr/p/book/9781138484030&#34;&gt;CRC website&lt;/a&gt;. The book is also available on &lt;a href=&#34;https://www.amazon.com/Reproducible-Finance-Portfolio-Analysis-Chapman/dp/1138484032&#34;&gt;Amazon as Kindle or paperback&lt;/a&gt; (but there’s only than 10 copies left as of right now).&lt;/p&gt;
&lt;p&gt;Thanks so much for reading, and happy coding!&lt;/p&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2018/10/29/reproducible-finance-the-book/&#39;;&lt;/script&gt;
      </description>
    </item>
    
  </channel>
</rss>
