% Asymptotic Anderson-Darling Test for IG Distribution
%
%
% GOAL : Program runs Anderson-Darling Test on M random samples of N rvs, devised by Reilly and Rueda
% (See "Goodness of Fit for Inv Gaussian Distbtn" by Federico J. O'Reilly and Raul Rueda
% The Canadian Journal of Statistics, vol. 20, no. 4, pp. 387-397, 1992)
%
% INPUT:
% 1) Anderson Darling Critical Value as a function of Theta for given Alpha Level Test
% Alpha values of 1%, 5%, and 10% can be specified by the user, along with necesary input files.
% 2) N random samples of M rvs each (read in from file or generated)
%
% FLOW:
% 1) INITIALIZE BASIC PARAMS & ARRAYS
% a) Read in Table of Shape Values (log base2 of Theta = Lambda/Mu)
% b) Read in Critical Values for Chosen Value of Alpha to be used in hypothesis test
% c) Read in Data File containing M random samples (M) of rvs in each sample (N) to be tested
% NOTICE THAT INPUT DATA MUST BE STRUCTURED SO IT IS AN MXN MATRIX
% 2) RUN AD Test
% a) Compute Mu,Lambda,and Theta (Shape Factor) for each Sample
% b) Compute AD Test Statistic
% c) Compute Critcal Value for given alpha level and compare to AD test statistic
% d) decide to accept H0 if rejection rate < alpha
% 3) Plot histogram of MLE for Theta from all random sample
%
% OUTPUT
% Percentage of times H0 rejected
%
% PROGRAM TEST
% This program can be tested by running the program using as the input file for the data to be tested
% a control file called "CONTROL_1000RanSamples_Mu1_Lambda3_20RVs.dat", which is a file of 1000 random samples
% each having 20 Ig RVs with Mu=1 and Lambda = 3.
% SHOULD OBTAIN REJECTION RATE OF 8.9%
%
% Written by M.T. Brenneman / Final Test on May 6th, 2009
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
clear all
close all
clc
%
global M N
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% STEP 1 : INITIALIZE BASIC PARAMS AND ARRAYS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% a) Read in Shape values from file
TableShape = csvread('ShapeVals.dat');
%
% b) SPECIFY ALPHA, then read in appropriate critical values from file
Alpha = 10; % Confidence Level of Test MUST BE SPECIFIED (Alpha = 1,5,10)
TableCV = Subr_InputCVs(Alpha); % Returns table of Critical Values for appropriate value of alpha
%
% c) Read in Data
G = csvread('UserSpecifiedInputFile.dat'); % Read data file
[M,N] = size(G); % Determine number of ransom samples (M) and number of rvs/sample (N)
NumRejects = 0; % Number of random samples for which H0 rejected a Alpha Confidence Level
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% STEP 2 : RUN AD TEST %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% a) COMPUTE MU, LAMBDA, AND SHAPE FACTOR FOR EACH RANDOM SAMPLE
[Mu,Lambda,Theta] = Subr_ComputeIgMLEParams(G);
%
% B) COMPUTE AD TEST STATISTIC FOR EACH RANDOM SAMPLE
[AD] = Subr_ComputeADTestStat(Mu,Lambda,G);
%
% C) TEST AD STAT AGAINST CRITICAL VALUE FOR GIVEN TYPE I ERROR CHOSEN
[CV] = Subr_ComputeCritVals(Theta,TableShape,TableCV,Alpha); % Compute Critical Values from Table Using Log Interp
for ii = 1:M
if AD(1,ii) > CV(1,ii)
NumRejects = NumRejects + 1;
end
end
%
% D) Decide to Reject or Accept H_0
Pvalue = 100*NumRejects/M;
if Pvalue > Alpha
disp(['Estimated Rejection rate = ' num2str(Pvalue) ' % > alpha level test at ' num2str(Alpha) ' %'])
disp('Reject H_0 that random sample is from Inv Gaussian distribution')
else
disp(['Estimated Rejection rate = ' num2str(Pvalue) ' % < alpha level test at ' num2str(Alpha) ' %'])
disp('Accept H_0 that random sample is from Inv Gaussian distribution')
end
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% STEP 3 : OUTPUT RESULTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% subplot(3,1,1)
% hist(Mu)
% title(['\mu values with mean = ' num2str(mean(Mu)) ])
% subplot(3,1,2)
% hist(Lambda)
% title(['\lambda values with mean = ' num2str(mean(Lambda))])
% subplot(3,1,3)
% hist(CV)
% title(['Distbtn of Critical Values Producing P value = ' num2str(100*EstPvalue) ' % for \alpha = ' num2str(Alpha) ' = %'])
% csvwrite('CV_10KRanSamp_16SideBands.dat',CV)