Пример #1
0
def test_conjugate():
    
    print '\nTesting to ensure partitions are correctly conjugated.'
    partition = [1]
    conjugate = [1]
    conj = parts.conjugate(partition)
    if conj != conjugate:
        print 'parts.conjugate() is broken. Test 1 FAIL '
    else:
        print 'parts.conjugate() works.  Test 1 PASS'
        
    partition = [10]
    conjugate = [1,1,1,1,1,1,1,1,1,1]
    conj = parts.conjugate(partition)
    if conj != conjugate:
        print 'parts.conjugate() is broken. Test 2 FAIL '
    else:
        print 'parts.conjugate() works. Test 2 PASS '
        
    partition = [3,2,1] # symmetrical partition and conjugate
    conjugate = [3,2,1]
    conj = parts.conjugate(partition)
    if conj != conjugate:
        print 'parts.conjugate() is broken. Test 3 FAIL'
    else:
        print 'parts.conjugate() works. Test 3 PASS'
    
    partition = [12,9,8,8,7,5,4,2,2,1]
    conjugate = [10, 9, 7, 7, 6, 5, 5, 4, 2, 1, 1, 1]
    conj = parts.conjugate(partition)
    if conj != conjugate:
        print 'parts.conjugate() is broken. Test 4 FAIL'
    else:
        print 'parts.conjugate() works. Test 4 PASS'
    
    return
Пример #2
0
def kdens_unbias(): 
    """ The code below compares random partitioning nplottions of Sage and Locey and McGlinn (2013)
    to full feasible sets. These analyses confirm that the algorithms are unbiased. The code
    uses full feasible sets, the random partitioning function in Sage, and the random partitioning
    for cases when 0' are or are not allowed."""

    algs = ['multiplicity','top_down','divide_and_conquer','bottom_up']
    colors = ['#00CED1','#FF1493','k','gray']

    fig = plt.figure()
    nplot = 1 # a variable used to designate subplots
    sample_size = 10000 # min number of macrostates needed to safely capture distributional
                      # features across the feasible set
    
    metrics = ['gini', 'variance', 'median', 'skewness', 'evar']
    metric = metrics[2]
        
    while nplot <= 4:
        ax =fig.add_subplot(2,2,nplot)

        if nplot < 3:
            q = 50 # values of q and n small enough to generate 
            n = 10 # the entire feasible set
        else:
            q = 100 # values of q and n requiring random samples
            n = 20  # of feasible sets
        
        partitions = []
        for i, alg in enumerate(algs):
            if nplot == 1 or nplot == 3:
                zeros = False
                D = {}
                partitions = parts.rand_partitions(q, n, sample_size, alg, D, zeros)
            else:
                D = {}
                zeros = True
                partitions = parts.rand_partitions(q, n, sample_size, alg, D, zeros)
                
            kdens = mt.get_kdens_obs(partitions, metric)
            plt.xlim(min(kdens[0]), max(kdens[0]))
            plt.plot(kdens[0], kdens[1], color=colors[i], lw=0.7)
            
        if nplot == 1: # using the full feasible set, no zero values (i.e. proper integer partitions)
            
            partitions = []
            numparts = parts.NrParts(q, n)    
            partition = parts.first_lexical(q, n, None)
            partitions.append(partition)
            ct2 = 0
            while len(partitions) < numparts:
                    
                partition = parts.next_restricted_part(partition)
                if len(partition) == n: partitions.append(partition) 
                else:
                    print 'bug in next_restricted_part()'
                    sys.exit()    
                
            kdens = mt.get_kdens_obs(partitions, metric)
            plt.xlim(min(kdens[0]), max(kdens[0]))
            plt.plot(kdens[0], kdens[1], color='r', lw=3.0, alpha=0.5)
                
        elif nplot == 2: # using the full feasible set, zero values included
            partitions = []    
                
            for p in Partitions(q):
                partition = list(p)
                
                if len(partition) == n:
                    partitions.append(partition) 
                    
                elif len(partition) < n:
                    zeros = [0]*(n-len(partition))
                    partition.extend(zeros)
                    partitions.append(partition)
            
            kdens = mt.get_kdens_obs(partitions, metric)
            plt.xlim(min(kdens[0]), max(kdens[0]))
            plt.plot(kdens[0], kdens[1], color='r', lw=3.0, alpha=0.5)
    
        elif nplot == 3: 
            partitions = []
            while len(partitions) < sample_size: # Use the random partition nplottion in Sage to generate partitions for q and n
                partition = Partitions(q).random_element()
                if len(partition) == n:
                    partitions.append(partition)
                     
                else:
                    partition = parts.conjugate(partition)
                    if len(partition) == n:
                        partitions.append(partition)
                             
            kdens = mt.get_kdens_obs(partitions, metric)
            plt.xlim(min(kdens[0]), max(kdens[0]))
            plt.plot(kdens[0], kdens[1], color='r', lw=3.0, alpha=0.5)
        
        elif nplot == 4:
            partitions = []
            while len(partitions) < sample_size: # Use the random partition nplottion in Sage to generate partitions for q and n
                part = list(Partitions(q).random_element())
                if len(part) == n:
                    partitions.append(part)
                
                elif len(part) < n:
                    zeros = [0]*(n - len(part))
                    part.extend(zeros)
                    partitions.append(part)
                
            kdens = mt.get_kdens_obs(partitions, metric)
            plt.xlim(min(kdens[0]), max(kdens[0]))
            plt.plot(kdens[0], kdens[1], color='r', lw=3.0, alpha=0.5)
                     
        if nplot == 1:
            plt.plot([0],[0], color='#00CED1', lw=2, label = 'Multiplicity')
            plt.plot([0],[0], color='#FF1493',lw=2, label='Top-down')    
            plt.plot([0],[0], color='k',lw=2, label='Divide & Conquer')
            plt.plot([0],[0], color='gray',lw=2, label='Bottom-up')
            plt.plot([0],[0], color='r',lw=2, label='FS q='+str(q)+', n='+str(n),alpha=0.5)
            plt.legend(bbox_to_anchor=(-0.02, 1.00, 2.24, .2), loc=10, ncol=5, mode="expand",prop={'size':8})#, borderaxespad=0.)
            
        if nplot == 1 or nplot == 3:
            plt.ylabel("density", fontsize=12)    
        
        if nplot == 3 or nplot == 4:
            plt.xlabel(metric, fontsize=12)
        
        print nplot
        nplot+=1
        
        plt.tick_params(axis='both', which='major', labelsize=8)
        
    plt.savefig('kdens_'+metric+'_'+str(sample_size)+'.png', dpi=500, pad_inches=0)