Beispiel #1
0
 def q(pt):
     #print('computing at point {}'.format(translator(pt)))
     try:
         ans = mdl_resid(tr(pt), return_format=['scaled residuals'])
     except BaseException as a:
         print('During optimization function evaluation failed at {}'.
               format(pt))
         print(a)
         ans = np.array([1e6])
     finally:
         gc.collect()
     return ans
Beispiel #2
0
            'pmeet_35': 0.40800438661571153,
            'preg_21': 0.07677173244887601,
            'preg_28': 0.06061469851763078,
            'preg_35': 0.01825557056243586,
            'u_shift_mar': 1.7329041070973545,
            'util_alp': 0.6182672481649074,
            'util_kap': 0.8081836080864513,
            'util_qbar': 0.5163163798943308,
            'disutil_marry_sm_mal_coef': 14.446603934890161,
            'disutil_shotgun_coef': 0.4904309002252879,
            'taste_shock_mult': 4.116448914683272,
            'high education': True,
            'n_psi': npsi
        }

        tar = target_values('high education')
        out, mdl, agents, res, mom = mdl_resid(
            x=x,
            targets=tar,
            return_format=[
                'distance', 'models', 'agents', 'scaled residuals', 'moments'
            ],
            #load_from='mdl.pkl',
            verbose=False,
            draw=True,
            cs_moments=False,
            moments_repeat=2)

        mdl[0].time_statistics()
        print('Done. Residual in point x0 is {}'.format(out))
        del ((out, mdl, agents, res, mom))
Beispiel #3
0
        xfix = None
        
    
        
    lb, ub, xdef, keys, translator = calibration_params(xfix=xfix)
    
    print('calibration adjusts {}'.format(keys))
    
    print('')
    print('')
    print('running tic tac...')
    print('')
    print('')
    
    
    
    #Tik Tak Optimization
    param=tiktak(xfix=xfix,N=20000,N_st=250,skip_local=False,skip_global=True,
                             resume_global=False,resume_local=False)
    
    print('f is {} and x is {}'.format(param[0],param[1]))
    
    #Now Re do the computation with graphs!
    out, mdl = mdl_resid(param[1],return_format=['distance','model'],
                         verbose=True,draw=True)
    
    
   
        

