Created
February 25, 2016 20:25
-
-
Save TheDataLeek/227427d7d2c2b195c553 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def bastard_svd(A, k, p): | |
| ######################## | |
| # Stage A # | |
| ######################## | |
| m, n = A.shape | |
| assert(k + p <= n) | |
| # (1) | |
| # Create diagonal matrix of random numbers | |
| D = np.diag(np.random.normal(size=n)) | |
| # Create vector J of length k + p from the set {1, 2, ..., n} | |
| J = np.random.choice(np.arange(n), | |
| size=(k + p), | |
| replace=False) | |
| # (2) | |
| # Apply full FFT to rows of AD, extract k + p columns | |
| Z = np.fft.fft(A.dot(D), axis=1) # Axis0 is columns, Axis1 is Rows | |
| Y = Z[:, J] | |
| # (3) | |
| # Create Orthonormal matrix Q by orthonomalizing columns of Y | |
| # Q = orth(Y) | |
| Q = slg.orth(Y) | |
| ######################## | |
| # Stage B # | |
| ######################## | |
| # (4) | |
| # B = Q* A | |
| B = Q.T.dot(A) | |
| # (5) | |
| # SVD decomposition of B, s is vector of singular values. | |
| Uhat, s, V = np.linalg.svd(B, full_matrices=False) # equiv to 'econ' | |
| #Uhat, s, V = np.linalg.svd(B) # equiv to 'econ' | |
| D = np.diag(s) | |
| print('{} {} {}'.format(Uhat.shape, D.shape, V.shape)) | |
| # (6) | |
| # U + Q Uhat | |
| U = Q.dot(Uhat) | |
| # Return decomposition | |
| print(np.linalg.norm(A - U.dot(D).dot(V))) | |
| return U, D, V |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment