Skip to content

Commit

Permalink
BUG: handle demand-only replacement of exogenous characteristic
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffgortmaker committed May 18, 2022
1 parent 840994e commit 8a772c9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions pyblp/economies/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,12 +842,13 @@ def market_factory(s: Hashable) -> Tuple[SimulationMarket, Array, Iteration, str

# compute delta and marginal costs market-by-market
true_delta = np.zeros_like(self.xi)
true_tilde_costs = np.zeros_like(self.omega)
true_tilde_costs = None if self.omega is None else np.zeros_like(self.omega)
iteration_stats: Dict[Hashable, SolverStats] = {}
generator = generate_items(self.unique_market_ids, market_factory, SimulationMarket.compute_exogenous)
for t, (delta_t, tilde_costs_t, iteration_stats_t, errors_t) in output_progress(generator, self.T, start_time):
true_delta[self._product_market_indices[t]] = delta_t
true_tilde_costs[self._product_market_indices[t]] = tilde_costs_t
if true_tilde_costs is not None:
true_tilde_costs[self._product_market_indices[t]] = tilde_costs_t
iteration_stats[t] = iteration_stats_t
errors.extend(errors_t)

Expand All @@ -859,14 +860,15 @@ def market_factory(s: Hashable) -> Tuple[SimulationMarket, Array, Iteration, str
(true_delta - self.xi - self.products.X1 @ self.beta) / self.beta[X1_index]
)
if X3_name is not None:
assert true_tilde_costs is not None
data_override[X3_name] = (
self.products.X3[:, [X3_index]] +
(true_tilde_costs - self.omega - self.products.X3 @ self.gamma) / self.gamma[X3_index]
)

# compute non-transformed marginal costs
true_costs = true_tilde_costs
if self.costs_type == 'log':
if self.costs_type == 'log' and true_costs is not None:
true_costs = np.exp(true_costs)

# structure the results
Expand Down
4 changes: 2 additions & 2 deletions pyblp/results/simulation_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class SimulationResults(Results):
simulation: 'Simulation'
product_data: RecArray
delta: Array
costs: Array
costs: Optional[Array]
computation_time: float
fp_converged: Array
fp_iterations: Array
Expand All @@ -133,7 +133,7 @@ class SimulationResults(Results):
_data_override: Dict[str, Array]

def __init__(
self, simulation: 'Simulation', data_override: Dict[str, Array], delta: Array, costs: Array,
self, simulation: 'Simulation', data_override: Dict[str, Array], delta: Array, costs: Optional[Array],
start_time: float, end_time: float, iteration_stats: Dict[Hashable, SolverStats],
profit_gradients: Optional[Dict[Hashable, Dict[Hashable, Array]]] = None,
profit_gradient_norms: Optional[Dict[Hashable, Dict[Hashable, Array]]] = None,
Expand Down

0 comments on commit 8a772c9

Please sign in to comment.