Constructing an efficient frontier with n stocks

When the number of stocks, n, increases, the correlation between each pair of stocks increases dramatically. For n stocks, we have n*(n-1)/2 correlations. For example, if n is 10, we have 45 correlations. Because of this, it is not a good idea to manually input those values. Instead, we generate means, standard deviations, and correlations by drawing random numbers from several uniform distributions. To produce correlated returns, first we generate n uncorrelated stock return time series and then apply Cholesky decomposition as follows:

import numpy as np import scipy as sp import pandas as pd import matplotlib.pyplot as plt from datetime import datetime as dt from scipy.optimize import minimize ...

Get Python for Finance - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.