Beispiel #4
0
def run(resume=False, high_e=True):

    xinit, targ_mode = get_point(high_e, read_wisdom=False)
    tar = target_values(targ_mode)

    if resume:
        x_load = filer('wisdom_refined.pkl', 0, 0)
        prob_meet_load = x_load['pmeet_exo']
        prob_preg_load = x_load['ppreg_exo']
        xinit = x_load

    out, mdl, agents, res, mom = mdl_resid(x=xinit,
                                           targets=tar,
                                           return_format=[
                                               'distance', 'models', 'agents',
                                               'scaled residuals', 'moments'
                                           ],
                                           verbose=False)

    print('initial distance is {}'.format(out))

    if resume:
        prob_meet_init = prob_meet_load
        prob_preg_init = prob_preg_load
    else:
        prob_meet_init = np.array(
            mdl[0].setup.pars['pmeet_t'][:mdl[0].setup.pars['Tmeet']])
        prob_preg_init = np.array([
            mdl[0].setup.upp_precomputed_fem[t][3]
            for t in range(mdl[0].setup.pars['Tmeet'])
        ])

    nopt = 10
    yfactor = 1.5

    for iopt in range(nopt):

        print('running esimation round {}'.format(iopt))

        print('estimating probabilities:')

        prob_meet_est = 0.0
        prob_preg_est = 0.0
        nrep = 4 if iopt > 0 else 1
        np.random.seed(12)

        for rep in range(nrep):
            o = AgentsEst(mdl, T=30, verbose=False, fix_seed=False)

            prob_meet_est += (1 / nrep) * o.pmeet_exo.copy()
            prob_preg_est += (1 / nrep) * o.ppreg_exo.copy()

        print('estimated pmeet = {}'.format(prob_meet_est))
        print('estimated ppreg = {}'.format(prob_preg_est))

        # this does binary search

        w = 1.0

        factor = 0.5

        ne = prob_meet_est.size
        nw = 10
        print('reference value is {}'.format(out))

        y_previous = out

        for i in range(nw):
            prob_meet_w = w * prob_meet_est + (1 - w) * prob_meet_init[:ne]
            prob_preg_w = w * prob_preg_est + (1 - w) * prob_preg_init[:ne]

            xsearch = xinit.copy()
            xsearch.update({
                'pmeet_exo': prob_meet_w,
                'ppreg_exo': prob_preg_w
            })

            out_w = mdl_resid(x=xsearch,
                              targets=tar,
                              return_format=['distance'],
                              verbose=False)

            print('with weight = {}, distance is {}'.format(w, out_w))

            if out_w < yfactor * out:
                print('found a potentially imporving weight for yfactor = {}'.
                      format(yfactor))
                break
            else:
                w = factor * w
                if i < nw - 1:
                    print('trying new weight = {}'.format(w))
                else:
                    print('no luck...')

        xfix = {
            k: xinit[k]
            for k in [
                'pmeet_21', 'pmeet_30', 'pmeet_40', 'preg_21', 'preg_28',
                'preg_35'
            ]
        }

        lb, ub, _, keys, translator = calibration_params(xfix=xfix)

        def tr(x):
            xx = translator(x)
            xx.update({'pmeet_exo': prob_meet_w, 'ppreg_exo': prob_preg_w})
            return xx

        x0 = [xinit[key] for key in keys]

        x0, lb, ub = np.array(x0), np.array(lb), np.array(ub)

        print('starting from {}'.format(tr(x0)))

        tar = target_values('high education')

        def q(pt):
            #print('computing at point {}'.format(translator(pt)))
            try:
                ans = mdl_resid(tr(pt), return_format=['scaled residuals'])
            except BaseException as a:
                print('During optimization function evaluation failed at {}'.
                      format(pt))
                print(a)
                ans = np.array([1e6])
            finally:
                gc.collect()
            return ans

        res = dfols.solve(q,
                          x0,
                          rhobeg=0.02,
                          rhoend=1e-5,
                          maxfun=60,
                          bounds=(lb, ub),
                          scaling_within_bounds=True,
                          objfun_has_noise=False,
                          npt=len(x0) + 5,
                          user_params={
                              'restarts.use_restarts': True,
                              'restarts.rhoend_scale': 0.5,
                              'restarts.increase_npt': True
                          })

        print(res)
        print('Result is {}'.format(tr(res.x)))
        filer('wisdom_refined.pkl', tr(res.x), True)
        print('wrote to the file!')

        xinit = tr(res.x)
        out, mdl, agents, res, mom = mdl_resid(x=xinit,
                                               return_format=[
                                                   'distance', 'models',
                                                   'agents',
                                                   'scaled residuals',
                                                   'moments'
                                               ])
        if out > y_previous:
            print('no reduction in function value obtained')
            yfactor = 0.5 * yfactor + 0.5

        y_previous = out
Beispiel #5
0
def run(adj_name, fix, educ_name, resume=False, noplot=False):
    # at first this takes point x saved in estimates.py
    # by calling get_point(high_e). x is a dict with parameter names and values
    # then it applies adjustment fix (a dict) to the point x
    # resulting file name is educ_name + adj_name
    # adj_name -- name of adjustment
    # fix -- dictionary containing parameters of setup.py to change
    # educ_name -- name of education group ("col" or "hs")
    # high_e -- input to get_point function, True for college, False for HS

    high_e = (educ_name == 'col' or educ_name == 'college')
    low_e = (educ_name == 'hs' or educ_name == 'high school')

    assert (high_e or low_e), 'wrong specifier for education'

    x, targ_mode = get_point(high_e, read_wisdom=False)

    tar = target_values(targ_mode)

    print('\n\n\n\n\n\n')
    print('doing {} {}'.format(educ_name, adj_name))

    x_new = x.copy()

    fix = fix.copy()

    if 'multiply' in fix:
        mult = fix.pop('multiply')
        for m in mult:
            x_new[m] *= mult[m]

    x_new.update(fix)

    print(x_new)

    name = '{} {}'.format(educ_name, adj_name)
    fname = '{}.pkl'.format(name)

    try:

        if resume:
            try:
                mom = filer(fname, 0, 0, repeat=False)
                skip = True
            except:
                skip = False
        else:
            skip = False

        if not skip:
            print("computing {}".format(fname))
            out, mom = mdl_resid(x=x_new,
                                 targets=tar,
                                 return_format=['distance', 'moments'],
                                 verbose=False,
                                 draw=False,
                                 cs_moments=False,
                                 save_to='mdl for {}'.format(fname),
                                 moments_save_name=name,
                                 moments_repeat=5,
                                 Tsim=42)
            print("file {} saved".format(fname))
        else:
            print("file {} already exists".format(fname))

        print('done, doing fit plots')

        try:
            if adj_name == 'baseline':
                fp = FitPlots(targ_mode=targ_mode,
                              compare=None,
                              base='{} baseline.pkl'.format(educ_name),
                              compare_name='Data',
                              base_name='baseline',
                              moments_aux=None,
                              noplot=noplot)
            else:
                fp = FitPlots(targ_mode=targ_mode,
                              compare='{} baseline.pkl'.format(educ_name),
                              base=fname,
                              compare_name='baseline',
                              base_name=adj_name,
                              moments_aux=None,
                              noplot=noplot)
        except:
            print('something wrong with fit plots...')

    except KeyboardInterrupt:
        raise (KeyboardInterrupt)
    except BaseException as a:
        print("file {} {}.pkl could not be produced. Exception:".format(
            name, adj_name))
        print(a)
        print(' ')
    return mom, fp
Beispiel #6
0
    lb, ub, x0, keys, translator = calibration_params(
    )  # bounds are set in a separate file

    ##### FIRST LET'S TRY TO RUN THE FUNCTION IN FEW POINTS

    print('Testing the workers...')
    from p_client import compute_for_values
    pts = [lb + rs(lb.shape) * (ub - lb) for _ in range(1)]
    pts = [('compute', translator(x)) for x in pts]
    outs = compute_for_values(pts, timeout=72000.0)
    print('Everything worked, output is {}'.format(outs))

    print('')
    print('')
    print('running tic tac...')
    print('')
    print('')

    #Tik Tak Optimization
    param = tiktak(N=2000, N_st=50, skip_local=False, skip_global=False)

    print('f is {} and x is {}'.format(param[0], param[1]))

    #Now Re do the computation with graphs!
    out, mdl = mdl_resid(param[1],
                         return_format=['distance', 'model'],
                         calibration_report=False,
                         verbose=True,
                         draw=True)
Beispiel #7
0
                   ('no partners', {
                       'pmeet_21': 0.0,
                       'pmeet_28': 0.0,
                       'pmeet_35': 0.0
                   }), ('no home production', {
                       'util_kappa': 0.001
                   })]

    for adj_name, fix in adjustments:
        x_new = x.copy()
        x_new.update(fix)

        try:
            out, mom = mdl_resid(x=x_new,
                                 targets=tar,
                                 return_format=['distance', 'moments'],
                                 verbose=False,
                                 draw=False,
                                 cs_moments=False,
                                 moments_save_name='{} {}'.format(
                                     name, adj_name),
                                 moments_repeat=5)
            print("file {} {}.pkl saved".format(name, adj_name))
        except KeyboardInterrupt:
            raise (KeyboardInterrupt)
        except BaseException as a:
            print("file {} {}.pkl could not be produced. Exception:".format(
                name, adj_name))
            print(a)
            print(' ')
Beispiel #8
0
                      ['sigma_psi', 'sigma_psi_mult'],
                      ['u_shift_mar', 'util_kap', 'util_alp'],
                      [
                          'sigma_psi', 'sigma_psi_mult', 'u_shift_mar',
                          'util_kap', 'util_alp'
                      ]]
    for rf in replace_fields:
        xnew = x_base.copy()
        for f in rf:
            xnew[f] = x_comp[f]
        xlist.append(xnew)

    replace_fields = ['base'] + replace_fields + ['compare']
    xlist.append(x_comp)

    assert False
    agents_list = list()
    for x, rf in zip(xlist, replace_fields):
        print(rf)
        print(x)
        out, agents = mdl_resid(x=x,
                                return_format=['distance', 'agents'],
                                verbose=False,
                                draw=False)
        agents_list.append(agents)

        print('Done. Residual in point x0 is {}'.format(out))

    dill.dump((agents_list, replace_fields, xlist),
              open('comparison.pkl', 'wb+'))
