Take the 2-minute tour ×
Quantitative Finance Stack Exchange is a question and answer site for finance professionals and academics. It's 100% free, no registration required.

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.

share|improve this question
    
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
2  
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
show 2 more comments

5 Answers

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.

share|improve this answer
add comment

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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
    
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. –  Tal Fishman Jan 18 '12 at 21:15
1  
@Tal Pandas is widely used in Finance, including for this problem, although it is only tangentially related. Wouldn't consider this SPAM, just uninformed. –  Shane Jan 19 '12 at 1:07
2  
Pandas doesn't contain MVO. Look at cvxopt, as suggested by @philippe. –  Shane Jan 19 '12 at 1:07
add comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.