Exemple #1
0
def test_with_numeric_policies():
    sim = run_sim(
        [v('a', 't*x + 1')],
        policies=pols(x=['100', '1000']),
        num_steps=2,
    )
    sim.assert_values_match('a', [1, 101, 201], policy=0)
    sim.assert_values_match('a', [1, 1001, 2001], policy=1)
Exemple #2
0
def test_broadcasting_dataset_var_in_policy_expression():
    sim = run_sim(
        [v('x', 'attr')],
        policies=pols(attr=['EXAMPLE_LABEL.workers == 1', '5']),
        dataset=WORKERS_DATASET_DEF,
        allow_errs=True,
    )
    sim.assert_errors_match([['attr', 'equation', 'extra dimension']])
Exemple #3
0
def test_join_fn_multiple_policies():
    sim = run_sim(
        [
            v('a_var', '3'),
            v('b_var',
              "join(EXAMPLE_LABEL.factor_4 == a_var, EXAMPLE_LABEL.factor_2)"),
        ],
        policies=pols(attribute1=['1', '3']),
        dataset=ASSETS_DATASET_DEF,
        num_sims=2,
    )
    assert (sim.results.b_var == 20).all()
Exemple #4
0
def test_with_policy_formulas():
    sim = run_sim(
        [
            v('a', 'where(t>1, x[t-1], 1)', '1'),
            v('b', 'x[t-1]', '0'),
        ],
        policies=pols(x=['a*2', 'a*3']),
    )
    sim.assert_values_match('a', [1, 1, 2, 4], policy=0)
    sim.assert_values_match('a', [1, 1, 3, 9], policy=1)
    sim.assert_values_match('b', [0, 2, 2, 4], policy=0)
    sim.assert_values_match('b', [0, 3, 3, 9], policy=1)
Exemple #5
0
def test_with_numeric_policies_and_two_sims():
    # As above, but with 2 sims
    sim = run_sim(
        [v('a', 't * x + 1')],
        policies=pols(x=['100', '1000']),
        num_steps=2,
        num_sims=2,
    )
    for sim_id in range(2):
        # Values should be identical across simulations.
        sim.assert_values_match('a', [1, 101, 201], policy=0, sim_id=sim_id)
        sim.assert_values_match('a', [1, 1001, 2001], policy=1, sim_id=sim_id)
Exemple #6
0
def test_join_fn_policy_dependent_mismatch():
    # For policy 1, this will give a valid 1:1 join. But for policy 2, it will
    # overmatch (due to there being 2 rows having factor_1=3)
    sim = run_sim(
        [
            v('b_var',
              "join(EXAMPLE_LABEL.factor_1 == att1, EXAMPLE_LABEL.factor_2)"),
        ],
        policies=pols(att1=["1", "3"], ),
        dataset=ASSETS_DATASET_DEF,
        allow_errs=True,
    )
    sim.assert_errors_match([['b_var', 'equation', 'Join']])
def test_dummy_classifier_multiple_policies():
    sim = run_sim(
        [
            v('pred', 'CLF.predict(t)'),
            v('prob', 'CLF.predict_proba(t)'),
        ],
        policies=pols(x=['1', '2']),
        models=clf_factory('dummy.pickle'),
    )
    df = sim.results
    # All predicted labels should be 0 or 1
    assert df.pred.isin([0, 1]).all()
    # All probabilities should be .5
    assert (df.prob == .5).all()
    assert len(df) == 4 * 2  # 4 timesteps, 2 policies, 1 sim
Exemple #8
0
def test_sum_constant_user_dataset_var():
    """Calling sum on a DatasetAdditionVar with a constant value
    (and also using multiple sims and policies)"""
    db = deepcopy(WORKERS_DATASET_DEF)
    db._add_var(v('seven', '7'))
    sim = run_sim(
        [
            v('x', 'sum(EXAMPLE_LABEL.seven)'),
        ],
        policies=pols(attribute1=['1', '2']),
        dataset=db,
        num_sims=5,
    )
    x = sim.results.x
    assert (x == 14).all()
    assert len(x) == 40  # 5 sims * 2 policies * 4 timesteps
