0001 function [g,grad,hess,flag] = moment_function(xparams,sample_moments,dataset,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_
0036 persistent mainStream mainState
0037 persistent priorObjectiveValue
0038
0039 flag = 1;
0040 grad=[];
0041 hess=[];
0042
0043 if nargin<5
0044 if isempty(mainStream)
0045 mainStream = RandStream.getDefaultStream;
0046 mainState = mainStream.State;
0047 else
0048 mainStream.State = mainState;
0049 end
0050 end
0051
0052 penalty = 0;
0053 for i=1:options.estimated_parameters.nb
0054 if ~isnan(options.estimated_parameters.upper_bound(i)) && xparams(i)>options.estimated_parameters.upper_bound(i)
0055 penalty = penalty + (xparams(i)-options.estimated_parameters.upper_bound(i))^2;
0056 end
0057 if ~isnan(options.estimated_parameters.lower_bound(i)) && xparams(i)<options.estimated_parameters.lower_bound(i)
0058 penalty = penalty + (xparams(i)-options.estimated_parameters.lower_bound(i))^2;
0059 end
0060 end
0061
0062 if penalty>0
0063 flag = 0;
0064 return;
0065 end
0066
0067 save('estimated_parameters.mat','xparams');
0068
0069
0070 noprint = options_.noprint; options_.noprint = 1;
0071 [local_determinacy_and_stability,info] = check; options_.noprint = noprint;
0072 if ~local_determinacy_and_stability
0073 flag = 0;
0074 return
0075 end
0076
0077 simulated_moments = zeros(size(sample_moments));
0078
0079
0080 clear perfect_foresight_simulation;
0081
0082 if nargin<5
0083 for s = 1:options.number_of_simulated_sample
0084 time_series = extended_path([],options.simulated_sample_size,1);
0085 data = time_series(dataset.observed_variables_idx,options.burn_in_periods+1:options.simulated_sample_size);
0086 eval(['tmp = ' options.moments_file_name '(data);'])
0087 simulated_moments = simulated_moments + tmp;
0088 simulated_moments = simulated_moments / options.number_of_simulated_sample;
0089 end
0090 else
0091 if ~isunix
0092 error('The parallel version of SMM estimation is not implemented for non unix platforms!')
0093 end
0094 job_number = 1;
0095 [Junk,hostname] = unix('hostname --fqdn');
0096 hostname = deblank(hostname);
0097 for i=1:length(parallel)
0098 machine = deblank(parallel(i).machine);
0099 if ~strcmpi(hostname,machine)
0100
0101 unix(['scp estimated_parameters.mat ' , parallel(i).login , '@' , machine , ':' parallel(i).folder ' > /dev/null']);
0102 else
0103 if ~strcmpi(pwd,parallel(i).folder)
0104
0105 unix(['cp estimated_parameters.mat ' , parallel(i).folder]);
0106 end
0107 end
0108 for j=1:parallel(i).number_of_jobs
0109 if (strcmpi(hostname,machine) && j>1) || ~strcmpi(hostname,machine)
0110 job_number = job_number + 1;
0111 unix(['ssh -A ' parallel(i).login '@' machine ' ./call_matlab_session.sh job' int2str(job_number) '.m &']);
0112 end
0113 end
0114 end
0115
0116 tStartMasterJob = clock;
0117 eval('job1;')
0118 tElapsedMasterJob = etime(clock, tStartMasterJob);
0119 TimeLimit = tElapsedMasterJob*1.2;
0120
0121 tStart = clock;
0122 tElapsed = 0;
0123 while tElapsed<TimeLimit
0124 if ( length(dir('./intermediary_results_from_master_and_slaves/simulated_moments_slave_*.dat'))==job_number )
0125 break
0126 end
0127 tElapsed = etime(clock, tStart);
0128 end
0129 try
0130 tmp = zeros(length(sample_moments),1);
0131 for i=1:job_number
0132 simulated_moments = load(['./intermediary_results_from_master_and_slaves/simulated_moments_slave_' int2str(i) '.dat'],'-ascii');
0133 tmp = tmp + simulated_moments;
0134 end
0135 simulated_moments = tmp / job_number;
0136 catch
0137 flag = 0;
0138 return
0139 end
0140 end
0141
0142 g = simulated_moments-sample_moments;