%   [m, PDF_GRU, CDF_GRU]=dist_GRU(Md,Mu,dM,Mmin,eps,b)
%
% EVALUATES THE DENSITY AND CUMULATIVE DISTRIBUTION FUNCTIONS OF MAGNITUDE 
%   UNDER THE UNLIMITED G-R LED MAGNITUDE DISTRIBUTION MODEL. 
%
% AUTHOR: S. Lasocki 06/2014 within IS-EPOS project.
%
% DESCRIPTION: The assumption on the unlimited Gutenberg-Richter relation 
% leads to the exponential distribution model of magnitude distribution 
% from and above the catalog completness level Mmin. The shape parameter of 
% this distribution and consequently the G-R b-value are calculated at 
% start-up of the stationary hazard assessment services in the
% unlimited Gutenberg-Richter estimation mode.
%
% The distribution function values are calculated for magnitude starting 
% from Md up to Mu with step dM.
% 
%INPUT:
%   Md - starting magnitude for distribution functions calculations
%   Mu - ending magnitude for distribution functions calculations
%   dM - magnitude step for distribution functions calculations
%   Mmin - lower bound of the distribution - catalog completeness level
%   eps - length of the round-off interval of magnitudes.
%   b - Gutenberg-Richter b-value
%
%OUTPUT:
%   m - vector of the independent variable (magnitude) m=(Md:dM:Mu)
%   PDF_GRT - PDF vector of the same length as m
%   CDF_GRT - CDF vector of the same length as m
%
%
% 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 [m, PDF_GRU, CDF_GRU]=dist_GRU_CI(Md,Mu,dM,Mmin,eps,b,bCI)

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

m=(Md:dM:Mu)';
beta=b*log(10);beta_low=bCI(1)*log(10);beta_high=bCI(2)*log(10);
PDF_GRU=beta*exp(-beta*(m-Mmin+eps/2));
CDF_GRU=1-exp(-beta*(m-Mmin+eps/2));
idx=find(CDF_GRU<0);
PDF_GRU(idx)=zeros(size(idx));CDF_GRU(idx)=zeros(size(idx));
idx=find(CDF_GRU>1);
PDF_GRU(idx)=zeros(size(idx));CDF_GRU(idx)=ones(size(idx));

CDF_low=1-exp(-beta_low*(m-Mmin+eps/2));
CDF_high=1-exp(-beta_high*(m-Mmin+eps/2));

CDF_GRU=[CDF_GRU CDF_low CDF_high];

end

