Loading .gitignore +1 −0 Changes for .gitignore: 1 added line, 0 removed lines. Original line number Diff line number Diff line *.pyc *~ drafts/* results/* DataHandler.py +9 −1 Changes for DataHandler.py: 9 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -139,7 +139,15 @@ class DataHandler(object): """ Write networks """ f.write("\n\n#Networks:\n\n") for k in range(alg.blocks): f.write("Block %s\n " % k) f.write("Block %s," % k) f.write(alg.blockdates[k] + "\n") if k > 0: f.write("Dev to prev,") f.write("{0:.3f},".format(alg.deviations[k-1])) if k < alg.blocks - 1: f.write("Dev to next,") f.write("{0:.3f}".format(alg.deviations[k])) f.write("\n") for feat in feats: f.write("," + feat) f.write("\n") Loading ParallelTVGL.py +17 −8 Changes for ParallelTVGL.py: 17 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -12,7 +12,7 @@ MAX_ITER = 10000 def mp_parallel_tvgl((thetas, z0s, z1s, z2s, u0s, u1s, u2s, emp_cov_mat, lambd, beta, rho, nju, indexes, out_queue, prev_pipe, next_pipe, proc_index, stopping_criteria), proc_index, stopping_criteria, pen_func), last=False): try: iteration = 0 Loading @@ -38,7 +38,8 @@ def mp_parallel_tvgl((thetas, z0s, z1s, z2s, u0s, u1s, u2s, for j, i in zip(indexes[:end], range(nn)): a = (z0s[i] + z1s[i] + z2s[i] - u0s[i] - u1s[i] - u2s[i])/3 at = a.transpose() m = nju*(a + at)/2 - emp_cov_mat[i] #m = nju*(a + at)/2 - emp_cov_mat[i] m = (a + at)/(2 * nju) - emp_cov_mat[i] d, q = np.linalg.eig(m) qt = q.transpose() sqrt_matrix = np.sqrt(d**2 + 4/nju*np.ones(dimension)) Loading @@ -59,7 +60,7 @@ def mp_parallel_tvgl((thetas, z0s, z1s, z2s, u0s, u1s, u2s, """ Z1-Z2 Update """ for i in range(1, n): a = thetas[i] - thetas[i-1] + u2s[i] - u1s[i-1] e = pf.group_lasso_penalty(a, beta, rho) e = getattr(pf, pen_func)(a, beta, rho) summ = thetas[i] + thetas[i-1] + u2s[i] + u1s[i-1] z1s[i-1] = 0.5*(summ - e) z2s[i] = 0.5*(summ + e) Loading Loading @@ -131,7 +132,8 @@ class ParallelTVGL(TVGL): None, self.pipes[i][0], i, stopping_criteria),)) stopping_criteria, self.penalty_function),)) elif i == self.processes - 1: p = multiprocessing.Process(target=mp_parallel_tvgl, args=((self.thetas[self.chunk * i:], Loading @@ -151,7 +153,8 @@ class ParallelTVGL(TVGL): self.pipes[i-1][1], None, i, stopping_criteria), True)) stopping_criteria, self.penalty_function), True)) else: p = multiprocessing.Process(target=mp_parallel_tvgl, args=((self.thetas[self.chunk * i:self.chunk*(i+1)+1], Loading @@ -171,7 +174,8 @@ class ParallelTVGL(TVGL): self.pipes[i-1][1], self.pipes[i][0], i, stopping_criteria),)) stopping_criteria, self.penalty_function),)) self.procs.append(p) def run_algorithm(self, max_iter=10000): Loading @@ -190,6 +194,11 @@ class ParallelTVGL(TVGL): for p in self.procs: p.join() self.iteration = iteration print self.iteration #print self.iteration self.run_time = '{0:.3g}'.format(time.time() - start_time) self.thetas = [np.round(theta, self.roundup) for theta in self.thetas] self.final_tuning(True, MAX_ITER) #self.thetas = [np.round(theta, self.roundup) for theta in self.thetas] def terminate_pools(self): for p in self.procs: p.terminate() TVGL.py +30 −4 Changes for TVGL.py: 30 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -9,13 +9,17 @@ class TVGL(object): np.set_printoptions(precision=3) def __init__(self, filename, blocks, lambd, beta, processes, penalty_function="Group Lasso"): processes, penalty_function="group_lasso", datecolumn=True): self.datecolumn = datecolumn self.processes = processes self.blocks = blocks self.penalty_function = penalty_function self.dimension = None self.emp_cov_mat = [0] * self.blocks self.real_thetas = [0] * self.blocks if self.datecolumn: self.blockdates = [0] * self.blocks self.read_data(filename) self.rho = self.get_rho() self.max_step = 0.1 Loading @@ -35,7 +39,7 @@ class TVGL(object): self.e = 1e-5 self.roundup = 1 def read_data(self, filename, comment="#", splitter=",", datecolumn=True): def read_data(self, filename, comment="#", splitter=","): with open(filename, "r") as f: comment_count = 0 for i, line in enumerate(f): Loading @@ -43,7 +47,7 @@ class TVGL(object): comment_count += 1 else: if self.dimension is None: if datecolumn: if self.datecolumn: self.dimension = len(line.split(splitter)) - 1 else: self.dimension = len(line.split(splitter)) Loading @@ -61,7 +65,9 @@ class TVGL(object): if i == 1: self.generate_real_thetas(line, splitter) continue if datecolumn: if count == 0 and self.datecolumn is True: start_date = line.strip().split(splitter)[0] if self.datecolumn: lst.append([float(x) for x in np.array(line.strip(). split(splitter)[1:])]) Loading @@ -71,6 +77,9 @@ class TVGL(object): split(splitter))]) count += 1 if count == self.obs: if self.datecolumn: end_date = line.strip().split(splitter)[0] self.blockdates[block] = start_date + " - " + end_date datablck = np.array(lst) tp = datablck.transpose() self.emp_cov_mat[block] = np.real( Loading Loading @@ -176,20 +185,37 @@ class TVGL(object): def final_tuning(self, stopping_criteria, max_iter): self.thetas = [np.round(theta, self.roundup) for theta in self.thetas] #self.only_true_false_edges() self.terminate_pools() if stopping_criteria: print "\nIterations to complete: %s" % self.iteration else: print "\nMax iterations (%s) reached" % max_iter def only_true_false_edges(self): for k in range(self.blocks): for i in range(self.dimension - 1): for j in range(i + 1, self.dimension): if self.thetas[k][i, j] != 0: self.thetas[k][i, j] = 1 self.thetas[k][j, i] = 1 else: self.thetas[k][i, j] = 0 self.thetas[k][j, i] = 0 def temporal_deviations(self): deviations = np.zeros(self.blocks - 1) for i in range(0, self.blocks - 1): dif = self.thetas[i+1] - self.thetas[i] np.fill_diagonal(dif, 0) deviations[i] = np.linalg.norm(dif) print deviations try: self.deviations = deviations/max(deviations) self.dev_ratio = float(max(deviations))/float(np.mean(deviations)) except ZeroDivisionError: self.deviations = deviations self.dev_ratio = 0 print "Temp deviations ratio: {0:.3g}".format(self.dev_ratio) def correct_edges(self): Loading matlab/admm_algorithm.mdeleted 100644 → 0 +0 −65 Changes for matlab/admm_algorithm.m: 0 added lines, 65 removed lines. Original line number Diff line number Diff line function Theta = admm_algorithm(Theta,Z0,Z1,Z2,U0,U1,U2,... rho,lambda,beta,nju,empirical_covariance_matrices) max_iteration = 20000; index = 0; stopping_criteria = 0; total_stamps = size(Theta,1); dimension = size(Theta{1},1); while index < max_iteration && stopping_criteria == 0 %% Theta-Update for i = 1:total_stamps A = (Z0{i}+Z1{i}+Z2{i}-U0{i}-U1{i}-U2{i})/3; M = nju*(A+A')/2 - empirical_covariance_matrices{i}; [Q,D] = eig(M) Theta{i} = nju/2*Q*(D+sqrt(D^2 + 4/nju*diag(ones(dimension,1))))*Q'; end %% Z0-Update for i = 1:total_stamps Z0{i} = soft_thres_od(Theta{i} + U0{i},lambda,rho); % soft threshold odd end %% Z1,Z2-Update for i = 2:total_stamps A = Theta{i} - Theta{i-1} + U2{i} - U1{i-1}; E = group_lasso_penalty(A,2*beta/rho); Z_pre = 0.5*[Theta{i-1}+Theta{i}+U1{i}+U2{i};Theta{i-1}+Theta{i}+U1{i}+U2{i}] + ... 0.5*[-E;E]; Z1{i-1} = Z_pre(1:dimension,:); Z2{i} = Z_pre(dimension+1:end,:); end %% U0-Update for i = 1:total_stamps U0{i} = U0{i} + Theta{i} - Z0{i}; end %% U1,U2-Update for i = 2:total_stamps U1{i-1} = U1{i-1} + Theta{i-1} - Z1{i-1}; U2{i} = U2{i} + Theta{i} - Z2{i}; end %% Stopping condition if index > 0 fro_norm = 0; for i = 1:total_stamps dif = Theta{i} - Theta_pre{i}; fro_norm = fro_norm + norm(dif,'fro'); end if fro_norm < 1e-5 stopping_criteria = 1; end end Theta_pre = Theta; index = index + 1; end index end No newline at end of file Loading
.gitignore +1 −0 Changes for .gitignore: 1 added line, 0 removed lines. Original line number Diff line number Diff line *.pyc *~ drafts/* results/*
DataHandler.py +9 −1 Changes for DataHandler.py: 9 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -139,7 +139,15 @@ class DataHandler(object): """ Write networks """ f.write("\n\n#Networks:\n\n") for k in range(alg.blocks): f.write("Block %s\n " % k) f.write("Block %s," % k) f.write(alg.blockdates[k] + "\n") if k > 0: f.write("Dev to prev,") f.write("{0:.3f},".format(alg.deviations[k-1])) if k < alg.blocks - 1: f.write("Dev to next,") f.write("{0:.3f}".format(alg.deviations[k])) f.write("\n") for feat in feats: f.write("," + feat) f.write("\n") Loading
ParallelTVGL.py +17 −8 Changes for ParallelTVGL.py: 17 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -12,7 +12,7 @@ MAX_ITER = 10000 def mp_parallel_tvgl((thetas, z0s, z1s, z2s, u0s, u1s, u2s, emp_cov_mat, lambd, beta, rho, nju, indexes, out_queue, prev_pipe, next_pipe, proc_index, stopping_criteria), proc_index, stopping_criteria, pen_func), last=False): try: iteration = 0 Loading @@ -38,7 +38,8 @@ def mp_parallel_tvgl((thetas, z0s, z1s, z2s, u0s, u1s, u2s, for j, i in zip(indexes[:end], range(nn)): a = (z0s[i] + z1s[i] + z2s[i] - u0s[i] - u1s[i] - u2s[i])/3 at = a.transpose() m = nju*(a + at)/2 - emp_cov_mat[i] #m = nju*(a + at)/2 - emp_cov_mat[i] m = (a + at)/(2 * nju) - emp_cov_mat[i] d, q = np.linalg.eig(m) qt = q.transpose() sqrt_matrix = np.sqrt(d**2 + 4/nju*np.ones(dimension)) Loading @@ -59,7 +60,7 @@ def mp_parallel_tvgl((thetas, z0s, z1s, z2s, u0s, u1s, u2s, """ Z1-Z2 Update """ for i in range(1, n): a = thetas[i] - thetas[i-1] + u2s[i] - u1s[i-1] e = pf.group_lasso_penalty(a, beta, rho) e = getattr(pf, pen_func)(a, beta, rho) summ = thetas[i] + thetas[i-1] + u2s[i] + u1s[i-1] z1s[i-1] = 0.5*(summ - e) z2s[i] = 0.5*(summ + e) Loading Loading @@ -131,7 +132,8 @@ class ParallelTVGL(TVGL): None, self.pipes[i][0], i, stopping_criteria),)) stopping_criteria, self.penalty_function),)) elif i == self.processes - 1: p = multiprocessing.Process(target=mp_parallel_tvgl, args=((self.thetas[self.chunk * i:], Loading @@ -151,7 +153,8 @@ class ParallelTVGL(TVGL): self.pipes[i-1][1], None, i, stopping_criteria), True)) stopping_criteria, self.penalty_function), True)) else: p = multiprocessing.Process(target=mp_parallel_tvgl, args=((self.thetas[self.chunk * i:self.chunk*(i+1)+1], Loading @@ -171,7 +174,8 @@ class ParallelTVGL(TVGL): self.pipes[i-1][1], self.pipes[i][0], i, stopping_criteria),)) stopping_criteria, self.penalty_function),)) self.procs.append(p) def run_algorithm(self, max_iter=10000): Loading @@ -190,6 +194,11 @@ class ParallelTVGL(TVGL): for p in self.procs: p.join() self.iteration = iteration print self.iteration #print self.iteration self.run_time = '{0:.3g}'.format(time.time() - start_time) self.thetas = [np.round(theta, self.roundup) for theta in self.thetas] self.final_tuning(True, MAX_ITER) #self.thetas = [np.round(theta, self.roundup) for theta in self.thetas] def terminate_pools(self): for p in self.procs: p.terminate()
TVGL.py +30 −4 Changes for TVGL.py: 30 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -9,13 +9,17 @@ class TVGL(object): np.set_printoptions(precision=3) def __init__(self, filename, blocks, lambd, beta, processes, penalty_function="Group Lasso"): processes, penalty_function="group_lasso", datecolumn=True): self.datecolumn = datecolumn self.processes = processes self.blocks = blocks self.penalty_function = penalty_function self.dimension = None self.emp_cov_mat = [0] * self.blocks self.real_thetas = [0] * self.blocks if self.datecolumn: self.blockdates = [0] * self.blocks self.read_data(filename) self.rho = self.get_rho() self.max_step = 0.1 Loading @@ -35,7 +39,7 @@ class TVGL(object): self.e = 1e-5 self.roundup = 1 def read_data(self, filename, comment="#", splitter=",", datecolumn=True): def read_data(self, filename, comment="#", splitter=","): with open(filename, "r") as f: comment_count = 0 for i, line in enumerate(f): Loading @@ -43,7 +47,7 @@ class TVGL(object): comment_count += 1 else: if self.dimension is None: if datecolumn: if self.datecolumn: self.dimension = len(line.split(splitter)) - 1 else: self.dimension = len(line.split(splitter)) Loading @@ -61,7 +65,9 @@ class TVGL(object): if i == 1: self.generate_real_thetas(line, splitter) continue if datecolumn: if count == 0 and self.datecolumn is True: start_date = line.strip().split(splitter)[0] if self.datecolumn: lst.append([float(x) for x in np.array(line.strip(). split(splitter)[1:])]) Loading @@ -71,6 +77,9 @@ class TVGL(object): split(splitter))]) count += 1 if count == self.obs: if self.datecolumn: end_date = line.strip().split(splitter)[0] self.blockdates[block] = start_date + " - " + end_date datablck = np.array(lst) tp = datablck.transpose() self.emp_cov_mat[block] = np.real( Loading Loading @@ -176,20 +185,37 @@ class TVGL(object): def final_tuning(self, stopping_criteria, max_iter): self.thetas = [np.round(theta, self.roundup) for theta in self.thetas] #self.only_true_false_edges() self.terminate_pools() if stopping_criteria: print "\nIterations to complete: %s" % self.iteration else: print "\nMax iterations (%s) reached" % max_iter def only_true_false_edges(self): for k in range(self.blocks): for i in range(self.dimension - 1): for j in range(i + 1, self.dimension): if self.thetas[k][i, j] != 0: self.thetas[k][i, j] = 1 self.thetas[k][j, i] = 1 else: self.thetas[k][i, j] = 0 self.thetas[k][j, i] = 0 def temporal_deviations(self): deviations = np.zeros(self.blocks - 1) for i in range(0, self.blocks - 1): dif = self.thetas[i+1] - self.thetas[i] np.fill_diagonal(dif, 0) deviations[i] = np.linalg.norm(dif) print deviations try: self.deviations = deviations/max(deviations) self.dev_ratio = float(max(deviations))/float(np.mean(deviations)) except ZeroDivisionError: self.deviations = deviations self.dev_ratio = 0 print "Temp deviations ratio: {0:.3g}".format(self.dev_ratio) def correct_edges(self): Loading
matlab/admm_algorithm.mdeleted 100644 → 0 +0 −65 Changes for matlab/admm_algorithm.m: 0 added lines, 65 removed lines. Original line number Diff line number Diff line function Theta = admm_algorithm(Theta,Z0,Z1,Z2,U0,U1,U2,... rho,lambda,beta,nju,empirical_covariance_matrices) max_iteration = 20000; index = 0; stopping_criteria = 0; total_stamps = size(Theta,1); dimension = size(Theta{1},1); while index < max_iteration && stopping_criteria == 0 %% Theta-Update for i = 1:total_stamps A = (Z0{i}+Z1{i}+Z2{i}-U0{i}-U1{i}-U2{i})/3; M = nju*(A+A')/2 - empirical_covariance_matrices{i}; [Q,D] = eig(M) Theta{i} = nju/2*Q*(D+sqrt(D^2 + 4/nju*diag(ones(dimension,1))))*Q'; end %% Z0-Update for i = 1:total_stamps Z0{i} = soft_thres_od(Theta{i} + U0{i},lambda,rho); % soft threshold odd end %% Z1,Z2-Update for i = 2:total_stamps A = Theta{i} - Theta{i-1} + U2{i} - U1{i-1}; E = group_lasso_penalty(A,2*beta/rho); Z_pre = 0.5*[Theta{i-1}+Theta{i}+U1{i}+U2{i};Theta{i-1}+Theta{i}+U1{i}+U2{i}] + ... 0.5*[-E;E]; Z1{i-1} = Z_pre(1:dimension,:); Z2{i} = Z_pre(dimension+1:end,:); end %% U0-Update for i = 1:total_stamps U0{i} = U0{i} + Theta{i} - Z0{i}; end %% U1,U2-Update for i = 2:total_stamps U1{i-1} = U1{i-1} + Theta{i-1} - Z1{i-1}; U2{i} = U2{i} + Theta{i} - Z2{i}; end %% Stopping condition if index > 0 fro_norm = 0; for i = 1:total_stamps dif = Theta{i} - Theta_pre{i}; fro_norm = fro_norm + norm(dif,'fro'); end if fro_norm < 1e-5 stopping_criteria = 1; end end Theta_pre = Theta; index = index + 1; end index end No newline at end of file