Ejemplo n.º 1
0
def get_all_backup_round_profiler(force, strategies):

    backups = backup.get_data(force)

    backups = [b for b in backups if b.pvp]

    profiler = RoundProfiler(strategies=strategies, const=200, force=force)

    profiler_backup = BackupRoundProfiler(size_player=len(backups * 2),
                                          size_round=len(backups),
                                          strategies=strategies)

    tqdm.write("Profiling rounds...")

    with tqdm(total=len(backups * 2)) as pbar:

        i = 0

        for rd_idx, b in enumerate(backups):

            for player in (0, 1):

                # Register information
                profiler_backup.score[i] = np.sum(b.profits[:, player])
                profiler_backup.user_id[i] = b.user_id[player]
                profiler_backup.room_id[i] = b.room_id
                profiler_backup.firm_id[i] = player

                i += 1

                pbar.update()

            # Save round's radius
            profiler_backup.round_id[rd_idx] = b.round_id
            profiler_backup.r[rd_idx] = b.r
            profiler_backup.display_opponent_score[
                rd_idx] = b.display_opponent_score

            # Prepare means to compare
            means = {
                "price":
                np.mean(b.prices[:, :]),
                "score":
                np.mean(b.profits[:, :]),
                "distance":
                np.mean(
                    np.absolute(b.positions[:, 0] - b.positions[:, 1]) / 21)
            }

            # Compare means with each strategies means
            for k, v in strategies.items():
                profiler_backup.fit_scores[rd_idx,
                                           k] = profiler.score(r=b.r,
                                                               means=means,
                                                               strategy=v)

    backup.save(profiler_backup, "data/round_profiler_all.p")

    return profiler_backup
Ejemplo n.º 2
0
def get_fit(force):

    backups = backup.get_data(force)

    backups = [b for b in backups if b.pvp]

    m = {
        0.25: score.Score(r=0.25),
        0.50: score.Score(r=0.5)
    }

    fit_backup = BackupFit(size=len(backups*2))

    with tqdm(total=len(backups*2)) as pbar:

        i = 0

        for b in backups:

            for player in (0, 1):

                # Register information
                fit_backup.display_opponent_score[i] = b.display_opponent_score
                fit_backup.r[i] = b.r
                fit_backup.score[i] = np.sum(b.profits[:, player])
                fit_backup.user_id[i] = b.user_id[player]
                fit_backup.room_id[i] = b.room_id
                fit_backup.round_id[i] = b.round_id
                fit_backup.firm_id[i] = player

                # Compute score
                kwargs = {
                    "dm_model": m[b.r],
                    "firm_id": player,
                    "active_player_t0": b.active_player_t0,
                    "positions": b.positions,
                    "prices": b.prices,
                    "t_max": b.t_max
                }

                for str_method in score.Score.names:

                    rm = RunModel(**kwargs, str_method=str_method)
                    sc = rm.run()
                    fit_backup.fit_scores[str_method][i] = sc

                    tqdm.write("[id={}, r={:.2f}, s={}] [{}] score: {:.2f}".format(
                        i, b.r, int(b.display_opponent_score), str_method, sc))

                i += 1
                pbar.update(1)

                tqdm.write("\n")

    backup.save(fit_backup, "data/fit.p")

    return fit_backup
Ejemplo n.º 3
0
def main(force):

    backups = backup.get_data(force)

    # For naming
    str_os_cond = {True: "opp_score", False: "no_opp_score"}

    str_pvp_cond = {True: "PVP", False: "PVE"}

    # Separate: Plot figure for every round
    tqdm.write("Create a figure for every round...\n")
    for b in tqdm(backups):

        str_pvp = str_pvp_cond[b.pvp]
        str_os = str_os_cond[b.display_opponent_score]

        fig_name_args = "separate", str_os, str_pvp, b.room_id, b.round_id, str_os, str_pvp

        fig_name = "fig/{}/{}/{}/room{}_round{}_{}_{}_separate.pdf".format(
            *fig_name_args)
        separate.plot(backup=b, fig_name=fig_name)
Ejemplo n.º 4
0
def count(force=False):

    backups = backup.get_data(force)

    n = {0.25: {True: 0, False: 0}, 0.50: {True: 0, False: 0}}
    for b in backups:
        if b.pvp:
            n[b.r][b.display_opponent_score] += 1

    print(
        "Rooms 0.25 with opp score:    {}\n"
        "Rooms 0.25 without opp score: {}\n"
        "Rooms 0.50 with opp score:    {}\n"
        "Rooms 0.50 without opp score: {}\n"
        "N subjects:                   {}\n".format(
            n[0.25][True],
            n[0.25][False],
            n[0.50][True],
            n[0.50][False],
            (n[0.25][True] + n[0.25][False] + n[0.50][True] + n[0.50][False]) *
            2,
        ))
