Python Forum
scipy.optimize.basinhopping generates unstable output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
scipy.optimize.basinhopping generates unstable output
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to optimize analog gauge reader? kadink 0 724 May-19-2023, 08:58 PM
Last Post: kadink
  Using scipy.optimize: curve_fit ju21878436312 0 934 Sep-24-2022, 01:15 PM
Last Post: ju21878436312
  SOLVED: scipy.optimize.least_squares problem Skytter13 2 2,717 Mar-06-2022, 10:17 AM
Last Post: Skytter13
  Help with Scipy optimize for numerical problem Jbjbjb1 0 1,533 Jun-22-2021, 05:03 AM
Last Post: Jbjbjb1
  GridSearchCV generates unexpected different best parameters. Please help! yili2005 0 1,540 Feb-25-2021, 03:52 PM
Last Post: yili2005
  Solve a system of non-linear equations in Python (scipy.optimize.fsolve) drudox 7 22,640 Aug-18-2018, 02:27 AM
Last Post: scidam
  minimize with scipy.optimize tobenmoben 0 2,733 Feb-17-2018, 01:47 PM
Last Post: tobenmoben

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020