# What is the reference python library for portfolio optimization?

Does anyone know of a python library/source that is able to calculate the traditional mean-variance portfolio? To press my luck, any resources where the library/source also contains functions such as alternative covariance functions (etc. shrinkage), Lower partial moment portfolio optimization, etc...

I have developed, like everyone else, and implemented one or two variants. Is it just me or there isn't much out there in terms of python for financial/portfolio applications. At least nothing out there matching efforts like Rmetrics for R.

• There are rpy and rpy2 so you can reap R's solutions in Python too... Mar 10 '11 at 1:54
• I have used rpy and rpy2, like it very much. Are many people using and happy with this flow in their work: python -> r -> model execute -> python.
– Gabe
Mar 10 '11 at 12:57
• I guess using R and the rstudio server is pretty tough to beat.
– Gabe
Mar 11 '11 at 13:04
• why do you need a library for this? Just calculate it, I did this once and it was just a few lines of python. Just looped over all possibilities with 0.1% density and it did not take long time to calculate. When you have many assets, use ready distributions (rather than many inner for-loops) to kill the $x^{n}$ -time complexity calculation -problem. It is easy. Let me know how you managed it. I feel you are doing this problem a way too challenging, start easily.
– hhh
Aug 1 '11 at 19:52
• Nothing in Python matches Rmetrics, etc. That being said, there could be plenty of reasons to just do this in Python, and some of the other comments/answers already address this. The answer to "python library for portfolio optimization" is not R. Jan 19 '12 at 1:10

Sorry for not being able to give more than one hyperlink, please do some web search for the project pages.

Portfolio optimization could be done in python using the cvxopt package which covers convex optimization. This includes quadratic programming as a special case for the risk-return optimization. In this sense, the following example could be of some use:

http://abel.ee.ucla.edu/cvxopt/examples/book/portfolio.html

Ledoit-Wolf shrinkage is for example covered in scikit.

I reproduced Ledoit and Wolf's experiment outlined in their paper "Honey I Shrunk the Covariance Matrix" in Python which includes an implementation of their method to shrink the covariance matrix (can be found here see the get_shrunk_covariance_matrix() method on line 417).

All the code for the entire thing is on Github here. I make use of the cvxopt module in this process as well. My results are not exactly consistent with Ledoit and Wolf's probably because I was under tremendous time pressure to get this done and I didn't fully utilize cvxopt. Despite this, I use a lot of the functions and techniques you're looking for (I think) plus a lot of other methods that may prove useful for finance people.

## Convex Optimisation - CVXOpt and CVXPy. Textbook by Boyd & Vandenberghe

Aside from CVXOPT (known for its cone programming, see http://cvxopt.org/) with extensive documentation by the authors, Boyd and Vandenberghe http://stanford.edu/~boyd/cvxbook/, there is CVXPY which provides an easier front end. CVXPY was designed and implemented by Steven Diamond, with input from Stephen Boyd and Eric Chu. http://www.cvxpy.org/en/latest/#

Note that Boyd and Vandenberghe is the go-to textbook (freely available at the links I gave above) by many in this area and many in the convex optimisation field consider this to be the current best reference. Of course, there are many innovations and other researchers differentiate themselves from B&V, but this is one of the best references in the area according to some of these cutting-edge researchers I know.

I know this is an old question, but Wes McKinney, the developer of pandas (mentioned in another answer) is releasing a new Python package called RapidQuant that I think might meet the OP's stated needs. It appears to include both non-standard risk definitions and portfolio optimization. However, it is not open source. While the OP didn't specifically mention that as a dealbreaker, it's an important distinction. Also, Wes has demonstrated cvxopt + pandas in the past, so it is possible that RQ wraps it.

Please note I have no connection with Lambda Foundry (the company that makes this product), I'm just a fan of Wes's work.

...and I completely agree, 1.5 years after this question was posted, that the lack of a widely-adopted financial library is a sore point for Python. While I'm sure most of us have written our own (though talk about a biased sample...), and yes, any optimization package will suffice, I think there's definitely room for improvement here.

• Have you used RapidQuant? Aug 23 '12 at 14:31
• I have had a demonstration of it but I have not licensed it myself. Aug 23 '12 at 14:40
• I have been meaning to get a demonstration. I have wanted to throw out my big ball of duct tape,er. research environment, for a while Aug 23 '12 at 19:06

You might look into pandas. It is a library with various statistical and financial data manipulation and analysis functions. The developer gave a presentation at the pygotham conference in 2011, and one in 2010 specifically on using pandas with quantitative finance.

• Hi banjanxed, I followed the links you posted and neither have anything to do with portfolio analysis or MVO. Rather, this seems to be a general purpose library for handling time series and the like. Unless you can make the case that these links have something to do with the question, they will be deleted as SPAM. Jan 18 '12 at 21:15
• @Tal Pandas is widely used in Finance, including for this problem, although it is only tangentially related. Wouldn't consider this SPAM, just uninformed. Jan 19 '12 at 1:07
• Pandas doesn't contain MVO. Look at cvxopt, as suggested by @philippe. Jan 19 '12 at 1:07

Disclaimer: I am the author and I just pushed it to sourceforce

PortOpt is a open-source wrapper to Quadprog++ (a C++ quadratic solver) for solving portfolio optimisation problems that supports agents' linear indifference curves toward risk.

It has a python binding that let optimise portfolio problems as easy as:

import portopt


However at this moment it doesn't contain anything else than solving the portfolio optimisation (and in particular, it doesn't have functions for alternative covariance functions nor lower partial moment portfolio optimization).

