NYC Data Science Academy| Blog
Bootcamps
Lifetime Job Support Available Financing Available
Bootcamps
Data Science with Machine Learning Flagship ๐Ÿ† Data Analytics Bootcamp Artificial Intelligence Bootcamp New Release ๐ŸŽ‰
Free Lesson
Intro to Data Science New Release ๐ŸŽ‰
Find Inspiration
Find Alumni with Similar Background
Job Outlook
Occupational Outlook Graduate Outcomes Must See ๐Ÿ”ฅ
Alumni
Success Stories Testimonials Alumni Directory Alumni Exclusive Study Program
Courses
View Bundled Courses
Financing Available
Bootcamp Prep Popular ๐Ÿ”ฅ Data Science Mastery Data Science Launchpad with Python View AI Courses Generative AI for Everyone New ๐ŸŽ‰ Generative AI for Finance New ๐ŸŽ‰ Generative AI for Marketing New ๐ŸŽ‰
Bundle Up
Learn More and Save More
Combination of data science courses.
View Data Science Courses
Beginner
Introductory Python
Intermediate
Data Science Python: Data Analysis and Visualization Popular ๐Ÿ”ฅ Data Science R: Data Analysis and Visualization
Advanced
Data Science Python: Machine Learning Popular ๐Ÿ”ฅ Data Science R: Machine Learning Designing and Implementing Production MLOps New ๐ŸŽ‰ Natural Language Processing for Production (NLP) New ๐ŸŽ‰
Find Inspiration
Get Course Recommendation Must Try ๐Ÿ’Ž An Ultimate Guide to Become a Data Scientist
For Companies
For Companies
Corporate Offerings Hiring Partners Candidate Portfolio Hire Our Graduates
Students Work
Students Work
All Posts Capstone Data Visualization Machine Learning Python Projects R Projects
Tutorials
About
About
About Us Accreditation Contact Us Join Us FAQ Webinars Subscription An Ultimate Guide to
Become a Data Scientist
    Login
NYC Data Science Acedemy
Bootcamps
Courses
Students Work
About
Bootcamps
Bootcamps
Data Science with Machine Learning Flagship
Data Analytics Bootcamp
Artificial Intelligence Bootcamp New Release ๐ŸŽ‰
Free Lessons
Intro to Data Science New Release ๐ŸŽ‰
Find Inspiration
Find Alumni with Similar Background
Job Outlook
Occupational Outlook
Graduate Outcomes Must See ๐Ÿ”ฅ
Alumni
Success Stories
Testimonials
Alumni Directory
Alumni Exclusive Study Program
Courses
Bundles
financing available
View All Bundles
Bootcamp Prep
Data Science Mastery
Data Science Launchpad with Python NEW!
View AI Courses
Generative AI for Everyone
Generative AI for Finance
Generative AI for Marketing
View Data Science Courses
View All Professional Development Courses
Beginner
Introductory Python
Intermediate
Python: Data Analysis and Visualization
R: Data Analysis and Visualization
Advanced
Python: Machine Learning
R: Machine Learning
Designing and Implementing Production MLOps
Natural Language Processing for Production (NLP)
For Companies
Corporate Offerings
Hiring Partners
Candidate Portfolio
Hire Our Graduates
Students Work
All Posts
Capstone
Data Visualization
Machine Learning
Python Projects
R Projects
About
Accreditation
About Us
Contact Us
Join Us
FAQ
Webinars
Subscription
An Ultimate Guide to Become a Data Scientist
Tutorials
Data Analytics
  • Learn Pandas
  • Learn NumPy
  • Learn SciPy
  • Learn Matplotlib
Machine Learning
  • Boosting
  • Random Forest
  • Linear Regression
  • Decision Tree
  • PCA
Interview by Companies
  • JPMC
  • Google
  • Facebook
Artificial Intelligence
  • Learn Generative AI
  • Learn ChatGPT-3.5
  • Learn ChatGPT-4
  • Learn Google Bard
Coding
  • Learn Python
  • Learn SQL
  • Learn MySQL
  • Learn NoSQL
  • Learn PySpark
  • Learn PyTorch
Interview Questions
  • Python Hard
  • R Easy
  • R Hard
  • SQL Easy
  • SQL Hard
  • Python Easy
Data Science Blog > Student Works > Asset Allocations Backtesting Using R Shiny

Asset Allocations Backtesting Using R Shiny

Joe Lu
Posted on Mar 7, 2020
The skills I demoed here can be learned through taking Data Science with Machine Learning bootcamp with NYC Data Science Academy.

