Skip to content
kdb+/q kalman beta matlab python
Branch: master
Clone or download
nicholasferguson Delete code.analysis.txt
merged this info into
Latest commit f2d4b3e Sep 11, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitattributes Update .gitattributes Sep 11, 2019
.gitignore 🎉 Added .gitattributes & .gitignore files Sep 11, 2019
KF_beta_EWA_EWC.m Rename kF_beta_EWA_EWC.m to KF_beta_EWA_EWC.m Sep 11, 2019 Rename to Sep 11, 2019 update Sep 11, 2019
beta.kalman.q initial Sep 11, 2019
fillMissingData.m initial Sep 11, 2019
inputData_EWA_EWC.csv initial Sep 11, 2019

This project has code written in matlab, python and q/KDB+ code. Code mirrors each other

My contribution was to convert matlab file over to q/KDB+, for Kalman gain portion.

Code refers to equations from: Book: Ernest P Chan "Algorithmic Trading - Strategies and their Rational" (2013)

Matlab file implements examples of computing beta between two ETFs, using kalman gain.

speed: python is slowest. Then matlab. Fastest is q/kdb+.

to time in kdb+/q/KDB

q)\t \l beta.kalman.q

  • Note: xAT is Transform of xA
  • Note: This script was run in kdb+ 64x, cmd line: q q.k -s 1 -p 5010

======Code flow analysis

+  R  Estimated measurement error covariance. 
+  Q  Estimated process error covariance, measurement variance prediction
+  K  Kalman Gain
+  P  State Variance
+  e  measurement prediction error ( This is 'M' in other literatures. )
+  yhat measurement prediction
  • ========================Init Data ================================

  • ===measurement/model equations===**********=====system/process equations============

  •   									| Vw:Vw*(delta%(1-delta))
      									| Ve:0.001;
      | xA:EWA cls px								
      | yC:EWC cls px
      | beta[;0]:0f;
  • ------------Start of LOOP through each Data Point of EWA/EWC Pair for its beta --------

  • ===measurement/model equations===**********=====system/process equations============

  • =======transform of data=====*************======Variance/Covariance Adj======

  •   								  | R:P+Vw
      | beta[;t]:beta[;t-1] (if not initial loop)
      | yhat,:sum xA[t;]*beta[;t]
      								  | Q,:(sumMV[R;xA[t;]]) + Ve	       Q=xA.R.xAT + Ve	
      | e,:yC[t]-yhat[t]
      								  | K:mmu[R;vvmu[xA[t;];(1%Q[t])]]   K=R.xAT.1/Q
      		 _______________<_Adj Var__________________________|				
      | beta[;t]:beta[;t]+K*\:e[t]
      								  | P:R-vvmu[mmu[xA[t;];R];K]  		P=R-K.xA.R
  • -----------------------------------End of LOOP----------------------------------

You can’t perform that action at this time.