I just pushed Python implementations of some common portfolio optimizers to my GitHub. It uses the CVXOPT library to solve the resulting quadratic programs. It supports the construction of Markowitz portfolios, minimum variance portfolios and tangency portfolios (both long-only or long/short).

I used an example from the paper: An Introduction to Shrinkage Estimation of the Covariance Matrix: A Pedagogic Illustration

I was able to get the same Shrinkage matrix. I have provided the same matrix they use in their paper.

Hope this helps

import numpy as np
import pandas
from math import pow

def get_shrunk_covariance_matrix(obs, c, zeros):
w_len=c.shape[0]
T=obs.shape[0]
print T, w_len
w=((T-1.0)/T)*c

sq_cov=c*c
sq_cov=sq_cov.as_matrix()
np.fill_diagonal(sq_cov, 0)
sq_cov=pandas.DataFrame(sq_cov).dropna()
frames =[]
for z in range(w_len):
LST=[]
for cc in range(T):
lst=[]
for ccc in range(w_len):
val = pow(obs.loc[cc][z]*obs.loc[cc][ccc]-w.loc[z][ccc],2)
lst.append(val)
LST.append(lst)

df=pandas.DataFrame(LST)
df[z]=0
frames.append(df)
result = pandas.concat(frames)

Sum_of_All_Estimated_Var=result.values.tolist()
sum1=0
for s in Sum_of_All_Estimated_Var: sum1+=sum(s)
a1=(T/pow((T-1),3))*sum1

Sum_of_All_S_ij_Squared=sq_cov.values.tolist()
a2=0
for s in Sum_of_All_S_ij_Squared: a2+=sum(s)

Optimal_Shrinkage_Intensity = a1/(a1+a2)
print Optimal_Shrinkage_Intensity

Shrinkage=(1-Optimal_Shrinkage_Intensity)*c +  Optimal_Shrinkage_Intensity*zeros
print Shrinkage

if __name__=="__main__":
n=np.matrix([[10,12,9,-2,17,8,12],  [-9,-11,2,-5,-7,2,-2],  [16,5,8,5,18,8,9], [6,-3,6,-13,1,4,2], [1,4,-9,5,8,-16,-1], [12,-1,2,22,11,6,10]])
mean = n.mean(axis=0)
n=n-mean
frame = pandas.DataFrame(n).dropna()
c=pandas.DataFrame(np.cov(frame,  rowvar=0), index=frame.columns, columns=frame.columns)
C=np.cov(frame,  rowvar=0)
D=C.diagonal()
zeros = np.zeros((C.shape[0], C.shape[0]), float)
np.fill_diagonal(zeros, D)
get_shrunk_covariance_matrix(frame, c, zeros)


Try portfolio_metrics @ https://github.com/tvaught/experimental/tree/master/portfolio_metrics

There is post describing the lib http://travisvaught.blogspot.com/2011/09/modern-portfolio-theory-python.html

I would suggest the qq-pat library (https://github.com/QuriQuant/qq-pat) with this library you can presently do minimum variance portfolio optimization using some simple code. This is a simple example with three assets:

import pandas as pd
import datetime
import qqpat

aapl = data.get_data_yahoo('AAPL',
start=datetime.datetime(2003, 10, 1),
end=datetime.datetime(2015, 1, 1))

spy = data.get_data_yahoo('TLT',
start=datetime.datetime(2003, 10, 1),
end=datetime.datetime(2015, 1, 1))

ibm = data.get_data_yahoo('IBM',
start=datetime.datetime(2003, 10, 1),
end=datetime.datetime(2015, 1, 1))

analyzer = qqpat.Analizer(data, column_type='price', titles=["APPL", "TLT", "IBM"])

analyzer.min_variance_portfolio_optimization(plotWeights=True)


Additionally you can use the parameter covarianceType to select the type of covariance matrix you want to use. For example you can use the following code for a Ledoit-Wolf type of shrinkage:

analyzer.min_variance_portfolio_optimization(covarianceType =qqpat.LEDOIT_WOLF, plotWeights=True):


I will be adding mean-variance optimization soon which will work in the same way. The library uses the CVXPY library for the optimization using the SCS solver which provides the fastest execution.

If anybody's still looking i think that you may find https://github.com/czielinski/portfolioopt interesting .

• Looks like you posted a link to Christian Zielinski's github - he also linked to it in the answers here! May 9 '17 at 13:57

Try Riskfolio-Lib http://riskfolio-lib.readthedocs.io/ It’s a open source python library that allows you to build optimal portfolio using 10 risk measures, black litterman model, build constraints, factor portfolios, robust covariance estimators, short weights, index tracking/replicating portfolios among others features.

• Bob Jansen I update my affiliation. I made Riskfolio-Lib, try it if you have time, it has a several examples. Jun 26 '20 at 0:43
• You spammed seven questions with this garbage in the past couple of hours. Kindly delete those posts and rethink what you want to get out of this site. We're not an advertising platform for your personal projects. You also seem to have a duplicate account. Jun 26 '20 at 3:14

Say you want to optimize for max sharpe ratio, you could do something like this with scipy:

import scipy.optimize as spopt

allocations = [] #allocations

def Sharpe():  #An  function to compute Sharpe ratio, return negative SR

compute Sharpe_Ratio

return -1*Sharpe_Ratio

bnd = [] #bounds

cns [] #constraints

result = spopt.minimize(Sharpe, allocations, method = 'SLSQP',bounds = bnd, constraints = cns)

• I believe the OP is looking for a library specifically for this type of problem and is aware that what you show is a possibility. Apr 8 '16 at 18:14