BQuant [2] - Correlation Matrices


Intro

A fairly common exploratory task is to generate a pairwise correlation matrix of some series. In this example, we’ll compute the correlation of quarterly revenue for members of SPX, along with correlation of quarterly change rates of revenue.

This example assumes you’re using BQuant (Desktop or Enterprise).

We’ll accomplish this using two different tools. First, with Pandas, one of the fundamental data tools in Python, and the second with DuckDB, which seamlessly provides an in-memory analytical SQL database.

I prefer the expressiveness and declarative structure of SQL over Pythonic dataframe libraries, but that’s personal preference, so I’ll show both.

BQL Query: Members of SPX

In BQL, we’ll use members(‘SPX Index’) to list the constituents of SPX for the past 11 quarters:

    get(sales_rev_turn)
    for(members('SPX Index'))
    with(fpo=range(-10Q, 0Q), fpt=Q, fill=prev)
    preferences(addcols=all)
  • members('SPX Index') returns the local exchange tickers for members of the index. If you need the fundamental tickers, you’d need: translatesymbols(members('SPX Index'), targetidtype='FUNDAMENTALTICKER')
  • Instead of members('SPX Index'), you could also pass a list of securities for(['IBM US Equity'], ['MSFT US Equity']
  • addcols=all: Returns the period and period_offset.
  • Aligning fiscal periods: You’ll have to decide what alignment you want. In this case, I’ll use fiscal period offsets, but this will align starting with the most recent reported fiscal quarter. Some judgment is needed to decide what you want.

Let’s see the code

View this Notebook directly at: https://github.com/paultiq/bqnt_examples/blob/main/bqnt_corr.ipynb

All the data points here are fake / randomized.

bqnt_corr.ipynb

bqnt_corr