コード例 #1
0
ファイル: testing.py プロジェクト: jpmccoy/agg-as-inference
def geom_grid(beliefs, meta, params, wp_grid, sm_grid):
    indiv = fwd.generate_individual(params)
    res = [ np.zeros((2, wp_grid-1, sm_grid, sm_grid)) for k in range(1)]
    #bij = np.zeros((10, grid, grid))
    no_meta = params.copy()
    no_meta['use_meta'] = False
    params_lo = params.copy()
    params_lo['own_noise_type'] = "log_odds"
    params_lo['meta_noise_type'] = "log_odds"
    params_lo['own_noise_default'] = 1
    params_lo['meta_noise_default'] = 1
    indiv_lo = fwd.generate_individual(params_lo)
    lo_no_meta = params_lo.copy()
    lo_no_meta['use_meta'] = False
    #params_lst = [params, no_meta, params_lo, lo_no_meta]
    #indiv_lst = [indiv, indiv, indiv_lo, indiv_lo]
    params_lst = [params]
    indiv_lst = [indiv]
    print("Log prob world commented out - do you want this??")
    print(datetime.datetime.now())
    for z in np.arange(wp_grid-1):
        print(z)
        gen_wp = z/wp_grid + 1/wp_grid
        wp = np.array([gen_wp, 1 - gen_wp])
        for gen0 in np.arange(sm_grid-1):
            gen_s0 = gen0 / sm_grid + (2 / sm_grid)
            for gen1 in np.arange(0, gen0+1):
                gen_s1 = gen1 / sm_grid + (1/sm_grid)
                sm = np.array([[gen_s0, gen_s1], [1 - gen_s0, 1 - gen_s1]])
                #bij[l, k, j] = fwd.bijection(sm, wp)[0]
                for w in range(2):
                    #lpw = aggregation.log_prob_world(w, wp)
                    lpw = 0
                    for rr, par_indiv in enumerate(zip(params_lst, indiv_lst)):
                        rll = aggregation.reports_ll_marg(beliefs, meta, w, sm,
                                                          wp, par_indiv[1]['own_noise'],
                                                          par_indiv[1]['meta_noise'],
                                                          par_indiv[0])
                        res[rr][w,z,gen0,gen1] = lpw + rll
    res = [np.where(r == 0, np.nan, r) for r in res]
    return res
コード例 #2
0
ファイル: overall.py プロジェクト: jpmccoy/agg-as-inference
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