function [s, flag] = SignificanceTestByPermutation(root, feature, m, threshold)
% TEST whether a given feature should be selected as significant by 
% permutation test. 
% input: root -- the specified root for the network
%        feature -- the variable that is under testing
%        m   -- the number of permutation needs to be 
%       threshold -- the cutoff for determining the significance
% output:  s - the calculated significance value
%            s = (mi-aver(mi_permuted))/(std(mi_permuted)
%          flag _-  1  significant  s > threshold
%                   0   not significant  s < threshold

% % % Copyright (c) 2015, Mark Styczynski and Weiwei Yin
% % % All rights reserved.
% % % 
% % % Redistribution and use in source and binary forms, with or without
% % % modification, are permitted provided that the following conditions are
% % % met:
% % % 
% % %     * Redistributions of source code must retain the above copyright
% % %       notice, this list of conditions and the following disclaimer.
% % %     * Redistributions in binary form must reproduce the above copyright
% % %       notice, this list of conditions and the following disclaimer in
% % %       the documentation and/or other materials provided with the distribution
% % % 
% % % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% % % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% % % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% % % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% % % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% % % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% % % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% % % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% % % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% % % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% % % POSSIBILITY OF SUCH DAMAGE.



if nargin < 2
    display('not enough input data for picking significant test') ; 
elseif nargin < 4
    
    m = 300 ; 
    threshold = 2.6 ; 
end 

% initialization
s = 0 ; 
flag = 0 ; 

% check if the lengths of root and feature are the same
if length(root) ~= length(feature)
    
    display('not equal length for data permutation calculation') ; 
    return ; 
end 

true_mi = mi(root, feature) ; 

numOfobs = length(root) ; 

%calculate the permutated 
for ii = 1:m
    
    perm_mi(ii) = mi(root(randperm(numOfobs)), feature(randperm(numOfobs))) ; 
end 

s = (true_mi - mean(perm_mi))/(std(perm_mi)) ; 

if abs(s) >= threshold
    
    flag = 1 ; 
end 

return ; 

end 