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
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stéphane Adjemian
dynare
Commits
0edd8e20
Commit
0edd8e20
authored
9 years ago
by
Johannes Pfeifer
Committed by
Stéphane Adjemian
9 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add penalty_hessian.m and penalty_objective_function.m
parent
544da84e
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
matlab/optimization/penalty_hessian.m
+94
-0
94 additions, 0 deletions
matlab/optimization/penalty_hessian.m
matlab/optimization/penalty_objective_function.m
+7
-0
7 additions, 0 deletions
matlab/optimization/penalty_objective_function.m
with
101 additions
and
0 deletions
matlab/optimization/penalty_hessian.m
0 → 100644
+
94
−
0
View file @
0edd8e20
function
hessian_mat
=
penalty_hessian
(
func
,
x
,
penalty
,
gstep
,
varargin
)
% --*-- Unitary tests --*--
% Computes second order partial derivatives with penalty_objective_function
%
% INPUTS
% func [string] name of the function
% x [double] vector, the Hessian of "func" is evaluated at x.
% penalty [double] penalty base used if function fails
% gstep [double] scalar, size of epsilon.
% varargin [void] list of additional arguments for "func".
%
% OUTPUTS
% hessian_mat [double] Hessian matrix
%
% ALGORITHM
% Uses Abramowitz and Stegun (1965) formulas 25.3.23
% \[
% \frac{\partial^2 f_{0,0}}{\partial {x^2}} = \frac{1}{h^2}\left( f_{1,0} - 2f_{0,0} + f_{ - 1,0} \right)
% \]
% and 25.3.27 p. 884
%
% \[
% \frac{\partial ^2f_{0,0}}{\partial x\partial y} = \frac{-1}{2h^2}\left(f_{1,0} + f_{-1,0} + f_{0,1} + f_{0,-1} - 2f_{0,0} - f_{1,1} - f_{-1,-1} \right)
% \]
%
% SPECIAL REQUIREMENTS
% none
%
% Copyright (C) 2001-2014 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/>.
if
~
isa
(
func
,
'function_handle'
)
func
=
str2func
(
func
);
end
n
=
size
(
x
,
1
);
h1
=
max
(
abs
(
x
),
sqrt
(
gstep
(
1
))
*
ones
(
n
,
1
))
*
eps
^
(
1
/
6
)
*
gstep
(
2
);
h_1
=
h1
;
xh1
=
x
+
h1
;
h1
=
xh1
-
x
;
xh1
=
x
-
h_1
;
h_1
=
x
-
xh1
;
xh1
=
x
;
f0
=
penalty_objective_function
(
x
,
func
,
penalty
,
varargin
{:});
f1
=
zeros
(
size
(
f0
,
1
),
n
);
f_1
=
f1
;
for
i
=
1
:
n
%do step up
xh1
(
i
)
=
x
(
i
)
+
h1
(
i
);
f1
(:,
i
)
=
penalty_objective_function
(
xh1
,
func
,
penalty
,
varargin
{:});
%do step up
xh1
(
i
)
=
x
(
i
)
-
h_1
(
i
);
f_1
(:,
i
)
=
penalty_objective_function
(
xh1
,
func
,
penalty
,
varargin
{:});
xh1
(
i
)
=
x
(
i
);
%reset parameter
end
xh_1
=
xh1
;
hessian_mat
=
zeros
(
size
(
f0
,
1
),
n
*
n
);
temp
=
f1
+
f_1
-
f0
*
ones
(
1
,
n
);
%term f_(1,0)+f_(-1,0)-f_(0,0) used later
for
i
=
1
:
n
if
i
>
1
%fill symmetric part of Hessian based on previously computed results
k
=
[
i
:
n
:
n
*
(
i
-
1
)];
hessian_mat
(:,(
i
-
1
)
*
n
+
1
:(
i
-
1
)
*
n
+
i
-
1
)
=
hessian_mat
(:,
k
);
end
hessian_mat
(:,(
i
-
1
)
*
n
+
i
)
=
(
f1
(:,
i
)
+
f_1
(:,
i
)
-
2
*
f0
)
.
/(
h1
(
i
)
*
h_1
(
i
));
%formula 25.3.23
for
j
=
i
+
1
:
n
%step in up direction
xh1
(
i
)
=
x
(
i
)
+
h1
(
i
);
xh1
(
j
)
=
x
(
j
)
+
h_1
(
j
);
%step in down direction
xh_1
(
i
)
=
x
(
i
)
-
h1
(
i
);
xh_1
(
j
)
=
x
(
j
)
-
h_1
(
j
);
hessian_mat
(:,(
i
-
1
)
*
n
+
j
)
=-
(
-
penalty_objective_function
(
xh1
,
func
,
penalty
,
varargin
{:})
-
penalty_objective_function
(
xh_1
,
func
,
penalty
,
varargin
{:})
+
temp
(:,
i
)
+
temp
(:,
j
))
.
/(
2
*
h1
(
i
)
*
h_1
(
j
));
%formula 25.3.27
%reset grid points
xh1
(
i
)
=
x
(
i
);
xh1
(
j
)
=
x
(
j
);
xh_1
(
i
)
=
x
(
i
);
xh_1
(
j
)
=
x
(
j
);
end
end
This diff is collapsed.
Click to expand it.
matlab/optimization/penalty_objective_function.m
0 → 100644
+
7
−
0
View file @
0edd8e20
function [fval,exit_flag,arg1,arg2] = penalty_objective_function(x0,fcn,penalty,varargin)
[fval,info,exit_flag,arg1,arg2] = fcn(x0,varargin{:});
if info(1) ~= 0
fval = penalty + info(2);
end
end
\ No newline at end of file
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