Can Principal Component Analysis be used on stock prices / non-stationary data? I am reading an example given in the book, Machine Learning for Hackers.
I will first elaborate on the example and then talk about my question.
Example:
Takes a dataset for 10 years of 25 share prices.
Runs PCA on the 25 share prices.
Compares the principal component with the Dow Jones Index.
Observes very strong similarity between PC and DJI!
From what I understand, the example is more like a toy to help newbies like me understand how effective a tool PCA is!
However, reading from another source, I see that share prices are non-stationary and running PCA on share prices is absurd. The sources from where I read totally ridicule the idea of calculating covariance and PCA for share prices.
Questions:


*

*How did the example work so well? The PCA of share prices and DJI were very close to one another. And the data is real data from 2002-2011 share prices.

*Can someone point me to some nice resource for reading up on stationary / non-stationary data? I am a programmer. I have a good math background. But I haven't done serious math for 3 years. I have started reading again about stuff like random walks, etc.
 A: I run these types of analysis professionally and can confirm that they indeed are useful. But please make sure you analyse returns not prices. This is also highlighted by the critique in Slender Means: 
To perform PCA, your data have to have a meaningful covariance matrix 
(or correlation matrix, but the conditions are equivalent). They analyze 
stock prices, which are non-stationary time series variables.

A typical usecase in our analysis is to quantify systemic risk in the market place. The more co-movement in the market, the less of a diversification you really have in your portfolio. This can, for example, be quantified by the amount of variance described by the first principal component. Which is identical to the value of the first eigenvalue. 
For financial data, one typically examines a moving window over time. Some form of decay factor that downweights older observations is useful. For daily data, anything from 20-60 days, for weekly data maybe 1-2 years, all depending on your needs. 
Note that for global financial markets, with tens- or hundreds of thousands of asset prices changing continuously, one typicall can't run a 100K vs 100K  covariance matrix. Instead, typical usecase is to run the analysis per country, per sector or other more meaningful groups. Alternatively break down the return by a set of underlying factors (value, size, quality, credit ....) and do the PCA / Covariance analysis on these. 
Some nice articles include Attilio Meucci's discussion on effective number of bets:
http://papers.ssrn.com/sol3/papers.cfm?abstract_id=1358533
, and also Ledoit and Wolf's Honey I shrunk the sample covariance matrix
http://www.math.umn.edu/~bemis/MFM/2014/spring/References/lw_shrinkage.pdf
For a financially oriented introduction to stationarity, why not start with Investopedia. It's not rigorous, but conveys the main ideas. 
Good luck!
EDIT: Here is a 3-stock example showing Apple, Google and Dow Jones with daily returns through 2015. The upper triangle shows correlation of return, the lower triangle show correlation of prices.

As can be seen, Apple has a higher price-correlation with Dow (bottom left 0.76) than return correlation (top right 0.66). What can we learn from that? Not much. Google has a negative price correlation with both Apple (-0.28) and Dow (-0.27). Again, not much to learn from that. However, the return correlations tell us that Apple and Google both have a fairly high correlation with the Dow (0.66 and 0.53 respectively). That tell us something about the co-movement (price-change) of assets in a portfolio. That is useful information.
The main point is that although price correlation can be just as easily computed, it is not interesting. Why? Because the price of a stock is not interesting in itself. The price change, however, is very interesting. 
A: This piece serves to partly answer the original question and some of the questions raised in comments to @JonEgil's answer.
Financial (logarithmic) returns* are approximately $i.i.d.$ (although there is often some conditional heteroskedasticity) -- while prices are approximately random walks. Under the assumption of $i.i.d.$ observations, principal component analysis would directly generalize from sample to population (i.e. the sample principal components would be estimating the population principal components), but this might not hold under non-$i.i.d.$ observations -- see this thread. This is why it makes sense to run PCA on (logarithmic) returns rather than prices. 
Ruey S. Tsay has argued for running PCA on residuals from econometric models of financial time series, since residuals are normally assumed to be $i.i.d.$ I think that this idea might be included some place in his "Multivariate Time Series Analysis with R and Financial Applications" textbook (he explained the idea to me in person, so I am not sure where it is written). 
* Logarithmic return on price $P_t$ is defined as $r:=\text{log}(P_t)-\text{log}(P_{t-1})=\text{log}\frac{P_t}{P_{t-1}}$. Logarithmic returns are used for convenience in place of percentage returns $r':=\frac{P_t-P_{t-1}}{P_{t-1}}$. The convenient feature of logarithmic returns  is that you may sum up $h$ individual logarithmic returns to get the total logarithmic return over $h$ periods, while this does not hold for percentage returns. For relatively small percentage returns (which is common in finance), logarithmic returns approximately equal percentage returns as the logarithm has approximately unit slope around one.
