Code: Alles auswählen
def weights_matrix(Cov):
# Creating a random number seed that adds up to 1
# Crating of gthe inital guess
w = np.transpose(np.array(np.random.dirichlet(np.ones(len(Cov)), size =1 )))
return w
# Defining the Optimization Values
def Optimization_Values(Expect , Cov, r_f, w):
# Expected return of the Portfolio
Mu = np.sum(np.transpose(w) * Expect.values)
# Standard deviation of the Portfolio
Std = np.sqrt(np.matmul(np.matmul(np.transpose(w),Cov.values), w))
# Sharpe Ratio Formula
Sharpe_Ratio = (Mu-r_f)/Std
return Mu, Std, Sharpe_Ratio
def Optimization_objective (Expect , Cov, r_f, w):
# Expected return of the Portfolio
Mu = np.sum(np.transpose(w) * Expect.values)
# Standard deviation of the Portfolio
Std = np.sqrt(np.matmul(np.matmul(np.transpose(w),Cov.values), w))
# Sharpe Ratio Formula
Sharpe_Ratio = (Mu-r_f)/Std
return Sharpe_Ratio
# Defining the Iptimization objective
def objective (Expect , Cov, r_f, w):
return -Optimization_objective(Expect , Cov, r_f, w)
# Defining the Constraint w = 1
def constraint(w):
PorV = 1 # Protfolio Restriction 1
for i in range(len(w)):
PorV = PorV - w[i]
return PorV
#%%###########################################################################
# Main calling of the functions
##############################################################################
cons = ({'type':'eq', 'fun':constraint})
w = weights_matrix(Cov)
Mu, Std, Sharpe_Ratio = Optimization_Values(Expect , Cov, r_f, w)
print(objective(Expect , Cov, r_f, w))
sol = minimize(objective(Expect , Cov, r_f, w), w, method='SLSQP', constraints = cons, options={'disp':True})
#Weights, PortRet = MeanVarinaceOpt (returns, Expect, Var, Cov, Corr, r_f)
Jetzt bekomme ich aber immer folgenden Fehler:
Bin damit leider überfordert und verstehe nicht so ganz was der Fehler ist habe es auch schon mit x0 = [1,1,1,1,1] ausprobiert kommt der selbe Fehler. Wenn mir jemand Helfen könnte wer ich sehr dankbar
Code: Alles auswählen
Traceback (most recent call last):
File "", line 122, in <module>
sol = minimize(objective(Expect , Cov, r_f, w), x0 = [1,1,1,1,1], method='SLSQP', constraints = cons, options={'disp':True})
File "lib/python3.8/site-packages/scipy/optimize/_minimize.py", line 625, in minimize
return _minimize_slsqp(fun, x0, args, jac, bounds,
File "anaconda3/lib/python3.8/site-packages/scipy/optimize/slsqp.py", line 368, in _minimize_slsqp
sf = _prepare_scalar_function(func, x, jac=jac, args=args, epsilon=eps,
File "anaconda3/lib/python3.8/site-packages/scipy/optimize/optimize.py", line 261, in _prepare_scalar_function
sf = ScalarFunction(fun, x0, args, grad, hess,
File "anaconda3/lib/python3.8/site-packages/scipy/optimize/_differentiable_functions.py", line 76, in __init__
self._update_fun()
File "anaconda3/lib/python3.8/site-packages/scipy/optimize/_differentiable_functions.py", line 166, in _update_fun
self._update_fun_impl()
File "/anaconda3/lib/python3.8/site-packages/scipy/optimize/_differentiable_functions.py", line 73, in update_fun
self.f = fun_wrapped(self.x)
File "anaconda3/lib/python3.8/site-packages/scipy/optimize/_differentiable_functions.py", line 70, in fun_wrapped
return fun(x, *args)
TypeError: 'numpy.ndarray' object is not callable