rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
porsche_get_Hessian_J.m
1 function [hessian, Delta_hessian]=porsche_get_Hessian_J(model, data)
2 %function [hessian, Delta_hessian]=porsche_get_Hessian_J(model, data)
3 %
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
8 % this function.
9 %
10 % input:
11 % - model
12 % - data: model_data oder reduced_data
13 %
14 % needed fields in model:
15 % model.optimization.opt_mode = 'reduced' or 'detailed'
16 %
17 % Oliver Zeeb, 31.05.2011
18 
19 if(model.verbose>=8)
20  disp('entered porsche_get_Jacobian')
21 end
22 
23 Delta_hessian = 0;
24 %disp('in porsche_get_Hessian_J: Fehlerschätzer noch nicht implementiert!');
25 
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!
30 
31 
32 model_original=model;
33 k=0;
34 
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
47  model=model_original;
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
50 
51  model=model_original;
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
54 
55  model=model_original;
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
58 
59  model=model_original;
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
62 
63  model=model_original;
64  partial_der = (- func_mu_p2hi + 16*func_mu_phi - 30*func_mu + 16*func_mu_mhi - func_mu_m2hi) / (12*h_i^2);
65 
66  hessian(i,i) = partial_der;
67 
68  if(model.verbose>=8)
69  disp('entered porsche_get_Jacobian')
70  k=k+1;
71  disp(['values calculated: ' num2str(k) ' of ' num2str((length(model.mu_names)*(length(model.mu_names)+1))/2) ]);
72  end
73 
74  else % i neq j --> calculate d_ij
75  %get the nedded values for the
76  %hessian-finite-difference-formula
77  model=model_original;
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
81 
82  model=model_original;
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
86 
87  model=model_original;
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
91 
92  model=model_original;
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
96 
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
99 
100  hessian(i,j) = partial_der;
101  hessian(j,i) = partial_der; %symmetric Hessian --> H(i,j) = H(j,i)
102 
103  if(model.verbose>=8)
104  disp('entered porsche_get_Jacobian')
105  k=k+1;
106  disp(['values calculated: ' num2str(k) ' of ' num2str((length(model.mu_names)*(length(model.mu_names)+1))/2) ]);
107  end
108  end
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)
112 
113 
114 %old: without flag model.optimization.opt_mode
115 % %detailed case
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
133 %
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
137 %
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
141 %
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
145 %
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);
148 %
149 % hessian(i,i) = partial_der;
150 %
151 % if(model.verbose>=8)
152 % disp('entered porsche_get_Jacobian')
153 % k=k+1;
154 % disp(['values calculated: ' num2str(k) ' of ' num2str((length(model.mu_names)*(length(model.mu_names)+1))/2) ]);
155 % end
156 %
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
164 %
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
169 %
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
174 %
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
179 %
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
182 %
183 % hessian(i,j) = partial_der;
184 % hessian(j,i) = partial_der; %symmetric Hessian --> H(i,j) = H(j,i)
185 %
186 % if(model.verbose>=8)
187 % disp('entered porsche_get_Jacobian')
188 % k=k+1;
189 % disp(['values calculated: ' num2str(k) ' of ' num2str((length(model.mu_names)*(length(model.mu_names)+1))/2) ]);
190 % end
191 % end
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)
195 %
196 %
197 % %reduced case
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
215 %
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
219 %
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
223 %
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
227 %
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);
230 %
231 % hessian(i,i) = partial_der;
232 %
233 %
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
241 %
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
246 %
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
251 %
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
256 %
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
259 %
260 % hessian(i,j) = partial_der;
261 % hessian(j,i) = partial_der; %symmetric Hessian --> H(i,j) = H(j,i)
262 % end
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)
266 %
267 %
268 % else
269 % warning('neither model_data nor reduced_data was given');
270 % J=0;
271 % end
272 
273 
274 end
function r = verbose(level, message, messageId)
This function displays messages depending on a message-id and/or a level. Aditionally you can set/res...
Definition: verbose.m:17