def inputs(): out = {} disc_fac = 0.9999 num_states = 300 num_buses = 200 num_periods = 1000 scale = 0.001 init_dict = { "groups": "group_4", "binsize": 5000, "model_specifications": { "discount_factor": disc_fac, "number_states": num_states, "maint_cost_func": "linear", "cost_scale": scale, }, "optimizer": {"approach": "NFXP", "algorithm": "scipy_L-BFGS-B"}, "simulation": { "discount_factor": disc_fac, "seed": 123, "buses": num_buses, "periods": num_periods, }, } out["trans_base"] = np.loadtxt(TEST_FOLDER + "repl_test_trans.txt") out["params_base"] = np.loadtxt(TEST_FOLDER + "repl_params_linear.txt") trans_mat = create_transition_matrix(num_states, out["trans_base"]) costs = calc_obs_costs(num_states, lin_cost, out["params_base"], scale) ev_known = calc_fixp(trans_mat, costs, disc_fac)[0] df = simulate(init_dict["simulation"], ev_known, costs, trans_mat) result_trans, result_fixp = estimate(init_dict, df) out["trans_est"] = result_trans["x"] out["params_est"] = result_fixp["x"] out["status"] = result_fixp["status"] return out
def test_regression_simulation(inputs): init_dict = random_init(inputs) # Draw parameter param_1 = np.random.normal(17.5, 2) param_2 = np.random.normal(21, 2) param_3 = np.random.normal(-2, 0.1) param_4 = np.random.normal(0.2, 0.1) params = np.array([param_1, param_2, param_3, param_4]) disc_fac = init_dict["simulation"]["discount_factor"] probs = np.array(init_dict["simulation"]["known_trans"]) num_states = 800 trans_mat = create_transition_matrix(num_states, probs) costs = calc_obs_costs(num_states, cubic_costs, params, 0.00001) ev = calc_fixp(trans_mat, costs, disc_fac)[0] df = simulate(init_dict["simulation"], ev, costs, trans_mat) v_disc = discount_utility(df, disc_fac) assert_allclose(v_disc / ev[0], 1, rtol=1e-02)
def simulate_data( seed, disc_fac, num_buses, num_periods, num_states, cost_params, trans_params, cost_func, scale, ): """ simulates a single data set with a given specification using the ``simulate`` function of ruspy. Parameters ---------- seed : int seed for the simulation function. disc_fac : float the discount factor in the Rust Model. num_buses : int the amount of buses that should be simulated. num_periods : int The number of periods that should be simulated for each bus. num_states : int the number of states for the which the mileage state is discretized. cost_params : np.array the cost parameters for which the data is simulated. trans_params : np.array the cost parameters for which the data is simulated.. cost_func : callable the cost function that underlies the data generating process. scale : float the scale of the cost function. Returns ------- df : pd.DataFrame Simulated data set for the given data generating process. """ init_dict = { "simulation": { "discount_factor": disc_fac, "periods": num_periods, "seed": seed, "buses": num_buses, }, } costs = calc_obs_costs(num_states, cost_func, cost_params, scale) trans_mat = create_transition_matrix(num_states, trans_params) ev = calc_fixp(trans_mat, costs, disc_fac)[0] df = simulate(init_dict["simulation"], ev, costs, trans_mat) return df
def test_regression_simulation_reduced_data_discounted_utility(inputs_sim): utility = simulate( inputs_sim["init_dict"], inputs_sim["ev"], inputs_sim["costs"], inputs_sim["trans_mat"], reduced_data="discounted utility", ) assert_almost_equal(utility, inputs_sim["v_disc"], decimal=4)
def test_regression_simulation_reduced_data_utility(inputs_sim): utilities = simulate( inputs_sim["init_dict"], inputs_sim["ev"], inputs_sim["costs"], inputs_sim["trans_mat"], reduced_data="utility", ) assert_array_equal( utilities, inputs_sim["df"]["utilities"].to_numpy().reshape( (inputs_sim["init_dict"]["buses"], inputs_sim["init_dict"]["periods"])), )
def test_regression_simulation(inputs): init_dict = random_init(inputs) df, unobs, utilities, num_states = simulate(init_dict["simulation"]) num_buses = init_dict["simulation"]["buses"] num_periods = init_dict["simulation"]["periods"] beta = init_dict["simulation"]["beta"] params = np.array(init_dict["simulation"]["params"]) probs = np.array(init_dict["simulation"]["known probs"]) v_disc_ = [0.0, 0.0] v_disc = discount_utility( v_disc_, num_buses, num_periods, num_periods, utilities, beta ) trans_mat = create_transition_matrix(num_states, probs) costs = myopic_costs(num_states, lin_cost, params) v_calc = calc_fixp(num_states, trans_mat, costs, beta) un_ob_av = 0 for bus in range(num_buses): un_ob_av += unobs[bus, 0, 0] un_ob_av = un_ob_av / num_buses assert_allclose(v_disc[1] / (v_calc[0] + un_ob_av), 1, rtol=1e-02)
def plot_convergence(init_dict): beta = init_dict["simulation"]["beta"] df, unobs, utilities, num_states = simulate(init_dict["simulation"]) costs = myopic_costs(num_states, lin_cost, init_dict["simulation"]["params"]) trans_probs = np.array(init_dict["simulation"]["known probs"]) trans_mat = create_transition_matrix(num_states, trans_probs) ev = calc_fixp(num_states, trans_mat, costs, beta) num_buses = init_dict["simulation"]["buses"] num_periods = init_dict["simulation"]["periods"] gridsize = init_dict["plot"]["gridsize"] num_points = int(num_periods / gridsize) v_exp = np.full(num_points, calc_ev_0(ev, unobs, num_buses)) v_start = np.zeros(num_points) v_disc = discount_utility(v_start, num_buses, gridsize, num_periods, utilities, beta) periods = np.arange(0, num_periods, gridsize) ax = plt.figure(figsize=(14, 6)) ax1 = ax.add_subplot(111) ax1.set_ylim([0, 1.3 * v_disc[-1]]) ax1.set_ylabel(r"Value at time 0") ax1.set_xlabel(r"Periods") ax1.plot(periods, v_disc, color="blue") ax1.plot(periods, v_exp, color="orange") plt.tight_layout() os.makedirs("figures", exist_ok=True) plt.savefig("figures/figure_1.png", dpi=300)
def inputs_sim(inputs): out = {} out["init_dict"] = init_dict = random_init(inputs)["simulation"] # Draw parameter param1 = np.random.normal(10.0, 2) param2 = np.random.normal(2.3, 0.5) params = np.array([param1, param2]) disc_fac = init_dict["discount_factor"] probs = np.array(init_dict["known_trans"]) num_states = 300 out["trans_mat"] = trans_mat = create_transition_matrix(num_states, probs) out["costs"] = costs = calc_obs_costs(num_states, lin_cost, params, 0.001) out["ev"] = ev = calc_fixp(trans_mat, costs, disc_fac)[0] out["df"] = simulate( init_dict, ev, costs, trans_mat, ) return out
init_dict = yaml.safe_load(y) beta = init_dict["simulation"]["beta"] init_dict["simulation"]["states"] = 90 v_exp_known = [] v_exp_real = [] v_disc = [] roh_plot = [] for roh in np.arange(0, 4, 0.1): roh_plot += [roh] init_dict["simulation"]["roh"] = roh worst_trans = get_worst_trans(init_dict["simulation"]) init_dict["simulation"]["real probs"] = worst_trans df, unobs, utilities, num_states = simulate(init_dict["simulation"]) costs = myopic_costs(num_states, lin_cost, init_dict["simulation"]["params"]) num_buses = init_dict["simulation"]["buses"] num_periods = init_dict["simulation"]["periods"] gridsize = init_dict["plot"]["gridsize"] real_trans_probs = np.array(init_dict["simulation"]["real probs"]) real_trans_mat = create_transition_matrix(num_states, real_trans_probs) ev_real = calc_fixp(num_states, real_trans_mat, costs, beta) known_trans_probs = np.array(init_dict["simulation"]["known probs"]) known_trans_mat = create_transition_matrix(num_states, known_trans_probs) ev_known = calc_fixp(num_states, known_trans_mat, costs, beta)
def inputs(): out = {} out["df"], out["unobs"], out["utilities"], num_states = simulate( init_dict["simulation"] ) return out