コード例 #1
0
ファイル: eggses.py プロジェクト: alexhhn/TDT4136
def grid_score(k, grid):
    width, height = grid_dimensions(grid)

    row_penalties = list(max(0, sum(row) - k) for row in grid)
    col_penalties = list(max(0, sum(row[j] for row in grid) - k) for j in range(width))

    grid = array(grid)
    flip = flipud(grid)

    r1 = range(-(width - 1), width)
    r2 = range(-(height - 1), height)

    dia_penalties = lambda m, dia_i: max(0, int(m.trace(dia_i)) - k)

    di1_penalties = list(dia_penalties(grid, d) for d in r1)
    di2_penalties = list(dia_penalties(flip, d) for d in r2)

    return sum(row_penalties) + sum(col_penalties) + sum(di1_penalties) + sum(di2_penalties)
コード例 #2
0
def grid_score(k, grid):
    width, height = grid_dimensions(grid)

    row_penalties = list(max(0, sum(row) - k) for row in grid)
    col_penalties = list(
        max(0,
            sum(row[j] for row in grid) - k) for j in range(width))

    grid = array(grid)
    flip = flipud(grid)

    r1 = range(-(width - 1), width)
    r2 = range(-(height - 1), height)

    dia_penalties = lambda m, dia_i: max(0, int(m.trace(dia_i)) - k)

    di1_penalties = list(dia_penalties(grid, d) for d in r1)
    di2_penalties = list(dia_penalties(flip, d) for d in r2)

    return sum(row_penalties) + sum(col_penalties) + sum(di1_penalties) + sum(
        di2_penalties)
