# 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... –  Dirk Eddelbuettel 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. –  Shane 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.

-
Here are some links to the scikits.learn package LW function: [example] scikit-learn.sourceforge.net/dev/auto_examples/… [LW class] scikit-learn.sourceforge.net/dev/modules/generated/… [LW function] scikit-learn.sourceforge.net/dev/modules/generated/… –  Gabe Mar 10 '11 at 12:58
I agree. cvxopt is the best solution at this stage. –  Shane Jan 19 '12 at 1:08

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? –  Michael WS Aug 23 '12 at 14:31
I have had a demonstration of it but I have not licensed it myself. –  jlowin 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 –  Michael WS Aug 23 '12 at 19:06

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.

-