Section 1 - Introduction

 O ver the past 100 years, the U.S. stock asset has averaged approximately 9-10% annually of assets.  This rate of return has been relatively stable over rolling 30-40 year time horizons (the standard investment horizon of a working professional in the United States).  Due to the amazing power of compounding over long periods of time, this would have turned $1,000 into a staggering $24,000 (assuming 9.5% compounding over 35 years).  Below is a simple demonstrate of the power of compounding:

Therefore, investing in the stock market's nearly double-digit returns over long periods of time is one of the greatest fountains of building wealth over one's adult life and achieving economic stability and financial independence.  And today, with the plethora of low-cost index funds (often 0.1% or less annually) that track the general stock market, combined with zero transaction fees (for buying / selling) at large brokerage shops such as BlackRock, Fidelity, Schwab, State Street, and Vanguard, it is easier than ever to access this goose that lays golden eggs.  (The listed ETFs below all cost below 0.1% in management fees, for example)

However, while this high rate of return is remarkably stable around 9-10% over long periods of time, over shorter periods of time, the stock market is notoriously volatile.  Worse yet, returns are skewed to downside: a really good year in the stock market returns +25%, compared to a really bad year, which returns -40%.  For example, during the past 20 years, we had the misfortune of experiencing two stock market crashes (the dot-com burst of 2001-2002) and the Great Financial Crisis of 2008, both of which almost cut the stock market index in half.

For those nearing retirement, without the longer time horizon to recover from such devastating losses, or for those about to make a large purchase, such as buying a new home to start a family, these kinds of crashes would completely ruin one's financial planning and lifestyle.  Even those without such constraints rarely have the nerves to tolerate a 50% hit to his/her portfolio, and as such, very few people allocate 100% of their investments into stocks, even if they are young.

In this post, I combine the highly effective and powerful data visualizations of R Shiny with my domain knowledge of the financial industry, to create a web application that helps investors choose a portfolio of appropriate target return + risk by backtesting the portfolio allocation over a time period of choice from the past 20 years.  The key takeaway is that this data set steps through the Great Financial Crisis of 2008, allowing us to stress-test how a portfolio allocation would have fared through such a challenging environment.  And during this process, we will see how holding a portfolio of different asset classes stabilizes one's portfolio elegantly through any economic environment, and produces a high risk-adjusted return (risk/reward ratio).

The underlying dataset behind this R Shiny app is the history of daily total returns (price changes + reinvested dividends / coupons) for seven main asset classes over the period of Jan 2002 to Jan 2020:

  1. Large cap U.S. stocks (Tickers: SPY, VOO, IVV, SPLG)
  2. Developed market stocks from regions such as Western Europe, Japan, Canada, Australia, etc (Tickers: VEA, IEFA, IDEV)
  3. Emerging market stocks from regions such as China, Brazil, South Africa, etc (Tickers: IEMG, SCHE, SPEM, VWO)
  4. Real estate investment trusts (Tickers: VNQ, IYR)
  5. 20+ Year Maturity U.S. Government Bonds (Tickers: SPTL, TLT, VGLT)
  6. Gold (Tickers: GLD, IAU)
  7. Energy stocks (Ticker: XLE)

For the sake of simplicity, and the fact that pro-growth assets (U.S. stocks, developed market stocks, EM stocks, energy sector stocks) have high correlation, we will focus mainly on U.S. Stocks, Long Maturity U.S. Government Bonds (a.k.a. Treasuries), and Gold.

Section 2 - Choosing Date and End Date

We are going to demonstrate this app by choosing Jan 2003 to Jan 2020 as our time frame. 

This changes which slice (subset) of the data that we take, which propagates through all of the next few sections of this web app and changes all of the resulting graphs and tables.

Section 3 - Distribution of Daily Returns

In this tab, we present our first analysis of how the daily returns of different assets are distributed

The most relevant assets to analyze are: U.S. stocks, U.S. Treasuries, and gold below.

The most obvious take-away is that stocks have a much more volatile distribution of returns than Treasuries.  A really bad day in Treasuries is a 2-4% loss in price terms, while a really bad day in stocks is a 5-10% loss.  Do stocks compensate for this much higher risk by providing higher returns in the long-run?  The next section answers this.

Section 4 - Time Series Plot

The table at the top of this section shows that emerging markets stocks have produced enormous returns in the past 17 years, with a very high fluctuation (vol) along the way, followed by U.S. stocks, which showed both lower returns and lower risk.  Conversely, traditionally "safe haven" assets such as gold and Treasuries showed lower returns and lower risks.

