Example #1
0
def test_uniform_acceptor():
    """Test the uniform acceptor."""
    def dist(x, x_0):
        return sum(abs(x[key] - x_0[key]) for key in x_0)

    distance = pyabc.SimpleFunctionDistance(dist)
    acceptor = pyabc.UniformAcceptor()
    eps = pyabc.ListEpsilon([1, 4, 2])

    x = {'s0': 1.5}
    x_0 = {'s0': 0}

    ret = acceptor(distance_function=distance,
                   eps=eps,
                   x=x,
                   x_0=x_0,
                   t=2,
                   par=None)

    assert ret.accept

    # now let's test again, including previous time points

    acceptor = pyabc.UniformAcceptor(use_complete_history=True)

    ret = acceptor(distance_function=distance,
                   eps=eps,
                   x=x,
                   x_0=x_0,
                   t=2,
                   par=None)

    assert not ret.accept
Example #2
0
 def __init__(self,
              get_acceptor=None,
              get_transition=None,
              get_eps=None,
              n_acc: int = 1000,
              n_pop: int = 20,
              eps_min: float = 0.0,
              min_acc_rate: float = 0.0,
              id_=None):
     if get_acceptor is None:
         get_acceptor = lambda: pyabc.UniformAcceptor()
     self.get_acceptor = get_acceptor
     if get_transition is None:
         get_transition = lambda: pyabc.MultivariateNormalTransition()
     self.get_transition = get_transition
     if get_eps is None:
         get_eps = lambda: pyabc.MedianEpsilon()
     self.get_eps = get_eps
     self.n_acc = n_acc
     self.n_pop = n_pop
     self.eps_min = eps_min
     self.min_acc_rate = min_acc_rate
     self.id = id_
and comparing that to the true analysis performed using either a noisy model,
or a stochastic acceptor.
"""

import pyabc
import sys
from study_abc_noise.model import NonIdAblePrioredModelVars
from study_abc_noise.util import create_sampler, get_timestamp
from study_abc_noise.vars import AnalysisVars, Task

mv = NonIdAblePrioredModelVars()

# create analysis settings
list_analysis_vars = []
for acceptor, id_ in [
    (pyabc.UniformAcceptor(), "deterministic"),
    (pyabc.UniformAcceptor(), "noisy_model"),
    (pyabc.StochasticAcceptor(temp_schemes=[
        pyabc.acceptor.scheme_acceptance_rate, pyabc.acceptor.scheme_decay
    ]), "stochastic_acceptor")
]:
    list_analysis_vars.append(
        AnalysisVars(get_acceptor=lambda acceptor=acceptor: acceptor, id_=id_))

# create tasks
tasks = []
for analysis_vars in list_analysis_vars:
    tasks.append(Task.from_vars(analysis_vars, mv, 0))
# overwrite deterministic setting
tasks[0].model = mv.get_model()
tasks[0].eps_min = 0.0
    muA=pyabc.RV("uniform", lim.lb, lim.interval_length))

# %% Define ABC-SMC model

distanceP2_adpt = pyabc.AdaptivePNormDistance(
    p=2,
    scale_function=pyabc.distance.root_mean_square_deviation,
    factors=factors)
# distanceP2 = pyabc.PNormDistance(p=2)
# kernel1 = pyabc.IndependentNormalKernel(var=1.0 ** 2)

# Measure distance and set it as minimum epsilon
# min_eps = distanceP2(obs_data_noisy, obs_data_raw)

# acceptor1 = pyabc.StochasticAcceptor()
acceptor_adpt = pyabc.UniformAcceptor(use_complete_history=True)

eps0 = pyabc.MedianEpsilon(50)
# eps1 = pyabc.Temperature()
# eps_fixed = pyabc.epsilon.ListEpsilon([50, 46, 43, 40, 37, 34, 31, 29, 27, 25,
#                                        23, 21, 19, 17, 15, 14, 13, 12, 11, 10])

# transition0 = pyabc.transition.LocalTransition(k=50, k_fraction=None)
# transition1 = pyabc.transition.GridSearchCV()

sampler0 = pyabc.sampler.MulticoreEvalParallelSampler(n_procs=48)

abc = pyabc.ABCSMC(
    models=solver.non_noisy_model,
    parameter_priors=paraPrior,
    acceptor=acceptor_adpt,