Shiny App for asset allocation backtesting

Mikhail Stukalo
Posted on Oct 21, 2018

Project Motivation

Retail investors often lack not only a general understanding of asset management principles but also the necessary tools to make an educated decision about their investment portfolio. Wealth advisers create portfolios that seemingly reflect the client’s risk/return objectives. However, the client is not equipped to objectively assess if the manager is doing a good job.

That’s why I decided that for my project for the NYC Data Science Academy, I ‘d attempt to create a prototype of the portfolio allocation backtesting that can potentially be used by asset managers to illustrate the performance of a client’s portfolio and to explain the key principles of the modern portfolio theory.

The beta version of the Shiny App can be found on , and the full code is presented on the GitHub.

A Little Bit of Theory

In 1952 Harry Markowitz suggested that assets should be evaluated based on the ratio of their expected risk and return. The ratio varies among different asset classes. We will use six asset classes:

  • US Equities (represented by S&P500)
  • European Stocks (MSCI European Stocks Index)
  • Emerging Market Stocks (MSCI Emerging Market Index)
  • US Treasury Bonds (Barclays US Treasury Total Return Index)
  • US Corporate Bonds (Barclays US Corp Bond Total Return Index)
  • Real Estate (Dow Jones US Real Estate Index).

We can look at the historical risk/return ratios of these asset classes for the period 1Q2000 – 3Q2018.

Quite expectedly, fixed income instruments offered the least risk. However, one snapshot does not give enough information for an optimal portfolio selection. Asset returns are non-stationary, which means that the distribution of returns may change with time.

Another takeaway from the Markowitz’ approach later elaborated by William Sharpe (1966) is that returns of asset classes are not perfectly correlated. Therefore, an investor can enjoy the benefits of diversification by including more assets in her portfolio.

The graph below shows the results of simulated portfolios (gray). For the simulation, we generated 10,000 possible combinations of asset weights that add up to 100%, i.e. a fully-invested portfolio. The weights were drawn from a uniform distribution (0,1), which means that the portfolios we simulate are long-only. The blue line shows the combination of optimal portfolios, i.e. portfolios that offer the highest expected return for a given amount of risk. In Markowitz’ framework, such a line would be called the Efficient Frontier. However, technically speaking, the Efficient Frontier includes short positions on some of the assets. We assume that ordinary investors typically do not engage in short-selling. Therefore, we restrict our analysis to long-only portfolios. To maintain keep the integrity of definitions. we will refer to this line as the “Optimal Line” and portfolios on the Optimal Line as “Optimal Portfolios.”

The Application

The prototype of the backtesting application is aimed at providing investors with the tool to evaluate the historical performance of different combinations of asset classes and to compare it to the optimal portfolio.

Below is the layout of the Backtesting page of the application.

A user can choose asset weights in the portfolio. The weights of other assets are adjusted automatically so that the portfolio stays fully invested. The pie chart in the right upper corner visualizes the allocation. Next, the user chooses the rebalancing schedule and the period for which the backtesting is performed. After pressing the Backtest button, he can see the historical performance of the portfolio and compare it to benchmarks such as S&P500 and a portfolio consisting of 60% S&P500, 10% Treasury Bonds, and 30% corporate Bonds. The graph shows the compound return of the portfolios and the table in the right low corner provides key performance metrics.

On the Allocation Comparison page, the user can compare his portfolio to two Optimal portfolios: one with a similar return but lower risk and another with a similar risk but higher return. Under the hood, the code performs a penalizing optimization using DEOptim package in R.


I can see two major limitations for this product.

First and foremost, we use historical data to find an optimal portfolio. Therefore, the results have an inherited hindsight bias. Consquently, if such a product is used by asset management clients, the clients should be aware that a) previous performance of assets does not guarantee the future performance, and that b) it is virtually impossible to create a portfolio that will be on the Optimal Line, unless you have a crystal ball and know the future returns. That means that the product can be used to evaluate the portfolio performance and see how close or far its performance was to the Optimal Line. But slight deviations from the optimal portfolio should not be viewed as a bad performance by an asset manager.

Second, in designing an application like this, we have to strike the balance between providing a wide variety of asset classes within a manageable interface, and not presenting an overwhelming number of portfolio selections. In my example, I kept six asset classes that probably represent the most popular ones among retail investors. If the application is used by asset managers in real life, the number of asset classes may be increased to reflect most of the viable options that the firm’s clients have. However, it is practically impossible to include all the investable asset classes without making portfolio selection too complicated for an average investor.

About Author

Mikhail Stukalo

Mikhail Stukalo

NYC Data Science Academy Fellow. Over 15 years of experience in investment banking and private equity. Research interests include quantitative asset management and application of Machine Learning in Finance. DBA with concentration in Finance from Georgia State University....
View all posts by Mikhail Stukalo >

Leave a Comment

Build Data Science Portfolio | NYC Data Science Academy Blog July 11, 2019
[…] Mikhail Stukalo - Shiny App for asset allocation backtesting […]

View Posts by Categories

Our Recent Popular Posts

View Posts by Tags

#python #trainwithnycdsa 2019 airbnb Alex Baransky alumni Alumni Interview Alumni Reviews Alumni Spotlight alumni story Alumnus API Application artist aws 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 Bundles California Cancer Research capstone Career Career Day citibike clustering Coding Course Demo Course Report D3.js data Data Analyst data science Data Science Academy Data Science Bootcamp Data science jobs Data Science Reviews Data Scientist Data Scientist Jobs data visualization Deep Learning Demo Day Discount dplyr employer networking feature engineering Finance Financial Data Science Flask gbm Get Hired ggplot2 googleVis Hadoop higgs boson Hiring hiring partner events Hiring Partners Industry Experts Instructor Blog Instructor Interview Job Job Placement Jobs Jon Krohn JP Morgan Chase Kaggle Kickstarter lasso regression Lead Data Scienctist Lead Data Scientist leaflet linear regression Logistic Regression machine learning Maps matplotlib Medical Research Meet the team meetup Networking neural network Neural networks New Courses nlp NYC NYC Data Science nyc data science academy NYC Open Data NYCDSA NYCDSA Alumni Online Online Bootcamp Online Training Open Data painter pandas Part-time Portfolio Development prediction Prework Programming PwC python python machine learning python scrapy python web scraping python webscraping Python Workshop R 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 Selenium sentiment analysis Shiny Shiny Dashboard Spark Special Special Summer Sports statistics streaming Student Interview Student Showcase SVM Switchup Tableau team TensorFlow Testimonial tf-idf Top Data Science Bootcamp twitter visualization web scraping Weekend Course What to expect word cloud word2vec XGBoost yelp