コード例 #3
0
def analytical_results(r_max, r_min, bailout_cost, discount_rate,
                       nr_of_periods, R, f_opt_bank_0):
    # Analytical results:

    # optimal value for period 0
    opt_pi_bank = [0] * nr_of_periods
    v_bank = [0] * nr_of_periods

    #opt_pi_bank[0] = optimize.fmin(f_opt_bank_0, 0.5) # optimization using: Simplex method: the Nelder-Mead
    opt_pi_bank[0] = optimize.minimize(f_opt_bank_0,
                                       0.5,
                                       method='L-BFGS-B',
                                       bounds=[(0.0, 1.0)])['x']
    v_bank[0] = -f_opt_bank_0(opt_pi_bank[0])

    # bank problem in the rest of the periods, eq.(28)
    f_bank = lambda x, y: -((x * (r_min + (r_max - r_min) * (1 - x)) +
                             (1 - x) * (bailout_cost + discount_rate * y)) /
                            (1 - discount_rate * x))
    for k in range(1, nr_of_periods):
        f_opt_bank_0 = lambda x: f_bank(x, v_bank[k - 1])
        #opt_pi_bank[k] = optimize.fmin(f_opt_bank_0, 0.7)
        opt_pi_bank[k] = optimize.minimize(f_opt_bank_0,
                                           0.7,
                                           method='L-BFGS-B',
                                           bounds=[(0.0, 1.0)])['x']
        v_bank[k] = -f_opt_bank_0(opt_pi_bank[k])

    # Analytical results:
    # check that analytical solutions coinside with optimal values:
    pi_bank_analytical = [0] * nr_of_periods
    v_bank_analytical = [0] * nr_of_periods
    temp_v_vheck = [0] * nr_of_periods
    pi_bank_analytical[0] = (1 - math.sqrt(1 - r_max * discount_rate /
                                           (r_max - r_min))) / discount_rate
    v_bank_analytical[0] = pi_bank_analytical[0] * R(
        pi_bank_analytical[0]) / (1 - discount_rate * pi_bank_analytical[0])
    for i in range(1, nr_of_periods):
        pi_bank_analytical[i] = (1 - math.sqrt(
            1 - discount_rate *
            (r_max -
             (bailout_cost + discount_rate * v_bank_analytical[i - 1]) *
             (1 - discount_rate)) / (r_max - r_min))) / discount_rate
        v_bank_analytical[i] = (
            pi_bank_analytical[i] * R(pi_bank_analytical[i]) +
            (1 - pi_bank_analytical[i]) *
            (bailout_cost + discount_rate * v_bank_analytical[i - 1])) / (
                1 - discount_rate * pi_bank_analytical[i])

    # We want to solve for Rmax that would give the pi_bank same as pi_state in
    # the last period:
    temp_fun = lambda r_max: pi_bank_analytical[0] - (1 - math.sqrt(
        1 - r_max * discount_rate / (r_max - r_min))) / discount_rate
    res_sol_temp_fun = optimize.fsolve(temp_fun, 5)

    # compare optimization results and analytical results:
    nr_per = [0] * nr_of_periods
    for i in range(0, nr_of_periods):
        nr_per[i] = i

    # Create plots and pdf file. Save pdf file into graphics folder
    fig2, bx = pyplot.subplots()
    bx.plot(nr_per,
            flipud(opt_pi_bank),
            label='Optimization Results',
            color='red',
            linestyle=':')
    bx.plot(nr_per,
            flipud(pi_bank_analytical),
            label='Analytical Results',
            color='blue',
            linestyle='--')
    bx.set_xlabel('Number of Periods')
    bx.set_ylabel('Probabilities')
    bx.set_title('Optimal probabilities for the bank')
    bx.legend(loc='upper left',
              frameon=0,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig2 = mpld3.fig_to_html(fig2)
    pyplot.savefig(
        'tbtf/modelfinite/graphics/F_Optimal_Probabilities_for_the_Bank.pdf',
        format='pdf')
    pyplot.close(fig2)

    return html_text_fig2, opt_pi_bank, pi_bank_analytical
コード例 #4
0
def joint_problem(r_max, r_min, bailout_cost, deadweight_cost, discount_rate,
                  nr_of_periods):
    # Joint problem: after each stage Bank promises pi <= pi_state, but recalculates it's value

    opt_pi_bank_joint = [0] * nr_of_periods
    opt_pi_state_joint = [0] * nr_of_periods
    v_bank_joint = [0] * nr_of_periods
    v_state_joint = [0] * nr_of_periods

    # n=0
    f_opt_bank_0 = lambda x: -x * (r_min + (r_max - r_min) *
                                   (1 - x)) / (1 - discount_rate * x)
    f_state_0 = lambda x: -deadweight_cost + bailout_cost + discount_rate * deadweight_cost * (
        1 - x) / (1 - discount_rate * x)

    #opt_pi_bank_joint[0] = optimize.fmin(f_opt_bank_0, 0.5) # optimization using: Simplex method: the Nelder-Mead
    opt_pi_bank_joint[0] = optimize.minimize(f_opt_bank_0,
                                             0.5,
                                             method='L-BFGS-B',
                                             bounds=[(0.0, 1.0)])['x']
    opt_pi_state_joint[0] = optimize.root(f_state_0, 0.5, method='lm').x

    # Bank "promises" the threshold pi in order to be saved at period n=1 even it it's optimal pi is lower:
    if opt_pi_bank_joint[0] < opt_pi_state_joint[0]:
        opt_pi_bank_joint[0] = opt_pi_state_joint[0]

    v_bank_joint[0] = -f_opt_bank_0(opt_pi_bank_joint[0])
    v_state_joint[0] = deadweight_cost * (1 - opt_pi_bank_joint[0]) / (
        1 - discount_rate * opt_pi_bank_joint[0])

    # Previous periods:
    f_bank = lambda x, y: -((x * (r_min + (r_max - r_min) * (1 - x)) +
                             (1 - x) * (bailout_cost + discount_rate * y)) /
                            (1 - discount_rate * x))
    f_state = lambda x,y: - deadweight_cost + bailout_cost + discount_rate * (bailout_cost + discount_rate * y) * \
                                                             (1 - x) / (1 - discount_rate * x)

    # Options = optimset('TolFun', 1e-15):
    for i in range(1, nr_of_periods):
        f_opt_bank = lambda x: f_bank(x, v_bank_joint[i - 1])
        f_state_opt = lambda x: f_state(x, v_state_joint[i - 1])

        #opt_pi_bank_joint[i] = optimize.fmin(f_opt_bank, 0.7)
        opt_pi_bank_joint[i] = optimize.minimize(f_opt_bank,
                                                 0.7,
                                                 method='L-BFGS-B',
                                                 bounds=[(0.0, 1.0)])['x']
        opt_pi_state_joint[i] = optimize.root(f_state_opt, 0.5, method='lm').x

        # BANKS's PROBABILITES CHANGED IF THEY ALE LOWER THAN PI_STATE AND THEN THEY ARE TAKEN FOR V's:
        if opt_pi_bank_joint[i] < opt_pi_state_joint[i]:
            opt_pi_bank_joint[i] = opt_pi_state_joint[i]

        v_bank_joint[i] = -f_opt_bank(opt_pi_bank_joint[i])
        v_state_joint[i] = (bailout_cost + discount_rate * v_state_joint[i-1]) * (1 - opt_pi_bank_joint[i]) /\
                           (1 - discount_rate * opt_pi_bank_joint[i])

    nr_per = [0] * nr_of_periods
    for i in range(0, nr_of_periods):
        nr_per[i] = i

    # Create plots and pdf file. Save pdf file into graphics folder
    fig10, jx = pyplot.subplots()
    jx.plot(nr_per,
            flipud(opt_pi_bank_joint),
            label='Bank',
            color='red',
            linestyle=':')
    jx.plot(nr_per,
            flipud(opt_pi_state_joint),
            label='State',
            color='blue',
            linestyle='--')
    jx.set_xlabel('Stage of the Game')
    jx.set_ylabel('Probability')
    jx.set_title('Optimal probabilities in joint game')
    jx.legend(loc='lower left',
              frameon=True,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig10 = mpld3.fig_to_html(fig10)
    pyplot.savefig(
        'tbtf/modelfinite/graphics/F_Optimal_Probabilities_in_Joint_Game.pdf',
        format='pdf')
    pyplot.close(fig10)

    fig11, kx = pyplot.subplots()
    kx.plot(nr_per, flipud(v_state_joint), color='red')
    kx.set_title('State value in the joint game')
    kx.legend(loc='lower left',
              frameon=True,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig11 = mpld3.fig_to_html(fig11)
    pyplot.savefig('tbtf/modelfinite/graphics/F_State_Value_in_Joint_Game.pdf',
                   format='pdf')
    pyplot.close(fig11)

    return html_text_fig10, html_text_fig11
コード例 #5
0
def states_problem_min_banks_prob_not_changed(bailout_cost, deadweight_cost,
                                              discount_rate, nr_of_periods,
                                              opt_pi_bank):
    # State's problem: min if Bank's probabilitie is not changed

    # Solve the equation from (20) on:
    v_state_opt_min = [0] * nr_of_periods
    pi_state_tresh_min = [0] * nr_of_periods
    opt_pi_bank_min = opt_pi_bank

    # n=0, pi=0 does not change
    f_state_0 = lambda x: -deadweight_cost + bailout_cost + discount_rate * deadweight_cost * (
        1 - x) / (1 - discount_rate * x)
    pi_state_tresh_min[0] = optimize.root(f_state_0, 0.5, method='lm').x
    v_state_opt_min[0] = deadweight_cost * (1 - opt_pi_bank_min[0]) / (
        1 - discount_rate * opt_pi_bank_min[0])

    # previous periods, n > 0:
    f_state = lambda x,y: - deadweight_cost + bailout_cost + discount_rate * (bailout_cost + discount_rate * y) * \
                                                             (1 - x) / (1 - discount_rate * x)

    n_end = 0

    for i in range(1, nr_of_periods):
        f_state_opt = lambda x: f_state(x, v_state_opt_min[i - 1])
        pi_state_tresh_min[i] = optimize.root(f_state_opt, 0.5, method='lm').x

        if min(deadweight_cost, bailout_cost +
               discount_rate * v_state_opt_min[i - 1]) == deadweight_cost:
            n_end = nr_of_periods - i + 1

        v_state_opt_min[i] = min(deadweight_cost, bailout_cost + discount_rate * v_state_opt_min[i-1]) *\
                             (1 - opt_pi_bank_min[i]) / (1 - discount_rate * opt_pi_bank_min[i])

    nr_per = [0] * nr_of_periods
    for i in range(0, nr_of_periods):
        nr_per[i] = i

    # Create plots and pdf file. Save pdf file into graphics folder
    fig8, hx = pyplot.subplots()
    hx.plot(nr_per,
            flipud(opt_pi_bank_min),
            label='Bank',
            color='red',
            linestyle=':')
    hx.plot(nr_per,
            flipud(pi_state_tresh_min),
            label='State',
            color='blue',
            linestyle='--')
    hx.set_xlabel('Stage of the Game')
    hx.set_ylabel('Probability')
    hx.set_title('Optimal Probabilities, State Chooses min(c,b+dv)')
    hx.legend(loc='lower left',
              frameon=True,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig8 = mpld3.fig_to_html(fig8)
    pyplot.savefig(
        'tbtf/modelfinite/graphics/F_Optimal_Probabilities_State_Chooses_min(c_b_dv).pdf',
        format='pdf')
    pyplot.close(fig8)

    fig9, ix = pyplot.subplots()
    ix.plot(nr_per, flipud(v_state_opt_min), label='Value', color='red')
    ix.set_xlabel('Stage of the Game')
    ix.set_ylabel('Value')
    ix.set_title('State value if it choses whether to save the bank')
    ix.legend(loc='lower left',
              frameon=True,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig9 = mpld3.fig_to_html(fig9)
    pyplot.savefig(
        'tbtf/modelfinite/graphics/F_State_Value_if_it_choses_whether_to_save_the_Bank.pdf',
        format='pdf')
    pyplot.close(fig9)

    return html_text_fig8, html_text_fig9
コード例 #6
0
def states_problem_change_banks_pi(bailout_cost, deadweight_cost,
                                   discount_rate, nr_of_periods, opt_pi_bank):
    # State's problem: Change Bank's pi if it's less then State's pi

    # solve the equation from (20) on:
    v_state_opt_switch = [0] * nr_of_periods
    pi_state_tresh_switch = [0] * nr_of_periods
    opt_pi_bank_switch = opt_pi_bank

    # n=0
    f_state_0 = lambda x: -deadweight_cost + bailout_cost + discount_rate * deadweight_cost * (
        1 - x) / (1 - discount_rate * x)
    pi_state_tresh_switch[0] = optimize.fsolve(f_state_0, 0.5)

    # Bank's "promises" the threshold pi in order to be saved at period n=1 even
    # if it's optimal pi is lower:
    if opt_pi_bank_switch[0] < pi_state_tresh_switch[0]:
        opt_pi_bank_switch[0] = pi_state_tresh_switch[0]

    v_state_opt_switch[0] = deadweight_cost * (1 - opt_pi_bank_switch[0]) / (
        1 - discount_rate * opt_pi_bank_switch[0])

    # previous periods
    f_state = lambda x,y: - deadweight_cost + bailout_cost + discount_rate * (bailout_cost + discount_rate * y) * \
                                                             (1 - x) / (1 - discount_rate * x)

    # solve for all the previous periods:
    for i in range(1, nr_of_periods):
        f_state_opt = lambda x: f_state(x, v_state_opt_switch[i - 1])
        pi_state_tresh_switch[i] = optimize.fsolve(f_state_opt, 0.5)

        # BANK's PROBABILITIES CHANGED IF THEY ALE LOWER THAN PI_STATE AND THEN THEY ARE TAKEN FOR V's:
        if opt_pi_bank_switch[i] < pi_state_tresh_switch[i]:
            opt_pi_bank_switch[i] = pi_state_tresh_switch[i]

        v_state_opt_switch[i] = (bailout_cost + discount_rate * v_state_opt_switch[i-1]) * (1 - opt_pi_bank_switch[i])\
                                / (1 - discount_rate * opt_pi_bank_switch[i])

    nr_per = [0] * nr_of_periods
    for i in range(0, nr_of_periods):
        nr_per[i] = i

    # Create plots and pdf file. Save pdf file into graphics folder
    fig6, fx = pyplot.subplots()
    fx.plot(nr_per, flipud(opt_pi_bank), label='Bank', color='red')
    fx.plot(nr_per,
            flipud(pi_state_tresh_switch),
            label='State',
            color='blue',
            linestyle=':')
    fx.plot(nr_per,
            flipud(opt_pi_bank_switch),
            label='Bank Switched',
            color='green',
            linestyle='--')
    fx.set_xlabel('Stage of the Game')
    fx.set_ylabel('Probability')
    fx.set_title('Optimal Probabilities')
    fx.legend(loc='lower left',
              frameon=True,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig6 = mpld3.fig_to_html(fig6)
    pyplot.savefig('tbtf/modelfinite/graphics/F_Optimal_Probabilities.pdf',
                   format='pdf')
    pyplot.close(fig6)

    fig7, gx = pyplot.subplots()
    gx.plot(nr_per,
            flipud(v_state_opt_switch),
            label='New value (Bank switches)',
            color='red',
            linestyle=':')
    gx.plot(nr_per,
            flipud(v_state_opt_switch),
            label='Old value',
            color='blue',
            linestyle='--')
    gx.set_title('State value if Bank switches to state pi')
    gx.legend(loc='lower left',
              frameon=True,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig7 = mpld3.fig_to_html(fig7)
    pyplot.savefig(
        'tbtf/modelfinite/graphics/F_State_Value_if_Bank_Switches_to_State_pi.pdf',
        format='pdf')
    pyplot.close(fig7)

    return html_text_fig6, html_text_fig7
コード例 #7
0
def states_problem(bailout_cost, deadweight_cost, discount_rate, nr_of_periods,
                   opt_pi_bank, pi_bank_analytical):
    # State's Problem

    # solve the equations from (20) on:
    v_state_opt = [0] * nr_of_periods
    pi_state_thresh = [0] * nr_of_periods

    # Period n=0
    f_state_0 = lambda x: -deadweight_cost + bailout_cost + discount_rate * deadweight_cost * (
        1 - x) / (1 - discount_rate * x)
    pi_state_thresh[0] = optimize.fsolve(f_state_0, 0.5)
    v_state_opt[0] = deadweight_cost * (1 - opt_pi_bank[0]) / (
        1 - discount_rate * opt_pi_bank[0])

    # analytical check:
    pi_state_analytical = [0] * nr_of_periods
    pi_state_analytical[0] = (
        deadweight_cost -
        (deadweight_cost - bailout_cost) / discount_rate) / bailout_cost

    # previous periods:
    f_state = lambda x,y: - deadweight_cost + bailout_cost + discount_rate * (bailout_cost + discount_rate * y) * (1 - x) /\
                                                             (1 - discount_rate * x)

    # solve for the rest of the periods:
    for i in range(1, nr_of_periods):
        f_state_opt = lambda x: f_state(x, v_state_opt[i - 1])
        pi_state_thresh[i] = optimize.fsolve(f_state_opt, 0.5)

        #BANK's PROBABILITIE'sS ARE TAKEN FOR v_state
        v_state_opt[i] = (
            bailout_cost + discount_rate * v_state_opt[i - 1]) * (
                1 - opt_pi_bank[i]) / (1 - discount_rate * opt_pi_bank[i])

        pi_state_analytical[i] = (
            bailout_cost + (discount_rate * v_state_opt[i - 1]) -
            (deadweight_cost - bailout_cost) / discount_rate) / (
                bailout_cost + (discount_rate * v_state_opt[i - 1]) -
                (deadweight_cost - bailout_cost))

    nr_per = [0] * nr_of_periods
    for i in range(0, nr_of_periods):
        nr_per[i] = i

    # Create plots and pdf file. Save pdf file into graphics folder
    fig3, cx = pyplot.subplots()
    cx.plot(nr_per,
            flipud(opt_pi_bank),
            label='Bank',
            color='red',
            linestyle=':')
    cx.plot(nr_per,
            flipud(pi_state_thresh),
            label='State',
            color='blue',
            linestyle='--')
    cx.set_xlabel('Stage of the Game')
    cx.set_ylabel('Probability')
    cx.set_title('Machine Results')
    cx.legend(loc='upper left',
              frameon=0,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig3 = mpld3.fig_to_html(fig3)
    pyplot.savefig('tbtf/modelfinite/graphics/F_Machine_Results.pdf',
                   format='pdf')
    pyplot.close(fig3)

    fig4, dx = pyplot.subplots()
    dx.plot(nr_per,
            flipud(pi_bank_analytical),
            label='Bank',
            color='red',
            linestyle=':')
    dx.plot(nr_per,
            flipud(pi_state_analytical),
            label='State',
            color='blue',
            linestyle='--')
    dx.set_xlabel('Stage of the Game')
    dx.set_ylabel('Probability')
    dx.set_title('Analytical Results')
    dx.legend(loc='upper left',
              frameon=0,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig4 = mpld3.fig_to_html(fig4)
    pyplot.savefig('tbtf/modelfinite/graphics/F_Analytical_Results.pdf',
                   format='pdf')
    pyplot.close(fig4)

    fig5, ex = pyplot.subplots()
    ex.plot(nr_per, flipud(v_state_opt), label='State_Value_Function')
    ex.set_title('State Value Function')
    html_text_fig5 = mpld3.fig_to_html(fig5)
    pyplot.savefig('tbtf/modelfinite/graphics/F_State_Value_Function.pdf',
                   format='pdf')
    pyplot.close(fig5)

    return html_text_fig3, html_text_fig4, html_text_fig5, opt_pi_bank
コード例 #8
0
def banks_problem_analytical_results(r_max, r_min, bailout_cost, discount_rate,
                                     nr_of_periods, R):
    # Bank's problem:

    # bank's problem at the last stage: max(pi) pi*R(pi)/(1-delta*pi)
    f_opt_bank_0 = lambda x: -x * (r_min + (r_max - r_min) *
                                   (1 - x)) / (1 - discount_rate * x)

    # Analytical results:

    # optimal value for period 0
    opt_pi_bank = [0] * nr_of_periods
    v_bank = [0] * nr_of_periods

    opt_pi_bank[0] = optimize.fmin(
        f_opt_bank_0,
        0.5)  # optimization using: Simplex method: the Nelder-Mead
    v_bank[0] = -f_opt_bank_0(opt_pi_bank[0])

    # bank problem in the rest of the periods, eq.(28)
    f_bank = lambda x, y: -((x * (r_min + (r_max - r_min) * (1 - x)) +
                             (1 - x) * (bailout_cost + discount_rate * y)) /
                            (1 - discount_rate * x))
    for k in range(1, nr_of_periods):
        f_opt_bank_0 = lambda x: f_bank(x, v_bank[k - 1])
        opt_pi_bank[k] = optimize.fmin(f_opt_bank_0, 0.7)
        v_bank[k] = -f_opt_bank_0(opt_pi_bank[k])

    # Analytical results:
    # check that analytical solutions coinside with optimal values:
    pi_bank_analytical = [0] * nr_of_periods
    v_bank_analytical = [0] * nr_of_periods
    temp_v_vheck = [0] * nr_of_periods
    pi_bank_analytical[0] = (1 - math.sqrt(1 - r_max * discount_rate /
                                           (r_max - r_min))) / discount_rate
    v_bank_analytical[0] = pi_bank_analytical[0] * R(
        pi_bank_analytical[0]) / (1 - discount_rate * pi_bank_analytical[0])
    for i in range(1, nr_of_periods):
        pi_bank_analytical[i] = (1 - math.sqrt(
            1 - discount_rate *
            (r_max -
             (bailout_cost + discount_rate * v_bank_analytical[i - 1]) *
             (1 - discount_rate)) / (r_max - r_min))) / discount_rate
        v_bank_analytical[i] = (
            pi_bank_analytical[i] * R(pi_bank_analytical[i]) +
            (1 - pi_bank_analytical[i]) *
            (bailout_cost + discount_rate * v_bank_analytical[i - 1])) / (
                1 - discount_rate * pi_bank_analytical[i])

    nr_per = [0] * nr_of_periods
    for i in range(0, nr_of_periods):
        nr_per[i] = i

    # Create plots and pdf file. Save pdf file into graphics folder
    fig1, ax = pyplot.subplots()
    ax.plot(nr_per,
            flipud(opt_pi_bank),
            label='Machine Results',
            color='red',
            linestyle=':')
    ax.plot(nr_per,
            flipud(pi_bank_analytical),
            label='Analytical Results',
            color='blue',
            linestyle='--')
    ax.set_xlabel('Number of Periods')
    ax.set_ylabel('Probabilities')
    ax.set_title('Optimal probabilities for the bank')
    ax.legend(loc='upper left',
              frameon=0,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig1 = mpld3.fig_to_html(fig1)
    pyplot.savefig(
        'tbtf/modelfinitebounded/graphics/FB_Optimal_Probabilities_for_the_Bank.pdf',
        format='pdf')
    pyplot.close(fig1)

    return html_text_fig1, opt_pi_bank, pi_bank_analytical
コード例 #9
0
def states_problem(bailout_cost, deadweight_cost, discount_rate, nr_of_periods,
                   opt_pi_bank):
    # State's Problem

    # solve the equations from (20) on:
    v_state_opt = [0] * nr_of_periods
    pi_state_thresh = [0] * nr_of_periods

    x = math.pow(5, 2)

    # Period n=0
    f_state_0 = lambda x: math.pow(
        -deadweight_cost + bailout_cost + discount_rate * deadweight_cost *
        (1 - x) / (1 - discount_rate * x), 2)
    pi_state_thresh[0] = optimize.fsolve(f_state_0, 0.5)
    v_state_opt[0] = deadweight_cost * (1 - opt_pi_bank[0]) / (
        1 - discount_rate * opt_pi_bank[0])

    # previous periods:
    f_state = lambda x, y: math.pow(
        -deadweight_cost + bailout_cost + discount_rate *
        (bailout_cost + discount_rate * y) * (1 - x) /
        (1 - discount_rate * x), 2)

    # solve for the rest of the periods:
    for i in range(1, nr_of_periods):
        f_state_opt = lambda x: f_state(x, v_state_opt[i - 1])
        pi_state_thresh[i] = optimize.fsolve(f_state_opt, 0.5)

        #BANK's PROBABILITIE'sS ARE TAKEN FOR v_state
        v_state_opt[i] = (bailout_cost + discount_rate * v_state_opt[i - 1]) * (1 - opt_pi_bank[i]) / \
                         (1 - discount_rate * opt_pi_bank[i])

    nr_per = [0] * nr_of_periods
    for i in range(0, nr_of_periods):
        nr_per[i] = i

    # Create plots and pdf file. Save pdf file into graphics folder
    fig2, ax = pyplot.subplots()
    ax.plot(nr_per,
            flipud(opt_pi_bank),
            label='Bank',
            color='red',
            linestyle=':')
    ax.plot(nr_per,
            flipud(pi_state_thresh),
            label='State',
            color='blue',
            linestyle='--')
    ax.set_xlabel('Stage of the Game')
    ax.set_ylabel('Probability')
    ax.set_title('Optimal Probabilities')
    ax.legend(loc='upper left',
              frameon=0,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig2 = mpld3.fig_to_html(fig2)
    pyplot.savefig(
        'tbtf/modelfinitebounded/graphics/FB_Optimal_Probabilities.pdf',
        format='pdf')
    pyplot.close(fig2)

    fig3, bx = pyplot.subplots()
    bx.plot(nr_per, flipud(v_state_opt), label='Bank', color='red')
    bx.set_title('State Value')
    bx.legend(loc='lower left',
              frameon=0,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig3 = mpld3.fig_to_html(fig3)
    pyplot.savefig('tbtf/modelfinitebounded/graphics/FB_State_Value.pdf',
                   format='pdf')
    pyplot.close(fig3)

    return html_text_fig2, html_text_fig3, v_state_opt