function b = admissible(o, d)

% Return true iff d is an admissible draw in a distribution characterized by o.
%
% INPUTS
% - o      [dprior]   Distribution specification for a n×1 vector of independent continuous random variables
% - d      [double]   n×1 vector.
%
% OUTPUTS
% - b      [logical]  scalar.
%
% REMARKS
% None.
%
% EXAMPLE
%
% >> Prior = dprior();
% >> Prior.add('alpha', 'shape', 'beta', 'mean', 0.5, 'std', 0.1);
% >> d = Prior.draw();
% >> Prior.admissible(d)
%    ans =
%
%   logical
%
%   1

% Copyright © 2023-2024 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 <https://www.gnu.org/licenses/>.

b = false;

if ~isequal(length(d), length(o.lb))
    return
end

if all(d>=o.lb & d<=o.ub)
    b = true;
end

return % --*-- Unit tests --*--

%@test:1
% Build a 14-parameter prior (2 of each distribution family) with add(),
% then verify that every draw is admissible.
prior_trunc = 1e-10;
Prior = dprior();
for rep = 1:2
    Prior.add(sprintf('p%d_beta',      rep), 'shape', 'beta',       'mean', 0.4, 'std', 0.2, 'prior_trunc', prior_trunc);
    Prior.add(sprintf('p%d_gamma',     rep), 'shape', 'gamma',      'mean', 0.4, 'std', 0.2, 'prior_trunc', prior_trunc);
    Prior.add(sprintf('p%d_normal',    rep), 'shape', 'normal',     'mean', 0.4, 'std', 0.2, 'prior_trunc', prior_trunc);
    Prior.add(sprintf('p%d_invgamma1', rep), 'shape', 'inv_gamma',  'mean', 0.4, 'std', 0.2, 'prior_trunc', prior_trunc);
    Prior.add(sprintf('p%d_uniform',   rep), 'shape', 'uniform',    'lower_bound', 0, 'upper_bound', 1, 'prior_trunc', prior_trunc);
    Prior.add(sprintf('p%d_invgamma2', rep), 'shape', 'inv_gamma2', 'mean', 0.4, 'std', 0.2, 'prior_trunc', prior_trunc);
    Prior.add(sprintf('p%d_weibull',   rep), 'shape', 'weibull',    'mean', 0.4, 'std', 0.2, 'prior_trunc', prior_trunc);
end

ndraws = 10;

try
    for i = 1:ndraws
        draw = Prior.draw();
        if ~Prior.admissible(draw)
            error('problem in admissible')
        end
    end
    t(1) = true;
catch
    t(1) = false;
end

T = all(t);

%@eof:1
