def test(N, D): box = dl.box_generator(N, D) dag = box[0] ext = box[1] tr_dag = tr.trans_red(dag) good_dag = tr_dag lp = dl.lpd(tr_dag, ext[1], ext[0]) path = lp[1] tr_dag = good_dag dim = mps.mpsd(tr_dag, path) mm_dim = mmd.MM_dimension(dag) print dim print mm_dim return [dim[0], mm_dim]
def interval_test(DAG,start,end): lp = dl.lpd(DAG,start,end) length = lp[2] print 'The longest path between %s and %s is %d edges long' %(start,end,length) interval = lc.interval(DAG,start,end) N = interval.number_of_nodes() E = interval.number_of_edges() print 'The interval contains %d nodes and %d edges' %(N,E) c = clus.clustering(interval) print 'For the interval, c+ is %f, c0 is %f, c- is %f' %(c[0],c[1],c[2]) #MMd = MM.MM_dimension(interval) MPSD = mp.mpsd(interval,lp[1]) #print 'The MM dimension of the interval is %f and the MPSD is %f' %(MMd,MPSD) print 'The MPSD is %f' %MPSD[0]
def ms_dim_with_removal(number_of_trials, number_of_probabilities_considered, N, D, filename): """ arguments: number_of_trials for each probability, output data is averaged over this. number_of_probabilities_considered = integer: number of data points N = integer: number of nodes in network D = integer: dimension of network filename = string: output file path e.g. './testing.txt' """ start_time = time.time() #start clock myfile = open(filename, 'w') p_step = 1.0/number_of_probabilities_considered #define size of step in probability (prob = prob that edge is removed given it's causally allowed) myfile.write(str(D)+"D box w/ random removal prob p, and longest path l. Number trials averaged over=" + str(number_of_trials) + " N="+ str(N) + '\n') myfile.write('p' + '\t' + 'ms dimension (p)' +'\t' + 'std error' + '\n') #send column headings to output file #initialize lists for each output variable p_list_for_ms = [] ms_average_list = [] ms_std_error_list = [] p_range = np.arange(0.0,1.0+p_step,p_step) for p in p_range: #Loop over x, where x=10^p, so we get more points around prob's closer to 1 and can see phase transition-y behavior more clearly. #initialize lists for each quantity for which we require an individual value to be stored after every trial (e.g. for calculations stats) ms_list = [] ms_diff_sq_list = [] ms_failed_trials = 0 ms_sum = ms_average = 0.0 for _ in range(int(number_of_trials)): model = models.box_model(D, N,1-p) #create model with prob of no edge given edge is causally allowed = p DAG_with_removal = model[0] #networkx DAG object from above model tr_DAG_with_removal = tr.trans_red(DAG_with_removal) #transitively reduce DAG - doesn't effect lp or MM dimension, DOES MIDPOINT SCALING extremes = model[1] #returns list of extremes from model if nx.has_path(DAG_with_removal, extremes[1], extremes[0]): longest_path_between_extremes = dl.lpd(tr_DAG_with_removal, extremes[1],extremes[0]) #calculate longest path between extremes longest_path = longest_path_between_extremes[1] length_of_longest_path = longest_path_between_extremes[2] #save length of longest path between extremes else: length_of_longest_path=0 #if no path between extremes, longest path = 0 is physical. if length_of_longest_path > 2: ms_dimension = ms.mpsd(DAG_with_removal, longest_path)[0] #need to trans red DAG first as otherwise lp method does not return the correct longest path. James said he'd sorted this? else: ms_dimension = 0 ms_failed_trials += 1 ms_list.append(ms_dimension) if len(ms_list)<number_of_trials: ms_list.append(None) ms_sum = sum(ms_list) if (number_of_trials - ms_failed_trials - 1)>0: ms_average = ms_sum/float(number_of_trials-ms_failed_trials) else: ms_average=None ms_diff_sq_sum = 0.0 if (number_of_trials - ms_failed_trials - 1)>0: for i in range(number_of_trials - ms_failed_trials): ms_diff_sq_list.append((ms_list[i] - ms_average)**2) ms_diff_sq_sum += ms_diff_sq_list[i] ms_std_dev = math.sqrt(ms_diff_sq_sum/float(number_of_trials - ms_failed_trials -1)) ms_std_error = ms_std_dev/math.sqrt(float(number_of_trials - ms_failed_trials)) else: ms_std_error = 0 p_list_for_ms.append(p) ms_average_list.append(ms_average) ms_std_error_list.append(ms_std_error) myfile.write(str(p)+ '\t' + str(ms_average) + '\t' + str(ms_std_error) + '\n') clean = clean_for_plotting(ms_average_list,p_list_for_ms, ms_std_error_list) ms_average_list = clean[0] p_list_for_ms = clean[1] ms_std_error_list = clean[2] print "finished ", p , "probability" elapsed = (time.time() - start_time) #calculate time for method completion print "finished. Time elapsed = " , elapsed return [p_list_for_ms, ms_average_list, ms_std_error_list]