### Import PyBLP and NumPy Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_ownership.ipynb Imports the necessary libraries for the example. Ensure pyblp is installed. ```python import pyblp import numpy as np np.set_printoptions(threshold=100) pyblp.__version__ ``` -------------------------------- ### Import Libraries and Load Data Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_differentiation_instruments.ipynb Imports necessary libraries and loads the product data. Ensure pyblp is installed. ```python import pyblp import numpy as np import pandas as pd np.set_printoptions(precision=3) pyblp.__version__ ``` ```python product_data = pd.read_csv(pyblp.data.BLP_PRODUCTS_LOCATION) product_data.head() ``` -------------------------------- ### Initialize pyblp Options and Version Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/blp.ipynb Sets global options for pyblp, such as the number of digits for output and verbosity. Also retrieves the installed pyblp version. ```python import pyblp import numpy as np import pandas as pd pyblp.options.digits = 2 pyblp.options.verbose = False pyblp.__version__ ``` -------------------------------- ### Configuration Modules Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/api.rst Modules for configuring global package options and accessing example data. ```APIDOC ## options ### Description Module for configuring global package options. ## data ### Description Module for accessing example data included with the package. ``` -------------------------------- ### Load Data with NumPy Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/data.ipynb Load product and agent data from CSV files into NumPy record arrays. Ensure NumPy is installed. ```python import numpy as np blp_product_data = np.genfromtxt(pyblp.data.BLP_PRODUCTS_LOCATION, delimiter=',', names=True, encoding='utf-8') blp_agent_data = np.genfromtxt(pyblp.data.BLP_AGENTS_LOCATION, delimiter=',', names=True, encoding='utf-8') ``` -------------------------------- ### Load Data and Define Problem Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/parallel.ipynb Load product data and define a simple Logit problem formulation. This setup is used for demonstrating parallel processing. ```python product_data = pd.read_csv(pyblp.data.NEVO_PRODUCTS_LOCATION) formulation = pyblp.Formulation('0 + prices', absorb='C(product_ids)') problem = pyblp.Problem(formulation, product_data) results = problem.solve() results ``` -------------------------------- ### Set Initial Sigma and Pi Estimates Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/petrin.ipynb Initialize the Sigma and Pi matrices with published estimates as starting values for the problem solver. These are crucial for the optimization process. ```python initial_sigma = np.diag([3.23, 0, 4.43, 0.46, 0.01, 2.58, 4.42, 0, 0, 0, 0]) initial_pi = np.array([ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 7.52, 31.13, 34.49, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0.57, 0, 0, 0, 0], [0, 0, 0, 0, 0.28, 0, 0, 0, 0], [0, 0, 0, 0, 0.31, 0, 0, 0, 0], [0, 0, 0, 0, 0.42, 0, 0, 0, 0], ]) ``` -------------------------------- ### Configure pyblp Options Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/simulation.ipynb Set global options for pyblp, such as the number of digits to display and verbosity. Also, check the installed pyblp version. ```python import pyblp import numpy as np import pandas as pd pyblp.options.digits = 2 pyblp.options.verbose = False pyblp.__version__ ``` -------------------------------- ### Import pyblp and Check Version Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_id_data.ipynb Imports the pyblp library and numpy, and prints the installed pyblp version. Ensure you have pyblp installed. ```python import pyblp import numpy as np np.set_printoptions(linewidth=1) pyblp.__version__ ``` -------------------------------- ### Import Libraries and Configure pyblp Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/logit_nested.ipynb Imports necessary libraries and sets pyblp options for digits and verbosity. This is a standard setup for pyblp applications. ```python import pyblp import numpy as np import pandas as pd pyblp.options.digits = 2 pyblp.options.verbose = False ``` -------------------------------- ### Check pyblp Version Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_matrix.ipynb Displays the currently installed version of the pyblp library. ```python pyblp.__version__ ``` -------------------------------- ### Import Libraries and Configure Pyblp Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/post_estimation.ipynb Imports necessary libraries and sets Pyblp options for digits and verbosity. This is a common setup for Pyblp analysis. ```python %matplotlib inline import pyblp import numpy as np import pandas as pd import matplotlib.pyplot as plt pyblp.options.digits = 2 pyblp.options.verbose = False ``` -------------------------------- ### Solve BLP Problem with Initial Values Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/simulation.ipynb Solve the constructed BLP problem using specified starting values for parameters. This is useful for jointly estimating the supply side and requires an initial value for the linear coefficient on prices. ```python results = problem.solve( sigma=0.5 * simulation.sigma, pi=0.5 * simulation.pi, beta=[None, 0.5 * simulation.beta[1, 0], None], optimization=pyblp.Optimization('l-bfgs-b', {'gtol': 1e-5}) ) results ``` -------------------------------- ### Import Libraries and Set Options Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_blp_instruments.ipynb Imports necessary libraries (pyblp, numpy, pandas) and sets print options for numpy arrays. This is a standard setup for numerical and data manipulation tasks. ```python import pyblp import numpy as np import pandas as pd np.set_printoptions(precision=3) pyblp.__version__ ``` -------------------------------- ### Define Agent Formulation Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/blp.ipynb Define the demographic variables for agents. This example uses the inverse of income, interacted with prices. ```python agent_formulation = pyblp.Formulation('0 + I(1 / income)') ``` -------------------------------- ### Load Data with Pandas Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/data.ipynb Load product and agent data from CSV files into pandas DataFrames. Pandas offers more flexibility for data manipulation and displays nicely in Jupyter notebooks. Ensure pandas is installed. ```python import pandas as pd blp_product_data = pd.read_csv(pyblp.data.BLP_PRODUCTS_LOCATION) blp_agent_data = pd.read_csv(pyblp.data.BLP_AGENTS_LOCATION) ``` -------------------------------- ### Initialize Simulation Configuration Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/simulation.ipynb Create a `Simulation` object with specified product formulations, true parameters (beta, sigma, gamma), product data, integration configuration, and a random seed. ```python simulation = pyblp.Simulation( product_formulations=( pyblp.Formulation('1 + prices + x'), pyblp.Formulation('0 + x'), pyblp.Formulation('0 + x + z') ), beta=[1, -2, 2], sigma=1, gamma=[1, 4], product_data=id_data, integration=integration, seed=0 ) simulation ``` -------------------------------- ### Get PyBLP Version Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/data.ipynb Check the installed version of the PyBLP library. ```python import pyblp pyblp.__version__ ``` -------------------------------- ### Get Pyblp Version Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/iteration.ipynb Retrieves the installed version of the Pyblp library. This is useful for checking compatibility or reporting issues. ```python import pyblp import numpy as np pyblp.__version__ ``` -------------------------------- ### Initialize pyblp and Display HTML Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/petrin.ipynb Imports necessary libraries and sets pyblp options for display and verbosity. Use this at the beginning of your session. ```python from IPython.display import display, HTML display(HTML("")) import pyblp import numpy as np import pandas as pd pyblp.options.digits = 2 pyblp.options.verbose = False pyblp.__version__ ``` -------------------------------- ### Create Product Data Dictionary Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_differentiation_instruments.ipynb This code demonstrates an alternative way to specify product data for the Problem class initialization using a dictionary. It includes selecting specific columns from an existing DataFrame and adding new demand instruments. ```python product_data_dict = {k: product_data[k] for k in ['market_ids', 'firm_ids', 'shares', 'prices', 'hpwt', 'air', 'mpd']} product_data_dict['demand_instruments'] = local_instruments ``` -------------------------------- ### Initialize PyBLP and Set Options Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/data_to_dict.ipynb Imports necessary libraries and sets global options for PyBLP, including precision for numerical output and verbosity. ```python import pyblp import numpy as np import pandas as pd np.set_printoptions(precision=1) pyblp.options.digits = 2 pyblp.options.verbose = False pyblp.__version__ ``` -------------------------------- ### Load Data and Define Problem Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/post_estimation.ipynb Loads product and agent data, defines product and agent formulations, and creates a pyblp Problem instance. Ensure data files are accessible. ```python product_data = pd.read_csv(pyblp.data.NEVO_PRODUCTS_LOCATION) agent_data = pd.read_csv(pyblp.data.NEVO_AGENTS_LOCATION) product_formulations = ( pyblp.Formulation('0 + prices', absorb='C(product_ids)'), pyblp.Formulation('1 + prices + sugar + mushy') ) agent_formulation = pyblp.Formulation('0 + income + income_squared + age + child') problem = pyblp.Problem(product_formulations, product_data, agent_formulation, agent_data) problem ``` -------------------------------- ### Initialize Problem with Product Rule Integration Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/nevo.ipynb Initialize a pyblp Problem using the defined product formulations and product rule integration configuration. This sets up the problem for estimation with exact integration. ```python pr_problem = pyblp.Problem(product_formulations, product_data, integration=pr_integration) pr_problem ``` -------------------------------- ### Initialize pyblp Problem Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/blp.ipynb Initialize the pyblp Problem by combining product and agent formulations with data. Specify the 'costs_type' for marginal cost functional form. ```python problem = pyblp.Problem(product_formulations, product_data, agent_formulation, agent_data, costs_type='log') ``` -------------------------------- ### Set Up Counterfactual Simulation Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/petrin.ipynb Initializes a PyBLP Simulation object for a counterfactual scenario where the minivan product is removed. Requires filtering product and agent data for the specific market and year, and excluding the minivan. ```python product_data_1984 = product_data[product_data['market_ids'] == year] xi_1984 = results.xi[product_data['market_ids'] == year] agent_data_1984 = agent_data[agent_data['market_ids'] == year] simulation = pyblp.Simulation( product_formulations=product_formulations[:2], product_data=product_data_1984[product_data_1984['mi'] == 0], xi=xi_1984[product_data_1984['mi'] == 0], agent_formulation=problem.agent_formulation, agent_data=agent_data_1984, beta=results.beta, sigma=results.sigma, pi=results.pi, ) ``` -------------------------------- ### Inspect Product Data with Pandas Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/data.ipynb Display the first few rows of the product data DataFrame to inspect its structure and content. ```python blp_product_data.head() ``` -------------------------------- ### Initialize PyBLP Problem Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/data_to_dict.ipynb Loads product data and initializes a PyBLP Problem object using a specified formulation. This sets up the data structure for analysis. ```python product_data = pd.read_csv(pyblp.data.NEVO_PRODUCTS_LOCATION) formulation = pyblp.Formulation('0 + prices', absorb='C(product_ids)') problem = pyblp.Problem(formulation, product_data) problem ``` -------------------------------- ### Build Demand-Side BLP Instruments Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_blp_instruments.ipynb Constructs demand-side BLP instruments using the `build_blp_instruments` function with a specified formulation and product data. Ensure `product_data` is loaded and accessible. ```python demand_instruments = pyblp.build_blp_instruments(pyblp.Formulation('1 + hpwt + air + mpd'), product_data) ``` ```python demand_instruments ``` -------------------------------- ### Define Custom Iteration Method Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/iteration.ipynb Defines a custom iteration method that can be passed to Pyblp. This example implements a simple iteration with a callback for every three evaluations. ```python def custom_method(initial, contraction, callback, max_evaluations, tol, norm): x = initial evaluations = 0 while evaluations < max_evaluations: x0, (x, weights, _) = x, contraction(x) evaluations += 1 if evaluations % 3 == 0: callback() if weights is None: difference = norm(x - x0) else: difference = norm(weights * (x - x0)) if difference < tol: break return x, evaluations < max_evaluations ``` -------------------------------- ### Build a Basic Matrix Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_matrix.ipynb Constructs a design matrix from a formulation and product data. This matrix is used as input for pyblp models. ```python matrix = pyblp.build_matrix(formulation, product_data) matrix ``` -------------------------------- ### Solve Updated Problem with Optimal Instruments Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/post_estimation.ipynb Solves the BLP problem again, this time incorporating the optimal excluded instruments. It starts the optimization at the consistent estimate of theta obtained previously. ```python updated_results = updated_problem.solve( results.sigma, results.pi, optimization=pyblp.Optimization('bfgs', {'gtol': 1e-5}), method='1s' ) updated_results ``` -------------------------------- ### Problem Class Initialization Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/api.rst Initialize a BLP-type problem by providing data and appropriate configurations. ```APIDOC ## Problem Class Given data and appropriate configurations, a BLP-type problem can be structured by initializing the following class. ### Problem Initialize a BLP-type problem. ### Method `Problem(data, product_formulation, demand_formulation, ...)` ### Parameters - `data` (dict or DataFrame) - Required - The data for the problem. - `product_formulation` (Formulation) - Required - The formulation for product data. - `demand_formulation` (Formulation) - Required - The formulation for demand data. - `product_data` (DataFrame) - Optional - Product data. - `agent_data` (DataFrame) - Optional - Agent data. - `integration` (Integration) - Optional - Configuration for integral approximation. - `iteration` (Iteration) - Optional - Configuration for fixed point problem iteration. - `optimization` (Optimization) - Optional - Configuration for optimization. - `parallel` (bool) - Optional - Whether to use parallel processing. - `verbose` (bool) - Optional - Whether to display progress information. ``` -------------------------------- ### Problem.solve Method Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/api.rst Solve the initialized BLP problem. ```APIDOC ## Problem.solve Method Once initialized, the following method solves the problem. ### Method `Problem.solve()` ### Description Solves the BLP problem using the configured data and settings. ``` -------------------------------- ### Initialize Sigma and Pi Parameters Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/blp.ipynb Set initial diagonal values for Sigma (covariance of unobserved heterogeneity) and Pi (coefficients for log income). Sigma is bounded to be positive. ```python initial_sigma = np.diag([3.612, 0, 4.628, 1.818, 1.050, 2.056]) initial_pi = np.c_[[0, -43.501, 0, 0, 0, 0]] ``` -------------------------------- ### Configure Integration with Gauss-Hermite Rule Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_integration.ipynb Configure integration settings to use a sparse grid with a level-5 Gauss-Hermite quadrature rule. This configuration is typically passed to the Problem class. ```python integration = pyblp.Integration('grid', 5) integration ``` -------------------------------- ### Solve Nested Logit with Single Nest Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/logit_nested.ipynb Constructs a copy of the product data, assigns a single `nesting_ids` value to all products, and then solves the nested logit problem using the `solve_nl` function. This setup represents a single nest for all products with the outside good in its own nest. ```python df1 = product_data.copy() df1['nesting_ids'] = 1 nl_results1 = solve_nl(df1) nl_results1 ``` -------------------------------- ### Construct pyblp Problem Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/logit_nested.ipynb Initializes a pyblp Problem object using the defined formulation and loaded product data. Displaying the problem object shows its dimensions and formulations. ```python problem = pyblp.Problem(logit_formulation, product_data) problem ``` -------------------------------- ### Configure pyblp Problem with Agent Data Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/nevo.ipynb Initialize a pyblp.Problem instance, incorporating both product and agent formulations and data. This step configures the model to account for demographic interactions. ```python nevo_problem = pyblp.Problem( product_formulations, product_data, agent_formulation, agent_data ) nevo_problem ``` -------------------------------- ### Initialize Problem with Monte Carlo Integration Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/nevo.ipynb Initialize a pyblp Problem using the defined product formulations and Monte Carlo integration configuration. This sets up the problem for estimation. ```python mc_problem = pyblp.Problem(product_formulations, product_data, integration=mc_integration) mc_problem ``` -------------------------------- ### Build Standard Ownership Matrix Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_ownership.ipynb Builds a standard ownership matrix from ID data. The first row is deleted to show markets with varying numbers of products. ```python id_data = pyblp.build_id_data(T=2, J=5, F=4) id_data = id_data[1:] standard_ownership = pyblp.build_ownership(id_data) standard_ownership ``` -------------------------------- ### Configure Integration for Agent Data Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/simulation.ipynb Set up an `Integration` configuration using a Gauss-Hermite product rule. This determines how agent data is constructed and integrated. ```python integration = pyblp.Integration('product', 9) integration ``` -------------------------------- ### Prepare Product Data with Demand Instruments Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_differentiation_instruments.ipynb This code snippet shows how to add new demand instruments to the product data. Ensure that 'product_data' is a pandas DataFrame or a similar structure and 'column' contains the instrument data. ```python product_data[f'demand_instruments{i}'] = column ``` -------------------------------- ### Construct BLP Problem from Simulation Results Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/simulation.ipynb Use the `to_problem` convenience method to construct a basic BLP problem from simulation results. This method creates instruments based on exogenous numerical variables. ```python problem = simulation_results.to_problem() problem ``` -------------------------------- ### Configure Sparse Grid Integration Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/integration.ipynb Sets up integration nodes and weights using a sparse grid method. Specify the grid level for accuracy. This method is deterministic and often performs well in BLP problems, even with fewer nodes than Monte Carlo. ```python integration = pyblp.Integration('grid', size=7) ``` -------------------------------- ### Build Alternative Ownership Matrix Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_ownership.ipynb Builds an ownership matrix using a custom kappa specification function. This allows for fine-grained control over firm ownership relationships. ```python alternative_ownership = pyblp.build_ownership(id_data, kappa_specification) alternative_ownership ``` -------------------------------- ### Configure Optimization with a Custom Method Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/optimization.ipynb Builds an optimization configuration using a custom grid search method. Set compute_gradient=False if your custom method does not compute analytic gradients. ```python optimization = pyblp.Optimization(custom_method, compute_gradient=False) optimization ``` -------------------------------- ### Configure Product Rule Integration Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/nevo.ipynb Configure product rule integration to exactly integrate polynomials up to a certain degree. The size parameter determines the level of the product rule. ```python pr_integration = pyblp.Integration('product', size=5) pr_integration ``` -------------------------------- ### Build Single-Product Firm Ownership Matrix Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_ownership.ipynb Builds a single-product firm ownership matrix. This special case string constructs a matrix where each firm produces only one product. ```python single_ownership = pyblp.build_ownership(id_data, 'single') single_ownership ``` -------------------------------- ### Compare Estimated Beta Parameters Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/simulation.ipynb Compare the estimated beta parameters from the solved problem with the true beta parameters from the simulation. ```python np.c_[simulation.beta, results.beta] ``` -------------------------------- ### Run All Tox Environments Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/testing.rst Execute all defined testing environments in tox. This command should be run from the top-level pyblp directory. ```bash tox ``` -------------------------------- ### Load Product Data with pandas Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/petrin.ipynb Loads product data, including prices, shares, and characteristics, from a CSV file using pandas. This data is essential for defining products in the BLP model. ```python product_data = pd.read_csv(pyblp.data.PETRIN_PRODUCTS_LOCATION) product_data.head() ``` -------------------------------- ### Construct pyblp Problem Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/petrin.ipynb Construct the pyblp.Problem object by combining product formulations, product data, agent formulation, and agent data. Specify the costs type. ```python problem = pyblp.Problem(product_formulations, product_data, agent_formulation, agent_data, costs_type='log') problem ``` -------------------------------- ### Compare Estimated Gamma Parameters Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/simulation.ipynb Compare the estimated gamma parameters from the solved problem with the true gamma parameters from the simulation. ```python np.c_[simulation.gamma, results.gamma] ``` -------------------------------- ### Define Product Formulations Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/blp.ipynb Define the linear, nonlinear, and supply-side characteristics for products using patsy-style formulas. Supports functions like 'log'. ```python product_formulations = ( pyblp.Formulation('1 + hpwt + air + mpd + space'), pyblp.Formulation('1 + prices + hpwt + air + mpd + space'), pyblp.Formulation('1 + log(hpwt) + air + log(mpg) + log(space) + trend') ) ``` -------------------------------- ### Display Bootstrapped Results Summary Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/post_estimation.ipynb After running the bootstrap, you can display a summary of the bootstrapped results. This summary provides information on computation time and the number of draws used. ```python bootstrapped_results ``` -------------------------------- ### Simulate Micro Data with PyBLP Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/post_estimation.ipynb Simulates micro data for a specified number of observations and market. It configures weights to include only consumers who purchase an inside good. ```python micro_dataset = pyblp.MicroDataset( name="Simulated micro data", observations=1_000, compute_weights=lambda t, p, a: np.ones((a.size, 1 + p.size)), market_ids=['C61Q1'], ) micro_data = results.simulate_micro_data( dataset=micro_dataset, seed=0, ) ``` -------------------------------- ### Configure PyBLP Options Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/parallel.ipynb Set global options for PyBLP, such as the number of digits for output and verbosity level. ```python import pyblp import pandas as pd pyblp.options.digits = 2 pyblp.options.verbose = False pyblp.__version__ ``` -------------------------------- ### Configure SQUAREM Iteration Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/iteration.ipynb Configures the SQUAREM iteration method with a specified norm and scheme. This method is suitable for accelerating convergence in optimization problems. ```python iteration = pyblp.Iteration('squarem', {'norm': np.linalg.norm, 'scheme': 1}) iteration ``` -------------------------------- ### Compute Post-Merger Prices (Full Simulation) Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/post_estimation.ipynb Estimates post-merger prices using a full merger simulation. This method solves the system of equations without assuming shares and their price derivatives are unaffected by the merger. ```python changed_prices = results.compute_prices( firm_ids=product_data['merger_ids'], costs=costs ) ``` -------------------------------- ### Calculate Parameter Ratio for Two Nests Case Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/logit_nested.ipynb Calculates the ratio of the 'prices' beta to (1 - 'log(within_share)' beta) for the two nests case. ```python nl2_results2.beta[0] / (1 - nl2_results2.beta[1]) ``` -------------------------------- ### Define Product Formulations Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/nevo.ipynb Define the demand-side linear characteristics (X1) and nonlinear characteristics (X2) using pyblp.Formulation. X1 formulation includes prices and absorbs fixed effects from product IDs. X2 formulation includes a constant, prices, sugar, and mushy. ```python X1_formulation = pyblp.Formulation('0 + prices', absorb='C(product_ids)') X2_formulation = pyblp.Formulation('1 + prices + sugar + mushy') product_formulations = (X1_formulation, X2_formulation) product_formulations ``` -------------------------------- ### Configure Monte Carlo Integration Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/integration.ipynb Sets up integration nodes and weights using Monte Carlo simulation. Specify the number of draws and a seed for reproducibility. Useful for large integration problems where exact integration is infeasible. ```python integration = pyblp.Integration('monte_carlo', size=1000, specification_options={'seed': 0}) ``` -------------------------------- ### Solve the pyblp Problem Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/blp.ipynb Solve the configured pyblp problem using initial parameters, bounds for marginal costs, and specified clustering for standard errors and the weighting matrix. `initial_update=True` updates the initial GMM weighting matrix and mean utility. ```python results = problem.solve( initial_sigma, initial_pi, costs_bounds=(0.001, None), W_type='clustered', se_type='clustered', initial_update=True, ) results ``` -------------------------------- ### Compare Estimated Sigma Parameters Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/simulation.ipynb Compare the estimated sigma parameters from the solved problem with the true sigma parameters from the simulation. ```python np.c_[simulation.sigma, results.sigma] ``` -------------------------------- ### Build Quadratic Differentiation Instruments Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_differentiation_instruments.ipynb Constructs quadratic differentiation instruments using sums over squared differences between rival and non-rival products. This version captures more continuous variation. ```python quadratic_instruments = pyblp.build_differentiation_instruments( formulation, product_data, version='quadratic' ) quadratic_instruments ``` -------------------------------- ### Display Demand Instruments Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_blp_instruments.ipynb Selects and displays the first 8 demand instruments from the loaded product data. This helps in understanding the structure and content of the demand-side instruments. ```python product_data[[f'demand_instruments{i}' for i in range(8)]] ``` -------------------------------- ### Build ID Data with pyblp Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_id_data.ipynb Constructs a panel of market and firm IDs. Specify the number of time periods (T) and firms (F) per market (J). ```python id_data = pyblp.build_id_data(T=2, J=5, F=4) id_data ``` -------------------------------- ### Run Specific Tox Environment Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/testing.rst Execute a single testing environment, such as the documentation build, using the -e flag with tox. This is useful for targeted testing. ```bash tox -e docs ``` -------------------------------- ### Define Product Formulations Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/petrin.ipynb Define the product formulations for demand and supply sides. These include various product characteristics and interactions. ```python product_formulations = ( pyblp.Formulation('1 + hpwt + space + air + mpd + fwd + mi + sw + su + pv + pgnp + trend + trend2'), pyblp.Formulation('1 + I(-prices) + hpwt + space + air + mpd + fwd + mi + sw + su + pv'), pyblp.Formulation('1 + log(hpwt) + log(wt) + log(mpg) + air + fwd + trend * (jp + eu) + log(q)'), ) product_formulations ``` -------------------------------- ### Micro Moment Classes Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/api.rst Classes for configuring micro moments, which are passed to the Problem.solve method. ```APIDOC ## Micro Moment Classes Micro dataset configurations are passed to micro part configurations, which are passed to micro moment configurations, which in turn can be passed to :meth:`Problem.solve`. - `MicroDataset` - `MicroPart` - `MicroMoment` ``` -------------------------------- ### Multiprocessing Context Manager Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/api.rst A context manager to enable parallel processing for market-by-market computations. ```APIDOC ## parallel ### Description Context manager for enabling parallel processing. ### Usage ```python with parallel(): # Code to be executed in parallel pass ``` ``` -------------------------------- ### Compare Mean Own Elasticities and Aggregate Elasticities Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/post_estimation.ipynb Compare the distribution of mean own elasticities with aggregate elasticities using a histogram. This visualization helps understand the difference in price sensitivity at the product and category levels. ```python plt.hist( [means.flatten(), aggregates.flatten()], color=['red', 'blue'], bins=50 ); plt.legend(['Mean Own Elasticities', 'Aggregate Elasticities']); ``` -------------------------------- ### Define a MicroDataset Configuration Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/petrin.ipynb Configure a MicroDataset by providing its name, the number of observations, and a function to compute survey weights. The compute_weights function takes market ID, products, and agents as input. ```python micro_dataset = pyblp.MicroDataset( name="CEX", observations=29125, compute_weights=lambda t, p, a: np.ones((a.size, 1 + p.size)), ) ``` -------------------------------- ### Perform Parametric Bootstrap Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/post_estimation.ipynb Use the `bootstrap` method to generate bootstrap samples and results. Specify the number of draws and a random seed for reproducibility. This is the first step in creating confidence intervals for post-estimation outputs. ```python bootstrapped_results = results.bootstrap(draws=100, seed=0) ``` -------------------------------- ### Calculate Parameter Ratio for Single Nest Case Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/logit_nested.ipynb Calculates the ratio of the 'prices' beta to (1 - 'log(within_share)' beta) for the single nest case. ```python nl2_results1.beta[0] / (1 - nl2_results1.beta[1]) ``` -------------------------------- ### Inspect Agent Data with Pandas Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/data.ipynb Display the first few rows of the agent data DataFrame to inspect its structure and content. ```python blp_agent_data.head() ``` -------------------------------- ### Update Problem and Solve with Optimal Micro Moments Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/post_estimation.ipynb Update the problem with the newly constructed optimal micro moments and re-solve it to obtain an efficient estimator. This step refines the estimation using the optimal micro moments. ```python updated_results = updated_problem.solve( results.sigma, results.pi, optimization=pyblp.Optimization('bfgs', {'gtol': 1e-5}), method='1s', micro_moments=optimal_micro_moments, ) ``` -------------------------------- ### Build Integration Agent Data Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/api/build_integration.ipynb Build integration nodes and weights for a specified dimension, typically representing product characteristics. The resulting agent_data object contains nodes and weights for integration. ```python agent_data = pyblp.build_integration(integration, 2) agent_data.nodes.shape ``` -------------------------------- ### Solve the Problem with Initial Parameters Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/post_estimation.ipynb Solves the defined pyblp problem using initial sigma and pi estimates, with a specified optimization method. This returns a ProblemResults object containing estimation outputs. ```python initial_sigma = np.diag([0.3302, 2.4526, 0.0163, 0.2441]) initial_pi = [ [ 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 ] ] results = problem.solve( initial_sigma, initial_pi, optimization=pyblp.Optimization('bfgs', {'gtol': 1e-5}), method='1s' ) results ``` -------------------------------- ### Load Agent Data with Pandas Source: https://github.com/jeffgortmaker/pyblp/blob/master/docs/notebooks/tutorial/nevo.ipynb Load agent data from a CSV file using pandas. This data includes market IDs, weights, Monte Carlo nodes, and demographic information. ```python agent_data = pd.read_csv(pyblp.data.NEVO_AGENTS_LOCATION) agent_data.head() ```