@@ -0,0 +1,1152 @@
+#!/usr/bin/python%0A%0Aimport numpy as np%0Afrom random import expovariate%0Afrom scipy import linalg%0A%0Adef simData(num_rows, num_cols, k, dstar):%0A%09%22%22%22 Generate data with lower latent rank%0A%09Parameters%0A%09----------%0A%09num_rows : int%0A%09%09%09%09total rows in matrix%0A%09num_cols : int%0A%09%09%09%09total columns in matrix%0A%09k :%09int%0A%09%09%09separation between signal and noise%0A%09dstar : int%0A%09%09%09%09latent_rank %3C= num_cols%0A%09%09%09%09rank of latent sub-matrix to Generate%0A%09Returns%0A%09-------%0A%09int matrix%0A%09%09Simulated data matrix containing low rank signal%0A%09%09and overlaid noise%0A%09%22%22%22%0A%09if(dstar %3E num_cols):%0A%09%09dstar = num_cols%0A%09noise = np.random.randn(num_rows,num_cols)/np.sqrt(num_rows)%0A%09S = linalg.svd(noise)%5B1%5D%0A%09s_1 = S%5BS.shape%5B0%5D-1%5D*k # smallest singular value%0A%09S = np.zeros((num_cols,num_rows)) # set S to matrix of zeroes%0A%09v_j_1 = s_1 # keep track of last singular value%0A%09# add exponential increments to singular values%0A%09for i in range(dstar):%0A%09%09v_j = expovariate(1)%0A%09%09S%5Bi,i%5D = v_j + v_j_1%0A%09%09v_j_1 = S%5Bi,i%5D%0A%0A%09# generate signal%0A%09U = linalg.svd(np.random.randn(num_rows,dstar))%5B0%5D%0A%09V = linalg.svd(np.random.randn(dstar,num_cols))%5B2%5D%0A%09signal = np.dot(np.dot(U,S),V.T) # U * S * V'%0A%09%0A%09return signal + noise%0A

Create new file