2011-12-12 19:05:25 +01:00
|
|
|
function [nodes,weights] = gauss_hermite_weights_and_nodes(n)
|
|
|
|
% Computes the weights and nodes for an Hermite Gaussian quadrature rule.
|
|
|
|
|
|
|
|
%@info:
|
|
|
|
%! @deftypefn {Function File} {@var{nodes}, @var{weights} =} gauss_hermite_weights_and_nodes (@var{n})
|
|
|
|
%! @anchor{gauss_hermite_weights_and_nodes}
|
|
|
|
%! @sp 1
|
|
|
|
%! Computes the weights and nodes for an Hermite Gaussian quadrature rule. designed to approximate integrals
|
|
|
|
%! on the infinite interval (-\infty,\infty) of an unweighted smooth function.
|
|
|
|
%! @sp 2
|
|
|
|
%! @strong{Inputs}
|
|
|
|
%! @sp 1
|
|
|
|
%! @table @ @var
|
|
|
|
%! @item n
|
|
|
|
%! Positive integer scalar, number of nodes (order of approximation).
|
|
|
|
%! @end table
|
|
|
|
%! @sp 1
|
|
|
|
%! @strong{Outputs}
|
|
|
|
%! @sp 1
|
|
|
|
%! @table @ @var
|
|
|
|
%! @item nodes
|
|
|
|
%! n*1 vector of doubles, the nodes (roots of an order n Hermite polynomial)
|
|
|
|
%! @item weights
|
|
|
|
%! n*1 vector of doubles, the associated weights.
|
|
|
|
%! @end table
|
|
|
|
%! @sp 2
|
|
|
|
%! @strong{This function is called by:}
|
|
|
|
%! @sp 2
|
|
|
|
%! @strong{This function calls:}
|
|
|
|
%! @sp 2
|
|
|
|
%! @end deftypefn
|
|
|
|
%@eod:
|
|
|
|
|
2017-05-18 18:36:38 +02:00
|
|
|
% Copyright (C) 2011-2017 Dynare Team
|
2011-12-12 19:05:25 +01:00
|
|
|
%
|
|
|
|
% 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/>.
|
|
|
|
|
2012-10-31 17:03:49 +01:00
|
|
|
% Original author: stephane DOT adjemian AT univ DASH lemans DOT fr
|
|
|
|
|
2011-12-12 19:05:25 +01:00
|
|
|
b = sqrt([1:n-1]/2);
|
|
|
|
JacobiMatrix = diag(b,1)+diag(b,-1);
|
|
|
|
[JacobiEigenVectors,JacobiEigenValues] = eig(JacobiMatrix);
|
|
|
|
[nodes,idx] = sort(diag(JacobiEigenValues));
|
|
|
|
JacobiEigenVector = JacobiEigenVectors(1,:);
|
|
|
|
JacobiEigenVector = transpose(JacobiEigenVector(idx));
|
|
|
|
weights = JacobiEigenVector.^2;
|
2011-12-16 15:14:59 +01:00
|
|
|
nodes = sqrt(2)*nodes;
|
|
|
|
|
|
|
|
%@test:1
|
|
|
|
%$ n = 5;
|
|
|
|
%$ [nodes,weights] = gauss_hermite_weights_and_nodes(n);
|
|
|
|
%$
|
|
|
|
%$ sum_of_weights = sum(weights);
|
|
|
|
%$
|
|
|
|
%$ % Expected nodes (taken from Judd (1998, table 7.4).
|
2017-05-16 15:10:20 +02:00
|
|
|
%$ enodes = [-2.020182870; -0.9585724646; 0; 0.9585724646; 2.020182870];
|
2011-12-16 15:14:59 +01:00
|
|
|
%$
|
|
|
|
%$ % Check the results.
|
2014-11-08 09:28:53 +01:00
|
|
|
%$ t(1) = dassert(1.0,sum_of_weights,1e-12);
|
|
|
|
%$ t(2) = dassert(enodes,nodes/sqrt(2),1e-8);
|
2011-12-16 15:14:59 +01:00
|
|
|
%$ T = all(t);
|
2011-12-16 16:02:32 +01:00
|
|
|
%@eof:1
|
|
|
|
|
|
|
|
%@test:2
|
|
|
|
%$ n = 9;
|
|
|
|
%$ [nodes,weights] = gauss_hermite_weights_and_nodes(n);
|
|
|
|
%$
|
|
|
|
%$ sum_of_weights = sum(weights);
|
2017-05-16 15:10:20 +02:00
|
|
|
%$ expectation = sum(weights.*nodes);
|
2011-12-16 16:02:32 +01:00
|
|
|
%$ variance = sum(weights.*(nodes.^2));
|
|
|
|
%$
|
|
|
|
%$ % Check the results.
|
2014-11-08 09:28:53 +01:00
|
|
|
%$ t(1) = dassert(1.0,sum_of_weights,1e-12);
|
|
|
|
%$ t(2) = dassert(1.0,variance,1e-12);
|
|
|
|
%$ t(3) = dassert(0.0,expectation,1e-12);
|
2011-12-16 16:02:32 +01:00
|
|
|
%$ T = all(t);
|
|
|
|
%@eof:2
|
|
|
|
|
|
|
|
%@test:3
|
|
|
|
%$ n = 9;
|
|
|
|
%$ [nodes,weights] = gauss_hermite_weights_and_nodes(n);
|
|
|
|
%$
|
|
|
|
%$ NODES = cartesian_product_of_sets(nodes,nodes);
|
|
|
|
%$ WEIGHTS = cartesian_product_of_sets(weights,weights);
|
|
|
|
%$ WEIGHTS = prod(WEIGHTS,2);
|
|
|
|
%$
|
|
|
|
%$ sum_of_weights = sum(WEIGHTS);
|
|
|
|
%$ expectation = transpose(WEIGHTS)*NODES;
|
|
|
|
%$ variance = transpose(WEIGHTS)*NODES.^2;
|
|
|
|
%$
|
|
|
|
%$ % Check the results.
|
2014-11-08 09:28:53 +01:00
|
|
|
%$ t(1) = dassert(1.0,sum_of_weights,1e-12);
|
|
|
|
%$ t(2) = dassert(ones(1,2),variance,1e-12);
|
|
|
|
%$ t(3) = dassert(zeros(1,2),expectation,1e-12);
|
2011-12-16 16:02:32 +01:00
|
|
|
%$ T = all(t);
|
2011-12-19 15:54:30 +01:00
|
|
|
%@eof:3
|
|
|
|
|
|
|
|
%@test:4
|
|
|
|
%$ n = 9; sigma = .1;
|
|
|
|
%$ [nodes,weights] = gauss_hermite_weights_and_nodes(n);
|
|
|
|
%$
|
|
|
|
%$ sum_of_weights = sum(weights);
|
2017-05-16 15:10:20 +02:00
|
|
|
%$ expectation = sum(weights.*nodes*.1);
|
2011-12-19 15:54:30 +01:00
|
|
|
%$ variance = sum(weights.*((nodes*.1).^2));
|
|
|
|
%$
|
|
|
|
%$ % Check the results.
|
2014-11-08 09:28:53 +01:00
|
|
|
%$ t(1) = dassert(1.0,sum_of_weights,1e-12);
|
|
|
|
%$ t(2) = dassert(.01,variance,1e-12);
|
|
|
|
%$ t(3) = dassert(0.0,expectation,1e-12);
|
2011-12-19 15:54:30 +01:00
|
|
|
%$ T = all(t);
|
|
|
|
%@eof:4
|