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 > R > Exploring Modern Portfolio Theory

Exploring Modern Portfolio Theory

Stephen Penrice
Posted on Nov 16, 2015

Using Shiny to Demo Modern Portfolio Theory

Stephen Penrice

November 7, 2015

Contributed by Stephen Penrice. Stephen took NYC Data Science Academy 12 week full time Data Science Bootcamp program between Sept 23 to Dec 18, 2015. The post was based on his second class project(due at 4th week of the program).

Introduction

I see Shiny as a tool for quickly and easily prototyping models without having to call in a development team. So I wanted to create an app that does non-trivial calculations on a set of user inputs and returns the results in a visual format. Modern Portfolio Theory, developed by Henry Markowitz in the 1950โ€™s, fit my requirements. I was also motivated by a desire to create an intuitive tool to help people understand the theory, something I wish I had had when I entered finance.

Modern Portfolio Theory begins with the assumption that returns on any risky asset are normally distributed. This in turn implies that any portfolio of risky assets has normally distributed returns, since a portfolio is simply a linear combination of individual assets. A normal distribution can be described by two parameters, its mean and standard deviation, which can be graphed in a plane. Customarily in finance the standard deviation is referred to as volatility (or vol) and graphed on the horizontal axis and the mean (or expected return) is graphed on the vertical axis. Modern Portfolio Theory states that for any set of risky assets the points corresponding to all possible portfolios of the assets all fit in the interior of a hyperbola (referred to as the โ€œEfficient Frontierโ€ or less commonly the โ€œMarkowitz Bulletโ€) and that the optimal portfolios are those whose points lie on the upper boundary of the hyperbola, since they represent the best possible expected return for a given level of volatility, assuming the investor holds only risky assets.

A wider range of portfolios is possible when an investor makes use of a risk-free asset (i.e. cash) to either reduce volatility and return by holding cash or increase volatility by borrowing cash to enable margin buying. In the case, all the optimal portfolios lie on a line that crosses the vertical axis at the risk-free rate r (corresponding to an all-cash portfolio, with expected return r and volatility 0) and a point on the hyperbola such that the line is tangent to the hyperbola.

Granted, this discussion is hard to follow without a visual, and hence the motivation for the Shiny app, which you can access here.

Using and Interpreting the App

The app has a menu of checkboxes allowing the user to select assets from a list of 16 publicly traded stocks. Once the stocks are selected, pressing the โ€œCalculate Efficient Frontierโ€ button displays the Efficient Frontier along with the Capital Allocation Line. The Efficient Frontier is the curve that shows, for every possible expected return, the minimum volatilty for the portfolios that have that expected return. So the only portfolios comprising the given assets that are of interest are the ones corresponding to points on the upper half of the Efficient Frontier, because if a portfolioโ€™s volatilty and expected return are in the interior of the โ€œMarkowitz Bullet,โ€ that means there is a portfolio on the upper boundary that has the same volatility but a higher expected return.

One thing to play around with in the app is the mix of assets. For example, you can see that the left end of the bullet moves to lower volatility as you add assets. This demonstrates the concept that diversified portfolios are less risky than one concentrated in a few assets. Notice that there is an option near the top of the page to โ€œHide Capital Allocation Line,โ€ which may be helpful when exploring the effects of diversity on volatility.

But what is the Capital Allocation line all about? It represents the volatility and expected return of optimal portfolios that include cash as well as risky assets. (Finance theory calls cash the โ€œrisk-freeโ€ asset, and the โ€œrisk-free rateโ€ is the interest rate that an investor receives/pays to save/borrow cash.) An investor can lower portfolio volatility by holding cash, or increase volatility by borrowing money. (Buying stocks with borrowed money amplifies both positve and negative returns. Suppose you buy $100 of a stock using $50 of your own money and $50 of borrowed money. A $5 movement in the stockโ€™s prices represents a 5% change in price but a 10% profit or loss on your investment.)

