def test_invalid_beta_throws(self): distances = dok_matrix(hand_net_dists) if issparse(distances): hand_net_incidence_local = dok_matrix(hand_net_incidence) else: hand_net_incidence_local = hand_net_incidence data_list = [distances] network_struct = ModelDataStruct(data_list, hand_net_incidence_local, data_array_names_debug=("distances", "u_turn")) beta_vec = np.array([-5]) model = RecursiveLogitModelPrediction(network_struct, initial_beta=beta_vec, mu=1) try: model.generate_observations(origin_indices=[0, 1, 2, 7], dest_indices=[1, 6, 3], num_obs_per_pair=4, iter_cap=15, rng_seed=1) except ValueError as e: print(str(e))
def test_bad_beta_fails(self, struct_bigger): model = RecursiveLogitModelPrediction(struct_bigger, initial_beta=-0.1) with pytest.raises(ValueError) as e: model.generate_observations(origin_indices=[0], dest_indices=[9], num_obs_per_pair=10) assert "exp(V(s)) contains negative values" in str(e.value)
def test_bad_indexfails(self, struct_bigger): model = RecursiveLogitModelPrediction(struct_bigger, initial_beta=-0.2) with pytest.raises(IndexError) as e: model.generate_observations(origin_indices=[0], dest_indices=[100], num_obs_per_pair=10) assert "Can only simulate observations from indexes which are in the model" in str( e.value) with pytest.raises(IndexError) as e: model.generate_observations(origin_indices=[0], dest_indices=[10], num_obs_per_pair=10) assert "but the final index is reserved for internal dummy sink state" in str( e.value)
def get_data(beta, seed=None): beta_vec_generate = np.array([beta]) model = RecursiveLogitModelPrediction(network_struct, initial_beta=beta_vec_generate, mu=1) obs_indices = [0] obs = model.generate_observations(origin_indices=obs_indices, dest_indices=[1], num_obs_per_pair=60, iter_cap=2000, rng_seed=seed, ) return obs
def get_data(beta_vec, seed=None): model = RecursiveLogitModelPrediction(network_struct, initial_beta=beta_vec, mu=1) obs = model.generate_observations( origin_indices=orig_indices, dest_indices=dest_indices, num_obs_per_pair=obs_per_pair, iter_cap=2000, rng_seed=seed, ) return obs
def _basic_consistencey_checks(distances): data_list = [distances] if issparse(distances): hand_net_incidence_local = dok_matrix(hand_net_incidence) else: hand_net_incidence_local = hand_net_incidence network_struct = ModelDataStruct(data_list, hand_net_incidence_local, data_array_names_debug=("distances", "u_turn")) beta_vec = np.array([-1]) model = RecursiveLogitModelPrediction(network_struct, initial_beta=beta_vec, mu=1) obs = model.generate_observations(origin_indices=[0, 1, 2, 7], dest_indices=[1, 6, 3], num_obs_per_pair=4, iter_cap=15, rng_seed=1) expected = TestSimulation._get_basic_consistency_expected( ALLOW_POSITIVE_VALUE_FUNCTIONS) assert obs == expected
data_list = [distances] network_struct = ModelDataStruct(data_list, incidence_mat, data_array_names_debug=("distances")) beta_vec = np.array([-16]) model = RecursiveLogitModelPrediction(network_struct, initial_beta=beta_vec, mu=1) # obs_indices = [i for i in range(8)] # obs = model.generate_observations(origin_indices=obs_indices, # dest_indices=obs_indices, # num_obs_per_pair=1, iter_cap=2000, rng_seed=1, # ) obs_indices = [1, 2, 3, 4, 5, 6, 7, 8] obs = model.generate_observations(origin_indices=obs_indices, dest_indices=[7, 3], num_obs_per_pair=20, iter_cap=2000, rng_seed=1, ) print(obs) print("\nPath in terms of arcs:") for path in obs: string = "Orig: " f = "Empty Path, should not happen" for arc_index in path: string += f"-{arc_index + 1}- => " string += ": Dest" print(string) obs_fname = "my_networks_obs2.json"
beta_sim = np.array([-0.8, -0.00015]) model = RecursiveLogitModelPrediction(network_struct, initial_beta=beta_sim, mu=1) print("Linear system size", model.get_exponential_utility_matrix().shape) # sparse sample for quick running example orig_indices = np.arange(0, node_max, 2) dest_indices = (orig_indices + 5) % node_max # sample every OD pair once # orig_indices = np.arange(0, node_max, 1) # dest_indices = np.arange(0, node_max, 1) obs_per_pair = 1 print(f"Generating {obs_per_pair * len(orig_indices) * len(dest_indices)} obs total per " f"configuration") seed = 42 obs = model.generate_observations(origin_indices=orig_indices, dest_indices=dest_indices, num_obs_per_pair=obs_per_pair, iter_cap=2000, rng_seed=seed) optimiser = optimisers.ScipyOptimiser(method='l-bfgs-b') beta_est_init = [-5, -0.00001] model_est = RecursiveLogitModelEstimation(network_struct, observations_record=obs, initial_beta=beta_est_init, mu=1, optimiser=optimiser) beta_est = model_est.solve_for_optimal_beta(verbose=False) print(f"beta expected: [{beta_sim[0]:6.4f}, {beta_sim[1]:6.4f}]," f" beta_actual: [{beta_est[0]:6.4f}, {beta_est[1]:6.4f}]") def test_est(): assert np.allclose([-1.02129578, -0.000175462809], beta_est)