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 # # Step 1: input area nStocks=20 sp.random.seed(1234) # produce the same random numbers n_corr=nStocks*(nStocks-1)/2 # number of correlation corr_0=sp.random.uniform(0.05,0.25,n_corr) # generate correlations...