Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
dynare
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dynare
dynare
Commits
0718a3eb
Commit
0718a3eb
authored
Jan 22, 2021
by
Johannes Pfeifer
Browse files
Options
Downloads
Patches
Plain Diff
dynare_minimize_objective: fix use of analytic derivatives by employing wrapper function
parent
ff427807
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
matlab/optimization/dynare_minimize_objective.m
+61
-28
61 additions, 28 deletions
matlab/optimization/dynare_minimize_objective.m
with
61 additions
and
28 deletions
matlab/optimization/dynare_minimize_objective.m
+
61
−
28
View file @
0718a3eb
...
@@ -77,15 +77,27 @@ switch minimizer_algorithm
...
@@ -77,15 +77,27 @@ switch minimizer_algorithm
% is not able to even move away from the initial point.
% is not able to even move away from the initial point.
optim_options
=
optimoptions
(
optim_options
,
'Algorithm'
,
'active-set'
);
optim_options
=
optimoptions
(
optim_options
,
'Algorithm'
,
'active-set'
);
end
end
if
options_
.
analytic_derivation
||
options_
.
mom
.
analytic_jacobian
==
1
optim_options
=
optimoptions
(
optim_options
,
'GradObj'
,
'on'
,
'TolX'
,
1e-7
);
%alter default TolX
end
if
~
isempty
(
options_
.
optim_opt
)
if
~
isempty
(
options_
.
optim_opt
)
eval
([
'optim_options = optimoptions(optim_options,'
options_
.
optim_opt
');'
]);
eval
([
'optim_options = optimoptions(optim_options,'
options_
.
optim_opt
');'
]);
end
end
if
options_
.
silent_optimizer
if
options_
.
silent_optimizer
optim_options
=
optimoptions
(
optim_options
,
'display'
,
'off'
);
optim_options
=
optimoptions
(
optim_options
,
'display'
,
'off'
);
end
end
if
options_
.
analytic_derivation
if
options_
.
analytic_derivation
||
options_
.
mom
.
analytic_jacobian
==
1
%use wrapper
optim_options
=
optimoptions
(
optim_options
,
'GradObj'
,
'on'
,
'TolX'
,
1e-7
);
%alter default TolX
func
=
@
(
x
)
analytic_gradient_wrapper
(
x
,
objective_function
,
varargin
{:});
if
~
isoctave
[
opt_par_values
,
fval
,
exitflag
,
output
,
lamdba
,
grad
,
hessian_mat
]
=
...
fmincon
(
func
,
start_par_value
,[],[],[],[],
bounds
(:,
1
),
bounds
(:,
2
),[],
optim_options
);
else
% Under Octave, use a wrapper, since fmincon() does not have an 11th
% arg. Also, only the first 4 output arguments are available.
[
opt_par_values
,
fval
,
exitflag
,
output
]
=
...
fmincon
(
func
,
start_par_value
,[],[],[],[],
bounds
(:,
1
),
bounds
(:,
2
),[],
optim_options
);
end
end
else
if
~
isoctave
if
~
isoctave
[
opt_par_values
,
fval
,
exitflag
,
output
,
lamdba
,
grad
,
hessian_mat
]
=
...
[
opt_par_values
,
fval
,
exitflag
,
output
,
lamdba
,
grad
,
hessian_mat
]
=
...
fmincon
(
objective_function
,
start_par_value
,[],[],[],[],
bounds
(:,
1
),
bounds
(:,
2
),[],
optim_options
,
varargin
{:});
fmincon
(
objective_function
,
start_par_value
,[],[],[],[],
bounds
(:,
1
),
bounds
(:,
2
),[],
optim_options
,
varargin
{:});
...
@@ -96,6 +108,7 @@ switch minimizer_algorithm
...
@@ -96,6 +108,7 @@ switch minimizer_algorithm
[
opt_par_values
,
fval
,
exitflag
,
output
]
=
...
[
opt_par_values
,
fval
,
exitflag
,
output
]
=
...
fmincon
(
func
,
start_par_value
,[],[],[],[],
bounds
(:,
1
),
bounds
(:,
2
),[],
optim_options
);
fmincon
(
func
,
start_par_value
,[],[],[],[],
bounds
(:,
1
),
bounds
(:,
2
),[],
optim_options
);
end
end
end
case
2
case
2
%simulating annealing
%simulating annealing
...
@@ -159,12 +172,20 @@ switch minimizer_algorithm
...
@@ -159,12 +172,20 @@ switch minimizer_algorithm
if
~
isempty
(
options_
.
optim_opt
)
if
~
isempty
(
options_
.
optim_opt
)
eval
([
'optim_options = optimoptions(optim_options,'
options_
.
optim_opt
');'
]);
eval
([
'optim_options = optimoptions(optim_options,'
options_
.
optim_opt
');'
]);
end
end
if
options_
.
analytic_derivation
optim_options
=
optimoptions
(
optim_options
,
'GradObj'
,
'on'
);
end
if
options_
.
silent_optimizer
if
options_
.
silent_optimizer
optim_options
=
optimoptions
(
optim_options
,
'display'
,
'off'
);
optim_options
=
optimoptions
(
optim_options
,
'display'
,
'off'
);
end
end
if
options_
.
analytic_derivation
||
options_
.
mom
.
analytic_jacobian
==
1
optim_options
=
optimoptions
(
optim_options
,
'GradObj'
,
'on'
);
if
~
isoctave
func
=
@
(
x
)
analytic_gradient_wrapper
(
x
,
objective_function
,
varargin
{:});
[
opt_par_values
,
fval
,
exitflag
]
=
fminunc
(
func
,
start_par_value
,
optim_options
);
else
% Under Octave, use a wrapper, since fminunc() does not have a 4th arg
func
=
@
(
x
)
analytic_gradient_wrapper
(
x
,
objective_function
,
varargin
{:});
[
opt_par_values
,
fval
,
exitflag
]
=
fminunc
(
func
,
start_par_value
,
optim_options
);
end
else
if
~
isoctave
if
~
isoctave
[
opt_par_values
,
fval
,
exitflag
]
=
fminunc
(
objective_function
,
start_par_value
,
optim_options
,
varargin
{:});
[
opt_par_values
,
fval
,
exitflag
]
=
fminunc
(
objective_function
,
start_par_value
,
optim_options
,
varargin
{:});
else
else
...
@@ -172,7 +193,7 @@ switch minimizer_algorithm
...
@@ -172,7 +193,7 @@ switch minimizer_algorithm
func
=
@
(
x
)
objective_function
(
x
,
varargin
{:});
func
=
@
(
x
)
objective_function
(
x
,
varargin
{:});
[
opt_par_values
,
fval
,
exitflag
]
=
fminunc
(
func
,
start_par_value
,
optim_options
);
[
opt_par_values
,
fval
,
exitflag
]
=
fminunc
(
func
,
start_par_value
,
optim_options
);
end
end
end
case
4
case
4
% Set default options.
% Set default options.
H0
=
1e-4
*
eye
(
n_params
);
H0
=
1e-4
*
eye
(
n_params
);
...
@@ -505,7 +526,12 @@ switch minimizer_algorithm
...
@@ -505,7 +526,12 @@ switch minimizer_algorithm
if
options_
.
silent_optimizer
if
options_
.
silent_optimizer
solveoptoptions
.
verbosity
=
0
;
solveoptoptions
.
verbosity
=
0
;
end
end
if
options_
.
analytic_derivation
||
options_
.
mom
.
analytic_jacobian
==
1
func
=
@
(
x
)
analytic_gradient_wrapper
(
x
,
objective_function
,
varargin
{:});
[
opt_par_values
,
fval
]
=
solvopt
(
start_par_value
,
func
,
1
,[],[],
solveoptoptions
);
else
[
opt_par_values
,
fval
]
=
solvopt
(
start_par_value
,
objective_function
,[],[],[],
solveoptoptions
,
varargin
{:});
[
opt_par_values
,
fval
]
=
solvopt
(
start_par_value
,
objective_function
,[],[],[],
solveoptoptions
,
varargin
{:});
end
case
102
case
102
if
isoctave
if
isoctave
error
(
'Optimization algorithm 2 is not available under Octave'
)
error
(
'Optimization algorithm 2 is not available under Octave'
)
...
@@ -534,8 +560,14 @@ switch minimizer_algorithm
...
@@ -534,8 +560,14 @@ switch minimizer_algorithm
eval
([
'optim_options = optimoptions(optim_options,'
options_
.
optim_opt
');'
]);
eval
([
'optim_options = optimoptions(optim_options,'
options_
.
optim_opt
');'
]);
end
end
if
options_
.
silent_optimizer
if
options_
.
silent_optimizer
optim_options
=
optimoptions
(
optim_options
,
'
Display
'
,
'off'
)
;
optim_options
.
Display
=
'off'
;
end
end
if
options_
.
analytic_derivation
||
options_
.
mom
.
analytic_jacobian
==
1
optim_options
.
SpecifyObjectiveGradient
=
true
;
func
=
@
(
x
)
analytic_gradient_wrapper
(
x
,
objective_function
,
varargin
{:});
[
opt_par_values
,
Resnorm
,
fval
,
exitflag
,
OUTPUT
,
LAMBDA
,
JACOB
]
=
...
lsqnonlin
(
func
,
start_par_value
,
bounds
(:,
1
),
bounds
(:,
2
),
optim_options
);
else
if
~
isoctave
if
~
isoctave
[
opt_par_values
,
Resnorm
,
fval
,
exitflag
,
OUTPUT
,
LAMBDA
,
JACOB
]
=
lsqnonlin
(
objective_function
,
start_par_value
,
bounds
(:,
1
),
bounds
(:,
2
),
optim_options
,
varargin
{:});
[
opt_par_values
,
Resnorm
,
fval
,
exitflag
,
OUTPUT
,
LAMBDA
,
JACOB
]
=
lsqnonlin
(
objective_function
,
start_par_value
,
bounds
(:,
1
),
bounds
(:,
2
),
optim_options
,
varargin
{:});
else
else
...
@@ -543,6 +575,7 @@ switch minimizer_algorithm
...
@@ -543,6 +575,7 @@ switch minimizer_algorithm
func
=
@
(
x
)
objective_function
(
x
,
varargin
{:});
func
=
@
(
x
)
objective_function
(
x
,
varargin
{:});
[
opt_par_values
,
Resnorm
,
fval
,
exitflag
,
OUTPUT
,
LAMBDA
,
JACOB
]
=
lsqnonlin
(
func
,
start_par_value
,
bounds
(:,
1
),
bounds
(:,
2
),
optim_options
);
[
opt_par_values
,
Resnorm
,
fval
,
exitflag
,
OUTPUT
,
LAMBDA
,
JACOB
]
=
lsqnonlin
(
func
,
start_par_value
,
bounds
(:,
1
),
bounds
(:,
2
),
optim_options
);
end
end
end
otherwise
otherwise
if
ischar
(
minimizer_algorithm
)
if
ischar
(
minimizer_algorithm
)
if
exist
(
minimizer_algorithm
)
if
exist
(
minimizer_algorithm
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment