import pdb
from PyMC import *
from numpy.random import *




trial=1000
success=60




class m1(MetropolisHastings):

    def __init__(self):     
        'Initialization of model parameters and nodes' 
        MetropolisHastings.__init__(self)

        self.data=trial,success
        self.parameter('p',init_val=0.5)
       
    def model(self):      
        n,x=self.data
        self.uniform_prior(self.p,0,1)        
        self.binomial_like(x,n,self.p)
        
        
        
         
        
  
def run(iter=20000,burn=10000,thin=10,model='m1',plot=False,conv=False,auto=False):

    #the instance of the sampler is stored in the dictionary
    models={'m1':m1()}
    #now we invoke sampler reps times, each time simulating the data with specified vectors n, p

    #first time through model self initializes with simulated data
    sampler=models[model]
    sampler.sample(iterations=iter,burn=burn,thin=thin,plot=plot)

    results=sampler.summary(burn=burn,thin=thin)
    if conv:
        sampler.convergence(burn=burn,thin=thin)
    if auto:
        sampler.autocorrelation(burn=burn,thin=thin)
    name='stats'+str(i)
    sampler.export(results,name)

    #replaces data object with new simulated data for next iteration

    sampler.data=simdata(n,p)

if __name__=="__main__":
    run()





