1 function [hessian, Delta_hessian]=porsche_get_Hessian_J(model, data)
2 %
function [hessian, Delta_hessian]=porsche_get_Hessian_J(model, data)
4 % This
function calculates the Hessian (w.r.t. the mus given in the model)
5 % of the objective-
function via finite differences either in
6 % detailed or reduced mode, according to model.optimization.opt_mode.
7 % The corresponding data (model_data or reduced_data) must be passed to
12 % - data: model_data oder reduced_data
14 % needed fields in model:
15 % model.optimization.opt_mode =
'reduced' or
'detailed'
17 % Oliver Zeeb, 31.05.2011
20 disp(
'entered porsche_get_Jacobian')
24 %disp('in porsche_get_Hessian_J: Fehlerschätzer noch nicht implementiert!');
26 h_range = 1000; % for constructing the difference quotient: how small is the variaton of the parameter
27 %note: h_range = 1000 gives quite good results when detailed and reduced
28 %calculation are compared. If h_range is too hig (e.g. 10.000), the results
29 %in the reduced case become worse!
35 %new: with flag model.optimization.opt_mode
36 %calculate derivative via small differences
37 func_mu = model.optimization.objective_function(model,data); %value of the objective function for the original parameter mu, calculated some lines above
38 hessian=zeros(length(model.mu_names));
39 for i=1:length(model.mu_names)
40 if model.optimization.params_to_optimize(i) == 1
41 h_i=(model.mu_ranges{i}(2)-model.mu_ranges{i}(1))/h_range;
42 for j=i:length(model.mu_names)
43 h_j=(model.mu_ranges{j}(2)-model.mu_ranges{j}(1))/h_range;
44 if i==j %use other formula
for d_ii than
for d_ij
45 %
get the nedded values
for the
46 %hessian-finite-difference-formula
48 model.(model.mu_names{i}) = model.(model.mu_names{i}) + 2*h_i;
49 func_mu_p2hi = model.optimization.objective_function(model,data); %objective_function in mu + 2*h_i
52 model.(model.mu_names{i}) = model.(model.mu_names{i}) + h_i;
53 func_mu_phi = model.optimization.objective_function(model,data); %objective_function in mu + h_i
56 model.(model.mu_names{i}) = model.(model.mu_names{i}) - h_i;
57 func_mu_mhi = model.optimization.objective_function(model,data); %objective_function in mu - h_i
60 model.(model.mu_names{i}) = model.(model.mu_names{i}) - 2*h_i;
61 func_mu_m2hi = model.optimization.objective_function(model,data); %objective_function in mu - 2*h_i
64 partial_der = (- func_mu_p2hi + 16*func_mu_phi - 30*func_mu + 16*func_mu_mhi - func_mu_m2hi) / (12*h_i^2);
66 hessian(i,i) = partial_der;
69 disp(
'entered porsche_get_Jacobian')
71 disp(['values calculated: ' num2str(k) ' of ' num2str((length(model.mu_names)*(length(model.mu_names)+1))/2) ]);
74 else % i neq j --> calculate d_ij
75 %get the nedded values for the
76 %hessian-finite-difference-formula
78 model.(model.mu_names{i}) = model.(model.mu_names{i}) + h_i;
79 model.(model.mu_names{j}) = model.(model.mu_names{j}) + h_j;
80 func_mu_phi_phj = model.optimization.objective_function(model,data); %objective_function in mu + h_i + h_j
83 model.(model.mu_names{i}) = model.(model.mu_names{i}) + h_i;
84 model.(model.mu_names{j}) = model.(model.mu_names{j}) - h_j;
85 func_mu_phi_mhj = model.optimization.objective_function(model,data); %objective_function in mu + h_i - h_j
88 model.(model.mu_names{i}) = model.(model.mu_names{i}) - h_i;
89 model.(model.mu_names{j}) = model.(model.mu_names{j}) + h_j;
90 func_mu_mhi_phj = model.optimization.objective_function(model,data); %objective_function in mu - h_i + h_j
93 model.(model.mu_names{i}) = model.(model.mu_names{i}) - h_i;
94 model.(model.mu_names{j}) = model.(model.mu_names{j}) - h_j;
95 func_mu_mhi_mhj = model.optimization.objective_function(model,data); %objective_function in mu - h_i - h_j
97 model=model_original; %setting the mus in the model back to their original values
98 partial_der = (func_mu_phi_phj - func_mu_phi_mhj - func_mu_mhi_phj + func_mu_mhi_mhj)/(4*h_i*h_j); %calculating the partial derivative
100 hessian(i,j) = partial_der;
101 hessian(j,i) = partial_der; %symmetric Hessian --> H(i,j) = H(j,i)
104 disp('entered porsche_get_Jacobian')
106 disp(['values calculated: ' num2str(k) ' of ' num2str((length(model.mu_names)*(length(model.mu_names)+1))/2) ]);
109 end %for j=i:length(model.mu_names)
110 end %if model.optimization.params_to_optimize(i) == 1
111 end %for i=1:length(model.mu_names)
114 %old: without flag model.optimization.opt_mode
116 % if strcmp(inputname(2),'model_data')
117 % model_data = varargin{1};
118 % %calculate derivative via small differences
119 % func_mu = model.optimization.objective_function(model,model_data); %value of the objective
function for the original parameter mu, calculated some lines above
120 % hessian=zeros(length(model.mu_names)); %%%% ACHTUNG!!! Oli, 30.05.11
121 % disp(
'in porsche_get_hessian_J: aufpassen, falls nicht alle params_to_optimimze = 1');
122 %
for i=1:length(model.mu_names)
123 %
if model.optimization.params_to_optimize(i) == 1
124 % h_i=(model.mu_ranges{i}(2)-model.mu_ranges{i}(1))/h_range;
125 %
for j=i:length(model.mu_names)
126 % h_j=(model.mu_ranges{j}(2)-model.mu_ranges{j}(1))/h_range;
127 %
if i==j %use other formula
for d_ii than
for d_ij
128 % %
get the nedded values
for the
129 % %hessian-finite-difference-formula
130 % model=model_original;
131 % model.(model.mu_names{i}) = model.(model.mu_names{i}) + 2*h_i;
132 % func_mu_p2hi = model.optimization.objective_function(model,model_data); %objective_function in mu + 2*h_i
134 % model=model_original;
135 % model.(model.mu_names{i}) = model.(model.mu_names{i}) + h_i;
136 % func_mu_phi = model.optimization.objective_function(model,model_data); %objective_function in mu + h_i
138 % model=model_original;
139 % model.(model.mu_names{i}) = model.(model.mu_names{i}) - h_i;
140 % func_mu_mhi = model.optimization.objective_function(model,model_data); %objective_function in mu - h_i
142 % model=model_original;
143 % model.(model.mu_names{i}) = model.(model.mu_names{i}) - 2*h_i;
144 % func_mu_m2hi = model.optimization.objective_function(model,model_data); %objective_function in mu - 2*h_i
146 % model=model_original;
147 % partial_der = (- func_mu_p2hi + 16*func_mu_phi - 30*func_mu + 16*func_mu_mhi - func_mu_m2hi) / (12*h_i^2);
149 % hessian(i,i) = partial_der;
151 %
if(model.verbose>=8)
152 % disp(
'entered porsche_get_Jacobian')
154 % disp([
'values calculated: ' num2str(k)
' of ' num2str((length(model.mu_names)*(length(model.mu_names)+1))/2) ]);
157 %
else % i neq j --> calculate d_ij
158 % %
get the nedded values
for the
159 % %hessian-finite-difference-formula
160 % model=model_original;
161 % model.(model.mu_names{i}) = model.(model.mu_names{i}) + h_i;
162 % model.(model.mu_names{j}) = model.(model.mu_names{j}) + h_j;
163 % func_mu_phi_phj = model.optimization.objective_function(model,model_data); %objective_function in mu + h_i + h_j
165 % model=model_original;
166 % model.(model.mu_names{i}) = model.(model.mu_names{i}) + h_i;
167 % model.(model.mu_names{j}) = model.(model.mu_names{j}) - h_j;
168 % func_mu_phi_mhj = model.optimization.objective_function(model,model_data); %objective_function in mu + h_i - h_j
170 % model=model_original;
171 % model.(model.mu_names{i}) = model.(model.mu_names{i}) - h_i;
172 % model.(model.mu_names{j}) = model.(model.mu_names{j}) + h_j;
173 % func_mu_mhi_phj = model.optimization.objective_function(model,model_data); %objective_function in mu - h_i + h_j
175 % model=model_original;
176 % model.(model.mu_names{i}) = model.(model.mu_names{i}) - h_i;
177 % model.(model.mu_names{j}) = model.(model.mu_names{j}) - h_j;
178 % func_mu_mhi_mhj = model.optimization.objective_function(model,model_data); %objective_function in mu - h_i - h_j
180 % model=model_original; %setting the mus in the model back to their original values
181 % partial_der = (func_mu_phi_phj - func_mu_phi_mhj - func_mu_mhi_phj + func_mu_mhi_mhj)/(4*h_i*h_j); %calculating the partial derivative
183 % hessian(i,j) = partial_der;
184 % hessian(j,i) = partial_der; %symmetric Hessian --> H(i,j) = H(j,i)
186 %
if(model.verbose>=8)
187 % disp(
'entered porsche_get_Jacobian')
189 % disp([
'values calculated: ' num2str(k)
' of ' num2str((length(model.mu_names)*(length(model.mu_names)+1))/2) ]);
192 % end %
for j=i:length(model.mu_names)
193 % end %
if model.optimization.params_to_optimize(i) == 1
194 % end %
for i=1:length(model.mu_names)
198 % elseif strcmp(inputname(2),
'reduced_data')
199 % reduced_data = varargin{1};
200 % %calculate derivative via small differences
201 % func_mu = model.optimization.objective_function(model,reduced_data); %value of the objective
function for the original parameter mu, calculated some lines above
202 % hessian=zeros(length(model.mu_names)); %%%% ACHTUNG!!! Oli, 30.05.11
203 % disp(
'in porsche_get_hessian_J: aufpassen, falls nciht alle params_to_optimimze =1');
204 %
for i=1:length(model.mu_names)
205 %
if model.optimization.params_to_optimize(i) == 1
206 % h_i=(model.mu_ranges{i}(2)-model.mu_ranges{i}(1))/h_range;
207 %
for j=i:length(model.mu_names)
208 % h_j=(model.mu_ranges{j}(2)-model.mu_ranges{j}(1))/h_range;
209 %
if i==j %use other formula
for d_ii than
for d_ij
210 % %
get the nedded values
for the
211 % %hessian-finite-difference-formula
212 % model=model_original;
213 % model.(model.mu_names{i}) = model.(model.mu_names{i}) + 2*h_i;
214 % func_mu_p2hi = model.optimization.objective_function(model,reduced_data); %objective_function in mu + 2*h_i
216 % model=model_original;
217 % model.(model.mu_names{i}) = model.(model.mu_names{i}) + h_i;
218 % func_mu_phi = model.optimization.objective_function(model,reduced_data); %objective_function in mu + h_i
220 % model=model_original;
221 % model.(model.mu_names{i}) = model.(model.mu_names{i}) - h_i;
222 % func_mu_mhi = model.optimization.objective_function(model,reduced_data); %objective_function in mu - h_i
224 % model=model_original;
225 % model.(model.mu_names{i}) = model.(model.mu_names{i}) - 2*h_i;
226 % func_mu_m2hi = model.optimization.objective_function(model,reduced_data); %objective_function in mu - 2*h_i
228 % model=model_original;
229 % partial_der = (- func_mu_p2hi + 16*func_mu_phi - 30*func_mu + 16*func_mu_mhi - func_mu_m2hi) / (12*h_i^2);
231 % hessian(i,i) = partial_der;
234 %
else % i neq j --> calculate d_ij
235 % %
get the nedded values
for the
236 % %hessian-finite-difference-formula
237 % model=model_original;
238 % model.(model.mu_names{i}) = model.(model.mu_names{i}) + h_i;
239 % model.(model.mu_names{j}) = model.(model.mu_names{j}) + h_j;
240 % func_mu_phi_phj = model.optimization.objective_function(model,reduced_data); %objective_function in mu + h_i + h_j
242 % model=model_original;
243 % model.(model.mu_names{i}) = model.(model.mu_names{i}) + h_i;
244 % model.(model.mu_names{j}) = model.(model.mu_names{j}) - h_j;
245 % func_mu_phi_mhj = model.optimization.objective_function(model,reduced_data); %objective_function in mu + h_i - h_j
247 % model=model_original;
248 % model.(model.mu_names{i}) = model.(model.mu_names{i}) - h_i;
249 % model.(model.mu_names{j}) = model.(model.mu_names{j}) + h_j;
250 % func_mu_mhi_phj = model.optimization.objective_function(model,reduced_data); %objective_function in mu - h_i + h_j
252 % model=model_original;
253 % model.(model.mu_names{i}) = model.(model.mu_names{i}) - h_i;
254 % model.(model.mu_names{j}) = model.(model.mu_names{j}) - h_j;
255 % func_mu_mhi_mhj = model.optimization.objective_function(model,reduced_data); %objective_function in mu - h_i - h_j
257 % model=model_original; %setting the mus in the model back to their original values
258 % partial_der = (func_mu_phi_phj - func_mu_phi_mhj - func_mu_mhi_phj + func_mu_mhi_mhj)/(4*h_i*h_j); %calculating the partial derivative
260 % hessian(i,j) = partial_der;
261 % hessian(j,i) = partial_der; %symmetric Hessian --> H(i,j) = H(j,i)
263 % end %
for j=i:length(model.mu_names)
264 % end %
if model.optimization.params_to_optimize(i) == 1
265 % end %
for i=1:length(model.mu_names)
269 % warning(
'neither model_data nor reduced_data was given');
function r = verbose(level, message, messageId)
This function displays messages depending on a message-id and/or a level. Aditionally you can set/res...