def calculateStandardErrorsByBootstrap(initial_estimate, N, seed=0, verbose=False): ''' Estimates the SolvingMicroDSOPs model N times, then calculates standard errors. ''' t_0 = time() # Generate a list of seeds for generating bootstrap samples RNG = np.random.RandomState(seed) seed_list = RNG.randint(2**31 - 1, size=N) # Estimate the model N times, recording each set of estimated parameters estimate_list = [] for n in range(N): t_start = time() # Bootstrap a new dataset by sampling bootstrap_data = (bootstrapSampleFromData(Data.scf_data_array, seed=seed_list[n])).T w_to_y_data_bootstrap = bootstrap_data[0, ] empirical_groups_bootstrap = bootstrap_data[1, ] empirical_weights_bootstrap = bootstrap_data[2, ] # Make a temporary function for use in this estimation run smmObjectiveFxnBootstrap = lambda parameters_to_estimate: smmObjectiveFxn( DiscFacAdj=parameters_to_estimate[0], CRRA=parameters_to_estimate[1], empirical_data=w_to_y_data_bootstrap, empirical_weights=empirical_weights_bootstrap, empirical_groups=empirical_groups_bootstrap) # Estimate the model with the bootstrap data and add to list of estimates this_estimate = minimizeNelderMead(smmObjectiveFxnBootstrap, initial_estimate) estimate_list.append(this_estimate) t_now = time() # Report progress of the bootstrap if verbose: print('Finished bootstrap estimation #' + str(n + 1) + ' of ' + str(N) + ' in ' + str(t_now - t_start) + ' seconds (' + str(t_now - t_0) + ' cumulative)') # Calculate the standard errors for each parameter estimate_array = (np.array(estimate_list)).T DiscFacAdj_std_error = np.std(estimate_array[0]) CRRA_std_error = np.std(estimate_array[1]) return [DiscFacAdj_std_error, CRRA_std_error]
def calculateStandardErrorsByBootstrap(initial_estimate,N,seed=0,verbose=False): ''' Estimates the SolvingMicroDSOPs model N times, then calculates standard errors. ''' t_0 = time() # Generate a list of seeds for generating bootstrap samples RNG = np.random.RandomState(seed) seed_list = RNG.randint(2**31-1,size=N) # Estimate the model N times, recording each set of estimated parameters estimate_list = [] for n in range(N): t_start = time() # Bootstrap a new dataset by sampling bootstrap_data = (bootstrapSampleFromData(Data.scf_data_array,seed=seed_list[n])).T w_to_y_data_bootstrap = bootstrap_data[0,] empirical_groups_bootstrap = bootstrap_data[1,] empirical_weights_bootstrap = bootstrap_data[2,] # Make a temporary function for use in this estimation run smmObjectiveFxnBootstrap = lambda parameters_to_estimate : smmObjectiveFxn(DiscFacAdj=parameters_to_estimate[0], CRRA=parameters_to_estimate[1], empirical_data = w_to_y_data_bootstrap, empirical_weights = empirical_weights_bootstrap, empirical_groups = empirical_groups_bootstrap) # Estimate the model with the bootstrap data and add to list of estimates this_estimate = minimizeNelderMead(smmObjectiveFxnBootstrap,initial_estimate) estimate_list.append(this_estimate) t_now = time() # Report progress of the bootstrap if verbose: print('Finished bootstrap estimation #' + str(n+1) + ' of ' + str(N) + ' in ' + str(t_now-t_start) + ' seconds (' + str(t_now-t_0) + ' cumulative)') # Calculate the standard errors for each parameter estimate_array = (np.array(estimate_list)).T DiscFacAdj_std_error = np.std(estimate_array[0]) CRRA_std_error = np.std(estimate_array[1]) return [DiscFacAdj_std_error, CRRA_std_error]
# Calculate Euclidean distance between simulated MPC averages and Table 9 targets diff = simulated_MPC_means - MPC_target if drop_corner: diff[0, 0] = 0.0 distance = np.sqrt(np.sum((diff)**2)) if verbose: print(simulated_MPC_means) else: print(center, spread, distance) return distance if __name__ == '__main__': guess = [0.92, 0.03] f_temp = lambda x: FagerengObjFunc(x[0], x[1]) opt_params = minimizeNelderMead(f_temp, guess, verbose=True) print('Finished estimating for scaling factor of ' + str(AdjFactor) + ' and "splurge amount" of $' + str(1000 * Splurge)) print('Optimal (beta,nabla) is ' + str(opt_params) + ', simulated MPCs are:') dist = FagerengObjFunc(opt_params[0], opt_params[1], True) print('Distance from Fagereng et al Table 9 is ' + str(dist)) # t_start = clock() # X = FagerengObjFunc(0.814,0.122) # t_end = clock() # print('That took ' + str(t_end - t_start) + ' seconds.') # print(X)
return [DiscFacAdj_std_error, CRRA_std_error] #================================================================= # Done defining objects and functions. Now run them (if desired). #================================================================= # Estimate the model using Nelder-Mead if estimate_model: initial_guess = [Params.DiscFacAdj_start, Params.CRRA_start] print( 'Now estimating the model using Nelder-Mead from an initial guess of' + str(initial_guess) + '...') model_estimate = minimizeNelderMead(smmObjectiveFxnReduced, initial_guess, verbose=True) print('Estimated values: DiscFacAdj=' + str(model_estimate[0]) + ', CRRA=' + str(model_estimate[1])) # Compute standard errors by bootstrap if compute_standard_errors: std_errors = calculateStandardErrorsByBootstrap(model_estimate, N=Params.bootstrap_size, seed=Params.seed, verbose=True) print('Standard errors: DiscFacAdj--> ' + str(std_errors[0]) + ', CRRA--> ' + str(std_errors[1])) # Make a contour plot of the objective function if make_contour_plot:
# plt.xlabel(Params.param_names[param_i+16]) # plt.ylabel('Sum of squared moment differences') # plt.show() if estimate_mortality: t0 = clock() MortalityLL(mort_test_params) t1 = clock() print('One mortality LL evaluation took ' + str(t1-t0) + ' seconds.') # Estimate some (or all) of the model parameters for the mortality MLE which_indices = np.array([0,1,2,3,4,5]) which_bool = np.zeros(6,dtype=bool) which_bool[which_indices] = True estimated_params = minimizeNelderMead(NegMortLL,mort_test_params,verbose=True,which_vars=which_bool) for i in which_indices.tolist(): print(Params.param_names[i+27] + ' = ' + str(estimated_params[i])) # Unpack the parameters Mortality0 = estimated_params[0] MortalitySex = estimated_params[1] MortalityAge = estimated_params[2] MortalityAgeSq = estimated_params[3] MortalityHealth = estimated_params[4] MortalityHealthSq = estimated_params[5] # Calculate the Z value of the mortality process Z = Mortality0 + Sex_mort*MortalitySex + Age_mort*MortalityAge + AgeSq_mort*MortalityAgeSq + HealthNow_mort*MortalityHealth + HealthSqNow_mort*MortalityHealthSq # Calculate the predicted probability of observed mortality event given the Z score
# Bootstrap a new dataset by resampling from the original data bootstrap_data = (bootstrapSampleFromData(Data.scf_data_array,seed=seed_list[n])).T w_to_y_data_bootstrap = bootstrap_data[0,] empirical_groups_bootstrap = bootstrap_data[1,] empirical_weights_bootstrap = bootstrap_data[2,] # Make a temporary function for use in this estimation run smmObjectiveFxnBootstrap = lambda parameters_to_estimate : smmObjectiveFxn(DiscFacAdj=parameters_to_estimate[0], CRRA=parameters_to_estimate[1], empirical_data = w_to_y_data_bootstrap, empirical_weights = empirical_weights_bootstrap, empirical_groups = empirical_groups_bootstrap) # Estimate the model with the bootstrap data and add to list of estimates this_estimate = minimizeNelderMead(smmObjectiveFxnBootstrap,initial_estimate) estimate_list.append(this_estimate) t_now = time() # Report progress of the bootstrap if verbose: print('Finished bootstrap estimation #' + str(n+1) + ' of ' + str(N) + ' in ' + str(t_now-t_start) + ' seconds (' + str(t_now-t_0) + ' cumulative)') # Calculate the standard errors for each parameter estimate_array = (np.array(estimate_list)).T DiscFacAdj_std_error = np.std(estimate_array[0]) CRRA_std_error = np.std(estimate_array[1]) return [DiscFacAdj_std_error, CRRA_std_error]
def calculateStandardErrorsByBootstrap(initial_estimate,N,seed=0,verbose=False): ''' Calculates standard errors by repeatedly re-estimating the model with datasets resampled from the actual data. Parameters ---------- initial_estimate : [float,float] The estimated [DiscFacAdj,CRRA], for use as an initial guess for each re-estimation in the bootstrap procedure. N : int Number of times to resample data and re-estimate the model. seed : int Seed for the random number generator. verbose : boolean Indicator for whether extra output should be printed for the user. Returns ------- standard_errors : [float,float] Standard errors calculated by bootstrap: [DiscFacAdj_std_error, CRRA_std_error]. ''' t_0 = time() # Generate a list of seeds for generating bootstrap samples RNG = np.random.RandomState(seed) seed_list = RNG.randint(2**31-1,size=N) # Estimate the model N times, recording each set of estimated parameters estimate_list = [] for n in range(N): t_start = time() # Bootstrap a new dataset by resampling from the original data bootstrap_data = (bootstrapSampleFromData(Data.scf_data_array,seed=seed_list[n])).T w_to_y_data_bootstrap = bootstrap_data[0,] empirical_groups_bootstrap = bootstrap_data[1,] empirical_weights_bootstrap = bootstrap_data[2,] # Make a temporary function for use in this estimation run smmObjectiveFxnBootstrap = lambda parameters_to_estimate : smmObjectiveFxn(DiscFacAdj=parameters_to_estimate[0], CRRA=parameters_to_estimate[1], empirical_data = w_to_y_data_bootstrap, empirical_weights = empirical_weights_bootstrap, empirical_groups = empirical_groups_bootstrap) # Estimate the model with the bootstrap data and add to list of estimates this_estimate = minimizeNelderMead(smmObjectiveFxnBootstrap,initial_estimate) estimate_list.append(this_estimate) t_now = time() # Report progress of the bootstrap if verbose: print('Finished bootstrap estimation #' + str(n+1) + ' of ' + str(N) + ' in ' + str(t_now-t_start) + ' seconds (' + str(t_now-t_0) + ' cumulative)') # Calculate the standard errors for each parameter estimate_array = (np.array(estimate_list)).T DiscFacAdj_std_error = np.std(estimate_array[0]) CRRA_std_error = np.std(estimate_array[1]) return [DiscFacAdj_std_error, CRRA_std_error]
def calculateStandardErrorsByBootstrap(initial_estimate, N, seed=0, verbose=False): ''' Calculates standard errors by repeatedly re-estimating the model with datasets resampled from the actual data. Parameters ---------- initial_estimate : [float,float] The estimated [DiscFacAdj,CRRA], for use as an initial guess for each re-estimation in the bootstrap procedure. N : int Number of times to resample data and re-estimate the model. seed : int Seed for the random number generator. verbose : boolean Indicator for whether extra output should be printed for the user. Returns ------- standard_errors : [float,float] Standard errors calculated by bootstrap: [DiscFacAdj_std_error, CRRA_std_error]. ''' t_0 = time() # Generate a list of seeds for generating bootstrap samples RNG = np.random.RandomState(seed) seed_list = RNG.randint(2**31 - 1, size=N) # Estimate the model N times, recording each set of estimated parameters estimate_list = [] for n in range(N): t_start = time() # Bootstrap a new dataset by resampling from the original data bootstrap_data = (bootstrapSampleFromData(Data.scf_data_array, seed=seed_list[n])).T w_to_y_data_bootstrap = bootstrap_data[0, ] empirical_groups_bootstrap = bootstrap_data[1, ] empirical_weights_bootstrap = bootstrap_data[2, ] # Make a temporary function for use in this estimation run smmObjectiveFxnBootstrap = lambda parameters_to_estimate: smmObjectiveFxn( DiscFacAdj=parameters_to_estimate[0], CRRA=parameters_to_estimate[1], empirical_data=w_to_y_data_bootstrap, empirical_weights=empirical_weights_bootstrap, empirical_groups=empirical_groups_bootstrap) # Estimate the model with the bootstrap data and add to list of estimates this_estimate = minimizeNelderMead(smmObjectiveFxnBootstrap, initial_estimate) estimate_list.append(this_estimate) t_now = time() # Report progress of the bootstrap if verbose: print('Finished bootstrap estimation #' + str(n + 1) + ' of ' + str(N) + ' in ' + str(t_now - t_start) + ' seconds (' + str(t_now - t_0) + ' cumulative)') # Calculate the standard errors for each parameter estimate_array = (np.array(estimate_list)).T DiscFacAdj_std_error = np.std(estimate_array[0]) CRRA_std_error = np.std(estimate_array[1]) return [DiscFacAdj_std_error, CRRA_std_error]