function Prob = ProbAllConstraintsSatisfied(this, X)
% The probability that ALL the constraints are satisfied at
% each row of X. Constraint without models are never satisfied.
%

%   Copyright 2016-2022 The MathWorks, Inc.

N = size(X,1);
% Coupled constraints
if this.PrivOptions.NumCoupledConstraints == 0
    % There are no constraints at all.
    ProbSat = 1;
else
    for i = numel(this.ConstraintModels):-1:1
        if isempty(this.ConstraintModels{i})
            % There is a constraint but it has no model yet.
            ProbSat(1:N,i) = 0;
        else
            [means, ysds] = predict(this.ConstraintModels{i}, X);
            fsds = bayesoptim.funStd(ysds, this.ConstraintModels{i}.Sigma);
            ProbSat(:,i) = normcdf(0, means, fsds);
        end
    end
end
% Error constraint
if isempty(this.ErrorModel)
    % There is no model of error yet.
    ProbErrorSat(1:N,1) = 1;
else
    [means, ysds] = predict(this.ErrorModel, X);
    fsds = bayesoptim.funStd(ysds, this.ErrorModel.Sigma);
    ProbErrorSat(:,1) = normcdf(0, means, fsds);
end
% XConstraint
IsXConstraintSat = satisfiesXConstraint(this, X);

% Calculate prob
Prob = prod(ProbSat, 2) .* ProbErrorSat .* IsXConstraintSat;
end
