1function [loss,info,exit_flag,vx,junk]=osr_obj(x,i_params,i_var,weights)
2% objective function for optimal simple rules (OSR)
3% INPUTS
4%   x                         vector           values of the parameters
5%                                              over which to optimize
6%   i_params                  vector           index of optimizing parameters in M_.params
7%   i_var                     vector           variables indices
8%   weights                   vector           weights in the OSRs
9%
10% OUTPUTS
11%   loss                      scalar           loss function returned to solver
12%   info                      vector           info vector returned by resol
13%   exit_flag                 scalar           exit flag returned to solver
14%   vx                        vector           variances of the endogenous variables
15%   junk                      empty            dummy output for conformable
16%                                              header
17%
18% SPECIAL REQUIREMENTS
19%   none
20% Copyright (C) 2005-2017 Dynare Team
21%
22% This file is part of Dynare.
23%
24% Dynare is free software: you can redistribute it and/or modify
25% it under the terms of the GNU General Public License as published by
26% the Free Software Foundation, either version 3 of the License, or
27% (at your option) any later version.
28%
29% Dynare is distributed in the hope that it will be useful,
30% but WITHOUT ANY WARRANTY; without even the implied warranty of
31% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32% GNU General Public License for more details.
33%
34% You should have received a copy of the GNU General Public License
35% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
36
37global M_ oo_ options_ optimal_Q_ it_
38%  global ys_ Sigma_e_ endo_nbr exo_nbr optimal_Q_ it_ ykmin_ options_
39
40junk = [];
41exit_flag = 1;
42vx = [];
43info=zeros(4,1);
44loss=[];
45% set parameters of the policiy rule
46M_.params(i_params) = x;
47
48% don't change below until the part where the loss function is computed
49it_ = M_.maximum_lag+1;
50[dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_);
51
52if info(1)
53    if info(1) == 3 || info(1) == 4 || info(1) == 5 || info(1)==6 ||info(1) == 19 ||...
54                info(1) == 20 || info(1) == 21 || info(1) == 23 || info(1) == 26 || ...
55                info(1) == 81 || info(1) == 84 ||  info(1) == 85
56        loss = 1e8;
57        info(4)=info(2);
58        return
59    else
60        loss = 1e8;
61        info(4)=0.1;
62        return
63    end
64end
65
66vx = get_variance_of_endogenous_variables(dr,i_var);
67loss = full(weights(:)'*vx(:));
68