def test_simulateAuction_straightMUa(self):
     agentType = "msStraightMUa"
     pricePrediction = jointGMM(n_components=2)
     pricePrediction.means_  = numpy.asarray([[10,5], [0,5]],dtype='float')
     pricePrediction.weights_ = numpy.asarray([0.5,0.5],dtype = 'float')
     
     bids = simulateAuction(agentType = agentType, nAgents = 5, nGames = 10, nProc = 5, m = 2, pricePrediction = pricePrediction, ret = 'bids')
Exemple #2
0
def main():
    desc = "Simulates a homogenous auction randomizing over valuations."
    
    parser = argparse.ArgumentParser(description=desc)
    
    parser.add_argument('-i','--input', action = 'store', dest = 'input', required = True,
                        help = "Full path to price prediction pickle file.")
    
    parser.add_argument('-a','--agentType', action = 'store', dest = 'agentType', required = True,
                        help = "The participating agent's strategy type.")
    
#    parser.add_argument('-m', action = 'store', dest = 'm', required = False, default = 5,
#                        help = "The number of goods at auction.")
    
    parser.add_argument('-na', '--nAgents', action = 'store', dest = 'n', required = False, 
                        type = int, default = 5,
                         help = "Number of participating agents")
    
    parser.add_argument('-ng', '--nGames', action = 'store', dest = 'ng', required = False, type=int,
                        default = 10000, help = "Number of Simulations (games) to run.")
    
#    parser.add_argument('-o', '--output', action = 'store', dest = 'output', required = False,
#                        help = "Basename (or path) for output files.")
    
    parser.add_argument('-p', '--parallel', action = 'store', dest = 'parallel', required = False,
                        type = bool, default = True, 
                        help = "Run simulations in parallel or serially.")
    
    parser.add_argument('-np', '--nProc', action = 'store', dest = 'nProc', required = False,
                        default = multiprocessing.cpu_count() - 1, type = int,
                        help = "Number of processes to spawn if running in parallel mode.")
    
    parser.add_argument('-vmin', '--minValuation', action = 'store', dest = 'vmin', required = False,
                        default = 0, type = float, help = "Minimum Valuation.")
    
    parser.add_argument('-vmax', '--maxValuation', action = 'store', dest = 'vmax', required = False,
                        default = 50, type = float, help = "Maximum Valuation.")
    
    parser.add_argument('-v', '--verbose', action = 'store', dest = 'verbose', type = bool,
                        required = False, default = True, help = "Output runtime info to stdout.")
    
    parser.add_argument('-nc','--normCorr', action = 'store', dest = 'nc', type = bool,
                        required = False, default = True, help = "Comput Sample normalized correlation matrix.")
    
    parser.add_argument('-si', '--selfIdx', action = 'store', dest = 'si', type = int,
                        required = False, default = 0, help = "Compute highest other agent bid wrt this agents' index.")
    
    args = parser.parse_args()
    
    
    
    with open(args.input,'r') as f:
        pp = pickle.load(f)
        
    if isinstance(pp,jointGMM):
        m = pp.m()
    else:
        raise ValueError("Unknown Price Prediction Type.")
    
    start = time.time()
    bids = simulateAuction(pricePrediction = pp,
                           nGames          = args.ng,
                           agentType       = args.agentType,
                           nAgents         = args.n,
                           m               = m,
                           minValuation    = args.vmin,
                           maxValuation    = args.vmax,
                           parallel        = args.parallel,
                           nProc           = args.nProc,
                           verbose         = args.verbose)
    end = time.time()
    
    if args.verbose:
        print 'Finished {0} simulations in {1} seconds.'.format(args.ng, end-start)
    
    oDir     = os.path.dirname(os.path.realpath(args.input))
    basename = os.path.basename(args.input).split('.')[0]
    basename = basename + "_{0}_{1}".format(args.agentType,args.ng)
    
    obids = os.path.join(oDir,basename + "_bids.npz")
    ohob  = os.path.join(oDir,basename + "_hob.txt")
    
    with open(obids,'w') as f:
        numpy.save(f, bids)
        
    idx2keep = numpy.arange(args.n)
    idx2keep = numpy.delete(idx2keep,args.si)
    hob = numpy.max(bids[:,idx2keep,:],1)
    
    with open(ohob,'w') as f:
        numpy.savetxt(f, hob)
        
    if args.nc:
        print 'Plotting Absolute Value of Normalized Correlation Matrix'
        nc = numpy.corrcoef(hob.T)
        
        ocorr = os.path.join(oDir, basename + "_normcorr.txt")
        numpy.savetxt(ocorr, nc)

        cmap = plt.get_cmap('jet')
        ofig = os.path.join(oDir,basename + "_normcorr.pdf")
        plt.matshow(numpy.abs(nc),vmin=0,vmax=1,cmap=cmap)
        plt.title('Absolute Normalized Correlation Matrix')
        plt.colorbar()
        plt.savefig(ofig)
    
        print 'Normalized Correlation Matrix:'
        print nc
