While running the python code for a cvxopt python demo file ‘portfolio’ (attached) I found that it generates a runtime error,viz.

see p. 2 for the error message.

However, when running the code with Python 25 it executes successfully, see below.

The python file for ‘portfolio’ is:

#!/usr/bin/python

import math

from cvxopt.base import matrix

from cvxopt.solvers import qp, options

from cvxopt import blas

import pylab

# Risk-return trade-off of page 187.

n = 4

S = matrix([ 4e-2, 6e-3, -4e-3, 0.0,

6e-3, 1e-2, 0.0, 0.0,

-4e-3, 0.0, 2.5e-3, 0.0,

0.0, 0.0, 0.0, 0.0 ], (n,n))

pbar = matrix([.12, .10, .07, .03])

G = matrix(0.0, (n,n))

G[::n+1] = -1.0

h = matrix(0.0, (n,1))

A = matrix(1.0, (1,n))

b = matrix(1.0)

N = 100

mus = [ 10**(5.0*t/N-1.0) for t in xrange(N) ]

options['show_progress'] = False

xs = [ qp(mu*S, -pbar, G, h, A, b)['x'] for mu in mus ]

returns = [ blas.dot(pbar,x) for x in xs ]

risks = [ math.sqrt(blas.dot(x, S*x)) for x in xs ]

pylab.subplot(211)

pylab.plot(risks, returns, 'k')

pylab.ylabel('expected return')

pylab.axis([0, 0.2, 0, 0.15])

pylab.yticks([0.00, 0.05, 0.10, 0.15])

pylab.subplot(212)

c1 = [ x[0] for x in xs ]

c2 = [ x[0] + x[1] for x in xs ]

c3 = [ x[0] + x[1] + x[2] for x in xs ]

c4 = [ x[0] + x[1] + x[2] + x[3] for x in xs ]

pylab.fill(risks + [.20], c1 + [0.0], '#D0D0D0')

pylab.fill(risks[-1::-1] + risks, c2[-1::-1] + c1 , '#B0B0B0')

pylab.fill(risks[-1::-1] + risks, c3[-1::-1] + c2, '#D0D0D0')

pylab.fill(risks[-1::-1] + risks, c4[-1::-1] + c3, '#B0B0B0')

pylab.axis([0.0, 0.2, 0.0, 1.0])

pylab.xlabel('standard deviation')

pylab.ylabel('allocation')

pylab.text(.15,.5,'x1')

pylab.text(.10,.7,'x2')

pylab.text(.05,.7,'x3')

pylab.text(.01,.7,'x4')

pylab.show()

4