def bel_ll_types(b0, b1, v0, world, sm, wp, noise_type, noise): bm = fwd.calc_belief_matrix(sm, wp) ll = 0 ll += np.log(prob_noisy(b0, bm[0, 0], noise_type, noise)) ll += np.log(prob_noisy(b1, bm[0, 1], noise_type, noise)) ll += np.log(my_binom_pmf(v0[0], v0[1], sm[0, world])) return ll
def beliefs_loglike_marg(responses, world, sm, wp, noise_type, noise): belief_matrix = fwd.calc_belief_matrix(sm, wp) num_ppl = responses.shape[1] probs = np.empty(num_ppl) prob_signals = sm[:, world] bayes_s0 = belief_matrix[0,:] for k in range(num_ppl): given_s0 = responses[0][k] prob_assuming_sigs = np.array([prob_noisy(given_s0, bs0, noise_type, noise[k]) for bs0 in bayes_s0]) probs[k] = np.sum(prob_signals * prob_assuming_sigs) loglike = np.sum(np.log(probs)) return loglike
def sm_expt(wp=np.array([.1, .9])): grid = 100 res = np.zeros((grid, grid)) for s in np.arange(grid): sm0 = s / grid for t in np.arange(0, s): sm1 = t / grid sm = np.array([[sm0, sm1], [1-sm0, 1-sm1]]) bel = fwd.calc_belief_matrix(sm, wp) if bel[0,0] > .5 and bel[0,1] < .5: res[s, t] = 1 plt.imshow(res, cmap=cm.Greys_r) 1/0
def try_proposal(): for k in range(1000): wp = fwd.sample_wp() sm = fwd.sample_sm_restriction() #print("wp: ", wp) #print("sm: ", sm) #print("beliefs: ", fwd.calc_belief_matrix(sm, wp)) for j in range(100): sm = prop_sm(sm, wp) #print("sm: ", sm) #print("beliefs: ", fwd.calc_belief_matrix(sm, wp)) beliefs = fwd.calc_belief_matrix(sm, wp) if beliefs[0][0] < .5 or beliefs[0][1] > .5: 1/0
def beliefs_loglike_binary(responses, signals, sm, wp, noise_type, noise): belief_matrix = fwd.calc_belief_matrix(sm, wp) probs = np.empty(len(signals)) for k in range(len(signals)): bayes_s0 = belief_matrix[0][signals[k]] given_s0 = responses[k] #probs[k] = prob_noisy(given_s0, bayes_s0, noise_type, noise[k]) probs0 = truncnorm.cdf(.5, -bayes_s0 / noise, (1-bayes_s0)/scale, loc=bayes_s0, scale=noise) #assuming truncnorm, write own func. if responses[k] == 0: probs[k] = probs0 else: probs[k] = 1 - probs0 loglike = np.sum(np.log(probs)) return loglike
def meta_loglike_marg(meta, world, sm, wp, noise, params, belief_matrix=None, meta_matrix=None): """Gives log prob of meta given responses, latent variables Args: meta - 2d np.ar, rows are options, cols ppl signals - np.arr, counting from 0 signals - np.arr sm - signal matrix; 2d np.arr, cols is world, rows are signals wp - np.arr giving prior on different world states noise_type - str giving noise model for meta beliefs noise - amt of meta noise (interpretation depends on noise_type) Returns: log prob """ if not params['use_meta']: return 0 if params['prior_wp_sm'] in ["none", "signal_assumption"]: if belief_matrix is None: belief_matrix = fwd.calc_belief_matrix(sm, wp) if meta_matrix is None: meta_matrix = fwd.calc_meta_matrix(sm, wp, belief_matrix) #see from belief matrix whether 0, 100 or meta_matrix. num_ppl = meta.shape[1] probs = np.empty(num_ppl) prob_sigs = sm[:, world] if params['prior_wp_sm'] in ["none", "signal_assumption"]: if belief_matrix[0, 1] > .5: bayes_s0_sig = np.ones(2) elif belief_matrix[0, 0] < .5: bayes_s0_sig = np.zeros(2) else: bayes_s0 = meta_matrix[0,:] else: bayes_s0_sig = meta_matrix[0,:] for k in range(num_ppl): given_s0 = meta[0][k] probs_assuming_sigs = np.array([prob_noisy(given_s0, bs0, params['noise_type'], noise[k]) for bs0 in bayes_s0_sigs]) probs[k] = np.sum(prob_sigs * probs_assuming_sigs) loglike = np.sum(np.log(probs)) return loglike
def wp_expt(sm=np.array([[.7, .4], [.3, .6]]), wp=np.array([.1, .9]), change_wp=True): params = setup_params.init_params_demo1() params['binary_beliefs'] = False indiv = fwd.generate_individual(params) grid = 100 diff = 0 res0 = np.zeros((grid-grid*diff, grid-grid*diff)) res1 = np.zeros((grid-grid*diff, grid-grid*diff)) bm0 = np.zeros((grid-grid*diff, grid-grid*diff)) bm1 = np.zeros((grid-grid*diff, grid-grid*diff)) mm0 = np.zeros((grid-grid*diff, grid-grid*diff)) mm1 = np.zeros((grid-grid*diff, grid-grid*diff)) mm0avg = np.zeros((grid-grid*diff, grid-grid*diff)) #hacky since grid needn't be nice num.: for k, gen in enumerate(np.arange(diff * grid, grid)): actual = {} gen_s0 = gen / grid + .01 if change_wp: gen_wp = np.array([gen_s0, 1 - gen_s0]) actual['wp'] = gen_wp actual['sm'] = sm else: actual['wp'] = wp gen_sm = np.array([[gen_s0, gen_s0 - diff], [1 - gen_s0, 1 - gen_s0 + diff]]) actual['sm'] = gen_sm actual['world'] = 0 actual['signals'] = fwd.sample_signals(actual['world'], actual['sm'], params['num_responses']) beliefs, meta = fwd.generate_data(actual, indiv, params) for j, ans in enumerate(np.arange(grid*diff, grid)): ans_s0 = ans / grid + .01 if change_wp: ans_wp = np.array([ans_s0, 1 - ans_s0]) ans_sm = actual['sm'] else: ans_wp = actual['wp'] ans_sm = np.array([[ans_s0, ans_s0 - diff], [1 - ans_s0, 1 - ans_s0 + diff]]) ll = np.array([aggregation.reports_ll_marg(beliefs, meta, w, ans_sm, ans_wp, indiv['own_noise'], indiv['meta_noise'], params) for w in [0, 1]]) ll += aggregation.log_prob_world(actual['world'], ans_wp) bel = fwd.calc_belief_matrix(ans_sm, ans_wp) bm0[k, j] = bel[0,0] bm1[k, j] = bel[0,1] met = fwd.calc_meta_matrix(ans_sm, ans_wp) mm0[k, j] = met[0,0] mm1[k, j] = met[0,1] if bel[0, 0] < .5: mm0avg[k,j] = 0 elif bel[0, 1] > .5: mm0avg[k,j] = 1 else: mm0avg[k,j] = met[0,0] if bel[0, 1] > .5: non_bij = 1 elif bel[0,0] < .5: non_bij = -1 else: non_bij = 0 res0[k, j] = ll[0] res1[k, j] = ll[1] plt.imshow(res0, cmap=cm.Greys_r) plt.imshow(res1, cmap=cm.Greys_r) 1/0 return res