Python Forum

Full Version: scipy.optimize.basinhopping generates unstable output
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Given expected returns "u" (11x1 vector)
, covariance matrix "Cov" (11x11 matrix), and risk coefficient alpha (constant)
I tried to find out the maximum of the quadratic utility function and the optimal portfolio weight (w) with basinhopping algorithm.( i.e. w·u - (w·Cov·w)/2 , w is the weight vector )
However, I found that the solutions generated were quite different.
For example, sometimes w could be [1,0...,0], sometime could be [0.4,0.4,...,0.2]
I have no idea what's going on.
Could anyone explain?

Here is my code:

alpha=3.35
number_of_asset=11
length_of_one_period=21

def target_portfolio(train_data):
    
    target_return=train_data.mean().values*length_of_one_period
    target_cov=train_data.cov().values*length_of_one_period
    
    def Utility(weight):
        return -np.dot(weight,pre_target_return)+0.5*alpha*np.dot(weight.T,np.dot(pre_target_cov,weight))

    def constraint1(weight):
        return 1-sum(weight)

    weight0=np.full(number_of_asset,1/number_of_asset)
    b=(0,1)
    bnds=(b,)*number_of_asset
    con1={"type":"eq","fun":constraint1}
    cons=[con1]
    minimizer_kwargs = {"method": "SLSQP","constraints":cons,"bounds":bnds}
    sol = basinhopping( Utility, weight0, minimizer_kwargs=minimizer_kwargs,niter=300)     
    
    return sol.x