Skip to content

Commit

Permalink
TST: relax passthrough test tolerance when prices enter nonlinearly i…
Browse files Browse the repository at this point in the history
…nto utility
  • Loading branch information
jeffgortmaker committed Dec 4, 2021
1 parent 25e4774 commit 238feb5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
14 changes: 7 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,10 @@ def medium_blp_simulation() -> SimulationFixture:
characteristics, a demographic interacted with second-degree prices, an alternative ownership structure, and a
scaled epsilon.
"""
id_data = build_id_data(T=4, J=25, F=6)
id_data = build_id_data(T=10, J=25, F=6)
simulation = Simulation(
product_formulations=(
Formulation('1 + x + y'),
Formulation('1 + x + prices'),
Formulation('1 + I(prices**2)'),
Formulation('1 + a + b')
),
Expand All @@ -238,20 +238,20 @@ def medium_blp_simulation() -> SimulationFixture:
'clustering_ids': np.random.RandomState(1).choice(range(20), id_data.size),
'ownership': build_ownership(id_data, lambda f, g: 1 if f == g else (0.1 if f > 3 and g > 3 else 0))
},
beta=[1, 2, 1],
beta=[1, 2, -3],
sigma=[
[0.5, 0],
[0.0, 0],
],
pi=[
[+0],
[-3]
[+0.0],
[-0.1]
],
gamma=[1, 1, 2],
agent_formulation=Formulation('0 + f'),
integration=Integration('product', 4),
xi_variance=0.0001,
omega_variance=0.0001,
xi_variance=0.00001,
omega_variance=0.00001,
correlation=0.8,
epsilon_scale=0.7,
seed=1,
Expand Down
11 changes: 9 additions & 2 deletions tests/test_blp.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ def test_costs(simulated_problem: SimulatedProblemFixture) -> None:
@pytest.mark.usefixtures('simulated_problem')
@pytest.mark.parametrize('iteration', [
pytest.param(Iteration('simple', {'atol': 1e-12}), id="simple"),
pytest.param(Iteration('lm', {'xtol': 1e-12, 'ftol': 0}, compute_jacobian=True), id="LM"),
pytest.param(Iteration('hybr', {'xtol': 1e-12, 'ftol': 0}, compute_jacobian=True), id="Powell"),
])
def test_prices(simulated_problem: SimulatedProblemFixture, iteration: Iteration) -> None:
"""Test that equilibrium prices computed with different methods are approximate the same.
Expand Down Expand Up @@ -781,13 +781,20 @@ def test_passthrough(simulated_problem: SimulatedProblemFixture) -> None:
t = int(product_data.market_ids[0])
costs = true_results.compute_costs(market_id=t)

# use a looser tolerance if prices enter nonlinearly into utility (this is harder to approximate)
atol = 1e-6
for key in ['X1', 'X2']:
for formulation in problem.products.dtype.fields[key][2]:
if any(s.name == 'prices' for s in formulation.differentiate('prices').free_symbols):
atol = 2e-3

# compute the exact passthrough matrix and approximate it with finite differences
exact = true_results.compute_passthrough(market_id=t)
approximate = compute_finite_differences(
lambda c: true_results.compute_prices(costs=c, market_id=t, iteration=Iteration('simple', {'atol': 1e-16})),
costs,
)
np.testing.assert_allclose(exact, approximate, atol=1e-6, rtol=0)
np.testing.assert_allclose(exact, approximate, atol=atol, rtol=0)


@pytest.mark.usefixtures('simulated_problem')
Expand Down

0 comments on commit 238feb5

Please sign in to comment.