def test_list_output(): vsim = elfi.tools.vectorize(lsimulator) vsum = elfi.tools.vectorize(lsummary) v = vsim(np.array([[.2, .8], [.3, .7]])) assert is_array(v) assert not isinstance(v[0], list) vsim = elfi.tools.vectorize(lsimulator, dtype=False) v = vsim(np.array([[.2, .8], [.3, .7]])) assert is_array(v) assert isinstance(v[0], list) obs = lsimulator([.2, .8]) elfi.new_model() p = elfi.Prior('dirichlet', [2, 2]) sim = elfi.Simulator(vsim, p, observed=obs) S = elfi.Summary(vsum, sim) d = elfi.Distance('euclidean', S) pool = elfi.OutputPool(['sim']) rej = elfi.Rejection(d, batch_size=100, pool=pool, output_names=['sim']) sample = rej.sample(100, n_sim=1000) mean = np.mean(sample.samples['p'], axis=0) # Crude test assert mean[1] > mean[0]
def test_dict_output(): vsim = elfi.tools.vectorize(simulator) vsum = elfi.tools.vectorize(summary) obs = simulator([.2, .8]) elfi.new_model() p = elfi.Prior('dirichlet', [2, 2]) sim = elfi.Simulator(vsim, p, observed=obs) S = elfi.Summary(vsum, sim) d = elfi.Distance('euclidean', S) pool = elfi.OutputPool(['sim']) rej = elfi.Rejection(d, batch_size=100, pool=pool, output_names=['sim']) sample = rej.sample(100, n_sim=1000) mean = np.mean(sample.samples['p'], axis=0) # Crude test assert mean[1] > mean[0]
def __init__(self, simulator, fn_distance, list_ss=None, prepared_ss=None, max_cardinality=4, seed=0): """Initialise the summary-statistics selection for the Two Stage Procedure. Parameters ---------- simulator : elfi.Node Node (often elfi.Simulator) for which the summary statistics will be applied. The node is the final node of a coherent ElfiModel (i.e. it has no child nodes). fn_distance : str or callable function Distance metric, consult the elfi.Distance documentation for calling as a string. list_ss : List of callable functions, optional List of candidate summary statistics. prepared_ss : List of lists of callable functions, optional List of prepared combinations of candidate summary statistics. No other combinations will be evaluated. max_cardinality : int, optional Maximum cardinality of a candidate summary-statistics combination. seed : int, optional """ if list_ss is None and prepared_ss is None: raise ValueError('No summary statistics to assess.') self.simulator = simulator self.fn_distance = fn_distance self.seed = seed if prepared_ss is not None: self.ss_candidates = prepared_ss else: self.ss_candidates = self._combine_ss( list_ss, max_cardinality=max_cardinality) # Initialising an output pool as the rejection sampling will be used several times. self.pool = elfi.OutputPool(simulator.name)
def test_pool_usage(sleep_model): # Add nodes to the pool pool = elfi.OutputPool(outputs=sleep_model.parameter_names + ['slept', 'summary', 'd']) rej = elfi.Rejection(sleep_model['d'], batch_size=5, pool=pool) quantile = .25 ts = time.time() res = rej.sample(5, quantile=quantile) td = time.time() - ts # Will make 5/.25 = 20 evaluations with mean time of .1 secs, so 2 secs total on # average. Allow some slack although still on rare occasions this may fail. assert td > 1.2 # Instantiating new inference with the same pool should be faster because we # use the prepopulated pool rej = elfi.Rejection(sleep_model['d'], batch_size=5, pool=pool) ts = time.time() res = rej.sample(5, quantile=quantile) td = time.time() - ts assert td < 1.2 # It should work if we remove the simulation, since the Rejection sampling # only requires the parameters and the discrepancy pool.remove_store('slept') rej = elfi.Rejection(sleep_model['d'], batch_size=5, pool=pool) ts = time.time() res = rej.sample(5, quantile=quantile) td = time.time() - ts assert td < 1.2 # It should work even if we remove the discrepancy, since the discrepancy can be recomputed # from the stored summary pool.remove_store('d') rej = elfi.Rejection(sleep_model['d'], batch_size=5, pool=pool) ts = time.time() res = rej.sample(5, quantile=quantile) td = time.time() - ts assert td < 1.2
def main(): global eng datadir = Path('/home/ben/phd/stfp/abc_results/gradientNet') eng = matlab.engine.start_matlab() eng.addpath(eng.genpath('~/phd/stfp/abc_code')) eng.addpath(eng.genpath('~/phd/easySim')) maxConnProbOB2E_prior = elfi.Prior('uniform', 0, 1) maxConnProbOB2I_prior = elfi.Prior('uniform', 0, 1) maxConnProbGC2E_prior = elfi.Prior('uniform', 0, 1) maxConnProbGC2I_prior = elfi.Prior('uniform', 0, 1) sigmaOB2PC_prior = elfi.Prior('uniform', 1e-6, 1000e-6) sigmaGC2PC_prior = elfi.Prior('uniform', 1e-6, 1000e-6) sim = elfi.Simulator(simulator, maxConnProbOB2E_prior, maxConnProbOB2I_prior, maxConnProbGC2E_prior, maxConnProbGC2I_prior, sigmaOB2PC_prior, sigmaGC2PC_prior, observed=0) S = elfi.Summary(score_network, sim) d = elfi.Distance('euclidean', S) #log_d = elfi.Operation(np.log, d) pool = elfi.OutputPool([ 'connProbOB2E_prior', 'connProbOB2I_prior', 'connProbGC2E_prior', 'connProbGC2I_prior', 'sigmaOB2PC_prior', 'sigmaGC2PC_prior', 'S', 'd' ]) ie_maxConnProbOB2E, ie_maxConnProbOB2I, ie_maxConnProbGC2E, ie_maxConnProbGC2I, ie_sigmaOB2PC, ie_sigmaGC2PC, ie_scores = eng.load_initial_evidence_gradientNet( '/home/ben/phd/stfp/abc_results/gradientNet', nargout=7) ie_maxConnProbOB2E = np.asarray(ie_maxConnProbOB2E) ie_maxConnProbOB2I = np.asarray(ie_maxConnProbOB2I) ie_maxConnProbGC2E = np.asarray(ie_maxConnProbGC2E) ie_maxConnProbGC2I = np.asarray(ie_maxConnProbGC2I) ie_sigmaOB2PC = np.asarray(ie_sigmaOB2PC) ie_sigmaGC2PC = np.asarray(ie_sigmaGC2PC) ie_scores = np.asarray(ie_scores) #ie_log_scores = np.log(ie_scores) if not ie_maxConnProbOB2E.any(): ie = 10 print('0 prior runs detected') else: ie = { 'maxConnProbOB2E_prior': ie_maxConnProbOB2E, 'maxConnProbOB2I_prior': ie_maxConnProbOB2I, 'maxConnProbGC2E_prior': ie_maxConnProbGC2E, 'maxConnProbGC2I_prior': ie_maxConnProbGC2I, 'sigmaOB2PC_prior': ie_sigmaOB2PC, 'sigmaGC2PC_prior': ie_sigmaGC2PC, 'd': ie_scores } print( str(ie_maxConnProbOB2E.size) + ' prior runs detected... using as initial evidence') bolfi = elfi.BOLFI(d, batch_size=1, initial_evidence=ie, update_interval=1, bounds={ 'maxConnProbOB2E_prior': (0, 1), 'maxConnProbOB2I_prior': (0, 1), 'maxConnProbGC2E_prior': (0, 1), 'maxConnProbGC2I_prior': (0, 1), 'sigmaOB2PC_prior': (1e-6, 1000e-6), 'sigmaGC2PC_prior': (1e-6, 1000e-6) }, acq_noise_var=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], pool=pool) posterior = bolfi.fit(n_evidence=500) with open(datadir / 'bolfi_result.pkl', 'wb') as fname: pickle.dump(bolfi, fname) #bolfi.plot_state() bolfi.plot_discrepancy() plt.show(block=True)
*prior_args, m["length"], m["recombination_rate"], m["mutation_rate"], priors, name="sim", model=m) elfi.Summary(elfi_sum, m["sim"], name="sum", model=m) elfi.Summary(elfi_sum_scaler, m["sum"], None, name="sum_scaler") # Placeholder (no scaling yet) elfi.Distance('euclidean', m["sum_scaler"], name='d', model=m) # Rejection to "train" sum stat scaler start_time = time.time() pool = elfi.OutputPool(['sum']) rej = elfi.Rejection(m['d'], batch_size=1, seed=1, pool=pool) rej.sample(train_scaler_n_sim, quantile=1, bar=False) # Accept all sum_stats = pool.get_store('sum') sum_stats = np.array(list(sum_stats.values())) sum_stats = sum_stats.reshape(-1, sum_stats.shape[2]) # Drop batches axis scaler = StandardScaler() scaler.fit(sum_stats) elapsed_time = time.time() - start_time logging.info( f"{train_scaler_n_sim} simulations to train standard scaler completed in {elapsed_time/60:.2f} minutes" ) m["sum_scaler"].become(elfi.Summary(elfi_sum_scaler, m["sum"], scaler, model=m)) # Now carries out scaling
return score S = elfi.Summary(get_score, sim) d = elfi.Distance('euclidean', S) log_d = elfi.Operation(np.log, d) ie = { 'm_prior': np.asarray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 's_prior': np.asarray([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), 'log_d': np.asarray([2, 1.6, 1.2, .8, 0, .8, 1.2, 1.6, 2, 2.4]) } pool = elfi.OutputPool(['m_prior', 's_prior', 'sim', 'S', 'd', 'log_d']) bolfi = elfi.BOLFI(log_d, batch_size=1, initial_evidence=ie, update_interval=5, bounds={ 'm_prior': (0, 20), 's_prior': (0, 20) }, acq_noise_var=[0, 0], pool=pool) posterior = bolfi.fit(n_evidence=200) savedir = Path('/home/ben/phd/stfp/abc_results/')
S7 = elfi.Summary(percentile_values,sim,60) S8 = elfi.Summary(percentile_values,sim,70) S9 = elfi.Summary(percentile_values,sim,80) S10 = elfi.Summary(percentile_values,sim,90) S11 = elfi.Summary(percentile_values,sim,100) # Distance d = elfi.Distance('euclidean', S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11) ################################ # BOLFI implementation: # log_d = elfi.Operation(np.log, sim.model['d']) output_pool = elfi.OutputPool(outputs=['wee','wei','wie','wii','be','bi','taue','taui','k','IH','psi_sigma','sim','d', 'S1','S2','S3','S4','S5','S6','S7','S8','S9','S10','S11']) # pool=output_pool bolfi = elfi.BOLFI(d, batch_size=1, initial_evidence=4, update_interval=4, bounds={'wee': (10, 30), 'wei': (6, 30), 'wie': (6, 30), 'wii': (0.6, 1.4), 'be':(1,5), 'bi':(3,7), 'taue':(11,26.2), 'taui':(5.7,24.5), 'k':(0.5,2.5), 'IH':(1.5,3.5), 'psi_sigma':(0.05,0.25)}, acq_noise_var=0.1, seed=seed) # np.save('bolfi_test_23112021_sim_data', bolfi) post = bolfi.fit(n_evidence=6) bolfi.plot_discrepancy() result_BOLFI = bolfi.sample(6)
connProbGC2E_prior = elfi.Prior('uniform',0,.5) connProbGC2I_prior = elfi.Prior('uniform',0,.5) sim = elfi.Simulator(simulator,connProbOB2E_prior,connProbOB2I_prior,connProbGC2E_prior,connProbGC2I_prior,observed=0) def score_network(score): return score S = elfi.Summary(score_network, sim) d = elfi.Distance('euclidean',S) #log_d = elfi.Operation(np.log, d) pool = elfi.OutputPool(['connProbOB2E_prior','connProbOB2I_prior','connProbGC2E_prior','connProbGC2I_prior','S','d','d']) ie_connProbOB2E,ie_connProbOB2I,ie_connProbGC2E,ie_connProbGC2I,ie_scores = eng.load_initial_evidence_randomNet('/home/ben/phd/stfp/abc_results/randomNet',nargout=5) ie_connProbOB2E = np.asarray(ie_connProbOB2E) ie_connProbOB2I = np.asarray(ie_connProbOB2I) ie_connProbGC2E = np.asarray(ie_connProbGC2E) ie_connProbGC2I = np.asarray(ie_connProbGC2I) ie_scores = np.asarray(ie_scores) #ie_log_scores = np.log(ie_scores) if not ie_connProbOB2E.any(): ie = 10 print('0 prior runs detected') else:
import matplotlib.pyplot as plt import logging logging.basicConfig(level=logging.INFO) plt.ion() seed = 1 np.random.seed(seed) import elfi from elfi.examples import ma2 model = ma2.get_model(seed_obs=seed) log_d = elfi.Operation(np.log, model['d']) pool = elfi.OutputPool(['log_d', 't1', 't2']) bolfi = elfi.BOLFI(log_d, batch_size=1, initial_evidence=20, update_interval=10, bounds={ 't1': (-2, 2), 't2': (-1, 1) }, acq_noise_var=[0.1, 0.1], seed=seed, pool=pool) post = bolfi.fit(n_evidence=200)