Ejemplo n.º 5
0
def get(force=False):

    backups = backup.get_data(force)

    backups = [b for b in backups if b.pvp]

    # ----------------- Data ------------------- #

    # Look at the parameters
    n_simulations = len(backups)
    n_positions = backups[0].n_positions

    # Containers
    dist = np.zeros(n_simulations)
    prices = np.zeros(n_simulations)
    scores = np.zeros(n_simulations)
    r = np.zeros(n_simulations)
    s = np.zeros(n_simulations, dtype=bool)

    for i, b in enumerate(backups):
        # Compute the mean distance between the two firms
        data = np.absolute(
            b.positions[:, 0] -
            b.positions[:, 1]) / n_positions

        dist[i] = np.mean(data)

        # Compute the mean price
        prices[i] = np.mean(b.prices[:, :])

        # Compute the mean profit
        scores[i] = np.mean(b.profits[:, :])

        r[i] = b.r
        s[i] = b.display_opponent_score

    return r, s, dist, prices, scores
Ejemplo n.º 6
0
def plot_violin_with_dashed_mean(force):

    backups = backup.get_data(force)

    # ----------------- Data ------------------- #

    # Look at the parameters
    n_simulations = len(backups)
    n_positions = backups[0].n_positions

    # Containers
    d = np.zeros(n_simulations)
    prices = np.zeros(n_simulations)
    scores = np.zeros(n_simulations)
    r = np.zeros(n_simulations)
    s = np.zeros(n_simulations, dtype=bool)

    for i, b in enumerate(backups):

        # Compute the mean distance between the two firms
        data = np.absolute(
            b.positions[:, 0] -
            b.positions[:, 1]) / n_positions

        d[i] = np.mean(data)

        # Compute the mean price
        prices[i] = np.mean(b.prices[:, :])

        # Compute the mean profit
        scores[i] = np.mean(b.profits[:, :])

        r[i] = b.r
        s[i] = b.display_opponent_score

    # ---------- Plot ----------------------------- #

    fig = plt.figure(figsize=(8, 4))

    sub_gs = matplotlib.gridspec.GridSpec(nrows=1, ncols=2)

    axes = (
        fig.add_subplot(sub_gs[0, 0]),
        fig.add_subplot(sub_gs[0, 1]),
    )

    s_values = (0, 1)
    r_values = (0.25, 0.5)

    arr = (scores, scores)

    mean_025_profit, mean_05_profit = _get_bot_mean_profits("profit", "profit")
    mean_025_comp, mean_05_comp = _get_bot_mean_profits("competition", "competition")
    arr_mean_profit = ((mean_025_profit, mean_05_profit), ) * 2
    arr_mean_comp = ((mean_025_comp, mean_05_comp), ) * 2
    xmins = (0.02, 0.6)
    xmaxs = (0.4, 0.98)

    plt.text(0.01, 140, "Display opponent score", fontsize=12)
    axes[0].set_title("\n\nFalse")
    axes[1].set_title("True")

    for idx in range(len(axes)):

        ax = axes[idx]

        ax.set_axisbelow(True)
        ax.set_ylim(0, 120)
        ax.set_ylabel("Score")
        ax.set_xticklabels(r_values)

        for i in range(2):
            ax.axhline(arr_mean_profit[idx][i], color='green', linewidth=1.2, linestyle="--",
                       label="Profit-based" if i == 0 else None,
                       zorder=1, xmin=xmins[i], xmax=xmaxs[i])

        for i in range(2):
            ax.axhline(arr_mean_comp[idx][i], color='red', linewidth=1.2, linestyle="--",
                       label="Competition-based" if i == 0 else None,
                       zorder=1, xmin=xmins[i], xmax=xmaxs[i])

        ax.legend()

        data = [arr[idx][(r == r_value) * (s == s_values[idx])] for r_value in (0.25, 0.50)]
        color = ['C0' if r_value == 0.25 else 'C1' for r_value in (0.25, 0.50)]

        customized_plot.violin(ax=ax, data=data, color=color, edgecolor=color, alpha=0.5)

    plt.tight_layout()
    plt.show()
