Initializing help system before first use

QP

To adapt the model developed in Chapter Building models to the new way of looking at the problem, we need to make the following changes:

  • New objective function: mean variance instead of total return
  • Removal of the risk-related constraint
  • Addition of a new constraint: target yield

The new objective function is the mean variance of the portfolio:

s,t ∈ SHARES VARst·fracs ·fract

where VARst is the variance/covariance matrix of all shares. This is a quadratic objective function (an objective function becomes quadratic either when a variable is squared, e.g., frac12, or when two variables are multiplied together, e.g., frac1 · frac2).

The target yield constraint can be written as follows:

s ∈ SHARES RETs·fracs ≥ TARGET

The limit on the North American shares, as well as the requirement to spend all of the money and the upper bounds on the fraction invested into each share, are retained. We therefore obtain the following complete mathematical model formulation:

minimize s,t ∈ SHARES VARst·fracs ·fract
s ∈ NA fracs ≥ 0.5
s ∈ SHARES fracs = 1
s ∈ SHARES RETs·fracs ≥ TARGET
∀ s ∈ SHARES: 0 ≤ fracs ≤ 0.3

Implementation with Python

The variance/covariance matrix is given in the data file foliocppqp.csv:

0.1,0,0,0,0,0,0,0,0,0
0,19,-2,4,1,1,1,0.5,10,5
0,-2,28,1,2,1,1,0,-2,-1
0,4,1,22,0,1,2,0,3,4
0,1,2,0,4,-1.5,-2,-1,1,1
0,1,1,1,-1.5,3.5,2,0.5,1,1.5
0,1,1,2,-2,2,5,0.5,1,2.5
0,0.5,0,0,-1,0.5,0.5,1,0.5,0.5
0,10,-2,3,1,1,1,0.5,25,8
0,5,-1,4,1,1.5,2.5,0.5,8,16

We can read this datafile with the csv package by using csv.reader().

For the definition of the objective function we now use a quadratic expression. Since we now wish to minimize the problem, we use the default optimization sense setting, and optimization as a continuous problem is again started with the method optimize (with an empty string argument indicating the default algorithm).

import xpress as xp
import csv

# Read the CSV file and store each row in a list
file_path = 'Data/foliocppqp.csv'
VAR = []
with open(file_path, 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        VAR.append([float(value) for value in row])

# Problem data
TARGET = 9
MAXNUM = 4
NSHARES = 10
RET = [5, 17, 26, 12, 8, 9, 7, 6, 31, 21]
NA = [0, 1, 2, 3]

# *******FIRST PROBLEM: UNLIMITED NUMBER OF ASSETS********
p = xp.problem(name="Folio")

# VARIABLES.
frac = p.addVariables(NSHARES, ub=0.3, name="frac")

# CONSTRAINTS.
# Minimum amount of North-American values.
p.addConstraint(xp.Sum(frac[i] for i in NA) >= 0.5)

# Spend all the capital.
p.addConstraint(xp.Sum(frac) == 1)

# Target yield.
p.addConstraint(xp.Sum(frac[i] * RET[i] for i in range(NSHARES)) >= TARGET)

# Objective: minimize mean variance.
variance = [frac[s]*frac[t]*VAR[s][t] for s in range(NSHARES) for t in range(NSHARES)]
p.setObjective(xp.Sum(variance))

# Solve.
p.optimize()

# Print problem status.
print(f"Problem status: \n\t Solve status: {p.attributes.solvestatus.name} \n\t Sol status: \
    {p.attributes.solstatus.name}")

# Solution printing.
print(f"With a target of {TARGET} minimum variance is {p.attributes.objval}")
sol = p.getSolution(frac)
for i in range(NSHARES):
    print(f"{frac[i].name} : {sol[i]*100:.2f} %")

This program produces the following solution output with a eight-core processor (notice that the default algorithm for solving QP problems is the Barrier algorithm, not the Simplex as in all previous examples):

Minimizing QP Folio using up to 20 threads and up to 31GB memory, with these control settings:
OUTPUTLOG = 1
NLPPOSTSOLVE = 1
XSLP_DELETIONCONTROL = 0
XSLP_OBJSENSE = 1
Original problem has:
         3 rows           10 cols           24 elements
        76 qobjelem
Presolved problem has:
         3 rows           10 cols           24 elements
        76 qobjelem
Presolve finished in 0 seconds
Heap usage: 399KB (peak 410KB, 85KB system)

Coefficient range                    original                 solved
  Coefficients   [min,max] : [ 1.00e+00,  3.10e+01] / [ 6.25e-02,  7.50e-01]
  RHS and bounds [min,max] : [ 3.00e-01,  9.00e+00] / [ 5.00e-01,  4.80e+00]
  Objective      [min,max] : [      0.0,       0.0] / [      0.0,       0.0]
  Quadratic      [min,max] : [ 2.00e-01,  5.60e+01] / [ 7.81e-03,  6.88e-01]
Autoscaling applied standard scaling

Using AVX2 support
Cores per CPU (CORESPERCPU): 20
Barrier starts after 0 seconds, using up to 20 threads, 14 cores
Matrix ordering - Dense cols.:      9   NZ(L):        92   Flops:          584

  Its   P.inf      D.inf      U.inf      Primal obj.     Dual obj.      Compl.
   0   9.07e+00   3.18e+00   5.90e+00   2.8650781e+01  -3.7227748e+01   3.8e+01
   1   1.13e-01   3.97e-02   7.37e-02   1.5905889e+00  -2.7297064e+00   4.3e+00
   2   3.80e-02   1.33e-02   2.47e-02   8.4161309e-01  -7.7236285e-01   1.6e+00
   3   3.15e-07   7.77e-15   8.88e-16   6.3727342e-01   4.0678183e-01   2.3e-01
   4   2.08e-08   4.44e-16   4.44e-16   5.6786295e-01   5.3925882e-01   2.9e-02
   5   1.67e-10   4.44e-16   8.88e-16   5.5827300e-01   5.5660966e-01   1.7e-03
   6   2.28e-16   3.64e-17   4.44e-16   5.5748192e-01   5.5732472e-01   1.6e-04
   7   1.64e-16   2.22e-16   1.11e-16   5.5739574e-01   5.5738826e-01   7.5e-06
   8   5.72e-17   4.44e-16   4.44e-16   5.5739341e-01   5.5739339e-01   2.6e-08
Barrier method finished in 0 seconds
Uncrunching matrix
Optimal solution found
Barrier solved problem
  8 barrier iterations in 0.01 seconds at time 0

Final objective                       : 5.573934132966770e-01
  Max primal violation      (abs/rel) : 6.591e-17 / 6.591e-17
  Max dual violation        (abs/rel) :       0.0 /       0.0
  Max complementarity viol. (abs/rel) : 2.321e-08 / 3.316e-09
Problem status:
	 Solve status: COMPLETED
	 Sol status:     OPTIMAL
With a target of 9 minimum variance is 0.557393413296677
frac(0) : 30.00 %
frac(1) : 7.15 %
frac(2) : 7.38 %
frac(3) : 5.46 %
frac(4) : 12.66 %
frac(5) : 5.91 %
frac(6) : 0.33 %
frac(7) : 30.00 %
frac(8) : 1.10 %
frac(9) : 0.00 %

© 2001-2025 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.