Exemple #3
0
def yw2Hob(**kwargs):
    oDir         = kwargs.get('oDir')
    if oDir == None:
        raise ValueError("Must specify oDir")
    if not os.path.exists(oDir):
        os.makedirs(oDir)
    
    agentType    = kwargs.get('agentType')
    if agentType == None:
        raise ValueError("Must specify agentType.")
    
    nAgents      = kwargs.get('nAgents',8)
    selfIdx      = kwargs.get('selfIdx',numpy.random.randint(nAgents))
    nGames       = kwargs.get('nGames',100)
    m            = kwargs.get('m',5)
    minPrice     = int(kwargs.get('minPrice',0))
    maxPrice     = int(kwargs.get('maxPrice',50))
    
    minValuation = kwargs.get('minValuation',0)
    maxValuation = kwargs.get('maxValuation',50)
    
    maxItr       = kwargs.get('maxItr', 100)
    tol          = kwargs.get('tol', 0.01)
    dampen       = kwargs.get('dampen', True)
    
    parallel     = kwargs.get('parallel',False)
    nProc        = kwargs.get('nProc',multiprocessing.cpu_count()-1)
    

    savePkl      = kwargs.get('savePkl',True)
    saveTime     = kwargs.get('saveTime',True)
    pltMarg      = kwargs.get('pltMarg',True)    
    pltKld       = kwargs.get('pltKld',True)
    pltKs        = kwargs.get('pltKs',True)
    verbose      = kwargs.get('verbose',True)
    
    if verbose:
        print 'oDir         = {0}'.format(oDir)
        print 'agentType    = {0}'.format(agentType)
        print 'nAgents      = {0}'.format(nAgents)
        print 'nGames       = {0}'.format(nGames)
        print 'm            = {0}'.format(m)
        print 'minPrice     = {0}'.format(minPrice)
        print 'maxPrice     = {0}'.format(maxPrice)
        print 'minValuation = {0}'.format(minValuation)
        print 'maxValuation = {0}'.format(maxValuation)
        print 'selfIdx      = {0}'.format(selfIdx)
        
        print 'maxItr       = {0}'.format(maxItr)
        print 'tol          = {0}'.format(tol)
        
        print 'parallel     = {0}'.format(parallel)
        print 'nProc        = {0}'.format(nProc)
    
        print 'savePkl      = {0}'.format(savePkl)
        print 'saveTime     = {0}'.format(saveTime)
        print 'pltMarg      = {0}'.format(pltMarg)
        print 'pltKld       = {0}'.format(pltKld)
        print 'pltKs        = {0}'.format(pltKs)
    
    if savePkl:
        pklDir = os.path.join(oDir, 'gmmPkl')
        if not os.path.exists(pklDir):
            os.makedirs(pklDir)
        else:
            [os.remove(f) for f in glob.glob(os.path.join(pklDir,'*.pkl'))]
            
    if pltMarg:
        margDir = os.path.join(oDir,'pltMarg')
        if not os.path.exists(margDir):
            os.makedirs(margDir)
        else:
            [os.remove(f) for f in glob.glob(os.path.join(margDir,'*.png'))]
            [os.remove(f) for f in glob.glob(os.path.join(margDir,'*.pdf'))]
            
    if saveTime:
        timeFile = os.path.join(oDir,'time.txt')
        if os.path.exists(timeFile):
            os.remove(timeFile)   
            
    klFile = os.path.join(oDir,'kld.txt')
    if os.path.exists(klFile):
        os.remove(klFile)
        
    ksFile = os.path.join(oDir,'ks.txt')
    if os.path.exists(ksFile):
        os.remove(ksFile)   
            
    #initial uniform distribution
    tempDist = []
    p = float(1)/round(maxPrice - minPrice)
    a = [p]*(maxPrice - minPrice)
    binEdges = numpy.arange(minPrice,maxPrice+1,1)
    for i in xrange(m):
        tempDist.append((numpy.atleast_1d(a),numpy.atleast_1d(binEdges)))
        
    currentDist = margDistSCPP(tempDist)
    
    del tempDist, p, a 
    
    if pltMarg:
        of = os.path.join(margDir,'yw2SccpHob_{0}_m{1}_n{2}_init.pdf'.format(agentType,m,nAgents))
        title = 'Marginal SCPP {0}'.format(agentType)
        xlabel = r'$q$'
        ylabel = r'$p(q)$'
        currentDist.graphPdfToFile(fname = of, title = title,  xlabel = xlabel, ylabel = ylabel)
        
    klList = []
    ksList = []
    for t in xrange(maxItr):
        if verbose:
            print ''
            print 'Iteration = {0}'.format(t)
            
        if dampen:
            kappa = numpy.float(maxItr - t) / maxItr
        else:
            kappa = 1
            
        if verbose:
            print 'kappa = {0}'.format(kappa)
            
        if verbose or saveTime:
            start = time.time()
            
        hob = simulateAuction( agentType       = agentType,
                               pricePrediction = currentDist,
                               m               = m,
                               nAgents         = nAgents,
                               nGames          = nGames,
                               parallel        = parallel,
                               nProc           = nProc,
                               minValuation    = minValuation,
                               maxValuation    = maxValuation,
                               retType         = 'hob',
                               selfIdx         = selfIdx,
                               verbose         = verbose)
        
        if verbose or saveTime:
            end = time.time()
            
        if verbose:
            print 'Simulated {0} auctions with {1} {2} agents in {3} seconds.'.format(nGames, nAgents, agentType, end-start)

        if savePkl:
            pklFile = os.path.join(pklDir, 'yw2ScppHob_{0}_m{1}_n{2}_{3:05d}.pkl'.format(agentType,m,nAgents,t))
            with open(pklFile,'wb') as f:
                pickle.dump(currentDist,f) 
                
        histData = []
        for goodIdx in xrange(hob.shape[1]):
            histData.append(numpy.histogram(hob[:,goodIdx],binEdges,density=True))
            
        newDist = margDistSCPP(histData)
        
        klList.append( klDiv(currentDist, newDist) )
        
        if verbose:
            print 'kld = {0}'.format(klList[-1])
            
        with open(klFile,'a') as f:
            f.write("{0}\n".format(klList[-1]))
        
        ksList.append( ksStat(currentDist,newDist) )
        
        if verbose:
            print 'kls = {0}'.format(ksList[-1])
            
        with open(ksFile,'a') as f:
            f.write("{0}\n".format(ksList[-1]))
        
        if saveTime:
            with open(timeFile,'a') as f:
                f.write("{0}\n".format(end-start))
                
        if pltMarg:
            of = os.path.join(margDir,'yw2SccpHob_{0}_m{1}_n{2}_{3:05d}.pdf'.format(agentType,m,nAgents,t))
            if verbose:
                print 'Plotting Marginal pdf to {0}'.format(of)
            title = 'Marginal SCPP {0}'.format(agentType)
            xlabel = r'$q$'
            ylabel = r'$p(q)$'
            newDist.graphPdfToFile(fname = of, title = title, xlabel = xlabel, ylabel = ylabel)
                
        if klList[-1] < tol or t == (maxItr - 1):
            
            if verbose:
                print 'TERMINATED:'
                if klList[-1] < tol:
                    print '\tkld = {0} < tol = {1}'.format(klList[-1],tol)
                else:
                     
                    print '\tt = {0}, maxItr = {1}'.format(t,maxItr)
                
            if pltKld:
                
                if verbose:
                    print 'Plotting K-L Divergence vs. Iteration.'
                    
                klPdf = os.path.join(oDir,'kld.pdf')
                plt.figure()
                plt.plot(range(len(klList)), klList)
                plt.xlim((0,maxItr))
                plt.ylabel('K-L Divergence')
                plt.xlabel('Iteration')
                plt.title('Marginal SCPP {0}'.format(agentType))
                plt.savefig(klPdf)
                plt.close()
            
            if pltKs:
                
                if verbose:
                    print 'Plotting K-S Stat vs. Iteration'
                
                ksPdf = os.path.join(oDir,'ks.pdf')
                plt.figure()
                plt.plot(range(len(ksList)), ksList)
                plt.xlim((0,maxItr))
                plt.ylabel('K-S Statistic')
                plt.xlabel('Iteration')
                plt.title('Marginal SCPP {0}'.format(agentType))
                plt.savefig(ksPdf)
                plt.close()
                
            
            if verbose:
                print 'DONE!!!!!'
            
            break
        else:
            if verbose:
                print 'Updating distribution.'
            currentDist = updateDist(currentDist,newDist,kappa)
            del hob, newDist
              
