On Tue, Apr 30, 2013 at 1:21 PM, Paul Blelloch <
paul.b...@gmail.com> wrote:
> I'm relatively new to pandas and thought that I'd start by processing some
> stock data as an example. I read in a bunch of stock data into a Panel
> using the read_data_yahoo function, and then calculated and stored the
> returns in a DataFrame. I then used the .cov() method to calculate the
> covariance. What I found was that though the covariance was symmetric it
> wasn't positive semi-definite. This wrecked havoc with some optimziation
> routines that I was running. Should the covariance be positive
> semi-definite? Is there any way to modify the calculation so that it is.
> Here's a relevant snippet of code:
>
> from pandas.io.data import *
>
> from pandas import *
>
> from numpy.linalg import eig
>
> print 'Reading stock data from Yahoo Finance'
>
> symbols = ['TRBCX', 'CMTFX', 'TREMX', 'PRFDX', 'PEXMX', 'PRITX', 'PRLAX',
>
> 'RPMGX', 'TRMCX', 'PRASX', 'PRNHX', 'OPGSX', 'TRREX', 'PRSCX',
>
> 'PRSVX', 'PRSGX', 'PSILX', 'PRHYX', 'PTTRX', 'RPSIX', 'PRTIX',
>
> 'TRRFX', 'TRRAX', 'TRRGX', 'TRRBX', 'TRRHX', 'TRRCX', 'TRRJX',
>
> 'TRRDX', 'TRRKX', 'TRRMX', 'TRRNX', 'TRRIX'] # List of all stock symbols to
> download
>
> stock_data = get_data_yahoo(symbols,start='1/1/1900') # Download data from
> YAHOO as a pandas Panel object
>
> adj_close = stock_data['Adj Close'] # Pull out adjusted closing prices as
> pandas DataFrame object
>
> returns = adj_close/adj_close.shift(1)-1 # Calculate simple returns
>
> covariance = returns.cov().values # Return the covariance matrix
>
> e = eig(covariance) # Check that all eigenvalues are positive
>
> print e[0] # Print eigenvalues
As far as I know, if you have missing values, and do only pairwise
deletion (instead of listwise), then positive semi-definiteness is not
guaranteed.
https://github.com/statsmodels/statsmodels/pull/631
It's not directly the algorithm of the reference in
https://github.com/statsmodels/statsmodels/issues/303
Josef
>
> --
> You received this message because you are subscribed to the Google Groups
> "PyData" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
pydata+un...@googlegroups.com.
> For more options, visit
https://groups.google.com/groups/opt_out.
>
>