The Capital Allocation Line is defined by two points. One point corresponds to the most conservative portfolio possible, consisting entirely of cash. This portfolio has zero volatility, its expected return is the risk-free rate, and it is optimal because it is the only portfolio with zero volatility. The other point that defines the Capital Allocation Line is the point where the line tangent to the efficient Frontier passes through the point we just looked at, and we can call the corresponding portfolio the โ€œTangent Portfolio.โ€ For any given level of volatilty, the optimal portfolio is some mixture of cash and the Tangent Portfolio, and the point representing its volatilty and expected return lies on the Capital Allocation Line.

The app illustrates these ideas nicely. The user can adjust the โ€œVolatility Targetโ€ slider at the bottom of the page and watch the dot representing the optimal portfolio move up and down the Capital Allocation Line. The app also displays the optimal allocation (expressed as a percentage of the investorโ€™s capital) given a choice of assets, a volatility target, and a risk-free rate. If you adjust the volatility target, you may notice that while the stock allocations change, their relative sizes remained fixed. That is because in any optimal portfolio the proportions on the risky assets are the same as in the Tangent Portfolio.

Note that a portfolio might include negative allocations to cash or stocks. Negative cash means borrowing to finance assset pruchases, and a negative stock allocation means โ€œshorting,โ€ i.e. borrowing a stock, then selling it and investing the proceeds in the hope that the price will go down, allowing the investor to repurchase it at the lower price and keep the difference after returning the borrowed shares.

Itโ€™s also interesting to think about what happens when the risk-free rate changes but the investorโ€™s volatility target remains the same. If interest rates go up, the investor has an incentive to hold more cash. But this will decrease the portfolioโ€™s volatility, so in order to compensate, the mix of stocks need to be riskier. The app illustrates this as well. If you increase the risk-free rate using the slider, you will see both an increase in the โ€œTangent Portfolio Volatiltyโ€ (calculated at the bottom of the page) and a rightward shift in the point of tangency on the Efficient Frontier. Likewise, a decrease in the risk-free rate creates an incentive to borrow, resulting in a need for a less risky mix of stocks, which can be seen in both the graph and the bar chart when you move the risk-free rate slider to the left. (A particularly good starting point for this particular exploration is to select Novartis, Merck, and Eli Lilly and set the target volatility to 21.5%. Remember to click the โ€œCalculate Efficient Frontierโ€ button!)

Using the โ€œLong Onlyโ€ checkbox at the top of the page, the user can also see the effect of restricting to long-only portfolios, i.e. protfolios that donโ€™t make use of borrowed cash or shorted stocks.

Now letโ€™s look at a few technical aspects of the app.

Interface

The interface has some features that help to prevent problematic inputs. If the user selects fewer than 2 assets, a message will be displayed indicating that at least two assets must be selected; this prevents an error in the function that calculates the optimal portfolios.

The range of values available on the risk-free rate slider is determined by the choice of assets. Since the efficient fontier is a hyperbola, it has an asymptote that the upper part of the efficient frontier approaches as the volatility goes to infinity. The slider prevents the risk-free rate from exceeding the y-intercept of this asymptote.

Back-end Code

In order to keep the focus of this project on the model, I elected to choose a small set of assets, collect the return data one time, and store the data in an Rds file. For this I used the quantmod package. Specifically, I downloaded monthly returns for the 16 stocks from January 2007 to October 2015. These returns are stored in โ€œlongโ€ format comprising there three columns: the return, the month, and the companyโ€™s ticker symbol.

library(dplyr)
library(quantmod)
library(tidyr)
library(stringr)

getReturns <- function(ticker){
  getSymbols(ticker)
  temp.returns <- periodReturn(get(ticker), period = 'monthly')
  temp.df <- data.frame(temp.returns)
  temp.df$date <- row.names(temp.df)
  temp.df$ticker <- ticker
  return(temp.df)
}

portfolioReturns <- function(tickers){
  dfs <- lapply(tickers, getReturns)
  stacked <- NULL
  for(i in 1:length(dfs))
    stacked <- rbind(stacked,dfs[[i]])
  return(stacked)
}

