def calculateKYratioDifference(sim_wealth, weights, total_output, target_KY): ''' Calculates the absolute distance between the simulated capital-to-output ratio and the true U.S. level. Parameters: ------------- sim_wealth : numpy.array Array with simulated wealth values. weights : numpy.array List of weights for each row of sim_wealth. total_output : float Denominator for the simulated K/Y ratio. target_KY : float Actual U.S. K/Y ratio to match. Returns: ------------ distance : float Absolute distance between simulated and actual K/Y ratios. ''' sim_K = weightedAverageSimData(sim_wealth, weights) sim_KY = sim_K / total_output distance = (sim_KY - target_KY)**1.0 return distance
def calculateKYratioDifference(sim_wealth,weights,total_output,target_KY): ''' Calculates the absolute distance between the simulated capital-to-output ratio and the true U.S. level. Parameters: ------------- sim_wealth : numpy.array Array with simulated wealth values. weights : numpy.array List of weights for each row of sim_wealth. total_output : float Denominator for the simulated K/Y ratio. target_KY : float Actual U.S. K/Y ratio to match. Returns: ------------ distance : float Absolute distance between simulated and actual K/Y ratios. ''' sim_K = weightedAverageSimData(sim_wealth,weights) sim_KY = sim_K/total_output distance = (sim_KY - target_KY)**1.0 return distance
def calcKappaMean(beta,nabla): ''' Calculates the average MPC for the given parameters. This is a very small sub-function of makeCSTWresults(). ''' beta_list = makeUniformDiscreteDistribution(beta,nabla,N=Params.pref_type_count) assignBetaDistribution(est_type_list,beta_list) multiThreadCommandsFake(est_type_list,results_commands) kappa_all = weightedAverageSimData(np.vstack((this_type.kappa_history for this_type in est_type_list)),np.tile(Params.age_weight_short/float(Params.pref_type_count),Params.pref_type_count)) return kappa_all
def calcKappaMean(beta, nabla): ''' Calculates the average MPC for the given parameters. This is a very small sub-function of makeCSTWresults(). ''' beta_list = makeUniformDiscreteDistribution(beta, nabla, N=Params.pref_type_count) assignBetaDistribution(est_type_list, beta_list) multiThreadCommandsFake(est_type_list, results_commands) kappa_all = weightedAverageSimData( np.vstack((this_type.kappa_history for this_type in est_type_list)), np.tile(Params.age_weight_short / float(Params.pref_type_count), Params.pref_type_count)) return kappa_all
def makeCSTWresults(beta, nabla, save_name=None): ''' Produces a variety of results for the cstwMPC paper (usually after estimating). ''' beta_list = makeUniformDiscreteDistribution(beta, nabla, N=Params.pref_type_count) assignBetaDistribution(est_type_list, beta_list) multiThreadCommandsFake(est_type_list, results_commands) if Params.do_lifecycle: # This can probably be removed sim_length = Params.total_T else: sim_length = Params.sim_periods sim_wealth = (np.vstack( (this_type.W_history for this_type in est_type_list))).flatten() sim_wealth_short = (np.vstack((this_type.W_history[0:sim_length] for this_type in est_type_list))).flatten() sim_kappa = (np.vstack( (this_type.kappa_history for this_type in est_type_list))).flatten() sim_income = (np.vstack((this_type.Y_history[0:sim_length] * np.asarray(this_type.temp_shocks[0:sim_length]) for this_type in est_type_list))).flatten() sim_ratio = (np.vstack( (this_type.W_history[0:sim_length] / this_type.Y_history[0:sim_length] for this_type in est_type_list))).flatten() if Params.do_lifecycle: sim_unemp = (np.vstack((np.vstack( (this_type.income_unemploy == np.asarray( this_type.temp_shocks[0:Params.working_T]), np.zeros((Params.retired_T, Params.sim_pop_size), dtype=bool))) for this_type in est_type_list))).flatten() sim_emp = (np.vstack((np.vstack( (this_type.income_unemploy != np.asarray( this_type.temp_shocks[0:Params.working_T]), np.zeros((Params.retired_T, Params.sim_pop_size), dtype=bool))) for this_type in est_type_list))).flatten() sim_ret = (np.vstack((np.vstack( (np.zeros((Params.working_T, Params.sim_pop_size), dtype=bool), np.ones((Params.retired_T, Params.sim_pop_size), dtype=bool))) for this_type in est_type_list))).flatten() else: sim_unemp = np.vstack((this_type.income_unemploy == np.asarray( this_type.temp_shocks[0:sim_length]) for this_type in est_type_list)).flatten() sim_emp = np.vstack((this_type.income_unemploy != np.asarray( this_type.temp_shocks[0:sim_length]) for this_type in est_type_list)).flatten() sim_ret = np.zeros(sim_emp.size, dtype=bool) sim_weight_all = np.tile( np.repeat(Params.age_weight_all, Params.sim_pop_size), Params.pref_type_count) sim_weight_short = np.tile( np.repeat(Params.age_weight_short, Params.sim_pop_size), Params.pref_type_count) if Params.do_beta_dist and Params.do_lifecycle: kappa_mean_by_age_type = (np.mean(np.vstack( (this_type.kappa_history for this_type in est_type_list)), axis=1)).reshape( (Params.pref_type_count * 3, DropoutType.T_total)) kappa_mean_by_age_pref = np.zeros( (Params.pref_type_count, DropoutType.T_total)) + np.nan for j in range(Params.pref_type_count): kappa_mean_by_age_pref[ j, ] = Params.d_pct * kappa_mean_by_age_type[ 3 * j + 0, ] + Params.h_pct * kappa_mean_by_age_type[ 3 * j + 1, ] + Params.c_pct * kappa_mean_by_age_type[ 3 * j + 2, ] kappa_mean_by_age = np.mean(kappa_mean_by_age_pref, axis=0) kappa_lo_beta_by_age = kappa_mean_by_age_pref[0, ] kappa_hi_beta_by_age = kappa_mean_by_age_pref[Params.pref_type_count - 1, ] lorenz_fig_data = makeLorenzFig(Params.SCF_wealth, Params.SCF_weights, sim_wealth, sim_weight_all) mpc_fig_data = makeMPCfig(sim_kappa, sim_weight_short) kappa_all = weightedAverageSimData( np.vstack((this_type.kappa_history for this_type in est_type_list)), np.tile(Params.age_weight_short / float(Params.pref_type_count), Params.pref_type_count)) kappa_unemp = np.sum( sim_kappa[sim_unemp] * sim_weight_short[sim_unemp]) / np.sum( sim_weight_short[sim_unemp]) kappa_emp = np.sum( sim_kappa[sim_emp] * sim_weight_short[sim_emp]) / np.sum( sim_weight_short[sim_emp]) kappa_ret = np.sum( sim_kappa[sim_ret] * sim_weight_short[sim_ret]) / np.sum( sim_weight_short[sim_ret]) my_cutoffs = [(0.99, 1), (0.9, 1), (0.8, 1), (0.6, 1), (0.5, 1), (0.4, 1), (0.0, 0.5)] kappa_by_ratio_groups = avgDataSlice(sim_kappa, sim_ratio, my_cutoffs, sim_weight_short) kappa_by_income_groups = avgDataSlice(sim_kappa, sim_income, my_cutoffs, sim_weight_short) quintile_points = extractPercentiles(sim_wealth_short, weights=sim_weight_short, percentiles=[0.2, 0.4, 0.6, 0.8]) wealth_quintiles = np.ones(sim_wealth_short.size, dtype=int) wealth_quintiles[sim_wealth_short > quintile_points[0]] = 2 wealth_quintiles[sim_wealth_short > quintile_points[1]] = 3 wealth_quintiles[sim_wealth_short > quintile_points[2]] = 4 wealth_quintiles[sim_wealth_short > quintile_points[3]] = 5 MPC_cutoff = extractPercentiles(sim_kappa, weights=sim_weight_short, percentiles=[2.0 / 3.0]) these_quintiles = wealth_quintiles[sim_kappa > MPC_cutoff] these_weights = sim_weight_short[sim_kappa > MPC_cutoff] hand_to_mouth_total = np.sum(these_weights) hand_to_mouth_pct = [] for q in range(5): hand_to_mouth_pct.append( np.sum(these_weights[these_quintiles == (q + 1)]) / hand_to_mouth_total) results_string = 'Estimate is beta=' + str(beta) + ', nabla=' + str( nabla) + '\n' results_string += 'Average MPC for all consumers is ' + mystr( kappa_all) + '\n' results_string += 'Average MPC in the top 1% of W/Y is ' + mystr( kappa_by_ratio_groups[0]) + '\n' results_string += 'Average MPC in the top 10% of W/Y is ' + mystr( kappa_by_ratio_groups[1]) + '\n' results_string += 'Average MPC in the top 20% of W/Y is ' + mystr( kappa_by_ratio_groups[2]) + '\n' results_string += 'Average MPC in the top 40% of W/Y is ' + mystr( kappa_by_ratio_groups[3]) + '\n' results_string += 'Average MPC in the top 50% of W/Y is ' + mystr( kappa_by_ratio_groups[4]) + '\n' results_string += 'Average MPC in the top 60% of W/Y is ' + mystr( kappa_by_ratio_groups[5]) + '\n' results_string += 'Average MPC in the bottom 50% of W/Y is ' + mystr( kappa_by_ratio_groups[6]) + '\n' results_string += 'Average MPC in the top 1% of y is ' + mystr( kappa_by_income_groups[0]) + '\n' results_string += 'Average MPC in the top 10% of y is ' + mystr( kappa_by_income_groups[1]) + '\n' results_string += 'Average MPC in the top 20% of y is ' + mystr( kappa_by_income_groups[2]) + '\n' results_string += 'Average MPC in the top 40% of y is ' + mystr( kappa_by_income_groups[3]) + '\n' results_string += 'Average MPC in the top 50% of y is ' + mystr( kappa_by_income_groups[4]) + '\n' results_string += 'Average MPC in the top 60% of y is ' + mystr( kappa_by_income_groups[5]) + '\n' results_string += 'Average MPC in the bottom 50% of y is ' + mystr( kappa_by_income_groups[6]) + '\n' results_string += 'Average MPC for the employed is ' + mystr( kappa_emp) + '\n' results_string += 'Average MPC for the unemployed is ' + mystr( kappa_unemp) + '\n' results_string += 'Average MPC for the retired is ' + mystr( kappa_ret) + '\n' results_string += 'Of the population with the 1/3 highest MPCs...' + '\n' results_string += mystr( hand_to_mouth_pct[0] * 100) + '% are in the bottom wealth quintile,' + '\n' results_string += mystr( hand_to_mouth_pct[1] * 100) + '% are in the second wealth quintile,' + '\n' results_string += mystr(hand_to_mouth_pct[2] * 100) + '% are in the third wealth quintile,' + '\n' results_string += mystr( hand_to_mouth_pct[3] * 100) + '% are in the fourth wealth quintile,' + '\n' results_string += 'and ' + mystr( hand_to_mouth_pct[4] * 100) + '% are in the top wealth quintile.' + '\n' print(results_string) if save_name is not None: with open('./Results/' + save_name + 'LorenzFig.txt', 'w') as f: my_writer = csv.writer( f, delimiter='\t', ) for j in range(len(lorenz_fig_data[0])): my_writer.writerow([ lorenz_fig_data[0][j], lorenz_fig_data[1][j], lorenz_fig_data[2][j] ]) f.close() with open('./Results/' + save_name + 'MPCfig.txt', 'w') as f: my_writer = csv.writer(f, delimiter='\t') for j in range(len(mpc_fig_data[0])): my_writer.writerow([lorenz_fig_data[0][j], mpc_fig_data[1][j]]) f.close() if Params.do_beta_dist and Params.do_lifecycle: with open('./Results/' + save_name + 'KappaByAge.txt', 'w') as f: my_writer = csv.writer(f, delimiter='\t') for j in range(len(kappa_mean_by_age)): my_writer.writerow([ kappa_mean_by_age[j], kappa_lo_beta_by_age[j], kappa_hi_beta_by_age[j] ]) f.close() with open('./Results/' + save_name + 'Results.txt', 'w') as f: f.write(results_string) f.close()
def makeCSTWresults(beta,nabla,save_name=None): ''' Produces a variety of results for the cstwMPC paper (usually after estimating). ''' beta_list = makeUniformDiscreteDistribution(beta,nabla,N=Params.pref_type_count) assignBetaDistribution(est_type_list,beta_list) multiThreadCommandsFake(est_type_list,results_commands) lorenz_distance = np.sqrt(betaDistObjective(nabla)) #lorenz_distance = 0.0 if Params.do_lifecycle: # This can probably be removed sim_length = Params.total_T else: sim_length = Params.sim_periods sim_wealth = (np.vstack((this_type.W_history for this_type in est_type_list))).flatten() sim_wealth_short = (np.vstack((this_type.W_history[0:sim_length] for this_type in est_type_list))).flatten() sim_kappa = (np.vstack((this_type.kappa_history for this_type in est_type_list))).flatten() sim_income = (np.vstack((this_type.Y_history[0:sim_length]*np.asarray(this_type.temp_shocks[0:sim_length]) for this_type in est_type_list))).flatten() sim_ratio = (np.vstack((this_type.W_history[0:sim_length]/this_type.Y_history[0:sim_length] for this_type in est_type_list))).flatten() if Params.do_lifecycle: sim_unemp = (np.vstack((np.vstack((this_type.income_unemploy == np.asarray(this_type.temp_shocks[0:Params.working_T]),np.zeros((Params.retired_T,Params.sim_pop_size),dtype=bool))) for this_type in est_type_list))).flatten() sim_emp = (np.vstack((np.vstack((this_type.income_unemploy != np.asarray(this_type.temp_shocks[0:Params.working_T]),np.zeros((Params.retired_T,Params.sim_pop_size),dtype=bool))) for this_type in est_type_list))).flatten() sim_ret = (np.vstack((np.vstack((np.zeros((Params.working_T,Params.sim_pop_size),dtype=bool),np.ones((Params.retired_T,Params.sim_pop_size),dtype=bool))) for this_type in est_type_list))).flatten() else: sim_unemp = np.vstack((this_type.income_unemploy == np.asarray(this_type.temp_shocks[0:sim_length]) for this_type in est_type_list)).flatten() sim_emp = np.vstack((this_type.income_unemploy != np.asarray(this_type.temp_shocks[0:sim_length]) for this_type in est_type_list)).flatten() sim_ret = np.zeros(sim_emp.size,dtype=bool) sim_weight_all = np.tile(np.repeat(Params.age_weight_all,Params.sim_pop_size),Params.pref_type_count) sim_weight_short = np.tile(np.repeat(Params.age_weight_short,Params.sim_pop_size),Params.pref_type_count) if Params.do_beta_dist and Params.do_lifecycle: kappa_mean_by_age_type = (np.mean(np.vstack((this_type.kappa_history for this_type in est_type_list)),axis=1)).reshape((Params.pref_type_count*3,DropoutType.T_total)) kappa_mean_by_age_pref = np.zeros((Params.pref_type_count,DropoutType.T_total)) + np.nan for j in range(Params.pref_type_count): kappa_mean_by_age_pref[j,] = Params.d_pct*kappa_mean_by_age_type[3*j+0,] + Params.h_pct*kappa_mean_by_age_type[3*j+1,] + Params.c_pct*kappa_mean_by_age_type[3*j+2,] kappa_mean_by_age = np.mean(kappa_mean_by_age_pref,axis=0) kappa_lo_beta_by_age = kappa_mean_by_age_pref[0,] kappa_hi_beta_by_age = kappa_mean_by_age_pref[Params.pref_type_count-1,] lorenz_fig_data = makeLorenzFig(Params.SCF_wealth,Params.SCF_weights,sim_wealth,sim_weight_all) mpc_fig_data = makeMPCfig(sim_kappa,sim_weight_short) kappa_all = weightedAverageSimData(np.vstack((this_type.kappa_history for this_type in est_type_list)),np.tile(Params.age_weight_short/float(Params.pref_type_count),Params.pref_type_count)) kappa_unemp = np.sum(sim_kappa[sim_unemp]*sim_weight_short[sim_unemp])/np.sum(sim_weight_short[sim_unemp]) kappa_emp = np.sum(sim_kappa[sim_emp]*sim_weight_short[sim_emp])/np.sum(sim_weight_short[sim_emp]) kappa_ret = np.sum(sim_kappa[sim_ret]*sim_weight_short[sim_ret])/np.sum(sim_weight_short[sim_ret]) my_cutoffs = [(0.99,1),(0.9,1),(0.8,1),(0.6,0.8),(0.4,0.6),(0.2,0.4),(0.0,0.2)] kappa_by_ratio_groups = avgDataSlice(sim_kappa,sim_ratio,my_cutoffs,sim_weight_short) kappa_by_income_groups = avgDataSlice(sim_kappa,sim_income,my_cutoffs,sim_weight_short) quintile_points = extractPercentiles(sim_wealth_short,weights=sim_weight_short,percentiles=[0.2, 0.4, 0.6, 0.8]) wealth_quintiles = np.ones(sim_wealth_short.size,dtype=int) wealth_quintiles[sim_wealth_short > quintile_points[0]] = 2 wealth_quintiles[sim_wealth_short > quintile_points[1]] = 3 wealth_quintiles[sim_wealth_short > quintile_points[2]] = 4 wealth_quintiles[sim_wealth_short > quintile_points[3]] = 5 MPC_cutoff = extractPercentiles(sim_kappa,weights=sim_weight_short,percentiles=[2.0/3.0]) these_quintiles = wealth_quintiles[sim_kappa > MPC_cutoff] these_weights = sim_weight_short[sim_kappa > MPC_cutoff] hand_to_mouth_total = np.sum(these_weights) hand_to_mouth_pct = [] for q in range(5): hand_to_mouth_pct.append(np.sum(these_weights[these_quintiles == (q+1)])/hand_to_mouth_total) results_string = 'Estimate is beta=' + str(beta) + ', nabla=' + str(nabla) + '\n' results_string += 'Lorenz distance is ' + str(lorenz_distance) + '\n' results_string += 'Average MPC for all consumers is ' + mystr(kappa_all) + '\n' results_string += 'Average MPC in the top percentile of W/Y is ' + mystr(kappa_by_ratio_groups[0]) + '\n' results_string += 'Average MPC in the top decile of W/Y is ' + mystr(kappa_by_ratio_groups[1]) + '\n' results_string += 'Average MPC in the top quintile of W/Y is ' + mystr(kappa_by_ratio_groups[2]) + '\n' results_string += 'Average MPC in the second quintile of W/Y is ' + mystr(kappa_by_ratio_groups[3]) + '\n' results_string += 'Average MPC in the middle quintile of W/Y is ' + mystr(kappa_by_ratio_groups[4]) + '\n' results_string += 'Average MPC in the fourth quintile of W/Y is ' + mystr(kappa_by_ratio_groups[5]) + '\n' results_string += 'Average MPC in the bottom quintile of W/Y is ' + mystr(kappa_by_ratio_groups[6]) + '\n' results_string += 'Average MPC in the top percentile of y is ' + mystr(kappa_by_income_groups[0]) + '\n' results_string += 'Average MPC in the top decile of y is ' + mystr(kappa_by_income_groups[1]) + '\n' results_string += 'Average MPC in the top quintile of y is ' + mystr(kappa_by_income_groups[2]) + '\n' results_string += 'Average MPC in the second quintile of y is ' + mystr(kappa_by_income_groups[3]) + '\n' results_string += 'Average MPC in the middle quintile of y is ' + mystr(kappa_by_income_groups[4]) + '\n' results_string += 'Average MPC in the fourth quintile of y is ' + mystr(kappa_by_income_groups[5]) + '\n' results_string += 'Average MPC in the bottom quintile of y is ' + mystr(kappa_by_income_groups[6]) + '\n' results_string += 'Average MPC for the employed is ' + mystr(kappa_emp) + '\n' results_string += 'Average MPC for the unemployed is ' + mystr(kappa_unemp) + '\n' results_string += 'Average MPC for the retired is ' + mystr(kappa_ret) + '\n' results_string += 'Of the population with the 1/3 highest MPCs...' + '\n' results_string += mystr(hand_to_mouth_pct[0]*100) + '% are in the bottom wealth quintile,' + '\n' results_string += mystr(hand_to_mouth_pct[1]*100) + '% are in the second wealth quintile,' + '\n' results_string += mystr(hand_to_mouth_pct[2]*100) + '% are in the third wealth quintile,' + '\n' results_string += mystr(hand_to_mouth_pct[3]*100) + '% are in the fourth wealth quintile,' + '\n' results_string += 'and ' + mystr(hand_to_mouth_pct[4]*100) + '% are in the top wealth quintile.' + '\n' print(results_string) if save_name is not None: with open('./Results/' + save_name + 'LorenzFig.txt','w') as f: my_writer = csv.writer(f, delimiter='\t',) for j in range(len(lorenz_fig_data[0])): my_writer.writerow([lorenz_fig_data[0][j], lorenz_fig_data[1][j], lorenz_fig_data[2][j]]) f.close() with open('./Results/' + save_name + 'MPCfig.txt','w') as f: my_writer = csv.writer(f, delimiter='\t') for j in range(len(mpc_fig_data[0])): my_writer.writerow([lorenz_fig_data[0][j], mpc_fig_data[1][j]]) f.close() if Params.do_beta_dist and Params.do_lifecycle: with open('./Results/' + save_name + 'KappaByAge.txt','w') as f: my_writer = csv.writer(f, delimiter='\t') for j in range(len(kappa_mean_by_age)): my_writer.writerow([kappa_mean_by_age[j], kappa_lo_beta_by_age[j], kappa_hi_beta_by_age[j]]) f.close() with open('./Results/' + save_name + 'Results.txt','w') as f: f.write(results_string) f.close()