Exemple #4
0
def jointGmmScpp(**kwargs):
    """
    NOTE: EXTRA MODEL IS FIT TO FULL COVAR GMM - regardless of
    covariance_type kwarg.
    """
    
    kwargs['oDir']         = kwargs.get('oDir')
    if kwargs['oDir'] == None:
        raise ValueError("Must specify output directory - oDir.")
    
    kwargs['agentType']    = kwargs.get('agentType')
    
    if kwargs['agentType'] == None:
        raise ValueError('Must specify agent type - agentType.')
    kwargs['nAgents']      = kwargs.get('nAgents',5)

    kwargs['selfIdx']      = kwargs.get('selfIdx',numpy.random.randint(kwargs['nAgents']))
    
    kwargs['nGames']       = kwargs.get('nGames',10000)
    kwargs['nklsamples']   = kwargs.get('nklsamples',1000)
    
    kwargs['maxItr']       = kwargs.get('maxItr',100)

    kwargs['tol']          = kwargs.get('tol',0.01)
    
    kwargs['aicCompMin']   = kwargs.get('aicCompMin',5)

    kwargs['aicCompMax']   = kwargs.get('aicCompMax',21)

    kwargs['aicMinCovar']  = kwargs.get('aicMinCovar',0.1)

    kwargs['minPrice']     = kwargs.get('minPrice',0)
    
    kwargs['maxPrice']     = kwargs.get('maxPrice',numpy.float('inf'))
    
    kwargs['covariance_type']   = kwargs.get('covariance_type','full')
    
    kwargs['m']            = kwargs.get('m',5)

    kwargs['minValuation'] = kwargs.get('vmin',0)
    kwargs['maxValuation'] = kwargs.get('vmax',50)

    kwargs['parallel']     = kwargs.get('parallel',True)
    
    kwargs['nProc']        = kwargs.get('nProc', multiprocessing.cpu_count())
    
    kwargs['verbose']      = kwargs.get('verbose', True)
    
    kwargs['pltMarg']      = kwargs.get('pltMarg', True)
    
    kwargs['l']            = kwargs.get('l')
    
    kwargs['timeStamp']    = timestamp_()

    ps = paramString(**kwargs)
            
    kwargs['oDir'] = os.path.join(kwargs['oDir'],ps)
    if not os.path.exists(kwargs['oDir']):
        os.makedirs(kwargs['oDir'])
    
    models = numpy.arange(kwargs['aicCompMin'], kwargs['aicCompMax'])
            
    if kwargs['verbose']:    
        table = []
        
        for k,v in kwargs.iteritems():
            table.append([k,str(v)])
        
        pprint_table(sys.stdout, table)
    
    with open(os.path.join(kwargs['oDir'],'params.txt'),'w') as f:
        pprint_table(f, table)
        
    with open(os.path.join(kwargs['oDir'],'params.json'),'w') as f:
        json.dump(kwargs, f)
        
    kwargs['pricePrediction'] = uniformpp(kwargs['m'],kwargs['minValuation'],kwargs['maxValuation'])
    
    idx2keep = numpy.arange(kwargs['nAgents'])
    idx2keep = numpy.delete(idx2keep, kwargs['selfIdx'])
    if kwargs['verbose']:
        print 'indicies to keep = {0}'.format(idx2keep)
    
    filePostfix = fileNamePostfix(**kwargs)
    
    
    for itr in xrange(kwargs['maxItr']):
        itrStart = time.time()
        if kwargs['verbose']:
            print 'Iteration {0}'.format(itr+1)
        
        simStart = time.time()
        bids = simulateAuction(**kwargs)
        simEnd = time.time()