full.list <- portfolioReturns(c('PFE','NVS','MRK','LLY',
                                'GS','JPM','MS','PNC',
                                'TWX','CMCSA','DIS','DISCA',
                                'WMT','TGT','HD','COST'))

full.list$date <- str_sub(full.list$date, 1, 7)
saveRDS(full.list,"eff_front_app/full_list.rds")

The modelโ€™s heavy lifting is done by the quadprog package. I used the following code to calculate the mean vector and covariance matrix from historical returns (after putting them in a โ€œwideโ€ data frame with a column for each ticker) and then calculate the minimum variance portfolio for a given target return. (The server calls these functions, as will be explained below.)

library(quadprog)

assetCov <- function(stacked, tickers){
  if(is.null(stacked))
    return(NULL)
  else {
    selected <- stacked[sapply(stacked$ticker, function(x) x %in% tickers),] %>%
                spread(.,ticker,monthly.returns)
    covMat <- 12*cov(selected[,-1], use = "complete.obs")
    return(covMat) 
  }
}

assetMean <- function(stacked, tickers){
  if(is.null(stacked))
    return(NULL)
  else {
    selected <- stacked[sapply(stacked$ticker, function(x) x %in% tickers),] %>%
      spread(.,ticker,monthly.returns)
    meanVector <- colMeans(selected[,-1])
    return(12*array(meanVector))
  }
}

mean.var.opt <- function(means, covMat, target, longOnly = FALSE){
  Dmat <- 2*covMat
  dvec <- matrix(0,nrow(covMat),1)
  Amat <- cbind(rep(1,nrow(covMat)),means)
  bvec <- matrix(c(1,target),1,2)
  if(longOnly){
    Amat <- cbind(Amat,diag(nrow(covMat)))
    bvec <- cbind(bvec, matrix(0,1,nrow(covMat)))
  }
  return(solve.QP(Dmat,dvec,Amat, bvec = bvec,meq=2))
}

Note that mean.var.opt has an optional longOnly flag that adds non-negativity constraints.

Server

For the most part the server acts as a intermediary between the interface and the portfolio optimizer. One of the more interesting pieces of server code is the function that calculates the Efficient Frontier.

  covMat <- reactive({assetCov(full.list,portfolio())})
  
  assetMns <- reactive({assetMean(full.list,portfolio())})

  riskyEF <- reactive({
              if(longOnlyFlag() & min(assetMns()) < 0)
                minmean <- 0
              else
                minmean <- min(assetMns())
              mult <- ifelse(longOnlyFlag(),1,2)
              mns <- seq(minmean,mult*max(assetMns()), by = 0.001)
              sds <- sapply(mns, function(x) mean.var.opt(assetMns(), covMat(),x,longOnlyFlag())[[2]]^0.5)
              data.frame(expRet = mns, Vol = sds, curve = "EF")
            })

This function sets maximum and minimum return targets (which depend on the selected assets and whether the long-only option is selected) and creates a list of values extending from the maximum to the minimum in steps of 0.1%. Then the server uses sapply to call the optimizer on each of these target inputs to obtain the corresponding minimum volatilty. The resulting points are stored in a data frame so they can be easily plotted with ggplot2.

Conclusion

This app could be enhanced with more options for user input. Obviously, it could provide more asset options and gather return data on demand. More fundamentally, it could provide the user with the capability to specify a mean vector and covariance matrix based on hid or her views, rather than taking historical returns as the best estimate of future performance. There could also be greater flexibility in specifying constraints, for example putting a cap on the proportion invested in a certain company or industry. In any case, the app provides a tool for visually exploring an important piece of finance theory.

About Author

Stephen Penrice

After starting his career as a Ph.D. in pure mathematics, Stephen has worked continuously to grow his technical proficiency in order to take on more and more challenges with an applied focus. His latest work in the finance...
View all posts by Stephen Penrice >

Leave a Comment

Cancel reply

You must be logged in to post 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