%
% TruncNormKS Matlab file, v.1.0.2011
%
%  TruncNormKS solves a Kolmogorov-Smirnov goodness-of-fit test for a
%  truncated Gaussian distribution. The Kolmogorov-Smirnov test is modified 
%  for the special case in which data are tested to follow not a Gaussian 
%  distribution but a truncated one. The theoretical distribution function 
%  to compare with the experimental one is derived in the core of the
%  function. This function was used in Bouzas et al. [1] where N(t), a Cox
%  process, is analyzed under the assumption that its mean, E[N(t)], has a
%  truncated Gaussian distribution at any instant of time.
%
% Syntax: function [H,p]=TruncNormKS(Sample,Mu,sd,alpha)

%  Inputs:
%      Although the theoretical distribution is truncated Gaussian only its
%      mean and its standard deviation are required because the function
%      considers as the lower and the upper truncation points, the minimum
%      and maximum observed values, respectively.
%
%      Sp         - Sample data separated by one blank space or by commas.                   
%      Mu         - Mean of the truncated Gaussian distribution to be assumed 
%                   in the null hypothesis.
%      sd         - Standard deviation of the truncated Gaussian distribution
%                   to be assumed in the null hypothesis.
%      alpha      - Significance level (default = 0.05).
%
%  Output:
%      The function returns H=0 if the hypothetic distribution may be adopted 
%      at a significance level alpha, and returns H=1 otherwise, as usual in 
%      a goodnes-of-fit test. The function returns a second value p, it is 
%      the associated p-value.
%
%  Created by (1)P.R. Bouzas (paula@ugr.es) and (2)N. Ruiz-Fuentes (nfuentes@ujaen.es)
%
%  Department of Statistics and Operations Research
%  (1) Universidad de Granada. Campus Universitario de Cartuja. 18071, Granada. Spain
%  (2) Universidad de Jaen. Campus Universitario de Las Lagunillas. 23071, Jaen. Spain  
%
%  Version 1.0. November 2011.
%
%  To cite this file, this would be in an appropriate format:
%  Bouzas, P.R. and Ruiz-Fuentes, N. (2011). TruncNormKS Matlab file. Free access DIGIBUG
%
%  References:
%  [1] Bouzas, P.R., A.M. Aguilera and Valderrama, M.J., Forecasting a class of 
%              doubly stochastic Poisson process, Statistical Papers, 43, 
%              507--523, 2002.
function [H,p]=TruncNormKS(Sample,Mu,sd,alpha)
if nargin < 4, 
    alpha = 0.05;  %(default)
end 

if nargin < 3, 
    error('Requires at least three input arguments.'); 
end 
clc
X=[];
syms L s
    n = length(Sample);
    Y = sort(Sample); 
    A = Y(1); 
    B = Y(n);
    X = Y(1);N=[1];
for i=2:n
    if Y(i)~=Y(i-1)
        X=[X,Y(i)];       
        N=[N,1];
    else
        N=[N(1:length(N)-1),N(length(N))+1];
        X=[X(1:length(X)-1),Y(i)];
    end
end
F=inline(int(exp(-s^2/2),s,(A-Mu)/sd,(B-Mu)/sd)^-1*int(exp(-s^2/2),s,(A-Mu)/sd,(L-Mu)/sd));

S=cumsum(N)./sum(N);
CDF=[];
for i=1:length(X)
    CDF=[CDF;X(i),F(X(i))];
end
[H,p] = KSTEST(Y,CDF,alpha,0);
if H==0
fprintf('\n Data COME from the specified truncated Normal distribution. p-value: %d', p)
else
fprintf('\n Data DOES NOT COME from the specified truncated Normal distribution. p-value: %d', p)
end