def fit_one(pre=None, ctry=None): """ Similar to gen_params (earlier anyway). Fits for a single country. """ if pre is None: pre = load_pre(ctry) by_product = pre.dropna().groupby(level='PRODUCT_NC') res = {name: gen_params(group, x0=[2, 1]) for name, group in by_product} res = pd.DataFrame(res).T for_csv, for_hd5 = opt_dict_format(res, names=['t1', 't2']) return (for_csv, for_hd5)
continue #--------------------------------------------------------------------- # GMM Estimation. # Without Weighting # res = {name: gen_params(group, [2, 1], name, country=ctry, W=None) # for name, group in by_product} #--------------------------------------------------------------------- # With Weighting res = {name: gen_params( group, [2, 1], name, country=ctry, W=True, options={'disp': False}) for name, group in by_product} print('Finshed estimation for {}'.format(ctry)) #--------------------------------------------------------------------- # Formatting and IO. res = pd.DataFrame(res).T for_csv, for_hd5 = opt_dict_format(res, names=['t1', 't2']) try: gmm_results.append('res_' + ctry, for_hd5) except: with open(base + 'failed_h5.txt', 'a') as f: f.write("Missed on {}".format(ctry)) res.to_csv('/Volumes/HDD/Users/tom/DataStorage/Comext/' 'yearly/new_ctry_{}_weighted.csv'.format(ctry)) # try: # for_csv.to_csv('/Volumes/HDD/Users/tom/DataStorage/Comext/' # 'yearly/new_ctry_{}_weighted.csv'.format(ctry)) # except: # with open(base + 'failed_csv.txt', 'a') as f: # f.write("Missed on {}".format(ctry)) m = 'Finshed country {0} in {1}'.format(ctry, datetime.utcnow() - t)