The error message is the following:
In fact, till the Sharpe Ratio and the frontier's plot everything was running smoothly.
ValueError Traceback (most recent call last) <ipython-input-182-1237a1515f04> in <module> 5 plt.colorbar(label='sharpe ratio') 6 plt.scatter(expectedVolatility[maxIndex],expectedReturn[maxIndex],c='red') ----> 7 plt.plot(volatility_opt,returns, '--') 8 plt.show() C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py in plot(scalex, scaley, data, *args, **kwargs) 2838 @_copy_docstring_and_deprecators(Axes.plot) 2839 def plot(*args, scalex=True, scaley=True, data=None, **kwargs): -> 2840 return gca().plot( 2841 *args, scalex=scalex, scaley=scaley, 2842 **({"data": data} if data is not None else {}), **kwargs) C:\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs) 1741 """ 1742 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D) -> 1743 lines = [*self._get_lines(*args, data=data, **kwargs)] 1744 for line in lines: 1745 self.add_line(line) C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in __call__(self, data, *args, **kwargs) 271 this += args[0], 272 args = args[1:] --> 273 yield from self._plot_args(this, kwargs) 274 275 def get_next_color(self): C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs) 397 398 if x.shape[0] != y.shape[0]: --> 399 raise ValueError(f"x and y must have same first dimension, but " 400 f"have shapes {x.shape} and {y.shape}") 401 if x.ndim > 2 or y.ndim > 2: ValueError: x and y must have same first dimension, but have shapes (1,) and (50,)For what concerns volatility_opt:
def negativeSR(w): w = np.array(w) R = np.sum(meanlogReturns*w) V = np.sqrt(np.dot(w.T, np.dot(Sigma,w))) SR = R/V return -1*SR
def b(w): return np.sum(w)-1 w0 = [0.25, 0.25, 0.25, 0.25] bounds = ((0,1), (0,1), (0,1), (0,1)) constraints = ({'type': 'eq', 'fun': b}) w_opt = minimize(negativeSR, w0, method='SLSQP', bounds=bounds, constraints=constraints) w_opt
returns = np.linspace(0, 1.50, num= 50, endpoint=True, retstep=False, dtype=None, axis=0) volatility_opt = [] def GR(w): w = np.array(w) R = np.sum(meanlogReturns*w) return R def minimizeMyvolatility(w): w =np.array(w) V = np.sqrt(np.dot(w.T, np.dot(Sigma, w))) return V for R in returns: constraints = ({'type': 'eq', 'fun': b}, {'type': 'eq', 'fun': lambda w: GR(w) - R}) opt = minimize(minimizeMyvolatility, w0, method = 'SLSQP', bounds = bounds, constraints = constraints) volatility_opt.append(opt['fun'])with b as :
def b(w): return np.sum(w)-1and Sigma and w as :
weight = np.zeros((a, 4)) expectedReturn = np.zeros(a) expectedVolatility = np.zeros(a) sharpeRatio = np.zeros(a) meanlogReturns = logReturns.mean() Sigma = logReturns.cov() for k in range(a): w = np.array(np.random.random(4)) w = w / np.sum(w) weight[k,:] = wReturns of 4 tickers from yahoo lib with their closing price from 4/01/22 to 4/01/22 (or 01/4/22 to 10/4/22). LogReturns is just the log function of returns multiplied by 250 days.
In fact, till the Sharpe Ratio and the frontier's plot everything was running smoothly.