While stocks produce higher returns in the long-run in exchange for their higher risk, there is no consolation prize for an individual investor if the loss in stocks is as high as 10% in a single day and almost 50% in a year.  However, just a casual glance at the graphs reveals that there are definitely large intervals of time where Treasuries and gold moved in non-correlated, or even opposite, directions from stocks.  Given this, can we use other assets to offset the losses in U.S. stocks during really bad days for the stock market (in exchange for somewhat lower returns)?

Section 5 - Scatterplots

 

The very strong positive correlation between E.M. stocks (returns of shares of companies in China, Brazil, India, Mexico, South Africa, etc) vs. domestic shares in the U.S. shows just how well-connected the global economy is, and how owning non-U.S. stocks would not shield your portfolio from losses during a recession or other type of adverse shock.

Gold does a better job.  On average, gold has close to zero correlation over long periods of time with respect to high-return-high-risk assets such as stocks and real estate.

But as this graph shows, long-duration government bonds, particularly Treasuries (from the U.S.), gilts (from the U.K.), bunds (from Germany), and Japanese government bonds are inversely correlated with stock returns.  This is because long-tenor interest rates (10 year maturity and beyond) drop (causing bond prices to rise) during periods of slowing economic growth, precisely when stocks tend to fall in price.

Section 6 - Correlation Matrix

In this section, we see a more visual representation of the correlations between these 7 types of assets.  The blue represents positive correlation while the red represents negative correlation (while white represents correlation of zero)

The blue section in the upper-left shows that essentially all of these assets (U.S. stocks, developed market stocks, E.M. stocks, U.S. real estate, and energy sector stocks) are highly positively correlated.  Gold has close to zero correlation with everything else.  Treasuries have a negative correlation compared to stocks and real estate.  Given this behavior, how would different combinations (percentage allocation) of stocks vs. Treasuries look in terms of the risk and return profile?  We find out in the next section.

Section 7 - Efficient-Market Frontier (2 Assets)

This section graphically shows the risk (vol) on the x-axis and return on the y-axis, for all of the possible allocations in a stock + Treasury portfolio, ranging from 100% Treasuries (the point on the bottom-center) to a portfolio with 100% stocks (the point on the top-right).

This shows that if we have a portfolio with mostly Treasuries, we can actually decrease the volatility (moving leftward) while increasing the expected return (moving upward) by adding stocks, given that stocks are both inversely correlated to Treasuries and higher returning than Treasuries.  We can continue to do this until we move to the blue area, where the ratio between return vs. risk is the best (highest return for least risk).  However, if we continue to increase our return by adding more stocks, beyond a certain point (around 40-50% stocks), when the allocation of a portfolio towards stocks is too high, the volatility starts to creep back up (move rightward).

We would never pick a sub-optimal point below the blue area because we can always increase our return while lowering risk by adding more stocks / lightening up on the Treasuries.  Most investors would pick an area above the blue zone, by deciding how much risk they want to take on.

This is the classic efficient-market frontier from finance textbooks, plotted out using real data points from 2003 to 2020.

Section 8 - Efficient Market Frontier (3 Assets)

If we increase our choice of assets to 3 (U.S. stocks, Treasuries, and gold), the efficient market frontier becomes similar to a crescent-moon shape.

The efficient area would be portfolio allocations in the top left, where we can maximize our return while minimizing our variance

The lesson from these graphical representations of efficient portfolio allocations is: we should increase our allocation of stocks in our portfolio, until the vol-decreasing-return-increasing trends stops, and we need to start deciding how much risk we want to take on in exchange for how much return we gain.  In the next section, we can find exactly where this point (of minimum variance) is, using data from our example of 2003 to 2020, with our 7 assets

Section 9 - The minimum variance (vol) portfolio

Solving for the allocations in a portfolio that gives us the minimum volatility, subject to the fact that their weights need to add up to 100%, is a convex optimization problem that has a unique (one and only one) solution: 55% Treasuries, 15% gold, and 30% risky assets (stocks).

Despite the fact that there are portfolio allocations with more stocks that have higher returns, this min-var portfolio did really well between 2003-2020, with a low volatility: 8.2% annualized return and an annualized vol of 7%

On a daily returns basis, the volatility is really low: even on a really, really, bad day, our min-var portfolio loss would have been about 2-3% (compared to 5-10% in a pure stock portfolio)

