Skip to content
Snippets Groups Projects
Select Git revision
  • master default
  • noGUIDE
  • 4.5
3 results

gui_stoch_simulation.m

Blame
  • Forked from Dynare / matlab-gui
    31 commits behind the upstream repository.
    gui_stoch_simulation.m 11.49 KiB
    function gui_stoch_simulation(tabId)
    % function gui_stoch_simulation(tabId)
    % interface for the DYNARE stoch_simul command
    %
    % INPUTS
    %   tabId:      GUI tab element which displays stoch_simul command interface
    %
    % OUTPUTS
    %   none
    %
    % SPECIAL REQUIREMENTS
    %   none
    
    % Copyright (C) 2003-2020 Dynare Team
    %
    % This file is part of Dynare.
    %
    % Dynare is free software: you can redistribute it and/or modify
    % it under the terms of the GNU General Public License as published by
    % the Free Software Foundation, either version 3 of the License, or
    % (at your option) any later version.
    %
    % Dynare is distributed in the hope that it will be useful,
    % but WITHOUT ANY WARRANTY; without even the implied warranty of
    % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    % GNU General Public License for more details.
    %
    % You should have received a copy of the GNU General Public License
    % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
    
    global project_info model_settings options_ dynare_gui_ oo_ M_
    
    bg_color = char(getappdata(0,'bg_color'));
    special_color = char(getappdata(0,'special_color'));
    
    handles = [];
    gui_size = gui_tools.get_gui_elements_size(tabId);
    
    % --- PANELS -------------------------------------
    handles.uipanelShocks = uipanel( ...
        'Parent', tabId, ...
        'Tag', 'uipanelShocks', ...
        'BackgroundColor', special_color, ...
        'Units', 'normalized', ...
        'Position', [0.01 0.18 0.48 0.73], ...
        'Title', '', ...
        'BorderType', 'none');
    
    uipanelShocks_CreateFcn;
    
    handles.uipanelVars = uipanel( ...
        'Parent', tabId, ...
        'Tag', 'uipanelVars', ...
        'UserData', zeros(1,0), ...
        'BackgroundColor', special_color, ...
        'Units', 'normalized', ...
        'Position', [0.51 0.18 0.48 0.7], ...
        'Title', '', ...
        'BorderType', 'none');
    
    handles = gui_tabs.create_endo_vars(handles, 'stoch_simul');
    
    handles.uipanelComm = uipanel( ...
        'Parent', tabId, ...
        'Tag', 'uipanelCommOptions', ...
        'UserData', zeros(1,0), ...
        'BackgroundColor', bg_color, ...
        'Units', 'normalized', ...
        'Position', [0.01 0.09 0.98 0.09], ...
        'Title', 'Current command options:');
    
    
    % --- STATIC TEXT -------------------------------------
    handles.text7 = uicontrol( ...
        'Parent', tabId, ...
        'Tag', 'text7', ...
        'Style', 'text', 'BackgroundColor', bg_color, ...
        'Units','normalized','Position',[0.51 0.92 0.48 0.05], ...
        'FontWeight', 'bold', ...
        'String', 'Select variables for simulation:', ...
        'HorizontalAlignment', 'left');
    
    handles.text8 = uicontrol( ...
        'Parent', tabId, ...
        'Tag', 'text8', ...
        'Style', 'text', 'BackgroundColor', bg_color, ...
        'Units','normalized','Position',[0.01 0.92 0.48 0.05], ...
        'FontWeight', 'bold', ...
        'String', 'Select structural shocks:', ...
        'HorizontalAlignment', 'left');
    
    if ~isfield(model_settings, 'stoch_simul')
        model_settings.stoch_simul = struct();
    end
    comm_str = gui_tools.command_string('stoch_simul', model_settings.stoch_simul);
    
    handles.stoch_simul = uicontrol( ...
        'Parent', handles.uipanelComm, ...
        'Tag', 'stoch_simul', ...
        'Style', 'text', 'BackgroundColor', bg_color, ...
        'Units', 'normalized', 'Position', [0.01 0.01 0.98 0.98], ...
        'FontAngle', 'italic','String', comm_str, ...
        'TooltipString', comm_str, ...
        'HorizontalAlignment', 'left');
    
    % --- Radio Buttons -------------------------------------
    handles.buttongroup = uibuttongroup( ...
        'Parent', tabId, ...
        'Position', [0.51 0.88 0.36 0.05], ...
        'Units', 'normalized', ...
        'SelectionChangedFcn', {@init_val, handles});
    
    uicontrol(handles.buttongroup, ...
        'Style', 'text', ...
        'Units', 'normalized', ...
        'Position', [0 -.2 1 1], ...
        'FontWeight', 'bold', ...
        'String', 'Initial value: ', ...
        'HorizontalAlignment', 'left');
    
    uicontrol(handles.buttongroup, ...
        'Style', 'radiobutton', ...
        'String', 'zero', ...
        'Units', 'normalized', ...
        'Position', [0.2 0 1 1]);
                  
    uicontrol(handles.buttongroup, ...
        'Style', 'radiobutton', ...
        'String', 'steady state', ...
        'Units', 'normalized', ...
        'Value', 1, ...
        'Position', [0.3 0 1 1]);
    
    uicontrol(handles.buttongroup, ...
        'Style', 'radiobutton', ...
        'String', 'smoother', ...
        'Units', 'normalized', ...
        'Position', [0.5 0 1 1]);
    
    
    % --- PUSHBUTTONS -------------------------------------
    handles.pushbuttonSimulation = uicontrol( ...
        'Parent', tabId, ...
        'Tag', 'pushbuttonSimulation', ...
        'Style', 'pushbutton', ...
        'Units','normalized', ...
        'Position', [gui_size.space gui_size.bottom gui_size.button_width_small gui_size.button_height], ...
        'String', 'Run simulation', ...
        'Callback', @pushbuttonSimulation_Callback);
    
    handles.pushbuttonReset = uicontrol( ...
        'Parent', tabId, ...
        'Tag', 'pushbuttonReset', ...
        'Style', 'pushbutton', ...
        'Units','normalized', ...
        'Position', [gui_size.space*2+gui_size.button_width_small gui_size.bottom gui_size.button_width_small gui_size.button_height], ...
        'String', 'Reset', ...
        'Callback', @reset);
    
    uicontrol( ...
        'Parent', tabId, ...
        'Tag', 'pushbuttonCloseTab', ...
        'Style', 'pushbutton', ...
        'Units', 'normalized', ...
        'Position', [gui_size.space*3+gui_size.button_width_small*2 gui_size.bottom gui_size.button_width_small gui_size.button_height], ...
        'String', 'Close this tab', ...
        'Callback', {@close_tab,tabId});
    
    uicontrol( ...
        'Parent', tabId, ...
        'Tag', 'pushbuttonSimulation', ...
        'Style', 'pushbutton', ...
        'Units','normalized', ...
        'Position', [gui_size.space*4+gui_size.button_width_small*3 gui_size.bottom gui_size.button_width_small gui_size.button_height], ...
        'String', 'Browse results...', ...
        'Enable', 'on', ...
        'Callback', @results);
    
    uicontrol( ...
        'Parent', tabId, ...
        'Tag', 'pushbuttonSimulation', ...
        'Style', 'pushbutton', ...
        'Units','normalized', ...
        'Position', [gui_size.space*5+gui_size.button_width_small*4 gui_size.bottom gui_size.button_width_small gui_size.button_height], ...
        'String', 'Close all output figures', ...
        'Enable', 'on', ...
        'Callback', @close_all);
    
    uicontrol( ...
        'Parent', tabId, ...
        'Tag', 'pushbuttonCommandDefinition', ...
        'Style', 'pushbutton', ...
        'Units', 'normalized', ...
        'Position', [1-gui_size.space-gui_size.button_width_small gui_size.bottom gui_size.button_width_small gui_size.button_height], ...
        'String', 'Define command options ...', ...
        'Callback', @define_command);
    
        function uipanelShocks_CreateFcn()
            data = cell(size(model_settings.shocks, 1), 2);
            data(:,1) = {false};
            data(:,2) = model_settings.shocks(:,1);
            handles.shocksTabGroup = uitabgroup(handles.uipanelShocks, 'Position', [0 0 1 1]);
            new_tab = uitab(handles.shocksTabGroup, 'Title', 'Shocks' , 'UserData', 1);
            currentPanel = uipanel('Parent', new_tab, 'BackgroundColor', 'white', 'BorderType', 'none');
            handles.shocksTable = uitable(currentPanel, ...
                'Data', data, ...
                'Units', 'normalized', ...
                'ColumnName', {'Active','Shock'}, ...
                'ColumnEditable', [true false], ...
                'RowName', [], ...
                'Position', [0 0 1 1]);
        end
    
        function pushbuttonSimulation_Callback(~, ~)
            old_oo_ = oo_;
            try
                backup = gui_auxiliary.backup_options(options_);
                options_ = default_option_values(M_);
                options_ = gui_auxiliary.add_backup_to_options(options_, backup);
                if ~isfield(M_, 'hessian_eq_zero')
                    options_.order = 1;
                end
                if any([handles.shocksTable.Data{:,1}])
                    options_.irf_shocks = char(handles.shocksTable.Data([handles.shocksTable.Data{:,1}],2));
                end
                if any([handles.endoTable.Data{:,1}])
                    model_settings.varlist_.stoch_simul = handles.endoTable.Data([handles.endoTable.Data{:,1}], 2);
                else
                    model_settings.varlist_.stoch_simul = {};
                end
                if ~isempty(model_settings.stoch_simul)
                    names = fieldnames(model_settings.stoch_simul);
                    for ii = 1:size(names, 1)
                        if isempty(model_settings.stoch_simul.(names{ii}))
                            gui_auxiliary.set_command_option(names{ii}, 1, 'check_option');
                        else
                            gui_auxiliary.set_command_option(names{ii}, model_settings.stoch_simul.(names{ii}));
                        end
                    end
                end
    
                gui_tools.project_log_entry('Running stochastic simulation','...');
                [jObj, guiObj] = gui_tools.create_animated_screen('Running stochastic simulation, please wait...', tabId);
                handles.pushbuttonResults.Enable = 'off';
                [info, oo_, options_, M_] = stoch_simul(M_, options_, oo_, model_settings.varlist_.stoch_simul);
                if info(1)
                    throw(['dynare error number ' info(1)]);
                end
                jObj.stop;
                jObj.setBusyText('Done');
                handles.pushbuttonResults.Enable = 'on';
                project_info.modified = true;
                project_info.stochastic_simulation_run = true;
                gui_set_menus(true);
            catch ME
                jObj.stop;
                jObj.setBusyText('Done with errors');
                gui_tools.show_error('Error in execution of stoch_simul command', ME, 'extended');
                oo_ = old_oo_;
            end
            delete(guiObj);
        end
    
        function reset(~, ~)
            handles.endoTable.Data(:, 1) = {false};
            handles.shocksTable.Data(:,1) = {false};
            model_settings.stoch_simul = struct();
            comm_str = gui_tools.command_string('stoch_simul', model_settings.stoch_simul);
            handles.stoch_simul.String = comm_str;
            handles.stoch_simul.TooltipString = comm_str;
        end
    
        function define_command(~, ~)
            h = gui_define_comm_options(dynare_gui_.stoch_simul, 'stoch_simul');
            uiwait(h);
            try
                new_comm = getappdata(0, 'stoch_simul');
                model_settings.stoch_simul = new_comm;
                comm_str = gui_tools.command_string('stoch_simul', new_comm);
    
                handles.stoch_simul.String = comm_str;
                gui_tools.project_log_entry('Defined stoch_simul command',comm_str);
            catch ME
                gui_tools.show_error('Error defining stoch_simul command', ME, 'basic');
            end
        end
    
        function results(~, ~)
            gui_results('stoch_smulation', dynare_gui_.stoch_simulation_results);
        end
    
        function close_all(~, ~)
            gui_tools.close_all_figures();
        end
    
        function close_tab(~, ~, hTab)
            gui_tabs.delete_tab(hTab);
        end
    end
    
    function init_val(~, evt, handles)
    global M_ oo_ model_settings
    if strcmp(evt.NewValue.String, 'zero')
        handles.endoTable.Data(:,3) = num2cell(zeros(M_.orig_endo_nbr, 1));
    elseif strcmp(evt.NewValue.String, 'steady state')
        if ~isfield(oo_, 'steady_state')
            gui_tools.show_warning('Must first calculate the steady state')
            evt.OldValue.Value = 1;
            return
        end
        handles.endoTable.Data(:,3) = num2cell(oo_.steady_state([model_settings.variables{:, 4}]));
    elseif strcmp(evt.NewValue.String, 'smoother')
        if ~isfield(oo_, 'SmoothedVariables')
            gui_tools.show_warning('Must first run smoother')
            evt.OldValue.Value = 1;
            return
        end
        fname = inputdlg('output file from smoother2histval');
        if isempty(fname)
            evt.OldValue.Value = 1;
        else
            histvalf(fname{:})
            handles.endoTable.Data(:,3) = num2cell(M_.endo_histval([model_settings.variables{:, 4}]));
        end
    end
    end