def test_calc_fixp(inputs, outputs): assert_array_almost_equal( calc_fixp( inputs["nstates"], outputs["trans_mat"], outputs["myop_costs"], inputs["beta"], ), outputs["fixp"], )
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)
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) v_calc = 0 for i in range(num_buses): v_calc = v_calc + unobs[i, 0, 0] + ev_known[0] v_calc = v_calc / num_buses v_exp_known += [v_calc] v_calc = 0 for i in range(num_buses): v_calc = v_calc + unobs[i, 0, 0] + ev_real[0] v_calc = v_calc / num_buses
from ruspy.estimation.estimation_cost_parameters import calc_fixp from ruspy.estimation.estimation_cost_parameters import lin_cost from ruspy.estimation.estimation_cost_parameters import myopic_costs from ruspy.estimation.estimation_cost_parameters import create_transition_matrix with open("init.yml") as y: init_dict = yaml.safe_load(y) 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"]["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))
def simulate_strategy(known_trans, increments, num_buses, num_periods, params, beta, unobs, maint_func): """ This function manages the simulation process. It initializes the auxiliary variables and calls therefore the subfuctions from estimation auxiliary. It then calls the decision loop, written for numba. As the state size of the fixed point needs to be a lot larger than the actual state, the size is doubled, if the loop hasn't run yet through all the periods. :param known_trans: A numpy array containing the transition probabilities the agent assumes. :param increments: A two dimensional numpy array containing for each bus in each period a random drawn state increase as integer. :param num_buses: The number of buses to be simulated. :type num_buses: int :param num_periods: The number of periods to be simulated. :type num_periods: int :param params: A numpy array containing the parameters shaping the cost function. :param beta: The discount factor. :type beta: float :param unobs: A three dimensional numpy array containing for each bus, for each period for the decision to maintain or replace the bus engine a random drawn utility as float. :param maint_func: The maintenance cost function. Only linear implemented so far. :return: The function returns the following objects: :states: : A two dimensional numpy array containing for each bus in each period the state as an integer. :decisions: : A two dimensional numpy array containing for each bus in each period the decision as an integer. :utilities: : A two dimensional numpy array containing for each bus in each period the utility as a float. :num_states: (int) : The size of the state space. """ num_states = int(200) start_period = int(0) states = np.zeros((num_buses, num_periods), dtype=int) decisions = np.zeros((num_buses, num_periods), dtype=int) utilities = np.zeros((num_buses, num_periods), dtype=float) while start_period < num_periods - 1: num_states = 2 * num_states known_trans_mat = create_transition_matrix(num_states, known_trans) costs = myopic_costs(num_states, maint_func, params) ev = calc_fixp(num_states, known_trans_mat, costs, beta) states, decisions, utilities, start_period = simulate_strategy_loop( num_buses, states, decisions, utilities, costs, ev, increments, num_states, start_period, num_periods, beta, unobs, ) return states, decisions, utilities, num_states
def select_fixp(trans_probs, state, num_states, costs, beta, max_it): trans_mat = create_transition_matrix(num_states, trans_probs) fixp = calc_fixp(num_states, trans_mat, costs, beta, max_it=max_it) return fixp[state]