At this point, we have graphically illustrated several concepts, using real data from 2003 to 2020:

  • Stocks have phenomenally high returns, but also high risk (volatility)
  • U.S. Treasuries have inverse correlation to stocks over the long-term, while gold has close to zero correlation with stocks.
  • Thus, adding U.S. Treasuries and gold to our stock portfolio can substantially reduce the risk of our portfolio, without giving up too much in returns
  • We should always choose an allocation where adding even more stocks will increase both risk and return.  There are allocations where we don't have enough stocks, where adding more stocks can increase return while reducing risk; and we should never pick those points.  Usually, for two-asset portfolios (U.S. stocks + long-duration Treasuries), this crossover happens around 40-50% U.S. stocks.
  • A minimum variance portfolio is approximately 55% long-duration Treasuries, 30% stocks, and 15% gold.  This min-var allocation varies based on time-period, but almost never exceeds 50% for stocks and there is always a 5-20% allocation to gold

It's time to design our own portfolio and to play around with the risk-reward profile for any portfolio of our own choice.

Section 10 - Customize Your Own Portfolio

In this section, we can design our own portfolio by picking allocations of these 7 assets which add up to 100%.  For instance, we will test this allocation:

Using R's powerful dplyr and ggplot2 libraries, we instantly see the profile of this portfolio allocation:

Using this tool, we can back-test and visualize the performance of any portfolio using real data, and send it through one of the worst financial crises of the U.S. to check its resilience.  As a result, we can tweak the portfolio allocations until the risk-reward is suitable for the end user, making this a simple and yet powerful supplement for any investor at any stage of their investment journey.

Section 11 - About the Author

Background

  • NYC Data Science Academy
  • Seven Years of Front Office Financial Industry Experience
  • M.S. Computational Finance, Carnegie Mellon
  • B.A. Mathematics & Economics, Cornell University

Contact

  • GitHub: https://github.com/jzl4/
  • Linkedin: https://www.linkedin.com/in/joe-lu-44945114/
  • Email: Joe.Zhou.Lu@gmail.com

Tools Used

  • R: Shiny Dashboard, dplyr, tidyr, ggplot2, quadprog, corrplot
  • Data from: yahoo finance
  • References: Modern Portfolio Theory / Capital Asset Pricing Model, Risk-Parity / All-Weather Portfolio Theory (particularly AQR and Bridgewater)

Sources:

  • BlackRock / iShares ETFs
  • Fidelity ETFs
  • Schwab ETFs
  • State Street ETFs
  • Vanguard ETFs
  • ETF.com
  • Macro Trends
  • https://www.nerdwallet.com/blog/investing/average-stock-market-return/
  • https://www.fool.com/investing/general/2016/04/22/how-have-stocks-fared-the-last-50-years-youll-be-s.aspx

About Author

Joe Lu

I am a data scientist with 4 years of modeling experience, and 7+ years of financial industry experience. I have a passion for leveraging the power of data science to solve business challenges. At Fidelity, I have upgraded,...
View all posts by Joe Lu >

Leave a Comment

No comments found.

View Posts by Categories

All Posts 2399 posts
AI 7 posts
AI Agent 2 posts
AI-based hotel recommendation 1 posts
AIForGood 1 posts
Alumni 60 posts
Animated Maps 1 posts
APIs 41 posts
Artificial Intelligence 2 posts
Artificial Intelligence 2 posts
AWS 13 posts
Banking 1 posts
Big Data 50 posts
Branch Analysis 1 posts
Capstone 206 posts
Career Education 7 posts
CLIP 1 posts
Community 72 posts
Congestion Zone 1 posts
Content Recommendation 1 posts
Cosine SImilarity 1 posts
Data Analysis 5 posts
Data Engineering 1 posts
Data Engineering 3 posts
Data Science 7 posts
Data Science News and Sharing 73 posts
Data Visualization 324 posts
Events 5 posts
Featured 37 posts
Function calling 1 posts
FutureTech 1 posts
Generative AI 5 posts
Hadoop 13 posts
Image Classification 1 posts
Innovation 2 posts
Kmeans Cluster 1 posts
LLM 6 posts
Machine Learning 364 posts
Marketing 1 posts
Meetup 144 posts
MLOPs 1 posts
Model Deployment 1 posts
Nagamas69 1 posts
NLP 1 posts
OpenAI 5 posts
OpenNYC Data 1 posts
pySpark 1 posts
Python 16 posts
Python 458 posts
Python data analysis 4 posts
Python Shiny 2 posts
R 404 posts
R Data Analysis 1 posts
R Shiny 560 posts
R Visualization 445 posts
RAG 1 posts
RoBERTa 1 posts
semantic rearch 2 posts
Spark 17 posts
SQL 1 posts
Streamlit 2 posts
Student Works 1687 posts
Tableau 12 posts
TensorFlow 3 posts
Traffic 1 posts
User Preference Modeling 1 posts
Vector database 2 posts
Web Scraping 483 posts
wukong138 1 posts