Beispiel #9
0
                      ('trend and all probs', he + pmeets + ppregs),
                      ('trend and abortions', he + abortions),
                      ('all but trend', prefs + pmeets + ppregs + abortions)]
    for name, rf in replace_fields:
        xnew = x_base.copy()
        for f in rf:
            xnew[f] = x_comp[f]
        xlist.append(xnew)

    replace_fields = ['base'] + replace_fields + ['compare']
    xlist.append(x_comp)

    names = ['college'] + [f[0] for f in replace_fields] + ['high school']

    names = ['educ comparison: ' + n for n in names]

    for x, name in zip(xlist, names):
        print(name)
        print(x)
        out, mom = mdl_resid(x=x,
                             targets=tar,
                             return_format=['distance', 'moments'],
                             verbose=False,
                             draw=False,
                             cs_moments=False,
                             save_to='mdl for {}'.format(name),
                             moments_save_name=name,
                             moments_repeat=5)

        print('Done. Residual in point x0 is {}'.format(out))
Beispiel #10
0
def main(read_wisdom=False,erase=False):
    
    high_e = True
    
    if erase:
        try:
            os.remove('az_dist_fem.pkl')
            print('removed')
        except:
            pass

        try:
            os.remove('az_dist_mal.pkl')
            print('removed')
        except:
            pass
    
    x, targ_mode = get_point(high_e,read_wisdom=read_wisdom)
    
        
    
    tar = target_values(targ_mode)


    this_name = 'college baseline' if high_e else 'high school baseline'
    out, mdl, agents, res, mom = mdl_resid(x=x,targets=tar,
                                      return_format=['distance','models','agents','scaled residuals','moments'],
                                      #load_from='mdl.pkl',
                                      verbose=True,draw=True,cs_moments=False,
                                      moments_save_name = this_name,
                                      moments_repeat=1)


    #agents.sim_graphs()

    
    mdl[0].time_statistics()

    print('Done. Residual in point x0 is {}'.format(out))

    from fit_plot import FitPlots
    fp = FitPlots(targ_mode=targ_mode,
                   compare=None,
                   base=this_name+'.pkl',
                   compare_name='data',
                   base_name='college baseline',
                   moments_aux=None) #,moments_aux=moments_aux)
    
    '''
    from fit_plot import FitPlots
    fp = FitPlots(targ_mode=targ_mode,
                   compare='col baseline.pkl',
                   base='col double social stigma.pkl',
                   compare_name='baseline',
                   base_name='double stigma',
                   moments_aux=None) #,moments_aux=moments_aux)
    
    '''
    mdl[0].mar_graphs(t = 4)
    
    return locals()
Beispiel #11
0
        0.662168, 0.778121, 1.95622, 0.300373, 0.586473, 0.020041, -0.0578633,
        1.09095
    ])

    #Name and location of files
    if system() == 'Windows':
        path = 'D:/blasutto/store_model'
    else:
        path = None

    out, mdl, agents, res = mdl_resid(
        x0,
        return_format=['distance', 'models', 'agents', 'scaled residuals'],
        #load_from=['mdl_save_bil.pkl','mdl_save_uni.pkl'],
        solve_transition=True,
        save_to=['mdl_save_bil.pkl', 'mdl_save_uni.pkl'],
        store_path=path,
        verbose=True,
        calibration_report=False,
        draw=graphs,
        graphs=graphs,
        welf=False)  #Switch to true for decomposition of welfare analysis

    print('Done. Residual in point x0 is {}'.format(out))

    #assert False

    #Indexes for the graphs
    if graphs:
        ai = 3
        zfi = 3
        zmi = 4