Ejemplo n.º 7
0
def main(force):

    backups = backup.get_data(force)

    backups = [b for b in backups if b.pvp]

    # ----------------- Data ------------------- #

    # Look at the parameters
    n_simulations = len(backups)
    n_positions = backups[0].n_positions

    # Containers
    d = np.zeros(n_simulations)
    prices = np.zeros(n_simulations)
    scores = np.zeros(n_simulations)
    r = np.zeros(n_simulations)
    s = np.zeros(n_simulations, dtype=bool)

    for i, b in enumerate(backups):

        # Compute the mean distance between the two firms
        data = np.absolute(b.positions[:, 0] - b.positions[:, 1]) / n_positions

        d[i] = np.mean(data)

        # Compute the mean price
        prices[i] = np.mean(b.prices[:, :])

        # Compute the mean profit
        scores[i] = np.mean(b.profits[:, :])

        r[i] = b.r
        s[i] = b.display_opponent_score

    # ---------- Plot ----------------------------- #

    fig = plt.figure(figsize=(4, 7), dpi=200)

    sub_gs = matplotlib.gridspec.GridSpec(nrows=3, ncols=2)

    axes = (fig.add_subplot(sub_gs[0, 0]), fig.add_subplot(sub_gs[0, 1]),
            fig.add_subplot(sub_gs[1, 0]), fig.add_subplot(sub_gs[1, 1]),
            fig.add_subplot(sub_gs[2, 0]), fig.add_subplot(sub_gs[2, 1]))

    y_labels = "Distance", "Price", "Profit"
    y_limits = (0, 1), (1, 11), (0, 120)

    s_values = (
        0,
        1,
    ) * 3

    arr = (d, d, prices, prices, scores, scores)

    # axes[0].text(2, 1.3, "Display opponent score", fontsize=12)
    axes[0].set_title("$s = 0$")
    axes[1].set_title("$s = 1$")

    for idx in range(len(axes)):

        ax = axes[idx]

        ax.set_axisbelow(True)

        # Violin plot
        data = [
            arr[idx][(r == r_value) * (s == s_values[idx])]
            for r_value in (0.25, 0.50)
        ]
        color = ['C0' if r_value == 0.25 else 'C1' for r_value in (0.25, 0.50)]

        customized_plot.violin(ax=ax,
                               data=data,
                               color=color,
                               edgecolor="white",
                               alpha=0.8)  # color, alpha=0.5)

    for ax in axes[0:2]:
        ax.set_yticks(np.arange(0, 1.1, 0.25))

    for ax in axes[2:4]:
        ax.set_yticks(np.arange(1, 11.1, 2))

    for ax in axes[-2:]:
        ax.set_xticklabels(["{:.2f}".format(i) for i in (0.25, 0.50)])
        ax.set_xlabel("$r$")

    for ax in axes[:4]:
        ax.tick_params(length=0, axis="x")
        ax.set_xticklabels([])

    for ax, y_label, y_lim in zip(axes[0::2], y_labels, y_limits):
        ax.text(-0.35,
                0.5,
                y_label,
                rotation="vertical",
                verticalalignment='center',
                horizontalalignment='center',
                transform=ax.transAxes,
                fontsize=12)
        ax.set_ylabel(" ")
        ax.tick_params(axis="y", labelsize=9)
        ax.set_ylim(y_lim)

    for ax, y_lim in zip(axes[1::2], y_limits):
        ax.set_ylim(y_lim)
        ax.tick_params(length=0, axis="y")
        ax.set_yticklabels([])

    plt.tight_layout()

    plt.savefig("fig/main_exp.pdf")
    plt.show()

    # ----------- Stats ----------------- #

    to_compare = [{
        "measure":
        "distance",
        "constant":
        "s = 0",
        "var":
        "r",
        "data": [d[(r == r_value) * (s == 0)] for r_value in (0.25, 0.50)]
    }, {
        "measure":
        "distance",
        "constant":
        "s = 1",
        "var":
        "r",
        "data": [d[(r == r_value) * (s == 1)] for r_value in (0.25, 0.50)]
    }, {
        "measure":
        "price",
        "constant":
        "s = 0",
        "var":
        "r",
        "data":
        [prices[(r == r_value) * (s == 0)] for r_value in (0.25, 0.50)]
    }, {
        "measure":
        "price",
        "constant":
        "s = 1",
        "var":
        "r",
        "data":
        [prices[(r == r_value) * (s == 1)] for r_value in (0.25, 0.50)]
    }, {
        "measure":
        "profit",
        "constant":
        "s = 0",
        "var":
        "r",
        "data":
        [scores[(r == r_value) * (s == 0)] for r_value in (0.25, 0.50)]
    }, {
        "measure":
        "profit",
        "constant":
        "s = 1",
        "var":
        "r",
        "data":
        [scores[(r == r_value) * (s == 1)] for r_value in (0.25, 0.50)]
    }, {
        "measure":
        "distance",
        "constant":
        "r = 0.25",
        "var":
        "s",
        "data": [d[(r == 0.25) * (s == s_value)] for s_value in (0, 1)]
    }, {
        "measure":
        "distance",
        "constant":
        "r = 0.50",
        "var":
        "s",
        "data": [d[(r == 0.50) * (s == s_value)] for s_value in (0, 1)]
    }, {
        "measure":
        "price",
        "constant":
        "r = 0.25",
        "var":
        "s",
        "data": [prices[(r == 0.25) * (s == s_value)] for s_value in (0, 1)]
    }, {
        "measure":
        "price",
        "constant":
        "r = 0.50",
        "var":
        "s",
        "data": [prices[(r == 0.50) * (s == s_value)] for s_value in (0, 1)]
    }, {
        "measure":
        "profit",
        "constant":
        "r = 0.25",
        "var":
        "s",
        "data": [scores[(r == 0.25) * (s == s_value)] for s_value in (0, 1)]
    }, {
        "measure":
        "profit",
        "constant":
        "r = 0.50",
        "var":
        "s",
        "data": [scores[(r == 0.50) * (s == s_value)] for s_value in (0, 1)]
    }]

    ps = []
    us = []

    for dic in to_compare:
        u, p = scipy.stats.mannwhitneyu(dic["data"][0], dic["data"][1])
        ps.append(p)
        us.append(u)

    valid, p_corr, alpha_c_sidak, alpha_c_bonf = \
        statsmodels.stats.multitest.multipletests(pvals=ps, alpha=0.01, method="b")

    for p, u, p_c, v, dic in zip(ps, us, p_corr, valid, to_compare):
        print(
            "[Diff in {} when {} depending on {}-value] "
            "Mann-Whitney rank test: u {}, p {:.3f}, p corr {:.3f}, significant: {}"
            .format(dic["measure"], dic["constant"], dic["var"], u, p, p_c, v))
        print()

    table = \
        r"\begin{table}[htbp]" + "\n" + \
        r"\begin{center}" + "\n" + \
        r"\begin{tabular}{llllllll}" + "\n" + \
        r"Measure & Variable & Constant & $u$ & $p$ (before corr.) " \
        r"& $p$ (after corr.) & Sign. at 1\% threshold \\" + "\n" + \
        r"\hline \\" + "\n"

    for p, u, p_c, v, dic in zip(ps, us, p_corr, valid, to_compare):

        p = "{:.3f}".format(p) if p >= 0.001 else "$<$ 0.001"
        p_c = "{:.3f}".format(p_c) if p_c >= 0.001 else "$<$ 0.001"
        v = "yes" if v else "no"
        table += r"{} & ${}$ & ${}$ & {} & {} & {} & {} \\"\
            .format(dic["measure"], dic["var"], dic["constant"], u, p, p_c, v) \
                 + "\n"

    table += \
        r"\end{tabular}" + "\n" + \
        r"\end{center}" + "\n" + \
        r"\caption{Significance tests for comparison using Mann-Withney's u. " \
        r"Bonferroni corrections are applied.}" + "\n" + \
        r"\label{table:significance_tests}" + "\n" + \
        r"\end{table}"

    print("*** Latex-formated table ***")
    print(table)
