Created
November 9, 2015 15:17
-
-
Save astocko/07d049f2220f3ab29661 to your computer and use it in GitHub Desktop.
Total Least Squares using PCA
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
"""Total Least Squares using PCA""" | |
# Copyright (c) 2015 Alexander Stocko <[email protected]> | |
# License: BSD 3 Clause | |
import numpy as np | |
import sklearn.decomposition | |
def tls_ratio(x, y): | |
A = np.stack([x, y]).T | |
pca = sklearn.decomposition.PCA(copy=True, n_components=2, whiten=False) | |
decomp = pca.fit(A) | |
return decomp.components_[0, 0] / decomp.components_[1, 0] | |
def moving_tls(x, y, period): | |
r = np.empty(len(x)) | |
r.fill(np.nan) | |
for i in range(period - 1, len(x)): | |
r[i] = tls_ratio(x[(i-period+1):i+1], y[(i-period+1):i+1]) | |
return r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment