evaluate_prior.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/user/bin/env python
  2. # coding=utf-8
  3. """
  4. @author: yannansu
  5. @created at: 12.08.21 17:22
  6. Evaluate a prior model by simulating the bias and measuring the corresponding Likelihood or sum of squared errors.
  7. """
  8. import numpy as np
  9. from data_analysis.fit_curves import FitCumNormal
  10. from data_analysis.get_MAP import get_MAP
  11. import pandas as pd
  12. x_grid = np.load("data_analysis/model_estimates/x_grid.npy")
  13. def simul_estimate(lh_data, prior, lh_lik):
  14. """
  15. :param lh_data:
  16. :param prior:
  17. :param lh_lik:
  18. :return:
  19. """
  20. unique_pairs = lh_data[['standard_stim', 'test_stim', 'actual_intensity']]. \
  21. drop_duplicates().sort_values(by=['standard_stim', 'test_stim'])
  22. stim_l = unique_pairs['standard_stim'].values.astype('float')
  23. stim_h = unique_pairs['test_stim'].values.astype('float')
  24. likelihoods_l, likelihoods_h = lh_lik[0], lh_lik[1]
  25. # Simulate for each stimulus value
  26. _, MAP_x_l = get_MAP(prior, likelihoods_l, x_grid)
  27. _, MAP_x_h = get_MAP(prior, likelihoods_h, x_grid)
  28. simul_n = np.shape(MAP_x_l)[1]
  29. simul_res = unique_pairs.copy()
  30. simul_res['intens'] = stim_h - stim_l
  31. simul_res['resp'] = lh_data.groupby(['standard_stim', 'test_stim']) \
  32. ['resp_as_larger'].apply(lambda g: g.sum() / len(g)).values
  33. simul_res['simul_resp'] = np.sum((MAP_x_h > MAP_x_l), axis=1) / simul_n
  34. simul_fit = simul_res.groupby('standard_stim').apply(
  35. lambda x: FitCumNormal(x['intens'], x['simul_resp'], guess=[0, 5], expectedMin=0.0, lapse=0.0))
  36. return simul_res, simul_fit
  37. # def each_loglik(lh_data, simul_res, simul_fit):
  38. def each_loglik(lh_data, simul_fit):
  39. log_lik_list = []
  40. # for k, grp in simul_res.groupby('standard_stim'):
  41. for k in list(simul_fit.keys()):
  42. stim_data = lh_data.query("standard_stim == @k")
  43. true_resp = stim_data['resp_as_larger']
  44. pred_resp = simul_fit.loc[k].eval(stim_data['actual_intensity'])
  45. log_lik = np.sum(true_resp * np.log(pred_resp) + (1 - true_resp) * np.log(1 - pred_resp))
  46. log_lik_list.append(log_lik)
  47. return log_lik_list
  48. def each_sqrt_error(simul_res, simul_fit):
  49. sqrt_error_list = []
  50. for k, grp in simul_res.groupby('standard_stim'):
  51. true_resp = grp['resp']
  52. pred_resp = simul_fit.loc[k].eval(grp['intens'])
  53. sqrt_error = sum((pred_resp - true_resp) ** 2)
  54. sqrt_error_list.append(sqrt_error)
  55. return sqrt_error_list
  56. """
  57. # Example: evaluate a uniform prior
  58. def prior_uniform():
  59. y = 1./(len(x_grid))
  60. return np.repeat(y, len(x_grid))
  61. sub = 's5'
  62. sub_in_data = 's05'
  63. all_lh_data = pd.read_csv('data/all_sel_data.csv').query('condition == "LH"')
  64. all_lh_estimates = pd.read_csv('data_analysis/pf_estimates/all_estimates.csv').query('condition == "LH"')
  65. test_data = all_lh_data.query('subject == @sub_in_data')
  66. flat_prior = prior_uniform()
  67. sub_path = "data_analysis/model_estimates/s5/s5"
  68. lik_l = np.load(sub_path + "_likelihoods_l.npy")
  69. lik_h = np.load(sub_path + "_likelihoods_h.npy")
  70. simul_res_flat, simul_fit_flat = simul_estimate(test_data, flat_prior, [lik_l, lik_h])
  71. loglik = np.sum(each_loglik(simul_res_flat, simul_fit_flat)
  72. """