Exemple #9
0
def test_time_indexing_dataset_vars():
    db = deepcopy(ASSETS_DATASET_DEF)
    db._add_var(v('ds_var1', 'EXAMPLE_LABEL.factor_1'))
    db._add_var(v('ds_var2', 'ds_var1[t-1]', '0'))

    sim = run_sim(
        [
            v('lagged_var',
              'sum(EXAMPLE_LABEL.ds_var1[t-1] + EXAMPLE_LABEL.ds_var2)', '0')
        ],
        policies=pols(attribute1=['1']),
        dataset=db,
    )
    sim.assert_values_match(
        'lagged_var',
        [0, 48, 48, 48],
    )
Exemple #10
0
def test_with_dataset_formulas_accessing_raw_data_and_attributes_and_vars():
    db = deepcopy(WORKERS_DATASET_DEF)
    db._add_var(
        v(
            'rev_per_worker',
            "EXAMPLE_LABEL.max_production / EXAMPLE_LABEL.workers * price_per_unit * a_var",
        ))
    sim = run_sim(
        [v('a_var', 't/10')],
        policies=pols(price_per_unit=['10', '5']),
        dataset=db,
        num_sims=4,
    )
    sim.assert_values_match(
        'EXAMPLE_LABEL.rev_per_worker',
        [50, 10 / 3],
        t=1,
        policy=1,
    )
Exemple #11
0
def test_with_dataset_formulas_accessing_raw_data():
    db = deepcopy(WORKERS_DATASET_DEF)
    db._add_var(v('t_again', 't'))

    sim = run_sim(
        policies=pols(price_per_unit=['10', '1']),
        dataset=db,
    )
    sim.assert_values_match(
        'EXAMPLE_LABEL.t_again',
        [
            1,
            1,
            2,
            2,
            3,
            3,
        ],
        policy=0,
    )
Exemple #12
0
def test_with_dataset_formulas_accessing_attributes():
    db = deepcopy(WORKERS_DATASET_DEF)
    db._add_var(
        v(
            'rev_per_worker',
            "EXAMPLE_LABEL.max_production / EXAMPLE_LABEL.workers * price_per_unit"
        ))

    sim = run_sim(
        [],
        policies=pols(price_per_unit=['10', '5']),
        dataset=db,
        num_sims=4,
    )
    sim.assert_values_match_across_time(
        'EXAMPLE_LABEL.rev_per_worker',
        [1000, 200 / 3],
        policy=0,
    )
    sim.assert_values_match_across_time(
        'EXAMPLE_LABEL.rev_per_worker',
        [500, 100 / 3],
        policy=1,
    )
Exemple #13
0
import pytest

from sample_datasets import *
from helpers import run_sim, pols
from decisionai.variable import Variable as v

from copy import deepcopy

# TODO: All through this file we're reusing some mocks, copying and mutating
# that's unlike actual usage patterns. Fix that.

# Used when we want to test a sim with multiple policies, but we don't really
# care about the contents of their attributes.
DUMMY_POLS = pols(unused_attr=['0', '5'])


def test_join_fn_simple():
    sim = run_sim(
        [
            v('a_var', '3'),
            v('b_var',
              "join(EXAMPLE_LABEL.factor_4 == a_var, EXAMPLE_LABEL.factor_2)"),
        ],
        dataset=ASSETS_DATASET_DEF,
        num_sims=2,
    )
    sim.assert_values_match(
        'b_var',
        [20, 20, 20, 20],
        sim_id=1,
    )
Exemple #14
0
def test_attribute_syntax_error():
    sim = run_sim(
            policies=pols(x=['1', 't-']),
    )
    sim.assert_errors_match([ ['x', 'equation'] ])