Esempio n. 1
0
def alp_lik(alp_trans, cdat, dparams, r, lw, gn):
    '''objective function for minimizing alp'''

    #get w
    pre_w = cdat['exptot'] / float(1000)
    w = np.log(pre_w)

    #untransform alp
    alp = untrans(alp_trans)

    #update preference parameters
    cdat = calc_gv.get_pp(cdat, alp, r, lw, gn)

    #Update observation types
    cdat = new_ot.ot_step(cdat, dparams, alp, r, lw, gn)

    #update preference parameters
    cdat = calc_gv.get_pp(cdat, alp, r, lw, gn)

    #get likelihood
    lik = new_ot.olik(cdat, dparams, w, gn)
    lik_sum = -lik.sum()

    print(alp)
    print('{0:.6f}'.format(lik_sum))

    return lik_sum
Esempio n. 2
0
def est_loop(cdat, boot, runs, gn, prepend, dp_file, calc_t):
    '''main estimation loop'''

    cdat_orig = deepcopy(cdat) #for use with bootstrap
    for k in range(runs):
    
        #Get sample from cdat for bootstrap
        if boot:
            randlist = np.random.randint(0,len(cdat),len(cdat))
            cdat = deepcopy(cdat_orig.iloc[randlist].reset_index())

        #Timestamp
        timestamp = datetime.datetime\
                        .fromtimestamp(time.time())\
                        .strftime('%Y_%m_%d_%H_%M_%S')

        #Get parameters
        alp, r, lw = get_pars()
        dparams = pd.read_csv(dp_file)

        alp = 0.0329
        old_alp = 100

        while (old_alp - alp) ** 2 > 1e-12:

            # Print information
            old_alp = deepcopy(alp)
            print(alp)

            print('params')
            #Infer preference parameters
            cdat = calc_gv.get_pp(cdat, alp, r, lw, gn)

            print('prefs')
            #Calculate distribution parameters
            dparams = upd_pd.pref_dist(cdat, gn, dparams, calc_t)

            print('ob_types')
            #Update observation types
            cdat = new_ot.ot_step(cdat, dparams, alp, r, lw, gn)

            print('params')
            #Infer preference parameters
            cdat = calc_gv.get_pp(cdat, alp, r, lw, gn)

            print('prefs')
            #Calculate distribution parameters
            dparams = upd_pd.pref_dist(cdat, gn, dparams, calc_t)

            print('alp')
            #Update alpha and partial lik
            alp, lik = upd_alp.alp_step(cdat, alp, r, lw, dparams, gn)
            print(alp)
            print(old_alp)

        #get the correct folder
        if boot:
            folder = 'boot'
        else:
            folder = 'results'
        cdat.to_csv(prepend + folder + '/cdat' + timestamp + '.csv')
        pd.DataFrame(dparams).T.to_csv(prepend + folder + '/params' + timestamp + '.csv')
        f = open(prepend + folder + '/alp' + timestamp + '.csv', 'w')
        f.write('{:.6f}'.format(alp))
        f.close()