Our Recent Popular Posts

AI 4 AI: ChatGPT Unifies My Blog Posts
by Vinod Chugani
Dec 18, 2022
Meet Your Machine Learning Mentors: Kyle Gallatin
by Vivian Zhang
Nov 4, 2020
NICU Admissions and CCHD: Predicting Based on Data Analysis
by Paul Lee, Aron Berke, Bee Kim, Bettina Meier and Ira Villar
Jan 7, 2020

View Posts by Tags

#python #trainwithnycdsa 2019 2020 Revenue 3-points agriculture air quality airbnb airline alcohol Alex Baransky algorithm alumni Alumni Interview Alumni Reviews Alumni Spotlight alumni story Alumnus ames dataset ames housing dataset apartment rent API Application artist aws bank loans beautiful soup Best Bootcamp Best Data Science 2019 Best Data Science Bootcamp Best Data Science Bootcamp 2020 Best Ranked Big Data Book Launch Book-Signing bootcamp Bootcamp Alumni Bootcamp Prep boston safety Bundles cake recipe California Cancer Research capstone car price Career Career Day ChatGPT citibike classic cars classpass clustering Coding Course Demo Course Report covid 19 credit credit card crime frequency crops D3.js data data analysis Data Analyst data analytics data for tripadvisor reviews data science Data Science Academy Data Science Bootcamp Data science jobs Data Science Reviews Data Scientist Data Scientist Jobs data visualization database Deep Learning Demo Day Discount disney dplyr drug data e-commerce economy employee employee burnout employer networking environment feature engineering Finance Financial Data Science fitness studio Flask flight delay football gbm Get Hired ggplot2 googleVis H20 Hadoop hallmark holiday movie happiness healthcare frauds higgs boson Hiring hiring partner events Hiring Partners hotels housing housing data housing predictions housing price hy-vee Income industry Industry Experts Injuries Instructor Blog Instructor Interview insurance italki Job Job Placement Jobs Jon Krohn JP Morgan Chase Kaggle Kickstarter las vegas airport lasso regression Lead Data Scienctist Lead Data Scientist leaflet league linear regression Logistic Regression machine learning Maps market matplotlib Medical Research Meet the team meetup methal health miami beach movie music Napoli NBA netflix Networking neural network Neural networks New Courses NHL nlp NYC NYC Data Science nyc data science academy NYC Open Data nyc property NYCDSA NYCDSA Alumni Online Online Bootcamp Online Training Open Data painter pandas Part-time performance phoenix pollutants Portfolio Development precision measurement prediction Prework Programming public safety PwC python Python Data Analysis python machine learning python scrapy python web scraping python webscraping Python Workshop R R Data Analysis R language R Programming R Shiny r studio R Visualization R Workshop R-bloggers random forest Ranking recommendation recommendation system regression Remote remote data science bootcamp Scrapy scrapy visualization seaborn seafood type Selenium sentiment analysis sentiment classification Shiny Shiny Dashboard Spark Special Special Summer Sports statistics streaming Student Interview Student Showcase SVM Switchup Tableau teachers team team performance TensorFlow Testimonial tf-idf Top Data Science Bootcamp Top manufacturing companies Transfers tweets twitter videos visualization wallstreet wallstreetbets web scraping Weekend Course What to expect whiskey whiskeyadvocate wildfire word cloud word2vec XGBoost yelp youtube trending ZORI

NYC Data Science Academy

NYC Data Science Academy teaches data science, trains companies and their employees to better profit from data, excels at big data project consulting, and connects trained Data Scientists to our industry.

NYC Data Science Academy is licensed by New York State Education Department.

Get detailed curriculum information about our
amazing bootcamp!

Please enter a valid email address
Sign up completed. Thank you!

Offerings

  • HOME
  • DATA SCIENCE BOOTCAMP
  • ONLINE DATA SCIENCE BOOTCAMP
  • Professional Development Courses
  • CORPORATE OFFERINGS
  • HIRING PARTNERS
  • About

  • About Us
  • Alumni
  • Blog
  • FAQ
  • Contact Us
  • Refund Policy
  • Join Us
  • SOCIAL MEDIA

    ยฉ 2025 NYC Data Science Academy
    All rights reserved. | Site Map
    Privacy Policy | Terms of Service
    Bootcamp Application