def test_calc_sobol(): si = 1.2380952380952384 parameters = {'x': ap.Range(0., 1.)} sample = ap.Sample(parameters, n=8, method='saltelli', calc_second_order=False) results = ap.Experiment(SobolModel, sample).run(display=False) results.calc_sobol(reporters='x') assert results.sensitivity.sobol['S1'][0] == si # Test if a non-varied parameter causes errors parameters = {'x': ap.Range(0., 1.), 'y': 1} sample = ap.Sample(parameters, n=8, method='saltelli', calc_second_order=False) results = ap.Experiment(SobolModel, sample).run(display=False) results.calc_sobol() assert results.sensitivity.sobol['S1'][0] == si # Test wrong sample type raises error parameters = {'x': ap.Range(0., 1.), 'y': 1} sample = ap.Sample(parameters, n=8) results = ap.Experiment(SobolModel, sample).run(display=False) with pytest.raises(AgentpyError): results.calc_sobol() # Test merging iterations # TODO Improve parameters = {'x': ap.Range(0., 1.)} sample = ap.Sample(parameters, n=8, method='saltelli', calc_second_order=False) results = ap.Experiment(SobolModel, sample, iterations=10).run(display=False) results.calc_sobol(reporters='x') assert results.sensitivity.sobol['S1'][0] == si # Test calc_second_order parameters = {'x': ap.Range(0., 1.), 'y': 1} sample = ap.Sample(parameters, n=8, method='saltelli', calc_second_order=True) results = ap.Experiment(SobolModel, sample).run(display=False) results.calc_sobol() assert results.sensitivity.sobol[('S2', 'x')][0].__repr__() == 'nan'
def test_seed(): parameters = {'x': ap.Range(), 'seed': 1} sample = ap.Sample(parameters, 2, randomize=True) assert list(sample) == [{ 'x': 0.0, 'seed': 272996653310673477252411125948039410165 }, { 'x': 1.0, 'seed': 40125655066622386354123033417875897284 }] sample = ap.Sample(parameters, 2, randomize=False) assert list(sample) == [{'x': 0.0, 'seed': 1}, {'x': 1.0, 'seed': 1}]
def test_sample_saltelli_second(): parameters = { 'a': ap.IntRange(1, 2), 'b': ap.Range(3, 3.5), 'c': ap.Values(*'xyz'), 'd': True } sample = ap.Sample(parameters, n=2, method='saltelli', calc_second_order=True) problem = { 'num_vars': 3, 'names': ['a', 'b', 'c'], 'bounds': [ [1., 3.], [3., 3.5], [0., 3.], ] } param_values = saltelli.sample(problem, 2, calc_second_order=True) for s1, s2 in zip(sample, param_values): assert s1['a'] == int(s2[0]) assert s1['b'] == s2[1] assert s1['c'] == parameters['c'].values[int(s2[2])] assert s1['d'] == parameters['d']
def test_linspace_zip(): parameters = { 'a': ap.Range(), # 0, 1 Default 'b': ap.Values(3, 4), } sample = ap.Sample(parameters, n=2, product=False) assert list(sample) == [{'a': 0.0, 'b': 3}, {'a': 1.0, 'b': 4}]
def test_repr(): v = ap.Values(1, 2) assert v.__repr__() == "Set of 2 parameter values" r = ap.Range(1, 2) assert r.__repr__() == "Parameter range from 1 to 2" ir = ap.IntRange(1, 2) assert ir.__repr__() == "Integer parameter range from 1 to 2" s = ap.Sample({'x': r}, 10) assert s.__repr__() == "Sample of 10 parameter combinations"
def test_random(): parameters = {'steps': 0, 'seed': ap.Values(1, 1, 2)} class Model(ap.Model): def setup(self): self.report('x', self.model.random.random()) sample = ap.Sample(parameters) exp = ap.Experiment(Model, sample, iterations=2, randomize=True) results = exp.run() l = list(results.reporters['x']) assert l[0] != l[1] assert l[0:2] == l[2:4] assert l[0:2] != l[4:6] sample = ap.Sample(parameters) exp = ap.Experiment(Model, sample, iterations=2, randomize=False) results = exp.run() l = list(results.reporters['x']) assert l[0] == l[1] assert l[0:2] == l[2:4] assert l[0:2] != l[4:6] exp = ap.Experiment(Model, parameters, iterations=2) results = exp.run() l1 = list(results.reporters['x']) assert l1 == [0.03542265363082542, 0.08363464439430013] parameters['seed'] = 1 exp = ap.Experiment(Model, parameters, iterations=2) results = exp.run() l2 = list(results.reporters['x']) assert l1 == l2
def test_testing_model(): parameters = {'steps': 2, 'px': ap.Values(1, 2)} sample = ap.Sample(parameters) settings = {'iterations': 2, 'record': True} pytest.model_instance = model = ModelType0(list(sample)[0]) pytest.model_results = model_results = model.run(display=False) exp = ap.Experiment(ModelType0, sample, **settings) pytest.exp_results = exp_results = exp.run(display=False) type_list = ['AgentType1', 'AgentType2', 'EnvType3', 'EnvType4'] assert list(model_results.variables.keys()) == type_list assert list(exp_results.variables.keys()) == type_list
def test_repr(): param_ranges = {'x': ap.Range(0., 1.), 'y': 1} sample = ap.Sample(param_ranges, n=10) results = ap.Experiment(MyModel, sample, record=True).run() assert results.__repr__() == repr
def step(self): self.agents.record('wealth') self.agents.wealth_transfer() # Perform single run parameters = {'n': 10, 'steps': 10} model = MoneyModel(parameters) results = model.run() # Perform multiple runs variable_params = { 'n': ap.IntRange(10, 500), 'steps': 10 } sample = ap.Sample(variable_params, n=49) exp = ap.Experiment( MoneyModel, sample, iterations=5, record=True ) results = exp.run()
def test_linspace_product(): parameters = { 'a': ap.IntRange(1, 2), 'b': ap.Range(3, 3.5), 'c': ap.Values(*'xyz'), 'd': True } sample = ap.Sample(parameters, n=3) assert list(sample) == [{ 'a': 1, 'b': 3.0, 'c': 'x', 'd': True }, { 'a': 1, 'b': 3.0, 'c': 'y', 'd': True }, { 'a': 1, 'b': 3.0, 'c': 'z', 'd': True }, { 'a': 1, 'b': 3.25, 'c': 'x', 'd': True }, { 'a': 1, 'b': 3.25, 'c': 'y', 'd': True }, { 'a': 1, 'b': 3.25, 'c': 'z', 'd': True }, { 'a': 1, 'b': 3.5, 'c': 'x', 'd': True }, { 'a': 1, 'b': 3.5, 'c': 'y', 'd': True }, { 'a': 1, 'b': 3.5, 'c': 'z', 'd': True }, { 'a': 2, 'b': 3.0, 'c': 'x', 'd': True }, { 'a': 2, 'b': 3.0, 'c': 'y', 'd': True }, { 'a': 2, 'b': 3.0, 'c': 'z', 'd': True }, { 'a': 2, 'b': 3.25, 'c': 'x', 'd': True }, { 'a': 2, 'b': 3.25, 'c': 'y', 'd': True }, { 'a': 2, 'b': 3.25, 'c': 'z', 'd': True }, { 'a': 2, 'b': 3.5, 'c': 'x', 'd': True }, { 'a': 2, 'b': 3.5, 'c': 'y', 'd': True }, { 'a': 2, 'b': 3.5, 'c': 'z', 'd': True }, { 'a': 2, 'b': 3.0, 'c': 'x', 'd': True }, { 'a': 2, 'b': 3.0, 'c': 'y', 'd': True }, { 'a': 2, 'b': 3.0, 'c': 'z', 'd': True }, { 'a': 2, 'b': 3.25, 'c': 'x', 'd': True }, { 'a': 2, 'b': 3.25, 'c': 'y', 'd': True }, { 'a': 2, 'b': 3.25, 'c': 'z', 'd': True }, { 'a': 2, 'b': 3.5, 'c': 'x', 'd': True }, { 'a': 2, 'b': 3.5, 'c': 'y', 'd': True }, { 'a': 2, 'b': 3.5, 'c': 'z', 'd': True }]
def test_errors(): parameters = {'x': ap.Range(), 'seed': 1} with pytest.raises(AgentpyError): sample = ap.Sample(parameters)