def initial_sampling(self, params): """Wrapper for parallelized initial pool sampling """ i = params theta_star = self.priors_sample() model = self.simz( theta_star ) rho = test_dist(self.data, model) while rho > self.eps0: theta_star = self.priors_sample() model = self.simz( theta_star ) rho = test_dist(self.data, model) data_list = [np.int(i)] for i_param in xrange(self.n_params): data_list.append(theta_star[i_param]) data_list.append(1./np.float(self.N)) data_list.append(rho) return np.array(data_list)
def importance_sampling(self, params): """ Wrapper for parallelized importance sampling """ i_part = params theta_star = weighted_sampling( self.theta_t_1, self.w_t_1 ) np.random.seed() theta_starstar = multivariate_normal( theta_star, self.sig_t_1 ).rvs(size=1) model_starstar = self.simz( theta_starstar ) rho = test_dist(self.data, model_starstar) while rho > self.eps_t: theta_star = weighted_sampling( self.theta_t_1, self.w_t_1 ) theta_starstar = multivariate_normal(theta_star, self.sig_t_1).rvs(size=1) model_starstar = self.simz( theta_starstar ) rho = test_dist(self.data, model_starstar) p_theta = self.prior_of_priors(theta_starstar) pos_t = np.dstack(self.theta_t_1) tmp_w_t = p_theta / np.sum(self.w_t_1 * multivariate_normal(self.theta_t[:,i_part], self.sig_t_1).pdf(pos_t)) data_list = [np.int(i_part)] for i_param in xrange(self.n_params): data_list.append(theta_starstar[i_param]) data_list.append(tmp_w_t) data_list.append(rho) return np.array(data_list)
def pmc_abc(data, N = 1000, eps0 = 0.01, T = 20 ): start_time = time.time() toolz = Params({ 'mu': {'shape': 'gauss', 'mean': .0, 'stddev': 1.0}, 'sigma': { 'shape': 'uniform', 'min': 0.0, 'max': 2.0} }) t = 0 theta_t = np.zeros((2, N)) w_t = np.zeros((N)) for i in xrange(N): theta_star = np.zeros(2) theta_star[0] = toolz.prior()[0].rvs(size=1)[0] theta_star[1] = toolz.prior()[1].rvs(size=1)[0] #print theta_star model = toolz.simulator( theta_star ) rho = test_dist(data[1], model(data[0])) while rho > eps0: theta_star[0] = toolz.prior()[0].rvs(size=1)[0] theta_star[1] = toolz.prior()[1].rvs(size=1)[0] model = toolz.simulator( theta_star ) rho = test_dist(data[1], model(data[0])) theta_t[:,i] = theta_star w_t[i] = 1.0/np.float(N) sig_t = 2.0 * np.cov( theta_t ) # covariance matrix print sig_t # write out np.savetxt( ''.join(['theta_w_t', str(t), '.dat']), np.c_[theta_t[0,:], theta_t[1,:], w_t ] ) print 'Initial Pool ', time.time() - start_time fig = plt.figure(1) sub = fig.add_subplot(111) sub.scatter( theta_t[0,:], theta_t[1,:], alpha = 1. , color = 'b' ) #s = 10.**10.*w_t/w_t.sum() , sub.set_xlim([-2. , 2.]) sub.set_ylim([ 0. , 2.]) sub.set_xlabel(r'$\mu$') sub.set_ylabel(r'$\sigma$') plt.savefig("theta_scatter"+str(t)+".png") plt.close() start_time = time.time() while t < T: eps_t = np.percentile(rho, 75) print 'Epsilon t', eps_t theta_t_1 = theta_t.copy() w_t_1 = w_t.copy() sig_t_1 = sig_t.copy() for i in xrange(N): start_time = time.time() theta_star = weighted_sampling( theta_t_1, w_t_1 ) theta_starstar = multivariate_normal( theta_star, sig_t_1 ).rvs(size=1) while theta_starstar[1] < 0 : theta_star = weighted_sampling( theta_t_1, w_t_1 ) theta_starstar = multivariate_normal(theta_star, sig_t_1).rvs(size=1) #print theta_starstar model_starstar = toolz.simulator( theta_starstar ) rho = test_dist(data[1], model_starstar(data[0])) while rho > eps_t: theta_star = weighted_sampling( theta_t_1, w_t_1 ) theta_starstar = multivariate_normal(theta_star, sig_t_1).rvs(size=1) while theta_starstar[1] < 0 : theta_star = weighted_sampling( theta_t_1, w_t_1 ) theta_starstar = multivariate_normal(theta_star, sig_t_1).rvs(size=1) #print theta_starstar model_starstar = toolz.simulator( theta_starstar ) rho = test_dist(data[1], model_starstar(data[0])) #print theta_star, theta_starstar theta_t[:,i] = theta_starstar #print sig_t_1 p_theta = toolz.prior()[0].pdf(theta_t[0,i]) * toolz.prior()[1].pdf(theta_t[1,i]) #print p_theta pos_t = np.dstack((theta_t_1[0,:],theta_t_1[1,:])) #print multivariate_normal(theta_t[:,i], sig_t_1).pdf(pos_t).shape , w_t_1.shape w_t[i] = p_theta / np.sum(w_t_1 * multivariate_normal(theta_t[:,i], sig_t_1).pdf(pos_t)) #print test_dist(data[1], model_starstar(data[0])), w_t[i] #print 'For loop ', time.time() - start_time sig_t = 2.0 * np.cov(theta_t) t += 1 fig = plt.figure(1) sub = fig.add_subplot(111) sub.scatter( theta_t[0,:], theta_t[1,:], alpha = 0.5 ) # s = w_t/w_t.sum() , sub.set_xlim([-2. , 2.]) sub.set_ylim([ 0. , 2.]) sub.set_xlabel(r'$\mu$') sub.set_ylabel(r'$\sigma$') plt.savefig("theta_scatter"+str(t)+".png") plt.close() np.savetxt( ''.join(['theta_w_t', str(t), '.dat']), np.c_[theta_t[0,:], theta_t[1,:], w_t ] ) print t, ' ;D'