top of page

Data Scientist Program


Free Online Data Science Training for Complete Beginners.

No prior coding knowledge required!

Portfolio Efficient Frontier in Python


Investors often aim at maximizing returns on investment for a given level of risk. This can be achieved by selecting a number of assets in which to invest in so as to minimize the risk and at the same time maximize the returns on investment. An efficient frontier represents a set of optimal portfolios that offer the highest expected returns for a defined level of risk (

In this tutorial, we will demonstrate how to construct an efficient portfolio of a two-asset portfolio based on the different weight combinations of the assets. The assets we will use for this demonstration are two S&P 500 Exchange Traded Funds (ETFs) of XLE and XLI. . We then proceed to select a suitable portfolio combination on the efficient frontier based on the risk tolerance and the required expected returns.

Import Required Packages

Load the Data

Plot of the Daily Close Prices

Calculate the Daily Log Returns

Daily and Annualized Standard Deviation of Returns

Correlation of Returns

Construction of an Efficient Frontier

Portfolio Weights

Portfolio Returns and Volatilities

Expected Returns using Capital Asset Pricing Model (CAPM)

Portfolio Efficient Frontier

Selecting a Portfolio with Defined Constraints

Github Link


Recent Posts

See All


bottom of page