Python Forum

Full Version: x and y must have same first dimension, but have shapes (1,) and (50,)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey there python community, after successfully plotting a graph, when it comes to plotting the frontier of it, I get this message:
ValueError: x and y must have same first dimension, but have shapes (1,) and (50,)
This is the graph:
plt.figure(figsize=(22,7))
plt.scatter(expectedVolatility,expectedReturn,c=sharpeRatio)
plt.xlabel('expected volatility')
plt.ylabel('expected log returns')
plt.colorbar(label='sharpe ratio')
plt.scatter(expectedVolatility[maxIndex],expectedReturn[maxIndex],c='red')
plt.plot(volatility_opt,returns, '--')
plt.show()
I know this kind of error is very common and in my humble opinion probably it arises from here:
returns = np.linspace(0, 1.50, num= 50, endpoint=True, retstep=False, dtype=None, axis=0)
volatility_opt = []
and the output is
 volatility_opt

[10.194341397342678] 
as the volatility_opt shouldn't be obviously just one value. I can't find yet precisely the origin, where could it be?
Include entire error message, including stack trace. If you post code, please post the code that is related to the issue. You say that volatility_opt should not be just one value, but you don't include any code showing how volatility opt is generated.
The error message is the following:
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)-1
and 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,:] = w
Returns 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.
An indentation error?
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'])
(Jan-12-2023, 03:46 PM)deanhystad Wrote: [ -> ]An indentation error?
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'])

Yes, that's it, now it works! I've triple-checked everything and as a pure rookie, I haven't considered the indentation...
Thank you very much for real, mr. Deanhystad :)
In Python, indentation is code.