simulation.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. import pandas as pd
  3. import pickle
  4. import numpy as np
  5. import argparse
  6. import matplotlib
  7. import matplotlib.pyplot as plt
  8. matplotlib.use("pgf")
  9. matplotlib.rcParams.update({
  10. "pgf.texsystem": "xelatex",
  11. 'font.family': 'serif',
  12. "font.serif" : "Times New Roman",
  13. 'text.usetex': True,
  14. 'pgf.rcfonts': False,
  15. })
  16. from sklearn.linear_model import LinearRegression
  17. def set_size(width, fraction=1, ratio = None):
  18. fig_width_pt = width * fraction
  19. inches_per_pt = 1 / 72.27
  20. if ratio is None:
  21. ratio = (5 ** 0.5 - 1) / 2
  22. fig_width_in = fig_width_pt * inches_per_pt
  23. fig_height_in = fig_width_in * ratio
  24. return fig_width_in, fig_height_in
  25. parser = argparse.ArgumentParser(description = 'plot_pred')
  26. parser.add_argument('data')
  27. parser.add_argument('fit')
  28. parser.add_argument('output')
  29. args = parser.parse_args()
  30. with open(args.data, 'rb') as fp:
  31. data = pickle.load(fp)
  32. fit = pd.read_parquet(args.fit)
  33. speakers = ['CHI', 'OCH', 'FEM', 'MAL']
  34. #n_simulations = fit['n_sim'].iloc[0]
  35. n_sim = data['n_sim']
  36. fit = fit[-1000:]
  37. sim = np.zeros(len(fit))
  38. true = np.zeros(len(fit))
  39. for i in range(len(fit)):
  40. f = fit.iloc[i]
  41. true_beta = f['chi_adu_coef']
  42. chi_truth = np.array([f[f'sim_truth.{k+1}.1'] for k in range(n_sim)])
  43. adu_truth = np.array([f[f'sim_truth.{k+1}.3']+f[f'sim_truth.{k+1}.4'] for k in range(n_sim)])
  44. chi_vtc = np.array([f[f'sim_vtc.{k+1}.1'] for k in range(n_sim)])
  45. adu_vtc = np.array([f[f'sim_vtc.{k+1}.3']+f[f'sim_vtc.{k+1}.4'] for k in range(n_sim)])
  46. regr = LinearRegression()
  47. regr.fit(adu_vtc.reshape(-1, 1), chi_vtc)
  48. sim[i] = regr.coef_[0]
  49. true[i] = true_beta
  50. fig, ax = plt.subplots(1,1,figsize=set_size(450,1,1))
  51. ax.scatter(true, sim, s = 1)
  52. ax.plot(np.linspace(0,5,4), np.linspace(0,5,4), color = 'black')
  53. ax.set_xlabel('true $K$')
  54. ax.set_ylabel('fit $\\hat{K}$')
  55. ax.set_xlim(0,1)
  56. ax.set_ylim(0,1)
  57. fig.savefig(args.output)