def __init__(self,config,ds,run,nsample,thin,burnin,levels=-1,randomize=True,randomSamplerOrder=True): self.config = Configuration(config,randomizePriors=randomSamplerOrder) if randomize: self.config.randomize() self.run = run self.nsample = nsample self.thin = thin self.burnin = burnin self.levels = levels self.randomSamplerOrder = randomSamplerOrder # default to using all levels if self.levels == -1: self.levels = self.config.levels # how many functions to use self.f = sum(self.config.nf[:self.levels+1]) self.x, self.p, self.dm, self.kernels, self.priors = self.config.get() self.yKernel = self.kernels[0] for i in range(self.levels+1): k = 'k%d'%(i+1) self.__dict__[k] = self.kernels[i+1] self.kernels = self.kernels[1:self.levels+2] if self.config.hierarchy and self.levels < self.config.levels: # self.yKernel = self.yKernel.kernels[-1] args = self.yKernel.kernels[:self.levels]+[self.yKernel.kernels[-1]] self.yKernel = Hierarchical(*args) self.y = pd.read_csv(os.path.join(config,ds,'data.csv')).values self.dm = self.dm[:self.f,:] # self.model = self.config.model self.model = Model(self.x,self.y,self.dm) if randomize: self.model.beta = self.config.model.beta[:,:self.f] self._buildSamplers() kwargs = {} kwargs['yKernel'] = self.yKernel for i,k in enumerate(self.kernels): kwargs['k%d'%(i+1)] = k self.freeze = Freezer(model=self.model,**kwargs) self.startSample = self.freeze.freeze() self.samples = []
def __init__(self,config,ds,run,nsample,thin,burnin,levels=-1,*args,**kwargs): Sample.__init__(self,config,ds,run,nsample,thin,burnin,levels,*args,**kwargs) self.levels=self.config.levels-1 kern = self.__dict__['k%d'%(self.levels+2)] self.yKernelOld = self.yKernel self.yKernel = Addition(self.yKernel,kern) del self.kernels[-1] kwargs = {} kwargs['yKernel'] = self.yKernel for i,k in enumerate(self.kernels): kwargs['k%d'%(i+1)] = k self.freeze = Freezer(model=self.model,**kwargs) self.startSample = self.freeze.freeze()
class Sample(object): def __init__(self,config,ds,run,nsample,thin,burnin,levels=-1,randomize=True,randomSamplerOrder=True): self.config = Configuration(config,randomizePriors=randomSamplerOrder) if randomize: self.config.randomize() self.run = run self.nsample = nsample self.thin = thin self.burnin = burnin self.levels = levels self.randomSamplerOrder = randomSamplerOrder # default to using all levels if self.levels == -1: self.levels = self.config.levels # how many functions to use self.f = sum(self.config.nf[:self.levels+1]) self.x, self.p, self.dm, self.kernels, self.priors = self.config.get() self.yKernel = self.kernels[0] for i in range(self.levels+1): k = 'k%d'%(i+1) self.__dict__[k] = self.kernels[i+1] self.kernels = self.kernels[1:self.levels+2] if self.config.hierarchy and self.levels < self.config.levels: # self.yKernel = self.yKernel.kernels[-1] args = self.yKernel.kernels[:self.levels]+[self.yKernel.kernels[-1]] self.yKernel = Hierarchical(*args) self.y = pd.read_csv(os.path.join(config,ds,'data.csv')).values self.dm = self.dm[:self.f,:] # self.model = self.config.model self.model = Model(self.x,self.y,self.dm) if randomize: self.model.beta = self.config.model.beta[:,:self.f] self._buildSamplers() kwargs = {} kwargs['yKernel'] = self.yKernel for i,k in enumerate(self.kernels): kwargs['k%d'%(i+1)] = k self.freeze = Freezer(model=self.model,**kwargs) self.startSample = self.freeze.freeze() self.samples = [] def load(self,f): samples = Analysis.loadSamples(f) self.startSample = samples[0] self.samples = samples self.freeze.push(**self.samples[-1]) def _buildSamplers(self): self.samplers = [] if self.config.hierarchy: self.samplers.append((self.yKernel.__dict__['k%d'%(self.levels+1)],'sigma', Slice('ySigma', lambda x: self.model.dataLikelihood(self.yKernel,**{'k%d_sigma'%(self.config.levels+1):x}), lambda x: self.priors['yKernel']['sigma'].logpdf(x), self.config.config.getfloat('yKernel','slice-w'),self.config.config.getfloat('yKernel','slice-m'),logspace=True) )) for i in range(self.levels): self.samplers.append((self.yKernel.__dict__['k%d'%(i+1)],'sigma', Slice('sigma', lambda x,i=i: self.model.dataLikelihood(self.yKernel,**{'k%d_sigma'%i:x}), lambda x: self.priors['k%d'%(i+1)]['sigma'].logpdf(x), self.config.config.getfloat('k%d'%(i+1),'slice-w'),self.config.config.getfloat('k%d'%(i+1),'slice-m'),logspace=True) )) self.samplers.append((self.yKernel.__dict__['k%d'%(i+1)],'lengthscale', Slice('lengthscale', lambda x,i=i: self.model.dataLikelihood(self.yKernel,**{'k%d_lengthscale'%i:x}), lambda x: self.priors['k%d'%(i+1)]['lengthscale'].logpdf(x), self.config.config.getfloat('k%d'%(i+1),'slice-w'),self.config.config.getfloat('k%d'%(i+1),'slice-m'),logspace=True) )) self.samplers.append((self.k1,'sigma', PriorSlice('k1-sigma', self.model, self.priors['functions'][0], self.priors['k1']['sigma'], 'sigma', self.config.config.getfloat('k1','slice-w'), self.config.config.getfloat('k1','slice-m'), logspace=True) )) self.samplers.append((self.k1,'lengthscale', PriorSlice('k1-lengthscale', self.model, self.priors['functions'][0], self.priors['k1']['lengthscale'], 'lengthscale', self.config.config.getfloat('k1','slice-w'), self.config.config.getfloat('k1','slice-m'), logspace=True) )) else: self.samplers.append((self.yKernel,'sigma', Slice('ySigma', lambda x: self.model.dataLikelihood(self.yKernel,sigma=x), lambda x: self.priors['yKernel']['sigma'].logpdf(x), self.config.config.getfloat('yKernel','slice-w'),self.config.config.getfloat('yKernel','slice-m'),logspace=True) )) for i in range(self.levels+1): k = 'k%d'%(i+1) self.samplers.append((self.__dict__[k],'sigma', PriorSlice('%s-sigma'%k, self.model, self.priors['functions'][i], self.priors[k]['sigma'], 'sigma', self.config.config.getfloat(k,'slice-w'), self.config.config.getfloat(k,'slice-m'), logspace=True) )) self.samplers.append((self.__dict__[k],'lengthscale', PriorSlice('%s-lengthscale'%k, self.model, self.priors['functions'][i], self.priors[k]['lengthscale'], 'lengthscale', self.config.config.getfloat(k,'slice-w'), self.config.config.getfloat(k,'slice-m'), logspace=True) )) def sample(self): for i in range(len(self.samples),self.nsample): self._sampleIteration() if i % self.thin == 0 and i > self.burnin: self.samples.append(self.freeze.freeze()) def _sampleIteration(self): order = range(self.levels+1+len(self.samplers)) if self.randomSamplerOrder: order = np.random.choice(order,len(order),replace=False) for o in order: if o < self.config.f: prior = self.priors['functions'][o] prior.sample(self.model,self.yKernel) else: obj, param, sampler = self.samplers[o-self.levels-1] obj.__dict__[param] = sampler.sample(obj.__dict__[param]) # print # for f,prior in self.priors['functions'].iteritems(): # if f > self.levels: # continue # prior.sample(self.model,self.yKernel) # # for obj, param, sampler in self.samplers: # obj.__dict__[param] = sampler.sample(obj.__dict__[param]) def save(self,dir): self.freeze.save(self.samples,os.path.join(dir,'samples.json')) self.freeze.save([self.startSample],os.path.join(dir,'startSample.json'))
ind = min(np.where(f < config.cumnf)[0]) + 1 # print f,ind kernel = kernels[ind] betaTrue[:, f] = scipy.stats.multivariate_normal.rvs( np.zeros(x.shape[0]), kernel.K(x)) # y = np.dot(betaTrue,dm) + scipy.stats.multivariate_normal.rvs(np.zeros(x.shape[0]),kernels[0].K(x),size=p).reshape((x.shape[0],p)) y = np.zeros((x.shape[0], dm.shape[1])) for i in range(dm.shape[1]): y[:, i] = np.dot(betaTrue, dm[:, i]) + scipy.stats.multivariate_normal.rvs( np.zeros(x.shape[0]), kernels[0].K(x)) model = Model(x, y, dm) model.beta = betaTrue kwargs = { 'k%d' % (i + 1): kernels[i + 1] for i in range(config.levels + 1) } freeze = Freezer(yKernel=kernels[0], model=model, **kwargs) trueSample = freeze.freeze() freeze.save([trueSample], os.path.join(args.configuration, args.label, 'parameters-true.json')) pd.DataFrame(y).to_csv(os.path.join(args.configuration, args.label, 'data.csv'), index=False)
class SampleIntegrate(Sample): """Integrate out the lowest level of hierarchical variation.""" def __init__(self,config,ds,run,nsample,thin,burnin,levels=-1,*args,**kwargs): Sample.__init__(self,config,ds,run,nsample,thin,burnin,levels,*args,**kwargs) self.levels=self.config.levels-1 kern = self.__dict__['k%d'%(self.levels+2)] self.yKernelOld = self.yKernel self.yKernel = Addition(self.yKernel,kern) del self.kernels[-1] kwargs = {} kwargs['yKernel'] = self.yKernel for i,k in enumerate(self.kernels): kwargs['k%d'%(i+1)] = k self.freeze = Freezer(model=self.model,**kwargs) self.startSample = self.freeze.freeze() def _buildSamplers(self): self.samplers = [] self.samplers.append((self.yKernel,'k1_sigma', Slice('ySigma', lambda x: self.model.dataLikelihood(self.yKernel,k1_sigma=x), lambda x: self.priors['yKernel']['sigma'].logpdf(x), self.config.config.getfloat('yKernel','slice-w'),self.config.config.getfloat('yKernel','slice-m'),logspace=True) )) for i in range(self.levels+1): k = 'k%d'%(i+1) self.samplers.append((self.__dict__[k],'sigma', PriorSlice('%s-sigma'%k, self.model, self.priors['functions'][i], self.priors[k]['sigma'], 'sigma', self.config.config.getfloat(k,'slice-w'), self.config.config.getfloat(k,'slice-m'), logspace=True) )) self.samplers.append((self.__dict__[k],'lengthscale', PriorSlice('%s-lengthscale'%k, self.model, self.priors['functions'][i], self.priors[k]['lengthscale'], 'lengthscale', self.config.config.getfloat(k,'slice-w'), self.config.config.getfloat(k,'slice-m'), logspace=True) )) k = 'k%d'%(self.levels+1) self.samplers.append((self.yKernel,'k2_sigma', Slice('ySigma2', lambda x: self.model.dataLikelihood(self.yKernel,k2_sigma=x), lambda x: self.priors[k]['sigma'].logpdf(x), self.config.config.getfloat(k,'slice-w'),self.config.config.getfloat(k,'slice-m'),logspace=True) )) self.samplers.append((self.yKernel,'k2_lengthscale', Slice('yLengthscale2', lambda x: self.model.dataLikelihood(self.yKernel,k2_lengthscale=x), lambda x: self.priors[k]['lengthscale'].logpdf(x), self.config.config.getfloat(k,'slice-w'),self.config.config.getfloat(k,'slice-m'),logspace=True) )) def _sampleIteration(self): order = range(self.levels+1+len(self.samplers)) if self.randomSamplerOrder: order = np.random.choice(order,len(order),replace=False) for o in order: if o <= self.levels: prior = self.priors['functions'][o] prior.sample(self.model,self.yKernel) else: obj, param, sampler = self.samplers[o-self.levels-1] # print param, obj if obj == self.yKernelOld: obj = self.yKernel if param == 'k1_sigma': obj.__dict__['k1'].sigma = sampler.sample(obj.__dict__['k1'].sigma) elif param == 'k2_sigma': obj.__dict__['k2'].sigma = sampler.sample(obj.__dict__['k2'].sigma) elif param == 'k2_lengthscale': obj.__dict__['k2'].lengthscale = sampler.sample(obj.__dict__['k2'].lengthscale) else: raise ValueError("unknown parameter %s!"%param) else: obj.__dict__[param] = sampler.sample(obj.__dict__[param])
lambda x: prior2.loglikelihood(model.beta, sigma=x), lambda x: scipy.stats.uniform(1e-2, 1e1).logpdf(x), .1, 10, logspace=True) k2LengthscaleSlice = Slice( 'k2Lengthscale', lambda x: prior2.loglikelihood(model.beta, lengthscale=x), lambda x: scipy.stats.uniform(1e-2, 1e1).logpdf(x), .1, 10, logspace=True) samples = [] freeze = Freezer(yKernel=yKernel, k1=k1, k2=k2, model=model) thin = 10 burnin = 0 nsample = 4000 ll = [] for i in range(nsample): prior.sample(model, yKernel) prior2.sample(model, yKernel) yKernel.sigma = ySigmaSlice.sample(yKernel.sigma) k1.sigma = kSigmaSlice.sample(k1.sigma) k1.lengthscale = kLengthscaleSlice.sample(k1.lengthscale) k2.sigma = k2SigmaSlice.sample(k2.sigma)
lambda x: model.dataLikelihood(yKernel,sigma=x), lambda x: scipy.stats.uniform(1e-6,1e1).logpdf(x), .1,2,logspace=True) kSigmaSlice = Slice('kSigma', lambda x: prior.loglikelihood(model.beta,sigma=x), lambda x: scipy.stats.uniform(1e-2,1e1).logpdf(x), .2,5,logspace=True) kLengthscaleSlice = Slice('kLengthscale', lambda x: prior.loglikelihood(model.beta,lengthscale=x), lambda x: scipy.stats.uniform(1e-2,1e1).logpdf(x), .2,5,logspace=True) samples = [] freeze = Freezer(yKernel=yKernel,k1=k1,model=model) trueParams = freeze.freeze() thin = 10 burnin = 0 nsample = 1000 ll = [] for i in range(nsample): prior.sample(model,yKernel) yKernel.sigma = ySigmaSlice.sample(yKernel.sigma) k1.sigma = kSigmaSlice.sample(k1.sigma) k1.lengthscale = kLengthscaleSlice.sample(k1.lengthscale)