def get_nevo_nested(): tick() problem_nl = pyblp.Problem(**nl_options) results_nl = problem_nl.solve(rho=0.375,optimization=pyblp.Optimization('return')) save_pyblp_results(results_nl, problem_nl,filename_nl) tock() return results_nl
def get_nevo_triple(): tick() problem_triple = pyblp.Problem(product_data=product_data_adj,**problem_options) results_triple=problem_triple.solve(sigma=init_sigma,pi=init_pi,**solve_options) save_pyblp_results(results_triple, problem_triple,filename_triple) tock() return results_triple
def solve_logit_blp(inst, my_products): # only one formulation logit_form = pyblp.Formulation('1 + prices + hpwt + air + mpd + space') # only demand instruments my_products['demand_instruments'] = inst logit_problem = pyblp.Problem(logit_form, my_products, add_exogenous=False) logit_results = logit_problem.solve(W=np.identity(inst.shape[1])) return logit_problem, logit_results
def solve_nl_nevo(df, rho=0.375): groups = df.groupby(['market_ids', 'nesting_ids']) df['demand_instruments20'] = groups['shares'].transform(np.size) nl_formulation = pyblp.Formulation('0 + prices') problem = pyblp.Problem(nl_formulation, df) res = problem.solve(rho=rho, optimization=pyblp.Optimization('return')) og = res.extract_diagonals(res.compute_diversion_ratios()).mean() print(og) return problem, res
def get_blp_nested(): tick() init_b = [ -5.37184814e+00, -1, 3.73890734e+00, 5.13047685e-01, -4.83872040e-03, 3.61379854e+00 ] problem_nl = pyblp.Problem(add_exogenous=False, **problem_no_X2) results_nl = problem_nl.solve(rho=0.8, beta=init_b, **solve_nl) save_pyblp_results(results_nl, problem_nl, filename_nl) tock() return results_nl
def fakeBLP(): product_data = pd.read_csv(pyblp.data.NEVO_PRODUCTS_LOCATION) X1_formulation = pyblp.Formulation('0 + prices', absorb='C(product_ids)') X2_formulation = pyblp.Formulation('1 + prices + sugar + mushy') product_formulations = (X1_formulation, X2_formulation) mc_integration = pyblp.Integration('monte_carlo', size=50, seed=0) pr_integration = pyblp.Integration('product', size=5) mc_problem = pyblp.Problem(product_formulations, product_data, integration=mc_integration) pr_problem = pyblp.Problem(product_formulations, product_data, integration=pr_integration) bfgs = pyblp.Optimization('bfgs') results1 = mc_problem.solve(sigma=np.eye(4), optimization=bfgs) elasticities = results1.compute_elasticities() diversions = results1.compute_diversion_ratios() print(results1) print(diversions) single_market = product_data['market_ids'] == 'C01Q1' plt.colorbar(plt.matshow(diversions[single_market]))
def get_blp_triple(): tick() sigma_triple = np.diag( [3.48532311, 0., 0.13802172, 2.65830791, 0.49728683, 0.90109011]) pi_triple = pi_base = np.c_[[0, -13.5483884, 0, 0, 0, 0]].astype(float) problem_triple = pyblp.Problem(add_exogenous=False, product_data=product_data_adj, **problem_options) results_triple = problem_triple.solve(sigma=sigma_triple, pi=pi_triple, **solve_options) save_pyblp_results(results_triple, problem_triple, filename_triple) tock() return results_triple
def get_blp_nocons(): tick() pi_base = np.c_[[0, -43.501, 0, 0, 0, 0]].astype(float) problem_base = pyblp.Problem(add_exogenous=False, product_data=product_data, **problem_options) sigma_noconst = np.diag([0, 0, 4.628, 1.818, 1.050, 2.056]) results_nocons = problem_base.solve(sigma=sigma_noconst, pi=pi_base, **solve_options) save_pyblp_results(results_nocons, problem_base, filename_nocons) tock() return results_nocons
def runBLP(parms, data): X1_formulation = pyblp.Formulation('0 + prices', absorb='C(product_ids)') X2_formulation = pyblp.Formulation('1 + prices + protein + fat') product_formulations = (X1_formulation, X2_formulation) mc_integration = pyblp.Integration('monte_carlo', size=50, seed=0) pr_integration = pyblp.Integration('product', size=5) mc_problem = pyblp.Problem(product_formulations, data, integration=mc_integration) pr_problem = pyblp.Problem(product_formulations, data, integration=pr_integration) # this is not the ideal optimizer and needs to be changed once the code has been proven to work opt = pyblp.Optimization('l-bfgs-b') #results1 = mc_problem.solve(sigma=np.ones((4, 4)), optimization=bfgs) results1 = mc_problem.solve(sigma=np.eye(4), optimization=opt) print(results1) elasticities = results1.compute_elasticities() diversions = results1.compute_diversion_ratios() single_market = data['market_ids'] == 18 plt.colorbar(plt.matshow(elasticities[single_market])) plt.colorbar(plt.matshow(diversions[single_market])) plt.show()
def get_blp_noalpha(): tick() sigma_noalpha = np.diag( [0.22539812, 10.21865474, -0.03314036, -0.07007719, 0.0184184]) beta_noalpha = [ -7.05576337, -0.31806557, -12.18847385, 2.22350266, 0.12090703, 2.71179815 ] problem_noalpha = pyblp.Problem(add_exogenous=False, **problem_no_alpha) results_noalpha = problem_noalpha.solve(sigma=sigma_noalpha, beta=beta_noalpha, **solve_options) save_pyblp_results(results_noalpha, problem_noalpha, filename_noalpha) tock() return results_noalpha
def get_nevo_logit(): tick() problem_logit = pyblp.Problem( product_formulations=( pyblp.Formulation('0 + prices', absorb='C(product_ids)') ), product_data = nevo_products, agent_formulation = None, agent_data = None ) results_logit = problem_logit.solve() save_pyblp_results(results_logit, problem_logit,filename_logit) tock() return results_logit
def get_blp_base(): tick() sigma_base = np.diag([3.612, 0, 4.628, 1.818, 1.050, 2.056]) pi_base = np.c_[[0, -43.501, 0, 0, 0, 0]].astype(float) beta_base = np.c_[[ -6.67862016, -5, 2.7741249, 0.57237907, 0.34009843, 3.91954976 ]] problem_base = pyblp.Problem(add_exogenous=False, product_data=product_data, **problem_options) results_base = problem_base.solve(sigma=sigma_base, pi=pi_base, **solve_options) save_pyblp_results(results_base, problem_base, filename_base) tock() return results_base
def solve_nl(df): groups = df.groupby(['market_ids', 'nesting_ids']) df['demand_instruments20'] = groups['shares'].transform(np.size) nl_formulation = pyblp.Formulation('0 + prices') problem = pyblp.Problem(nl_formulation, df) return problem.solve(rho=0.7)
Code draws strongly from the pyblp docs written by Jeff Gortmaker: https://pyblp.readthedocs.io/en/stable/_notebooks/tutorial/nevo.html I actually know Jeff (former coworker) and he gave me lots of advice when I was applying to grad schools. Small world! """ import pyblp import numpy as np import pandas as pd pyblp.options.digits = 2 pyblp.options.verbose = False product_data = pd.read_csv(pyblp.data.NEVO_PRODUCTS_LOCATION) logit_formulation = pyblp.Formulation('prices', absorb='C(product_ids)') problem = pyblp.Problem(logit_formulation, product_data) logit_results = problem.solve() #logit_results def solve_nl(df): groups = df.groupby(['market_ids', 'nesting_ids']) df['demand_instruments20'] = groups['shares'].transform(np.size) nl_formulation = pyblp.Formulation('0 + prices') problem = pyblp.Problem(nl_formulation, df) return problem.solve(rho=0.7) df1 = product_data.copy() df1['nesting_ids'] = 1
'0 + I(1 / income)'), costs_type='log', agent_data=blp_agents) solve_options = dict( costs_bounds=(0.001, None), W_type='clustered', se_type='clustered', initial_update=True, iteration=pyblp.Iteration('squarem', {'atol': 1e-14}), optimization=pyblp.Optimization('bfgs', {'gtol': 1e-5}), scale_objective=False, ) blp_problem = pyblp.Problem(add_exogenous=False, product_data=blp_products, **problem_options) # why does it only work this way? # using the original loading function works for base anyways... filename_blp_base = dict_dir / 'blp_results_base.npy' results_blp = load_blp_base(blp_problem, filename_blp_base) # Get the alpha_i blp_agents['alpha'] = np.abs(results_blp.pi[1].item() / blp_agents.income) ## copy only the alphas blp_alpha = blp_agents['alpha'].copy() # %% ############################
x1_form = blp.Formulation('0 + prices', absorb='C(brand)') x2_form = blp.Formulation('1 + prices + sugar + mushy') forms = (x1_form, x2_form) agent_form = blp.Formulation('0 + income + income_sq + age + child') # simulate 20 individuals """ There appear to be some irregularities in the 'v' and 'demog' data from Nevo--they don't appear to represent what the document says they do; load the Nevo data directly to get sensible results """ demog = pd.read_csv(blp.data.NEVO_AGENTS_LOCATION) agent_form = blp.Formulation('0 + income + income_squared + age + child') problem = blp.Problem(forms, ps3, agent_form, demog) # make initial guess for sigma and pi sigma0 = np.diag([0.3302, 2.4526, 0.0163, 0.2441]) pi0 = np.array([[5.4819, 0, 0.2037, 0], [15.8935, -1.2000, 0, 2.6342], [-0.2506, 0, 0.0511, 0], [1.2650, 0, -0.8091, 0]]) # specify optimization options bfgs = blp.Optimization('bfgs', {'gtol': 1e-5}) # solve the model blp_results = problem.solve(sigma0, pi0, optimization=bfgs, method='1s') blp_results # calculate markups and marginal costs mc = blp_results.compute_costs()
X1_formulation = pyblp.Formulation('0 + quality + prices + satellite + wired') X2_formulation = pyblp.Formulation('0 + satellite + wired') product_formulations = (X1_formulation, X2_formulation) SIGMA0 = np.eye(2) SIGMA_BOUNDS = ([[-1e2, -1e2], [-1e2, -1e2]], [[1e2, 1e2], [1e2, 1e2]]) BETA_BOUNDS = ([1e-3, -1e2, 1e-3, 1e-3], [1e2, -1e-3, 1e2, 1e2]) INTEGRATION = pyblp.Integration('product', size=17) OPTI = pyblp.Optimization('l-bfgs-b', {'gtol': 1e-6}) # In[7]: # estimate the model problem = pyblp.Problem(product_formulations, product_data, integration=INTEGRATION) results_demand = problem.solve(sigma=SIGMA0, optimization=OPTI, sigma_bounds=SIGMA_BOUNDS, beta_bounds=BETA_BOUNDS) # In[8]: # update the results with optimal instruments instrument_results = results_demand.compute_optimal_instruments( method='approximate') updated_problem = instrument_results.to_problem() updated_demand = updated_problem.solve(results_demand.sigma, optimization=OPTI,
optimization=pyblp.Optimization('bfgs', {'gtol': 1e-5}), scale_objective=True, method='2s', ) nl_data=nevo_products.copy() nl_data['nesting_ids']=1 nl_options=dict( product_formulations=( pyblp.Formulation(formula='prices', absorb='C(product_ids)') ), product_data=nl_data ) # Setup initial problem and get optimal IV problem = pyblp.Problem(product_data=nevo_products,**problem_options) # %% # Base def get_nevo_base(): tick() results_base = problem.solve(sigma=init_sigma,pi=init_pi,**solve_options) save_pyblp_results(results_base, problem, filename_base) tock() return results_base # %% # No Constant def get_nevo_nocons(): tick()