#        simFile = os.path.realpath(os.path.join(kwargs['oDir'],"simulationTime_{0}.txt".format(ps)))
        simFile = os.path.join(kwargs['oDir'],'simTime_0.01.txt')
#        if not simFile:
#            with open(os.path.realpath(simFile),'w+') as f:
#                numpy.savetxt(f, numpy.atleast_1d(simEnd-simStart))
#        else:
#        with open(os.path.join(kwargs['oDir'],"simulationTime_{0}.txt".format(ps)),'a+') as f:
        with open(simFile,'a+') as f:
            numpy.savetxt(f, numpy.atleast_1d(simEnd-simStart)) 
            
        if kwargs['verbose']:
            print 'Simulated {0} auctions in {1} seconds'.format(kwargs['nGames'],simEnd-simStart)
            
        del simStart, simEnd
        
        bidsFile = 'bids_{0:04}_{1}.npy'.format(itr,filePostfix)
        with open(os.path.join(kwargs['oDir'], bidsFile),'w') as f:
            numpy.save(f, bids)
            
        hob = numpy.max(bids[:,idx2keep,:],1)
        hobFile = os.path.join(kwargs['oDir'],'hob_{0:04}_{1}.txt'.format(itr,filePostfix))
        with open(hobFile,'w') as f:
            numpy.savetxt(f,hob)
        
        del bids
                    
        nextpp = jointGMM(covariance_type = kwargs.get('covariance_type'))
        temppp, aicValues, compRange = nextpp.aicFit(X=hob, compRange = models, min_covar = kwargs['aicMinCovar'], verbose = kwargs['verbose'])
        
        aicFile = os.path.join(kwargs['oDir'],'aic_{0:03}_{1}.pdf'.format(itr+1,filePostfix))
        
        pltAic(compRange,aicValues,itr,aicFile)
        
        del hob,temppp,compRange
        
        ppFile = os.path.join(kwargs['oDir'], 'gmmScpp_{0:04}_{1}.pkl'.format(itr,filePostfix))
        with open(ppFile,'w') as f:
            pickle.dump(nextpp,f)
            
        if kwargs['pltMarg']:
            oFile = os.path.join(kwargs['oDir'],'marg_{0:04}_{1}.pdf'.format(itr,filePostfix))
            nextpp.pltMarg(oFile = oFile)
        
        with open(os.path.join(kwargs['oDir'],'aic_{0:04}_{1}.txt'.format(itr,filePostfix)),'a') as f:
            numpy.savetxt(f,numpy.atleast_1d(aicValues).T)
        
        if kwargs['verbose']:
            print 'AIC Fit: number of components = {0}'.format(nextpp.n_components)
            
        with open(os.path.join(kwargs['oDir'],'n_components_{0}.txt'.format(filePostfix)), 'a') as f:
            numpy.savetxt(f,numpy.atleast_1d(nextpp.n_components))
            
        if itr > 0:
            kld = numpy.abs(apprxJointGmmKL(kwargs['pricePrediction'], nextpp, 
                            nSamples = kwargs['nklsamples'], verbose = kwargs['verbose']))
            
            with open(os.path.join(kwargs['oDir'],'kld_{0}.txt'.format(filePostfix)),'a') as f:
                numpy.savetxt(f,numpy.atleast_1d(kld))
                
            if kwargs['verbose']:
                print 'Symmetric KL Distance = {0}'.format(kld)
        
        itrEnd = time.time()
        with open(os.path.join(kwargs['oDir'], "itrTime_{0}.txt".format(filePostfix)),'a') as f:
            numpy.savetxt(f, numpy.atleast_1d(itrEnd-itrStart))
            
        kwargs['pricePrediction'] = nextpp
        
        if itr > 0:
            if kld < kwargs['tol']:
                if kwargs['verbose']:
                    print 'kld = {0} < tol = {1}'.format(kld, kwargs['tol'])
                    print 'CONVERGED!'
                    
                break
        else:
            print ''
            
    with open(os.path.join(kwargs['oDir'],'kld_{0}.txt'.format(filePostfix)),'r') as f:
        kld = numpy.loadtxt(f, 'float')
     
    f, ax = plt.subplots()
    plt.plot(kld,'r-',linewidth=3)
    plt.title("Absolute Symmetric K-L Divergence")
    plt.xlabel("Iteration")
    plt.ylabel(r"|kld|")
    plt.savefig(os.path.join(kwargs['oDir'],'kld_{0}.pdf'.format(ps)))
    
    del kld
    
    with open(os.path.join(kwargs['oDir'],'n_components_{0}.txt'.format(filePostfix)),'r') as f:
        comp = numpy.loadtxt(f)
        
    f,ax = plt.subplots()
    colors = ['#0A0A2A']*len(aicValues)
    ax.bar(range(len(comp)), comp, color=colors, align = 'center')
    ax.set_ylabel('GMM Model (Number of Components)')
    ax.set_xlabel('Iteration')
    ax.set_title('Model Selection')
    plt.ylim([0,numpy.max(comp) + 0.5])
    plt.savefig(os.path.join(kwargs['oDir'],'n_components_{0}.pdf'.format(ps)))
    
    del comp
    
    if kwargs['verbose']:
        print 'Simulating {0} auctions after scpp converged.'.format(kwargs['nGames'])
    
    # To check if distribution is SCPP, after convergence simulate
    # more bids then evaluate measures of similarity between the resulting 
    # bids and the scpp candidate.
    start = time.time()    
    extraBids = simulateAuction(**kwargs)
    end = time.time()
    
    with open(os.path.join(kwargs['oDir'],'extraBids_{0}.npy'.format(filePostfix)), 'w') as f:
        numpy.save(f, extraBids)
    
    if kwargs['verbose']:
        print 'Simulated {0} holdout auctions in {1} seconds'.format(kwargs['nGames'],end-start)
        
    extraHob = numpy.max(extraBids[:,idx2keep,:],1)
    with open(os.path.join(kwargs['oDir'],'extraHob_{0}.txt'.format(filePostfix)),'w') as f:
        numpy.savetxt(f, extraHob)
    
    ll = numpy.sum(kwargs['pricePrediction'].eval(extraHob)[0])
    
    if kwargs['verbose']:
        print 'log-likelihood hold out = {0}'.format(ll)
        
    with open(os.path.join(kwargs['oDir'],'extraHobLL_{0}.txt'.format(filePostfix)),'w') as f:
        numpy.savetxt(f,numpy.atleast_1d(ll))
        
    # fit another model to held out data and
    # compute the last skl between the scpp and 
    # the extra model
    
    extraModel = jointGMM()
    gmm, aicValues, compRange = extraModel.aicFit(X=extraHob, compRange = models, min_covar = kwargs['aicMinCovar'], verbose = kwargs['verbose'])
    kld = numpy.abs(apprxJointGmmKL(kwargs['pricePrediction'], extraModel, nSamples = kwargs['nklsamples'], verbose = kwargs['verbose']))
    
    f,ax = plt.subplots()
    colors = ['#777777']*len(aicValues)
    colors[numpy.argmin(aicValues)] = 'r'
    ax.bar(compRange, aicValues, color=colors, align = 'center')
    ax.set_ylabel('AIC score')
    ax.set_xlabel('GMM Model (Number of Components)')
    ax.set_title('Iteration {0}'.format(itr+1))
    plt.ylim([0,numpy.max(aicValues) + 0.5])
    extraAicFile = "extraAic_{0:04}_{1}.pdf".format(itr+1,ps)
    plt.savefig(os.path.join(kwargs['oDir'],extraAicFile))
    
    del gmm,aicValues, compRange
    
    if kwargs['verbose']:
        print 'SKL-D between SCPP proposal and extra model = {0}'.format(kld)
        
    extraSkdFile = os.path.join(kwargs['oDir'],'extraHobSkl_{0}.txt'.format(filePostfix))
    with open(extraSkdFile,'w') as f:
        numpy.savetxt(f, numpy.atleast_1d(kld))