#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Thu May 28 11:06:41 2020 @author: egorkozlov """ from estimates import get_point high_e = True x, targ_mode = get_point(high_e) from fit_plot import FitPlots what = 'no skills depreciation' fp = FitPlots( targ_mode=targ_mode, base='col {}.pkl'.format(what), compare='col baseline.pkl', base_name=what, compare_name='baseline', #graphs_title_add="Experiment: Removing Subsistence Constraint", moments_aux=None) #,moments_aux=moments_aux)
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
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
from calibration_params import calibration_params if __name__ == '__main__': #from numba import config #config.NUMBA_NUM_THREADS = 2 fix_values = False if fix_values: keys_fix = [ 'sigma_psi', 'sigma_psi_init', 'mu_psi_init', 'u_shift_mar', 'util_alp', 'util_kap' ] x_high, _ = get_point(True, read_wisdom=False) xfix = {k: x_high[k] for k in keys_fix} else: xfix = None lb, ub, xdef, keys, translator = calibration_params(xfix=xfix) print('calibration adjusts {}'.format(keys)) print('') print('') print('running tic tac...') print('') print('')
if system() != 'Darwin' and system() != 'Windows': import os os.environ['QT_QPA_PLATFORM'] = 'offscreen' from residuals import mdl_resid print('Hi!') import os os.environ['MKL_CBWR'] = 'AUTO' from estimates import get_point if __name__ == '__main__': x_base, tm = get_point(True, read_wisdom=False) # high education tar = target_values(tm) x_comp, _ = get_point(False, read_wisdom=False) # low education pmeets = ['pmeet_21', 'pmeet_30', 'pmeet_40'] ppregs = ['preg_21', 'preg_28', 'preg_35'] abortions = ['abortion_costs', 'p_abortion_access'] prefs = [ 'disutil_marry_sm_mal', 'disutil_shotgun', 'u_lost_divorce', 'util_qbar' ] # form xlist xlist = [x_base] he = ['high education'],
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()