% **************************************************************
% * MATLAB FRAMEWORK CAP DISCREPANCY:                          *
% *                                                            *
% *   AUTHOR    : Holger Heitsch                               *
% *   INSTITUTE : WIAS Berlin                                  *
% *   (C)       : 2020                                         *
% *                                                            *
% **************************************************************

% **************************************************************
%
%   EMPIRICAL MEASURE ON SPHERICAL CAP
%
% **************************************************************

% **************************************************************
%
%   Computing the empirical measure on a spherical cap
%
%   Input: X - array n x N of spherical points
%              (n dimension, N number of samples)
%          w - normal direction of hyperplane
%          t - shift of hyperplane in [-1,1]
%
%   Return:   emp - empirical measure
%           c_emp - complementary empirical measure
% 
% **************************************************************


function [emp, c_emp] = emp_measure (X, w, t)

    % set computational accuracy
    tolerance = 1e-12;
    
    % number of samples
    N = size (X,2);

    % initialize
    e   = 0;
    c_e = 0;

    for (i=1:N)
        % counting cap points
        z = X(:,i)' * w;

        if (z >= t - tolerance) e   = e   + 1; end
        if (z <= t + tolerance) c_e = c_e + 1; end

    end
    
    % return values
    emp   = e / N;
    c_emp = c_e / N;

end

