def large_blp_simulation() -> SimulationFixture: """Solve a simulation with 20 markets, varying numbers of products per market, a linear constant, log-linear coefficients on prices, a linear/nonlinear/cost characteristic, another three linear characteristics, another two cost characteristics, demographics interacted with prices and the linear/nonlinear/cost characteristic, dense parameter matrices, a log-linear cost specification, and local differentiation instruments. """ id_data = build_id_data(T=20, J=20, F=9) keep = np.arange(id_data.size) np.random.RandomState(0).shuffle(keep) id_data = id_data[keep[:int(0.5 * id_data.size)]] simulation = Simulation( product_formulations=(Formulation('1 + x + y + z + q'), Formulation('0 + I(-prices) + x'), Formulation('0 + log(x) + log(a) + log(b)')), product_data={ 'market_ids': id_data.market_ids, 'firm_ids': id_data.firm_ids, 'clustering_ids': np.random.RandomState(2).choice(range(30), id_data.size) }, beta=[1, 1, 2, 3, 1], sigma=[[+0.5, 0], [-0.1, 2]], pi=[[2, 1, 0], [0, 0, 2]], gamma=[0.1, 0.2, 0.3], agent_formulation=Formulation('1 + f + g'), integration=Integration('product', 4), xi_variance=0.00001, omega_variance=0.00001, correlation=0.9, distributions=['lognormal', 'normal'], costs_type='log', seed=2) simulation_results = simulation.replace_endogenous() simulated_data_override = { 'demand_instruments': np.c_[build_differentiation_instruments( Formulation('0 + x + y + z + q'), simulation_results.product_data), build_matrix(Formulation('0 + a + b'), simulation_results. product_data)], 'supply_instruments': np.c_[build_differentiation_instruments( Formulation('0 + x + a + b'), simulation_results.product_data), build_matrix(Formulation('0 + y + z + q'), simulation_results. product_data)] } simulated_micro_moments = [ FirstChoiceCovarianceMoment( X2_index=1, demographics_index=1, value=0, market_ids=simulation.unique_market_ids[:5]), FirstChoiceCovarianceMoment( X2_index=0, demographics_index=1, value=0, market_ids=simulation.unique_market_ids[-3:]) ] return simulation, simulation_results, simulated_data_override, simulated_micro_moments
def large_blp_simulation() -> SimulationFixture: """Solve a simulation with 20 markets, varying numbers of products per market, a linear constant, linear/nonlinear prices, a linear/nonlinear/cost characteristic, another three linear characteristics, another two cost characteristics, demographics interacted with prices and the linear/nonlinear/cost characteristic, dense parameter matrices, a log-linear cost specification, and local differentiation instruments on the demand side. """ id_data = build_id_data(T=20, J=20, F=9) keep = np.arange(id_data.size) np.random.RandomState(0).shuffle(keep) id_data = id_data[keep[:int(0.5 * id_data.size)]] simulation = Simulation( product_formulations=(Formulation('1 + prices + x + y + z + q'), Formulation('0 + prices + x'), Formulation('0 + log(x) + log(a) + log(b)')), beta=[1, -10, 1, 2, 3, 1], sigma=[[1, -0.1], [0, +2.0]], gamma=[0.1, 0.2, 0.3], product_data={ 'market_ids': id_data.market_ids, 'firm_ids': id_data.firm_ids, 'clustering_ids': np.random.RandomState(2).choice(range(30), id_data.size) }, agent_formulation=Formulation('0 + f + g'), pi=[[1, 0], [0, 2]], integration=Integration('product', 4), xi_variance=0.00001, omega_variance=0.00001, correlation=0.9, costs_type='log', seed=2) simulation_results = simulation.solve() differentiation_instruments = np.c_[ build_differentiation_instruments(Formulation('0 + x + y + z + q'), simulation_results.product_data), build_matrix(Formulation('0 + a + b'), simulation_results.product_data )] simulation_results.product_data = update_matrices( simulation_results.product_data, { 'demand_instruments': (differentiation_instruments, simulation_results.product_data.demand_instruments.dtype) }) return simulation, simulation_results
# pd.DataFrame(demand_instruments).describe() # # demand_instruments = demand_instruments[:,5] # demand_instruments = demand_instruments.reshape((len(demand_instruments),1)) # product_data['demand_instruments0'] = demand_instruments[:,0] # # supply_instruments = pyblp.build_blp_instruments(pyblp.Formulation('obs_cost'), product_data) # supply_instruments = supply_instruments[:,3] # supply_instruments.reshape((len(supply_instruments),1)) # product_data['supply_instruments0'] = supply_instruments # In[3]: # quad diff instruments demand_instruments = pyblp.build_differentiation_instruments( pyblp.Formulation('0 + quality + obs_cost'), product_data, version='quadratic') demand_instruments = demand_instruments[:, 2:4] demand_instruments = demand_instruments.reshape((len(demand_instruments), 2)) product_data['demand_instruments0'] = demand_instruments[:, 0] product_data['demand_instruments1'] = demand_instruments[:, 1] supply_instruments = pyblp.build_differentiation_instruments( pyblp.Formulation('0 + obs_cost'), product_data, version='quadratic') supply_instruments = supply_instruments[:, 1] supply_instruments.reshape((len(supply_instruments), 1)) product_data['supply_instruments0'] = supply_instruments product_data_diff = product_data.copy() # In[4]:
def large_blp_simulation() -> SimulationFixture: """Solve a simulation with 20 markets, varying numbers of products per market, a linear constant, log-linear coefficients on prices, a linear/nonlinear/cost characteristic, another three linear characteristics, another two cost characteristics, demographics interacted with prices and the linear/nonlinear/cost characteristic, dense parameter matrices, a log-linear cost specification, and local differentiation instruments. """ id_data = build_id_data(T=20, J=20, F=9) keep = np.arange(id_data.size) np.random.RandomState(0).shuffle(keep) id_data = id_data[keep[:int(0.5 * id_data.size)]] product_ids = id_data.market_ids.copy() for t in np.unique(id_data.market_ids): product_ids[id_data.market_ids == t] = np.arange( (id_data.market_ids == t).sum()) simulation = Simulation( product_formulations=(Formulation('1 + x + y + z + q'), Formulation('1 + I(-prices) + x'), Formulation('0 + log(x) + log(a) + log(b)')), product_data={ 'market_ids': id_data.market_ids, 'firm_ids': id_data.firm_ids, 'product_ids': product_ids, 'clustering_ids': np.random.RandomState(2).choice(range(30), id_data.size) }, beta=[1, 1, 2, 3, 1], sigma=[[0, +0.0, 0], [0, +0.5, 0], [0, -0.2, 2]], pi=[[0, 0, 0], [2, 1, 0], [0, 0, 2]], gamma=[0.1, 0.2, 0.3], agent_formulation=Formulation('1 + f + g'), integration=Integration('product', 4), xi_variance=0.00001, omega_variance=0.00001, correlation=0.9, distributions=['normal', 'lognormal', 'normal'], costs_type='log', seed=2) simulation_results = simulation.replace_endogenous() simulated_data_override = { 'demand_instruments': np.c_[build_differentiation_instruments( Formulation('0 + x + y + z + q'), simulation_results.product_data), build_matrix(Formulation('0 + a + b'), simulation_results. product_data)], 'supply_instruments': np.c_[build_differentiation_instruments( Formulation('0 + x + a + b'), simulation_results.product_data), build_matrix(Formulation('0 + y + z + q'), simulation_results. product_data)] } simulated_micro_moments = [ DemographicExpectationMoment(product_ids=[0], demographics_index=1, value=0, observations=simulation.N), DemographicExpectationMoment( product_ids=[None, 0], demographics_index=1, value=0, observations=simulation.N, market_ids=simulation.unique_market_ids[1:4], market_weights=[0.2, 0.4, 0.4], ), DemographicCovarianceMoment( X2_index=0, demographics_index=2, value=0, observations=simulation.N, market_ids=simulation.unique_market_ids[3:5]), DiversionProbabilityMoment( product_id1=1, product_id2=0, value=0, observations=simulation.N, market_ids=simulation.unique_market_ids[6:10]), DiversionProbabilityMoment( product_id1=None, product_id2=1, value=0, observations=simulation.N, market_ids=[simulation.unique_market_ids[8]]), DiversionProbabilityMoment( product_id1=1, product_id2=None, value=0, observations=simulation.N, market_ids=[simulation.unique_market_ids[9]]), DiversionCovarianceMoment( X2_index1=1, X2_index2=1, value=0, observations=simulation.N, market_ids=[simulation.unique_market_ids[12]]), ] return simulation, simulation_results, simulated_data_override, simulated_micro_moments
def large_blp_simulation() -> SimulationFixture: """Solve a simulation with 20 markets, varying numbers of products per market, a linear constant, log-linear coefficients on prices, a linear/nonlinear/cost characteristic, another three linear characteristics, another two cost characteristics, demographics interacted with prices and the linear/nonlinear/cost characteristic, dense parameter matrices, a log-linear cost specification, and local differentiation instruments. """ id_data = build_id_data(T=20, J=20, F=9) keep = np.arange(id_data.size) np.random.RandomState(0).shuffle(keep) id_data = id_data[keep[:int(0.5 * id_data.size)]] product_ids = id_data.market_ids.copy() for t in np.unique(id_data.market_ids): product_ids[id_data.market_ids == t] = np.arange( (id_data.market_ids == t).sum()) simulation = Simulation( product_formulations=(Formulation('1 + x + y + z + q'), Formulation('1 + I(-prices) + x'), Formulation('0 + log(x) + log(a) + log(b)')), product_data={ 'market_ids': id_data.market_ids, 'firm_ids': id_data.firm_ids, 'product_ids': product_ids, 'clustering_ids': np.random.RandomState(2).choice(range(30), id_data.size) }, beta=[1, 1, 2, 3, 1], sigma=[[0, +0.0, 0], [0, +0.5, 0], [0, -0.2, 2]], pi=[[0, 0, 0], [2, 1, 0], [0, 0, 2]], gamma=[0.1, 0.2, 0.3], agent_formulation=Formulation('1 + f + g'), integration=Integration('product', 4), xi_variance=0.00001, omega_variance=0.00001, correlation=0.9, rc_types=['linear', 'log', 'linear'], costs_type='log', seed=2, ) simulation_results = simulation.replace_endogenous() simulated_data_override = { 'demand_instruments': np.c_[build_differentiation_instruments( Formulation('0 + x + y + z + q'), simulation_results.product_data), build_matrix(Formulation('0 + a + b'), simulation_results. product_data)], 'supply_instruments': np.c_[build_differentiation_instruments( Formulation('0 + x + a + b'), simulation_results.product_data), build_matrix(Formulation('0 + y + z + q'), simulation_results. product_data)] } inside_diversion_micro_dataset = MicroDataset( name="diversion from 1", observations=simulation.N, compute_weights=lambda _, p, a: np.tile(p.product_ids == 1, (a.size, 1, 1 + p.size)), market_ids=simulation.unique_market_ids[6:10], ) outside_diversion_micro_dataset = MicroDataset( name="diversion from outside", observations=simulation.N, compute_weights=lambda _, p, a: np.concatenate([ np.ones((a.size, 1, 1 + p.size)), np.zeros((a.size, p.size, 1 + p.size)) ], axis=1), market_ids=[simulation.unique_market_ids[8]], ) simulated_micro_moments = simulation_results.replace_micro_moment_values([ MicroMoment( name="demographic 1 expectation for 0", dataset=MicroDataset( name="product 0", observations=simulation.N, compute_weights=lambda _, p, a: np.tile( p.product_ids.flat == 0, (a.size, 1)), ), value=0, compute_values=lambda _, p, a: np.tile(a.demographics[:, [1]], (1, p.size)), ), MicroMoment( name="demographic 1 expectation for 0 and outside", dataset=MicroDataset( name="product 0 and outside", observations=simulation.N, compute_weights=lambda _, p, a: np.c_[ np.ones((a.size, 1)), np.tile(p.product_ids.flat == 0, (a.size, 1))], market_ids=simulation.unique_market_ids[1:4], ), value=0, compute_values=lambda _, p, a: np.tile(a.demographics[:, [1]], (1, 1 + p.size)), ), MicroMoment( name="1 to 0 diversion ratio", dataset=inside_diversion_micro_dataset, value=0, compute_values=lambda _, p, a: np.concatenate([ np.zeros((a.size, p.size, 1)), np.tile(p.product_ids.flat == 0, (a.size, p.size, 1)) ], axis=2), ), MicroMoment( name="outside to 1 diversion ratio", dataset=outside_diversion_micro_dataset, value=0, compute_values=lambda _, p, a: np.concatenate([ np.zeros((a.size, 1 + p.size, 1)), np.tile(p.product_ids.flat == 1, (a.size, 1 + p.size, 1)) ], axis=2), ), MicroMoment( name="1 to outside diversion ratio", dataset=inside_diversion_micro_dataset, value=0, compute_values=lambda _, p, a: np.concatenate([ np.ones((a.size, p.size, 1)), np.zeros((a.size, p.size, p.size)) ], axis=2), ), MicroMoment( name="diversion interaction", dataset=MicroDataset( name="inside first and second", observations=simulation.N, compute_weights=lambda _, p, a: np.ones( (a.size, p.size, p.size)), market_ids=[simulation.unique_market_ids[12]], ), value=0, compute_values=lambda _, p, a: (np.tile(p.X2[:, [1]], (a.size, 1, p.size)) * np.tile(p.X2[:, [1]].T, (a.size, p.size, 1))), ), ]) return simulation, simulation_results, simulated_data_override, simulated_micro_moments