def test_equality_for_myopic_agents_and_tiny_delta(): """Test equality of simulated data and likelihood with myopia and tiny delta.""" # Get simulated data and likelihood for myopic model. params, options = generate_random_model(myopic=True) simulate = rp.get_simulate_func(params, options) df = simulate(params) log_like = get_log_like_func(params, options, df) likelihood = log_like(params) # Get simulated data and likelihood for model with tiny delta. params.loc["delta", "value"] = 1e-12 df_ = simulate(params) log_like = rp.get_log_like_func(params, options, df_) likelihood_ = log_like(params) # The continuation values are different because for delta = 0 the backward induction # is completely skipped and all continuation values are set to zero whereas for a # tiny delta, the delta ensures that continuation have no impact. columns = df.filter(like="Continu").columns.tolist() pd.testing.assert_frame_equal(df.drop(columns=columns), df_.drop(columns=columns)) np.testing.assert_almost_equal(likelihood, likelihood_, decimal=12)
def test_return_scalar_for_likelihood(model): params, options = process_model_or_seed(model) options["n_periods"] = 3 simulate = get_simulate_func(params, options) df = simulate(params) log_like = get_log_like_func(params, options, df, return_scalar=True) value = log_like(params) assert isinstance(value, float) log_like_contribs = get_log_like_func(params, options, df, return_scalar=False) outputs = log_like_contribs(params) assert isinstance(outputs, dict)
def test_generate_random_model(): """Test if random model specifications can be simulated and processed.""" params, options = generate_random_model() df = simulate_truncated_data(params, options) log_like = get_log_like_func(params, options, df) crit_val = log_like(params) assert isinstance(crit_val, float)
def test_return_output_dict_for_likelihood(model): params, options = process_model_or_seed(model) options["n_periods"] = 3 simulate = get_simulate_func(params, options) df = simulate(params) log_like = get_log_like_func(params, options, df, return_scalar=False) log_like = log_like(params) assert isinstance(log_like["value"], float) assert isinstance(log_like["contributions"], np.ndarray) assert isinstance(log_like["comparison_plot_data"], pd.DataFrame)
def test_simulation_and_estimation_with_different_models(): """Test the evaluation of the criterion function not at the true parameters.""" # Simulate a dataset params, options = generate_random_model() df = simulate_truncated_data(params, options) # Evaluate at different points, ensuring that the simulated dataset still fits. log_like = get_log_like_func(params, options, df) params_ = add_noise_to_params(params, options) params.equals(params_) log_like(params)
def test_invariant_results_for_two_estimations(): params, options = generate_random_model() df = simulate_truncated_data(params, options) log_like = get_log_like_func(params, options, df) # First estimation. crit_val = log_like(params) # Second estimation. crit_val_ = log_like(params) assert crit_val == crit_val_
def test_return_comparison_plot_data_for_likelihood(model): params, options = process_model_or_seed(model) options["n_periods"] = 3 simulate = get_simulate_func(params, options) df = simulate(params) log_like = get_log_like_func(params, options, df, return_comparison_plot_data=False) log_like = log_like(params) assert isinstance(log_like, float) log_like = get_log_like_func(params, options, df, return_comparison_plot_data=True) log_like, df = log_like(params) assert isinstance(log_like, float) assert isinstance(df, pd.DataFrame)
def test_invariance_of_model_solution_in_solve_and_criterion_functions(model): params, options = process_model_or_seed(model) solve = get_solve_func(params, options) state_space = solve(params) simulate = get_simulate_func(params, options) df = simulate(params) state_space_sim = simulate.keywords["solve"].keywords["state_space"] log_like = get_log_like_func(params, options, df) _ = log_like(params) state_space_crit = log_like.keywords["solve"].keywords["state_space"] for state_space_ in [state_space_sim, state_space_crit]: assert state_space.core.equals( state_space_.core.reindex_like(state_space.core)) apply_to_attributes_of_two_state_spaces( state_space.wages, state_space_.wages, np.testing.assert_array_equal, ) apply_to_attributes_of_two_state_spaces( state_space.nonpecs, state_space_.nonpecs, np.testing.assert_array_equal, ) apply_to_attributes_of_two_state_spaces( state_space.expected_value_functions, state_space_.expected_value_functions, np.testing.assert_array_equal, ) apply_to_attributes_of_two_state_spaces( state_space.base_draws_sol, state_space_.base_draws_sol, np.testing.assert_array_equal, )
def compute_log_likelihood(params, options): df = simulate_truncated_data(params, options) log_like = get_log_like_func(params, options, df) crit_val = log_like(params) return crit_val