def beta_proposal(current, var): alpha_beta_fwd = jmutils.beta_shape(current, var) proposed = beta.rvs(*alpha_beta_fwd) fwd_prob = beta.pdf(proposed, *alpha_beta_fwd) alpha_beta_back = jmutils.beta_shape(proposed, var) back_prob = beta.pdf(current, *alpha_beta_back) log_back_fwd = math.log(back_prob / fwd_prob) return proposed, log_back_fwd
def make_noisy_probs(exact, noise_type, noise): """Noisify probabilities Args: exact - 2D np.array, rows are options, cols are ppl model_params: dict Outputs: 2d np.aray, rows are options, cols are ppl """ if noise_type == "noiseless": return exact elif noise_type == "binomial": num_hypothetical_trials = noise num_successes = nr.binomial(num_hypothetical_trials, exact[0]) noisy0 = num_successes / num_hypothetical_trials return np.array([noisy0, 1 - noisy0]) elif noise_type == "beta": alpha_beta = jmutils.beta_shape(exact[0], noise) noisy0 = nr.beta(*alpha_beta) return np.array([noisy0, 1 - noisy0]) elif noise_type == "truncnorm": scale = noise noisy0 = truncnorm.rvs(-exact[0] / scale, (1 - exact[0]) / scale, loc=exact[0], scale=scale) return np.array([noisy0, 1 - noisy0]) elif noise_type == "log_odds": lo = np.log(exact[0] / exact[1]) noisy_lo = nr.normal(lo, noise) given_1 = 1 / (math.exp(noisy_lo) + 1) return np.array([1 - given_1, given_1]) else: print("Error: meta noise_type not specified correctly")
def prob_noisy(given, bayes, noise_type, noise): """Prob of saying given when rational bayesian would say bayes Args: given: in [0,1], what respondent said bayes: in [0, 1] model_params: dict, incl keys with noise info Returns: prob in [0, 1] Different types of noise models: beta, binomial, noiseless. """ if noise_type == "noiseless": tolerance = 1e-04 if jmutils.fuzzy_equals(given, bayes, tolerance): return 1 else: return 0 elif noise_type == "binomial": num_trials = noise num_successes = int(given * num_trials) #return binom.pmf(num_successes, num_trials, bayes) return my_binom_pmf(num_successes, num_trials, bayes) elif noise_type == "beta": print("Optimize the beta stuff almost certainly!!!") alpha_beta = jmutils.beta_shape(bayes, noise) return beta.pdf(given, *alpha_beta) elif noise_type == "truncnorm": scale = noise #return truncnorm.pdf(given, -bayes / scale, (1 - bayes) / scale, # loc=bayes, scale=scale) return mytruncpdf_01bounds(given, bayes, noise) else: print("Error: meta noise_type not specified correctly") sys.exit()
def prob_noisy_vec(given, bayes, noise_type, noise): if noise_type == "truncnorm": return tn_pdf_01(given, bayes, noise) elif noise_type == "beta": if hasattr(noise, "__len__"): #return np.array([beta.pdf(g, *jmutils.beta_shape(bayes, n)) for g, n in zip(given, noise)]) return np.array([beta.pdf(g, *mode_beta(bayes, n)) for g, n in zip(given, noise)]) else: return beta.pdf(given, *jmutils.beta_shape(bayes, noise)) else: print("Unknown noise type") sys.exit("prob_noisy_vec: Unknown noise type")