0001 function [r,flag] = smm_objective(xparams,sample_moments,weighting_matrix,options,parallel)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 global M_ options_ oo_
0036 persistent mainStream mainState
0037 persistent priorObjectiveValue
0038
0039 flag = 1;
0040
0041 if nargin<5
0042 if isempty(mainStream)
0043 if matlab_ver_less_than('7.12')
0044 mainStream = RandStream.getDefaultStream;
0045 else
0046 mainStream = RandStream.getGlobalStream;
0047 end
0048 mainState = mainStream.State;
0049 else
0050 mainStream.State = mainState;
0051 end
0052 end
0053
0054 if isempty(priorObjectiveValue)
0055 priorObjectiveValue = Inf;
0056 end
0057
0058 penalty = 0;
0059 for i=1:options.estimated_parameters.nb
0060 if ~isnan(options.estimated_parameters.upper_bound(i)) && xparams(i)>options.estimated_parameters.upper_bound(i)
0061 penalty = penalty + (xparams(i)-options.estimated_parameters.upper_bound(i))^2;
0062 end
0063 if ~isnan(options.estimated_parameters.lower_bound(i)) && xparams(i)<options.estimated_parameters.lower_bound(i)
0064 penalty = penalty + (xparams(i)-options.estimated_parameters.lower_bound(i))^2;
0065 end
0066 end
0067
0068 if penalty>0
0069 flag = 0;
0070 r = priorObjectiveValue + penalty;
0071 return;
0072 end
0073
0074 save('estimated_parameters.mat','xparams');
0075
0076
0077 noprint = options_.noprint; options_.noprint = 1;
0078 [local_determinacy_and_stability,info] = check(M_,options_,oo_); options_.noprint = noprint;
0079 if ~local_determinacy_and_stability
0080 r = priorObjectiveValue * (1+info(2));
0081 flag = 0;
0082 return
0083 end
0084
0085 simulated_moments = zeros(size(sample_moments));
0086
0087
0088 clear perfect_foresight_simulation;
0089
0090 if nargin<5
0091 for s = 1:options.number_of_simulated_sample
0092 time_series = extended_path([],options.simulated_sample_size,1);
0093 data = time_series(options.observed_variables_idx,options.burn_in_periods+1:options.simulated_sample_size);
0094 eval(['tmp = ' options.moments_file_name '(data);'])
0095 simulated_moments = simulated_moments + tmp;
0096 simulated_moments = simulated_moments / options.number_of_simulated_sample;
0097 end
0098 else
0099 if ~isunix
0100 error('The parallel version of SMM estimation is not implemented for non unix platforms!')
0101 end
0102 job_number = 1;
0103 [Junk,hostname] = unix('hostname --fqdn');
0104 hostname = deblank(hostname);
0105 for i=1:length(parallel)
0106 machine = deblank(parallel(i).machine);
0107 if ~strcmpi(hostname,machine)
0108
0109 unix(['scp estimated_parameters.mat ' , parallel(i).login , '@' , machine , ':' parallel(i).folder ' > /dev/null']);
0110 else
0111 if ~strcmpi(pwd,parallel(i).folder)
0112
0113 unix(['cp estimated_parameters.mat ' , parallel(i).folder]);
0114 end
0115 end
0116 for j=1:parallel(i).number_of_jobs
0117 if (strcmpi(hostname,machine) && j>1) || ~strcmpi(hostname,machine)
0118 job_number = job_number + 1;
0119 unix(['ssh -A ' parallel(i).login '@' machine ' ./call_matlab_session.sh job' int2str(job_number) '.m &']);
0120 end
0121 end
0122 end
0123
0124 tStartMasterJob = clock;
0125 eval('job1;')
0126 tElapsedMasterJob = etime(clock, tStartMasterJob);
0127 TimeLimit = tElapsedMasterJob*1.2;
0128
0129 tStart = clock;
0130 tElapsed = 0;
0131 while tElapsed<TimeLimit
0132 if ( length(dir('./intermediary_results_from_master_and_slaves/simulated_moments_slave_*.dat'))==job_number )
0133 break
0134 end
0135 tElapsed = etime(clock, tStart);
0136 end
0137 try
0138 tmp = zeros(length(sample_moments),1);
0139 for i=1:job_number
0140 simulated_moments = load(['./intermediary_results_from_master_and_slaves/simulated_moments_slave_' int2str(i) '.dat'],'-ascii');
0141 tmp = tmp + simulated_moments;
0142 end
0143 simulated_moments = tmp / job_number;
0144 catch
0145 r = priorObjectiveValue*1.1;
0146 flag = 0;
0147 return
0148 end
0149 end
0150
0151 r = transpose(simulated_moments-sample_moments)*weighting_matrix*(simulated_moments-sample_moments);
0152 priorObjectiveValue = r;
0153
0154 if (options.optimization_routine>0) && exist('optimization_path.mat')
0155 load('optimization_path.mat');
0156 new_state = [ r; xparams];
0157 estimated_parameters_optimization_path = [ estimated_parameters_optimization_path , new_state ];
0158 save('optimization_path.mat','estimated_parameters_optimization_path');
0159 end