-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementing Kalman Filter for Pair Trading Strategy Enhancement #7
Draft
chraberturas
wants to merge
10
commits into
main
Choose a base branch
from
3-study-using-kalman-filter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d3b7cba
Add files via upload
Kokechacho 7d46624
Add files via upload
Kokechacho bc05865
Delete KalmanInQ.q
Kokechacho 97bbbd6
Add files via upload
Kokechacho 404ec75
Add files via upload
Kokechacho d2bfdf8
Add files via upload
Kokechacho dc874a4
Delete kf.q
Kokechacho 1347796
Add files via upload
Kokechacho 34833fe
Update kalman_filter.q
Kokechacho 444d54c
Update kalman_filter.q
Kokechacho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Reference | ||
// https://www.quantstart.com/articles/State-Space-Models-and-the-Kalman-Filter/ | ||
|
||
// Auxiliary functions | ||
// Creates a diagonal matrix | ||
eye:{(2#x)#1f,x#0f} | ||
// Creates a 0's symetric matrix | ||
zeros: {(x#0f)} | ||
// JUST WORKS FOR 1X2 MATRIXES -> transform a 1x2 matrix to 2x1 | ||
transpose: {enlist each x} | ||
|
||
// This function implements the Kalman Filter algorithm to estimate the state of a linear dynamic system. | ||
// It takes the following parameters: | ||
/ x: The observation matrix (measurement) at time t. | ||
/ y: The state transition matrix (predicted state) at time t. | ||
/ delta: The observation covariance matrix, representing the uncertainty in the measurements. | ||
/ estimates: The estimated state mean vector at time t-1. | ||
/ covariances: The estimated state covariance matrix at time t-1. | ||
|
||
// The function returns: | ||
/ estimates: estimated updated state mean vector at time t | ||
/ covariances: estimated updated state covariance matrix at time t. | ||
|
||
kalmanFilter:{[x;y;delta;estimates; covariances] | ||
// Initialize static values | ||
G: eye 2; | ||
W: (delta % (1-delta)) * eye 2; | ||
V: 1f; | ||
m0: estimates; | ||
c0: covariances; | ||
Ft: x, 1f; | ||
|
||
alphat: G mmu m0; // a_t = G_t * m_t-1 (EQ 1) | ||
Rt: ((G mmu c0) mmu flip[G]) + W; // R_t = G_t * C_t-1 * T(G) + W_t (EQ 2) | ||
ft: Ft mmu alphat; // f_t = T(F_t) * a_t (EQ 5) | ||
et: y - ft; // e_t = y_t - f_t (EQ 3) | ||
Qt: ((Ft mmu Rt) mmu transpose[Ft]) + V; // Q_t = T(F_t) * R_t * F_t + V_t (EQ 6) | ||
At: ((Rt mmu transpose[Ft]) mmu 1%Qt); // A_t = R_t * F_t * inv(Q_t) (EQ 7) | ||
mt: (At * et) + alphat; // m_t = a_t + A_t * e_t (EQ 4) | ||
Ct:Rt-flip[enlist At] mmu enlist first[Qt] * At; // C_t = R_t - A_t * Q_t * T(A_t) (EQ 8) | ||
(mt;Ct)} // Return new updates | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider this shorter version :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can try that but I dont know if it is going to cause some problems as we need floats and not booleans or integers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't cause problems, try e.g.:
4f*eye 4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation causes problems, I think it has to do with types