function set_defaults_and_backcompute(o, shape, prior_trunc)

% When the user provides (p6, p7) directly, sets default bounds,
% computes p1/p2 (mean/std) from the hyperparameters, computes p5 (mode),
% and sets lb/ub and id* vectors.

% Copyright © 2026 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/>.

switch shape
  case 1 % Beta
    if isnan(o.p3), o.p3 = 0; end
    if isnan(o.p4), o.p4 = 1; end
    o.p1 = o.p3 + (o.p4 - o.p3) * o.p6 / (o.p6 + o.p7);
    o.p2 = (o.p4 - o.p3) * sqrt(o.p6 * o.p7 / ((o.p6 + o.p7)^2 * (o.p6 + o.p7 + 1)));
    o.p5 = compute_prior_mode([o.p6, o.p7, o.p3, o.p4], 1);
  case 2 % Gamma
    if isnan(o.p3), o.p3 = 0; end
    if isnan(o.p4), o.p4 = Inf; end
    o.p1 = o.p3 + o.p6 * o.p7;
    o.p2 = sqrt(o.p6) * o.p7;
    o.p5 = compute_prior_mode([o.p6, o.p7, o.p3], 2);
  case 3 % Normal
    if isnan(o.p3), o.p3 = -Inf; end
    if isnan(o.p4), o.p4 = Inf; end
    o.p1 = o.p6;
    o.p2 = o.p7;
    o.p5 = o.p1;
  case 4 % Inverse Gamma (type 1)
    if isnan(o.p3), o.p3 = 0; end
    if isnan(o.p4), o.p4 = Inf; end
    o.p1 = NaN;
    o.p2 = NaN;
    o.p5 = compute_prior_mode([o.p6, o.p7, o.p3], 4);
  case 5 % Uniform
    if isnan(o.p3), o.p3 = o.p6; end
    if isnan(o.p4), o.p4 = o.p7; end
    o.p1 = (o.p6 + o.p7) / 2;
    o.p2 = (o.p7 - o.p6) / (2 * sqrt(3));
    o.p5 = NaN;
  case 6 % Inverse Gamma (type 2)
    if isnan(o.p3), o.p3 = 0; end
    if isnan(o.p4), o.p4 = Inf; end
    o.p1 = NaN;
    o.p2 = NaN;
    o.p5 = compute_prior_mode([o.p6, o.p7, o.p3], 6);
  case 8 % Weibull
    if isnan(o.p3), o.p3 = 0; end
    if isnan(o.p4), o.p4 = Inf; end
    o.p1 = NaN;
    o.p2 = NaN;
    o.p5 = compute_prior_mode([o.p6, o.p7, o.p3], 8);
end
if length(o.p5) > 1
    o.p5 = o.p5(1);
end
o.p11 = NaN;

tmpstruct.pshape = shape;
tmpstruct.p3 = o.p3;
tmpstruct.p4 = o.p4;
tmpstruct.p6 = o.p6;
tmpstruct.p7 = o.p7;
bounds = prior_bounds(tmpstruct, prior_trunc);
o.lb = bounds.lb;
o.ub = bounds.ub;

o.idbeta      = find(shape == 1);
o.idgamma     = find(shape == 2);
o.idgaussian  = find(shape == 3);
o.idinvgamma1 = find(shape == 4);
o.iduniform   = find(shape == 5);
o.idinvgamma2 = find(shape == 6);
o.idweibull   = find(shape == 8);
