jeffgortmaker / pyblp

BLP Demand Estimation with Python
https://pyblp.readthedocs.io
MIT License
240 stars 83 forks source link

ValueError ("The detected shape was (3,) + inhomogeneous part.") when running simulation.ipynb from the tutorial #156

Closed lmbanunes closed 8 months ago

lmbanunes commented 8 months ago

Hi Jeff,

I was trying to run the simulation.ipynb (unchanged) from the tutorial and when running the following cell:

results = problem.solve(
    sigma=0.5 * simulation.sigma, 
    pi=0.5 * simulation.pi,
    beta=[None, 0.5 * simulation.beta[1], None],
    optimization=pyblp.Optimization('l-bfgs-b', {'gtol': 1e-5})
)
results

I got the error message below:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[7], line 1
----> 1 results = problem.solve(
      2     sigma=0.5 * simulation.sigma, 
      3     pi=0.5 * simulation.pi,
      4     beta=[None, 0.5 * simulation.beta[1], None],
      5     optimization=pyblp.Optimization('l-bfgs-b', {'gtol': 1e-5})
      6 )
      7 results

File ~\AppData\Local\miniconda3\envs\p3_9_blp\lib\site-packages\pyblp\economies\problem.py:544, in ProblemEconomy.solve(self, sigma, pi, rho, beta, gamma, sigma_bounds, pi_bounds, rho_bounds, beta_bounds, gamma_bounds, delta, method, initial_update, optimization, scale_objective, check_optimality, finite_differences, error_behavior, error_punishment, delta_behavior, iteration, fp_type, shares_bounds, costs_bounds, W, center_moments, W_type, se_type, covariance_moments_mean, micro_moments, micro_sample_covariances, resample_agent_data)
    541     raise ValueError("initial_update cannot be False with micro_moments and no initial W specified.")
    543 # validate parameters before compressing unfixed parameters into theta and outputting related information
--> 544 parameters = Parameters(
    545     self, sigma, pi, rho, beta, gamma, sigma_bounds, pi_bounds, rho_bounds, beta_bounds, gamma_bounds,
    546     bounded=optimization._supports_bounds, allow_linear_nans=True
    547 )
    548 theta = parameters.compress()
    549 theta_bounds = parameters.compress_bounds()

File ~\AppData\Local\miniconda3\envs\p3_9_blp\lib\site-packages\pyblp\parameters.py:200, in Parameters.__init__(self, economy, sigma, pi, rho, beta, gamma, sigma_bounds, pi_bounds, rho_bounds, beta_bounds, gamma_bounds, bounded, allow_linear_nans, check_alpha)
    198 self.pi = self.initialize_matrix("pi", "demographics were formulated", pi, [(economy.K2, economy.D)])
    199 self.rho = self.initialize_matrix("rho", "nesting IDs were specified", rho, [(economy.H, 1), (1, 1)])
--> 200 self.beta = self.initialize_matrix("beta", "X1 was formulated", beta, [(economy.K1, 1)], allow_linear_nans)
    201 self.gamma = self.initialize_matrix("gamma", "X3 was formulated", gamma, [(economy.K3, 1)], allow_linear_nans)
    203 # fill the upper triangle of sigma with zeros

File ~\AppData\Local\miniconda3\envs\p3_9_blp\lib\site-packages\pyblp\parameters.py:301, in Parameters.initialize_matrix(name, condition_name, values, shapes, allow_nans)
    299 # validate the matrix
    300 if values is not None:
--> 301     matrix = np.c_[np.asarray(values, options.dtype)]
    302 if (values is not None and matrix.size > 0) != all(r * c > 0 for r, c in shapes):
    303     raise ValueError(f"{name} should be specified only when {condition_name}.")

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part.

Do you have any tips on how to overcome this?

Thank a lot for your time, Leonardo

jeffgortmaker commented 8 months ago

Thanks for reporting this Leonardo! Looks like something may have changed with array coercion since I wrote that tutorial.

The issue is that simulation.beta is technically a column vector, so simulation.beta[1] selects a vector (of one element) rather than a single number. You can replace this with simulation.beta[1, 0] to overcome it.

lmbanunes commented 7 months ago

Thanks for solving the issue Jeff!