%   [x,z]=ExcProbGRT(opt,xd,xu,dx,y,Mmin,lamb,eps,b,Mmax)
%
%EVALUATES THE EXCEEDANCE PROBABILITY VALUES USING THE UPPER-BOUNDED G-R 
%   LED MAGNITUDE DISTRIBUTION MODEL.
%
% AUTHOR: S. Lasocki 06/2014 within IS-EPOS project.
%
% DESCRIPTION: The assumption on the upper-bounded Gutenberg-Richter 
% relation leads to the upper truncated exponential distribution to model 
% magnitude distribution from and above the catalog completness level 
% Mmin. The shape parameter of this distribution, consequently the G-R
% b-value and the end-point of the distriobution Mmax as well as the
% activity rate of M>=Mmin events are calculated at start-up of the 
% stationary hazard assessment services in the upper-bounded 
% Gutenberg-Richter estimation mode.
% 
% The exceedance probability of magnitude M' in the time period of 
% length T' is the probability of an earthquake of magnitude M' or greater 
% to occur in T'. Depending on the value of the parameter opt the 
% exceedance probability values are calculated for a fixed time period T'
% and different magnitude values or for a fixed magnitude M' and different
% time period length values. In either case the independent variable vector
% starts from xd, up to xu with step dx. In either case the result is 
% returned in the vector z.
%
%INPUT:
%   opt - determines the mode of calculations. opt=0 - fixed time period
%       length (y), different magnitude values (x), opt=1 - fixed magnitude 
%       (y), different time period lengths (x)
%   xd - starting value of the changeable independent variable 
%   xu - ending value of the changeable independent variable
%   dx - step change of the changeable independent variable
%   y - fixed independent variable value: time period length T' if opt=0, 
%        magnitude M' if opt=1
%   Mmin - lower bound of the distribution - catalog completeness level
%   lamb - mean activity rate for events M>=Mmin
%   eps - length of the round-off interval of magnitudes.
%   b - Gutenberg-Richter b-value
%   Mmax - upper limit of magnitude distribution


%OUTPUT:
%   x - vector of changeable independent variable: magnitudes if opt=0, 
%       time period lengths if opt=1, 
%       x=(xd:dx:xu)
%   z - vector of exceedance probability values of the same length as x
%
% LICENSE
%     This file is a part of the IS-EPOS e-PLATFORM.
%
%     This 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.
% 
%     This program 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.
% 

function [x,z]=ExcProbGRT(opt,xd,xu,dx,y,Mmin,lamb,eps,b,Mmax)


% -------------- VALIDATION RULES -------------  K_21NOV2016
 if dx<=0;error('Step must be greater than 0');end
%----------------------------------------------------------


beta=b*log(10);
if opt==0
    if xd<Mmin; xd=Mmin;end
    if xu>Mmax; xu=Mmax;end
end
x=(xd:dx:xu)';
if opt==0
    z=1-exp(-lamb*y.*(1-Cdfgr(x,beta,Mmin-eps/2,Mmax)));
else
    z=1-exp(-lamb*(1-Cdfgr(y,beta,Mmin-eps/2,Mmax)).*x);
end

end

function [y]=Cdfgr(t,beta,Mmin,Mmax)

%CDF of the truncated upper-bounded exponential distribution (truncated G-R
% model
% Mmin - catalog completeness level
% Mmax - upper limit of the distribution
% beta - the distribution parameter
% t - vector of magnitudes (independent variable)
% y - CDF vector

mian=(1-exp(-beta*(Mmax-Mmin)));
y=(1-exp(-beta*(t-Mmin)))/mian;
idx=find(y>1);
y(idx)=ones(size(idx));
end

