rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
eop_master_gui.m
1 function eop_master_gui(varargin)
2 %function eop_master_GUI(model,detailed_data,[sim_data.U],[reduced_data], [callbackfigure], [cbhandle])
3 %
4 % Builds a GUI for the european_option_pricing_model, which has 4 sliders
5 % for the 4 differnet parameters, another slider for the time and a last
6 % slider for N, the number of used reduced basis vectors. In the top right
7 % the reduced simulation to the current settings is plottet and in the top
8 % left the error ||u-u_N|| in a discrete L2-Norm is plottet, dependant on
9 % the number N of used reduced basis vectors. With the button "Knopf" in
10 % the center a detailed simulation is done and plottet to the current
11 % setting and theerror ||u-u_N|| for the current settings is updated.
12 
13 
14 %% construct the GUI
15 if nargin < 6 % no callback -> new figure
16 % create new figure g and extract the input and write the important parts in ud.
17 % ud will be passed on for callbacks etc.
18  g = figure;
19  ud = [];
20  list = varargin;
21  if nargin < 3
22  list{3} = [];
23  end
24 
25  if nargin < 4
26  list{4} = [];
27  end;
28 
29  if nargin >=2
30  ud.model = list{1};
31  ud.detailed_data = list{2};
32  else
33  error('see description for the right input arguments!');
34  end
35 % write size of RB in ud.model
36  if ~isfield(ud.model,'N')
37  ud.model.N = ceil(ud.model.get_rb_size(ud.model,ud.detailed_data)/2);
38  end
39 % write reduced data in ud
40  if isempty(list{4})
41  disp('Preparing offline-quantities...')
42  ud.reduced_data = gen_reduced_data(ud.model, ud.detailed_data);
43  else
44  ud.reduced_data = list{4};
45  end
46 % if not in the input do a detailed_simulation for the current mu and save
47 % it. If in the input the detailed_simulation should have been done for the
48 % current mu.
49  if isempty(list{3})
50  detailed_simulation_data = detailed_simulation(ud.model, ud.detailed_data);
51  ud.detailed_simulation_data = detailed_simulation_data.U;
52  else
53  ud.detailed_simulation_data = list{3};
54  end
55  ud.first_mu = ud.model.get_mu(ud.model); % save this first mu, since the detailed_simulation was done with it (later used for the error)
56 
57  % setting GUI size parameters
58  di = 0.05;
59  textwidth= 0.15;
60  textheight = 0.05;
61  yscale_uicontrols = 1;
62  % define some stuff so that the slider positioning works
63  for i=1:length(ud.model.mu_names)
64  Positioner1 = ceil(i/2);
65  if i/2 ~= floor(i/2)
66  Positioner2 = 0;
67  elseif i/2 == floor(i/2)
68  Positioner2 = 0.5;
69  end
70  % various sliders for the parameters - use current mus as first slider value/position
71  mu = ud.model.get_mu(ud.model);
72  val = mu(i);
73  ra = ud.model.mu_ranges{i};
74  norm_val = (val-ra(1))/(ra(2)-ra(1));
75  txt(i) = uicontrol(g,'Style','text','Units','normalized',...
76  'Position',...
77  [di+Positioner2,(Positioner1*di+(Positioner1-1)*textheight)*yscale_uicontrols,...
78  textwidth,textheight*yscale_uicontrols],...
79  'String',[ud.model.mu_names{i},'=',num2str(val)],...
80  'Tag',['text_',ud.model.mu_names{i}]);
81  slider(i) = uicontrol(g,'Style','slider','Units','normalized',...
82  'Position',...
83  [ 2*di+textwidth+Positioner2, ...
84  (Positioner1*di+(Positioner1-1)*textheight)*yscale_uicontrols, ...
85  (1-3*di-textwidth)/3,...
86  textheight*yscale_uicontrols],...
87  'Tag',['slider_',ud.model.mu_names{i}],...
88  'Min',0, ...
89  'Max',1, ...
90  'value',norm_val, ...
91  'Callback','eop_master_gui([],[],[],[],[],[],gcbf,gcbo)');
92  end
93 
94  % N = number of RB slider
95  i = ceil(length(ud.model.mu_names)/2)+1;
96  txt(i) = uicontrol(g,'Style','text','Units','normalized',...
97  'Position', ...
98  [di,(i*di+(i-1)*textheight)*yscale_uicontrols,...
99  textwidth,textheight*yscale_uicontrols],...
100  'String',['RB-size N = ',num2str(ud.model.N)],...
101  'Tag','text_N');
102  slider(i) = uicontrol(g,'Style','slider','Units','normalized',...
103  'Position',...
104  [2*di+textwidth, ...
105  (i*di + (i-1)*textheight)*yscale_uicontrols, ...
106  (1-3*di-textwidth)/3,textheight*yscale_uicontrols],...
107  'Tag','slider_N',...
108  'min',1,'max', ...
109  ud.model.get_rb_size(ud.model,ud.detailed_data),...
110  'value',ud.model.N, ...
111  'Callback','eop_master_gui([],[],[],[],[],[],gcbf,gcbo)',...
112  'sliderstep',...
113  [1/((ud.model.get_rb_size(ud.model,ud.detailed_data))-1),0.1]);
114  %Time-slider
115  txt(i) = uicontrol(g,'Style','text','Units','normalized',...
116  'Position', ...
117  [di+0.5,(i*di+(i-1)*textheight)*yscale_uicontrols,...
118  textwidth,textheight*yscale_uicontrols],...
119  'String','timestep 1','Tag','text_time');
120  slider(i) = uicontrol(g,'Style','slider','Units','normalized',...
121  'Position',...
122  [2*di+textwidth+0.5, ...
123  (i*di + (i-1)*textheight)*yscale_uicontrols, ...
124  (1-3*di-textwidth)/3,textheight*yscale_uicontrols],...
125  'Tag','slider_time',...
126  'min',1,'max',ud.model.nt+1,'value',1, ...
127  'Callback','eop_master_gui([],[],[],[],[],[],gcbf,gcbo)',...
128  'sliderstep',[1/ud.model.nt,0.1]);
129 
130  % graphics axes
131  axes('Units','normalized',...
132  'Position',[di,((i+2)*di+i*textheight)*yscale_uicontrols,...
133  di+textwidth+(1-3*di-textwidth)/3,1-((i+3)*di + i*textheight)*yscale_uicontrols],...
134  'Tag','axes_1');
135 
136  axes('Units','normalized',...
137  'Position',[di+0.5,((i+2)*di+i*textheight)*yscale_uicontrols,...
138  di+textwidth+(1-3*di-textwidth)/3,1-((i+3)*di + i*textheight)*yscale_uicontrols],...
139  'Tag','axes_2');
140  Master_Button = uicontrol(g,'Style','pushbutton','Units','normalized','String','Knopf',...
141  'Position',[0.5-di/4,((i+2)*di+i*textheight)*yscale_uicontrols-6/4*di,di,di],...
142  'Tag','Master_button','Callback','eop_master_gui([],[],[],[],[],[],gcbf,gcbo)');
143 
144  set(g,'Color',[0.8147 0.9058 0.1270]); % fancy backgroundcolour
145  set(g,'Userdata',ud); % take the structure ud into the Userdata in g, so it is enough to have g.
146  replot(g,'new');
147 end
148 
149 %% callback
150 
151 if nargin >=6 % callback
152 % get the handles of the current figure and the current callback object
153  cbf = varargin{7};
154  cbo = varargin{8};
155  ud = get(cbf,'Userdata'); % extract data
156  tag = get(cbo,'Tag'); % tag of the callback object
157 % updates the correct callbackobject and gives back a sourcestring for the
158 % replot function to work properly
159  if isequal(tag,'slider_time')
160  val = round(get(cbo,'value'));
161  set(cbo,'value',val);
162  ud.timestep = val;
163  txt = findobj(cbf,'Tag','text_time');
164  set(txt,'String',['timestep = ',num2str(val)]);
165  sourcestr = 'time';
166  elseif isequal(tag,'slider_N')
167  val = round(get(cbo,'value'));
168  set(cbo,'value',val);
169  ud.model.N = val;
170  txt = findobj(cbf,'Tag','text_N');
171  set(txt,'String',['RB-size N = ',num2str(val)]);
172  sourcestr = 'N';
173  elseif isequal(tag,'Master_button')
174  sourcestr = 'Master';
175  else
176  found_cbo = 0;
177  for i=1:length(ud.model.mu_names)
178  if isequal(tag,['slider_',ud.model.mu_names{i}])
179  found_cbo = 1;
180  % rescaling of the mu values since the slider is normed to [0,1]
181  val = get(cbo,'value')*...
182  (ud.model.mu_ranges{i}(2)-...
183  ud.model.mu_ranges{i}(1)) + ...
184  ud.model.mu_ranges{i}(1);
185  mu = ud.model.get_mu(ud.model);
186  mu(i) = val;
187  ud.model = ud.model.set_mu(ud.model,mu);
188  txt = findobj(cbf,'Tag',['text_',ud.model.mu_names{i}]);
189  set(txt,'String',[ud.model.mu_names{i},' = ',num2str(val)]);
190  sourcestr = 'mu';
191  end
192  end
193  if ~found_cbo
194  error('callback object unknown!!');
195  end
196  end
197  set(cbf,'Userdata',ud); % update userdata
198  replot(cbf, sourcestr);
199 end
200 
201 
202 function replot(g,tag)
203 ud = get(g,'Userdata'); % extract userdata
204 
205 if ismember(tag,{'new','mu','N','time'})
206  if strcmp(tag,'new') == 1
207  ud.timestep = 1;
208  end
209  reduced_data = extract_reduced_data_subset(ud.model,ud.reduced_data); % reduced data of the desired size
210  simulation_data = rb_simulation(ud.model,reduced_data);
211  rb_sim_data=rb_reconstruction(ud.model,ud.detailed_data,simulation_data);
212  ud.sim_data = rb_sim_data; % save the new rb_simulation
213  % go to the correct axis
214  ax2 = findobj(g,'Tag','axes_2');
215  set(gcf,'CurrentAxes',ax2);
216  % to plot the new data of the correct time step
217  plot_vertex_data(ud.detailed_data.grid,ud.sim_data.U(:,ud.timestep),[]);
218  set(gca,'Title',text('String','the reduced simulation')) % has to be set again :(
219  if strcmp(tag,'N') == 1 || strcmp(tag,'new') == 1 % plot the error above N the actual size of the RB
220  disp(['performing RB simulation with N = ',num2str(ud.model.N)]);
221  if strcmp(tag,'new') == 1
222  ud.model = ud.model.set_mu(ud.model,ud.first_mu);
223  ud.current_mu = ud.first_mu;
224  else
225  ud.model = ud.model.set_mu(ud.model,ud.current_mu);
226  end
227  reduced_data = extract_reduced_data_subset(ud.model,ud.reduced_data);
228  simulation_data = rb_simulation(ud.model,reduced_data);
229  rb_sim_data=rb_reconstruction(ud.model,ud.detailed_data,simulation_data);
230  ud.sim_data = rb_sim_data;
231  % new error
232  Fehler = eop_fd_norm(ud.detailed_simulation_data, ud.sim_data.U, ud.detailed_data.grid);
233  % correct axis
234  ax = findobj(g,'Tag','axes_1');
235  set(gcf,'CurrentAxes',ax);
236  fprintf('plotting...\n');
237  % plot error
238  plot(0:ud.model.deltaT:ud.model.T,Fehler','r o :');
239  set(gca,'Tag','axes_1');
240  set(gca,'Title',text('String','the error ||u-u_N|| above N'))
241  set(get(gca,'XLabel'),'String','time t');
242  set(get(gca,'YLabel'),'String','error');
243  set(gca,'XTick',0:2*ud.model.deltaT:ud.model.T);
244  end
245 
246 elseif isequal(tag,'Master')
247  % master button performs a detailed_simulation to the current mu and
248  % updates the error
249  detailed_simulation_data = detailed_simulation(ud.model, ud.detailed_data);
250  ud.detailed_simulation_data = detailed_simulation_data.U;
251  if ud.model.compute_output_functional == 1
252  params.plot_output_title = ['the output functional for mu = ' mat2str(get_mu(ud.model))];
253  params.plot_title = ['the detailed simulation for the current parameter mu = ', mat2str(round((get_mu(ud.model).*1000))/1000)];
254  else
255  params.plot_title = ['the detailed simulation for the current parameter mu = ', mat2str(round((get_mu(ud.model).*1000))/1000)];
256  end
257  % plots it in a seperate window
258  lin_evol_plot_sim_data(ud.model,ud.detailed_data,detailed_simulation_data,params);
259  ud.current_mu = get_mu(ud.model);
260  reduced_data = extract_reduced_data_subset(ud.model,ud.reduced_data);
261  simulation_data = rb_simulation(ud.model,reduced_data);
262  rb_sim_data=rb_reconstruction(ud.model,ud.detailed_data,simulation_data);
263  ud.sim_data = rb_sim_data;
264  % error update in the GUI
265  Fehler = eop_fd_norm(ud.detailed_simulation_data, ud.sim_data.U, ud.detailed_data.grid);
266  ax = findobj(g,'Tag','axes_1');
267  set(g,'CurrentAxes',ax);
268  figure(g);
269  fprintf('plotting...\n');
270  plot(0:ud.model.deltaT:ud.model.T,Fehler','r o :');
271  set(gca,'Tag','axes_1');
272  set(gca,'Title',text('String','the error ||u-u_N|| above N'))
273  set(get(gca,'XLabel'),'String','time t');
274  set(get(gca,'YLabel'),'String','error');
275  set(gca,'XTick',0:2*ud.model.deltaT:ud.model.T);
276 end
277 set(g,'Userdata',ud); % update user data
function model = european_option_pricing_model(params)
model = european_option_pricing_model(params)
function norm = eop_fd_norm(fd_function1, fd_function2,gridbase grid, unused1)
norm = eop_fd_norm(fd_function1, fd_function2, grid, ~)
Definition: eop_fd_norm.m:17