# A Byte of my 2.2-lb Brain

Just sharing stuff…

# PSE Correlation-Based Network

I find the idea of visualizing stock price correlations in a network interesting (albeit not really new).

Two nights ago, the founder of the BOH Society (@genkumag) pointed me to the Slice Matrix site, which describes itself as “a groundbreaking platform for real-time and historical analysis of financial markets, revealing the hidden fabric of price correlation and investor herding behavior.” Looking at some of the features of the platform, which, if I’m not mistaken, also implements the approach introduced by Bonanno et al.  (among others), I was inspired to carry out the concept on the Philippine Market. The final output is a network of assets as shown below.

### Financial Network Here, we are looking at a minimum spanning tree (MST) constructed from the price correlations of Philippine equities from June to July 2016. This snapshot allows us to see which stocks move in approximately the same way as  which stocks in terms of price. I’ll elaborate a bit more on this below. For those who are not familiar with correlations, the plots above roughly demonstrate what they are.

In finance/stock trading, correlation is just a measure of the extent at which two equities behave with respect to each other.

Essentially, the MST maximizes the overall correlation of the connections in the resulting graph. Visualizing the network topology can be useful for those who are looking to diversify portfolios.

#### Building the MST

The idea is pretty straightforward and can be summarised into three major steps.

1. Set up the correlation matrix for the stock prices in the Philippine Stock Exchange.
2. Convert the resulting correlation matrix into a distance matrix.
• Approach 1: Use `d(i,j) = √[2 x (1 - c(i,j))]`
• Approach 2: Use `d(i,j) = 1 - abs(c(i,j))`
3. Build a minimum spanning tree from the distance matrix.

I took the time series for all stocks from June-July 2016. For the given time range, below is the resulting stock price correlation matrix. In finance/stock trading, correlation is just a measure of the extent at which two equities behave with respect to each other. Does an asset go up in value when another specific one goes up? Does it go in the opposite direction? Or does it remain unaffected? Correlation values may range from -1 (negatively correlated) to +1 (positively correlated). When the correlation coefficient is zero, it suggests that two assets do not harmonize with each other. From the correlation matrix, we move to Step 2, which is to build a distance matrix. Based on what I have read, there are at least two ways to approach this (as enumerated above). In the first approach , we distinguish between positively and negatively correlated securities; in the second approach, on the other hand, we just quantify the strength of correlation between two assets (regardless if it’s +/-).  The distance matrix D is necessary to construct the minimum spanning tree (MST). First, a graph G is constructed from D where the nodes are the assets and the links represent the relationships (distance measures) between two equities; ergo, the links are weighted in terms of the distance measure. Remember, the higher the correlation between two entities, the “shorter” their “distance” is to each other (i.e., they are closer). Depending on the distance metric one uses, the topology of the resulting network may vary.

From the graph G, we then construct MST’s using Kruskal’s algorithm. Below is the pseudo-code copied from the Wiki on the algorithm.

```KRUSKAL(G):
1 A = ∅
2 foreach v ∈ G.V:
3    MAKE-SET(v)
4 foreach (u, v) in G.E ordered by weight(u, v), increasing:
5    if FIND-SET(u) ≠ FIND-SET(v):
6       A = A ∪ {(u, v)}
7       UNION(u, v)
8 return A```

I am not going to elaborate on the concept of MST in this post; but, I’ll leave an excerpt (from Wiki) below on its use.

There are quite a few use cases for minimum spanning trees. One example would be a telecommunications company which is trying to lay out cables in new neighborhood. If it is constrained to bury the cable only along certain paths (e.g. along roads), then there would be a graph representing which points are connected by those paths. Some of those paths might be more expensive, because they are longer, or require the cable to be buried deeper; these paths would be represented by edges with larger weights. Currency is an acceptable unit for edge weight – there is no requirement for edge lengths to obey normal rules of geometry such as the triangle inequality. A spanning tree for that graph would be a subset of those paths that has no cycles but still connects to every house; there might be several spanning trees possible. A minimum spanning tree would be one with the lowest total cost, thus would represent the least expensive path for laying the cable.

Below, I show the resulting trees from the two approaches employed in this blog post. Each node is a stock (company) listed in the PSE. I followed the configuration of SliceMatrix wherein the size of a node i just corresponds to the number of neighboring nodes directly connected to i. The color, on the other hand, corresponds to the price change of the securities {green, > +10%; red, < -10%; blue, [-10%, 10%]; gray (null values as I haven’t really cleaned the datasets I have.}. What one needs to know is that the closer the nodes are to each other (especially when they are directly connected), the higher their correlation is (or, the more synchronized their movement is). Essentially, we can tell which stocks are more directly affected by which stocks. Furthermore, the graphs below are way easier to appreciate than the huge correlation matrix shown in the figure above.

Why is it helpful to construct MST diagrams for stock prices? I like how Marcel Rešovský et al. put it in their paper titled “Minimum Spanning Tree: Application in the Currency Market” where they focused on the currency market (instead of the equity):

The MST diagram is an appropriate tool to identify the currencies playing a central role in the world currency market as well as the peripheral currencies having limited power to influence the market.

P.S. This is just an exploratory post.  (1) I have yet to examine the results I got from the method discussed. (2) I have not explored yet how the relationships change when the temporal range is varied (I have historical data from 2006 to present). It would be nice if I could find the time to see how the network topologies change with respect to varying temporal ranges. Also, (3) I would like to see how meaningful this approach is vis-à-vis  Philippine stocks.

1. G. Bonanno, G. Caldarelli, F. Lillo, R. N. Mantegna, Topology of correlation-based minimal spanning trees in real and model markets. In Physical Review. 2003, vol. 68.
2. M. Rešovský, D. Horváth, V. Gazda, M. Siničáková, Minimum Spanning Tree
Application in the Currency Market. In Currency Market. 2013.

### 2 comments on “PSE Correlation-Based Network”

1. spyfrat
August 19, 2016 Reblogged this on Spyfrat's Call.

Like

### Information

This entry was posted on August 16, 2016 by in Blog, Geek, Philippines, Stock Trading, Uncategorized and tagged , , , , .