# Plot preference and position effect estimation error rate of EI-TLR under Example 3
# Reproduce the left and middle panels of Figure 3 in the appendix

import os
import sys
import numpy as np
import matplotlib.pyplot as plt

sys.path.append('/Your Path/Data_Code/Methods')
from Pre_Functions import lin_reg
from math import log


# read data
# pre_est_err_mean: mean error for preference estimates
# pos_est_err_mean: mean error for position effect estimates
# range_epoch: the list of numbers of exploration epochs
os.chdir('/Your Path/Data_Code/Simulation/Results/ErrorRate')
pre_est_err_mean = np.load("Ex3_pre_est_err_mean_1.npy")
pos_est_err_mean = np.load("Ex3_pos_est_err_mean_1.npy")
range_epoch = np.load("Ex3_range_epoch_1.npy")


# plot log preference estimation errors v.s. log epoch numbers
log_pre_est_err = np.zeros(len(pre_est_err_mean))
log_range_epoch = np.zeros(len(range_epoch))

for i in range(len(range_epoch)):
    log_range_epoch[i] = log(range_epoch[i])
    log_pre_est_err[i] = log(pre_est_err_mean[i])

alpha,beta = lin_reg(log_range_epoch,log_pre_est_err)
print(alpha,beta)

plt.figure()
log_l = plt.scatter(log_range_epoch,log_pre_est_err,s=5,label='_nolegend_')
plt.axis([min(log_range_epoch),max(log_range_epoch),min(log_pre_est_err),max(log_pre_est_err)])
plt.plot([0,100],[alpha,alpha+100*beta])
plt.legend(labels=[str(round(beta,4))+'x+'+str(round(alpha,4))],loc='best',fontsize = 12)
plt.xlabel("Log Epoch Number",fontsize = 20)
plt.ylabel("Log Preference Estimation Error",fontsize = 20)
plt.title("Example 3",fontsize = 24)
plt.show()


# plot log position effect estimation errors v.s. log epoch numbers
log_pos_est_err = np.zeros(len(pos_est_err_mean))
log_range_epoch = np.zeros(len(range_epoch))

for i in range(len(range_epoch)):
    log_range_epoch[i] = log(range_epoch[i])
    log_pos_est_err[i] = log(pos_est_err_mean[i])

alpha,beta = lin_reg(log_range_epoch,log_pos_est_err)
print(alpha,beta)

plt.figure()
log_l = plt.scatter(log_range_epoch,log_pos_est_err,s=5,label='_nolegend_')
plt.axis([min(log_range_epoch),max(log_range_epoch),min(log_pos_est_err),max(log_pos_est_err)])
plt.plot([0,100],[alpha,alpha+100*beta])
plt.legend(labels=[str(round(beta,4))+'x+'+str(round(alpha,4))],loc='best',fontsize = 12)
plt.xlabel("Log Epoch Number",fontsize = 20)
plt.ylabel("Log Position Effect Estimation Error",fontsize = 20)
plt.title("Example 3",fontsize = 24)
plt.show()