Skip to content
All library documents

Python Tools for Mean-Variance Portfolio Optimization

Article Quant Q&A · Author: Gabe

Summary

This discussion surveys Python approaches to portfolio construction, focusing on mean-variance and minimum-variance optimization. It points to convex optimization packages that can solve quadratic portfolio problems, with higher-level interfaces for expressing models, and mentions covariance estimation methods such as Ledoit–Wolf shrinkage. Examples include implementations of Markowitz, minimum-variance, tangency, and long-only or long-short portfolios.

The replies also describe specialized packages and example code, including a shrinkage covariance implementation and libraries offering broader risk measures, constraints, factor portfolios, and index tracking. The thread is a collection of historical recommendations rather than a systematic comparison: feature availability and package status vary, and one cited implementation reports that its reproduced shrinkage results did not exactly match the paper. It does not establish which library is currently best, and the original question’s request for lower-partial-moment support is not comprehensively answered.

Key ideas

  • Convex optimization packages can formulate mean-variance portfolio construction as quadratic optimization.
  • Ledoit–Wolf covariance shrinkage is available through Python tools and can be implemented alongside an optimizer.
  • Portfolio examples cover minimum-variance, tangency, and Markowitz allocations, with both long-only and long-short variants.
  • Specialized libraries may add risk measures, constraints, factor portfolios, or index tracking.
  • The recommendations are historical and do not provide a controlled comparison of package accuracy or current maintenance.

Tags

Full text
# What is the reference python library for portfolio optimization?


# 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.

## Answer by philippe (score 18)

https://quant.stackexchange.com/a/677

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.

## Answer by strimp099 (score 4)

https://quant.stackexchange.com/a/4004

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.

## Answer by NBF (score 4)

https://quant.stackexchange.com/a/25469

### 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.

## Answer by jlowin (score 3)

https://quant.stackexchange.com/a/3996

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.

## Answer by banjanxed (score 2)

https://quant.stackexchange.com/a/2796

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.

## Answer by Antonello (score 2)

https://quant.stackexchange.com/a/11475

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
results = portopt.solveport(covar_matrix,means_vector,risk_adv_coef)
```

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).

## Answer by Christian Zielinski (score 1)

https://quant.stackexchange.com/a/18509

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).

## Answer by BERTO (score 1)

https://quant.stackexchange.com/a/25544

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)
```

## Answer by DANY CAJAS (score 1)

https://quant.stackexchange.com/a/55120

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.

## Answer by nitin (score 0)

https://quant.stackexchange.com/a/7720

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

## Answer by Daniel Fernandez (score 0)

https://quant.stackexchange.com/a/25466

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
from pandas_datareader import data
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))                                 

data = pd.concat([aapl['Adj Close'], spy['Adj Close'], ibm['Adj Close']], axis=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.

## Answer by Vaibhav Arora (score 0)

https://quant.stackexchange.com/a/34122

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

## Answer by Mr.A (score -2)

https://quant.stackexchange.com/a/25308

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)
```

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.