I have very simple model as mentioned bellow:
Assume that:
CW = [1.004455981050443, 0.9937806249035503, 0.9963341786199054, 1.000775606323324, 1.0006315883554697]
# Suppose That these are Percent of return of each bond
I want to solve this optimization and identify value of bi .
How can I code this problem in Python?
Update:
Suppose that I want to solve a optimization problem in my portfolio.
My final goal is to identify weights of investment(amounts between 0 and 1) in every bond that exist in the portfolio.
My bonds in portfolio :
- Bond 1
- Bond 2
- Bond 3
- Bond 4
- Bond 5
so my portfolio contains 5 bonds.
I put the model(objective function and subjective in
I wrote this code for solve this Optimization like this:
from scipy.optimize import minimize
import math as mt
CW = [0.9862898856860483, 0.9944441063388774, 0.9934069612349462, 0.9952892270523128, 0.9951914282293151] # Suppose That these are Percent of return of each bond
def Objective(x):
x1 = x[0]
x2 = x[1]
x3 = x[2]
x4 = x[3]
x5 = x[4]
fin = (- (mt.log10(CW[0]* x1))) + (- (mt.log10(CW[1]* x2))) + (- (mt.log10(CW[2]* x3))) + (- (mt.log10(CW[3]* x4))) + (- (mt.log10(CW[4]* x5)))
return fin
def Equality_Constraint(x):
x1 = x[0]
x2 = x[1]
x3 = x[2]
x4 = x[3]
x5 = x[4]
return x1 + x2 + x3 + x4 + x5 - 1
bounds_x1 = (0, 1)
bounds_x2 = (0, 1)
bounds_x3 = (0, 1)
bounds_x4 = (0, 1)
bounds_x5 = (0, 1)
Bounds = [bounds_x1, bounds_x2, bounds_x3, bounds_x4, bounds_x5]
Constraint1 = {'type' : 'eq', 'fun': Equality_Constraint}
Constraint = [Constraint1]
x0 = [0.2, 0.2, 0.2, 0.2, 0.2] #This is an initial Value
Result = minimize(Objective, x0, method='SLSQP' , bounds=Bounds, constraints=Constraint)
print(Result)
Output
fun: 3.510281934983529
jac: array([-2.17147237, -2.17147237, -2.17147237, -2.17147237, -2.17147234])
message: 'Optimization terminated successfully'
nfev: 6
nit: 1
njev: 1
status: 0
success: True
x: array([0.2, 0.2, 0.2, 0.2, 0.2])
Here is the problem! cause it says I should devote 20% of my total money for bound1, 20% for bound2 and so on. but its not true! the percent of investment(Weights) should be different! cause my return of bond4 = 0.9952892270523128 that this is best return between returns of each bond that mentioned as CW List ! This result of solver says all weights should be 0.2 and this is optimal solution! but this doesn't seems to be correct! they should be different weights! Now can any one help me to find out where is the problem? or how can I achieve correct weights through solve optimization problem?
