<reponame>osagha/turktools<gh_stars>0
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sympy import Symbol, solve
from sympy.abc import a, b, c
from fit_beta import fit_beta_mean_uncertainty, kl_dirichlet

RESULTS_FILE = "../results/results_beta_exp.csv"
FILTER_BY = "range"
results = pd.read_csv(RESULTS_FILE, header=0)

# Filter results by range
if "range" in FILTER_BY:
    results = results[results.apply(lambda x: x["helpfulness_range"] < 0.75 and
                                              x["prior_range"] < 0.75 and
                                              x["posterior_range"] < 0.75, axis=1)]
if "upper_right" in FILTER_BY:
    results = results[results.apply(lambda x: x["prior_mturk_july"] > 0.5 and
                                              x["posterior_mturk_july"] > 0.5, axis=1)]



def map_certainty_to_variance(certainty):
    """
    Let's just fit a line with these endpoints:
    7 --> 0.001
    1 --> 0.07 (the max possible value, when \alpha=\beta=1, is 0.08133333)
    """
    return 0.0815 - (0.0115 * certainty)


def hyperbola_map(certainty, a, b, c):
    variance = a / (certainty - c) + b
    return variance

for x in np.arange(0.002, 0.07, 0.002):
    params = solve([a/(1-c)+b-0.07, a/(7-c)+b-0.001, a/(6-c)+b-x])[0]
    a_x, b_x, c_x = float(params[a]), float(params[b]), float(params[c])

    for field in ["prior", "posterior"]:
        results[field + f"_beta_{x}"] = results.apply(
            lambda z: fit_beta_mean_uncertainty(z[field + "_mturk_july"],
                                                hyperbola_map(z[field + "_certainty_july"], params[a], params[b], params[c])), axis=1)

    results[f"kl_beta_{x}"] = results.apply(lambda z: kl_dirichlet(z[f"posterior_beta_{x}"], z[f"prior_beta_{x}"]), axis=1)

    fig, axs = plt.subplots(1, 3, figsize=(11, 4))

    g = sns.scatterplot(data=results, x="prior_mturk_july", y="posterior_mturk_july", hue="kl_july_and_mturk_probs_exp_10", ax=axs.flat[0], alpha=0.7, s=80)
    g.legend_.set_title("KL")
    g.title.set_text(f"Prior & Posterior vs. KL (exp)")
    g.set_xlabel("Prior")
    g.set_ylabel("Posterior")

    g = sns.scatterplot(data=results, x="prior_mturk_july", y="posterior_mturk_july", hue="helpfulness_mean", ax=axs.flat[1], alpha=0.7, s=80)
    g.legend_.set_title("Helpfulness")
    g.title.set_text(f"Prior & Posterior vs. Helpfulness")
    g.set_xlabel("Prior")
    g.set_ylabel("")
    g.set_yticklabels([])

    g = sns.scatterplot(data=results, x="prior_mturk_july", y="posterior_mturk_july", hue=f"kl_beta_{x}", ax=axs.flat[2], alpha=0.7, s=80)
    g.legend_.set_title("Beta KL")
    g.title.set_text(f"Prior & Posterior vs. KL Beta (exp)")
    g.set_xlabel("Prior")
    g.set_ylabel("")
    g.set_yticklabels([])

    plt.subplots_adjust(left=0.05, right=0.95, wspace=0.1, )

    plt.savefig(f"../figures/beta/scatter/prior_posterior_vs_helpfulness_kl_klbeta_{x}.png")
    # fig.close()
# plt.savefig(f"../figures/beta/scatter/prior_posterior_vs_helpfulness_kl_klbeta_upper_right_unfiltered.png")


import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sympy import Symbol, solve
from sympy.abc import a, b, c
from fit_beta import fit_beta_mean_uncertainty, kl_dirichlet

RESULTS_FILE = "../results/results_beta_exp.csv"
FILTER_BY = "range"
results = pd.read_csv(RESULTS_FILE, header=0)

# Filter results by range
if "range" in FILTER_BY:
    results = results[results.apply(lambda x: x["helpfulness_range"] < 0.75 and
                                              x["prior_range"] < 0.75 and
                                              x["posterior_range"] < 0.75, axis=1)]
if "upper_right" in FILTER_BY:
    results = results[results.apply(lambda x: x["prior_mturk_july"] > 0.5 and
                                              x["posterior_mturk_july"] > 0.5, axis=1)]



def map_certainty_to_variance(certainty):
    """
    Let's just fit a line with these endpoints:
    7 --> 0.001
    1 --> 0.07 (the max possible value, when \alpha=\beta=1, is 0.08133333)
    """
    return 0.0815 - (0.0115 * certainty)


def hyperbola_map(certainty, a, b, c):
    variance = a / (certainty - c) + b
    return variance

for x in np.arange(0.002, 0.07, 0.002):
    params = solve([a/(1-c)+b-0.07, a/(7-c)+b-0.001, a/(6-c)+b-x])[0]
    a_x, b_x, c_x = float(params[a]), float(params[b]), float(params[c])

    for field in ["prior", "posterior"]:
        results[field + f"_beta_{x}"] = results.apply(
            lambda z: fit_beta_mean_uncertainty(z[field + "_mturk_july"],
                                                hyperbola_map(z[field + "_certainty_july"], params[a], params[b], params[c])), axis=1)

    results[f"kl_beta_{x}"] = results.apply(lambda z: kl_dirichlet(z[f"posterior_beta_{x}"], z[f"prior_beta_{x}"]), axis=1)

    fig, axs = plt.subplots(1, 3, figsize=(11, 4))

    g = sns.scatterplot(data=results, x="prior_mturk_july", y="posterior_mturk_july", hue="kl_july_and_mturk_probs_exp_10", ax=axs.flat[0], alpha=0.7, s=80)
    g.legend_.set_title("KL")
    g.title.set_text(f"Prior & Posterior vs. KL (exp)")
    g.set_xlabel("Prior")
    g.set_ylabel("Posterior")

    g = sns.scatterplot(data=results, x="prior_mturk_july", y="posterior_mturk_july", hue="helpfulness_mean", ax=axs.flat[1], alpha=0.7, s=80)
    g.legend_.set_title("Helpfulness")
    g.title.set_text(f"Prior & Posterior vs. Helpfulness")
    g.set_xlabel("Prior")
    g.set_ylabel("")
    g.set_yticklabels([])

    g = sns.scatterplot(data=results, x="prior_mturk_july", y="posterior_mturk_july", hue=f"kl_beta_{x}", ax=axs.flat[2], alpha=0.7, s=80)
    g.legend_.set_title("Beta KL")
    g.title.set_text(f"Prior & Posterior vs. KL Beta (exp)")
    g.set_xlabel("Prior")
    g.set_ylabel("")
    g.set_yticklabels([])

    plt.subplots_adjust(left=0.05, right=0.95, wspace=0.1, )

    plt.savefig(f"../figures/beta/scatter/prior_posterior_vs_helpfulness_kl_klbeta_{x}.png")
    # fig.close()
# plt.savefig(f"../figures/beta/scatter/prior_posterior_vs_helpfulness_kl_klbeta_upper_right_unfiltered.png")
# Filter results by range Let's just fit a line with these endpoints:     7 --> 0.001     1 --> 0.07 (the max possible value, when \alpha=\beta=1, is 0.08133333) # fig.close() # plt.savefig(f"../figures/beta/scatter/prior_posterior_vs_helpfulness_kl_klbeta_upper_right_unfiltered.png")