Ejemplo n.º 8
0
def main(force):

    backups = backup.get_data(force)

    bins = np.arange(0, 3800, 500)

    bounds = ["{}~{}".format(i, j) for i, j in zip(bins[:-1], bins[1:])]

    fig = plt.figure(figsize=(12, 8))
    axes = fig.add_subplot(211), fig.add_subplot(212)

    for s, ax in zip((1, 0), axes):

        scores = {0.25: [], 0.5: []}

        for b in backups:

            if b.pvp and b.display_opponent_score is bool(s):
                for player in (0, 1):
                    sum_profit = np.sum(b.profits[:, player])
                    scores[b.r].append(sum_profit)

        if np.max([max(i) for i in scores.values()]) > max(bins):
            raise Exception("Max bound has been reached")

        y_upper_bound = 55

        ind = np.arange(len(bins) - 1)

        for r, color in zip((0.25, 0.50), ("C0", "C1")):

            sc = np.array(scores[r])

            print(
                "Score (r = {:.2f}, s = {}) mean: {:.2f}, std: {:.2f}, min:{}, max: {}"
                .format(r, s, np.mean(sc), np.std(sc), np.min(sc), np.max(sc)))

            d = np.digitize(sc, bins=bins)

            n = len(sc)

            y = []
            for i in ind:

                y.append(len(sc[d == i]) / n * 100)

            if np.max(y) > y_upper_bound:
                raise Exception(
                    "Max bound has been reached ({:.2f} > {})".format(
                        np.max(y), y_upper_bound))

            width = 0.35  # the width of the bars

            ax.bar(ind - width / 2 if r == 0.25 else ind + width / 2,
                   y,
                   width,
                   label='r = {:.2f}'.format(r),
                   alpha=0.5,
                   edgecolor=color)

        ax.set_xticks(ind)
        ax.set_xticklabels(bounds, fontsize=8)

        ax.set_ylim(0, y_upper_bound)

        ax.set_ylabel("Proportion (%)")

        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.spines['bottom'].set_visible(False)
        ax.tick_params(length=0)
        ax.set_title('s = {}'.format(s))

    plt.tight_layout()
    plt.legend()
    plt.savefig("fig/pool_score_distribution.pdf")
    plt.show()