function retRes = GEBCauchy(inX,inSD,inPatt)
%function retRes = GEBCauchy(inX,inSD,inPatt)
% Constructing Generalized Shrinkage/Thresholding Esitimation with Empirical Bayes.
% Only output posterior medians.
%
% Model: Y = beta + Epsilon, Epsilon ~ N(0, sigmae);
%        Y -- observed value;
%        beta -- parameter of interest;
%        Epsilon -- random error;
%
% Prior: beta ~ (1-wp-wn)*delta_0 + wp*N+(0, \theta+^{-1}-1) + wn*N(0, \theta-^{-1}-1);
%        \theta+ ~ Beta(0.5,1);
%        \theta- ~ Beta(0.5,1);
%
% Note:
%       inPatt=[-1,1,1] implies that wp, wn \in [0,1];
%       inPatt=[1,1,1] implies that wp=wn \in [0,0.5];
%       inPatt=[1,0,1] implies that wp+wn=1, i.e., wp=wn=0.5;
%       inPatt=[0,1,1] implies that wn=0, wp \in [0,2/3];
%       inPatt=[-1,1,0] implies that wp=0, wn \in [0, 2/3];
%
% Dabao Zhang     July 1, 2006
%
% Revised by:
%   Dabao Zhang, August 16, 2007 -- Reset too small values.
%
% Copyright (c) 2006 by Zhang & Zhang.


%tic;

%----------------------------------------------------------------
% Default settings.

% Calculate the sampling standard deviation
if( nargin<2 ) inSD = median(abs(inX))/norminv(0.75,0,1); end
if( isempty(inSD) ) inSD = median(abs(inX))/norminv(0.75,0,1); end

if( nargin<3 ) inPatt = [-1,1,1]; end
% End of settings.
%----------------------------------------------------------------

%----------------------------------------------------------------
% Standardize the data
x = inX/inSD;   x(abs(x)<1e-6) = 1e-6;
p = length(x);

retRes.sd = inSD;   % standard deviation of inX
retRes.mu = inX;    % estimate of the mean of inX
retRes.priorp = [0; 0];  % prior probabilities (wn,wp)
retRes.postp = zeros(p,2);  % posterior probabilities (P(inX(j)<0|...),P(inX(j)>0|...)).

%disp('Calculate Generalized Empirical Bayes Thresholding Estimate...');

%----------------------------------------------------------------
% Empirical Bayes Shrinkage Estimate

% Calculate the marginal MLE for wn, wp
retRes.priorp = GEBCwMLE(x,inPatt);

% Estimate the posterior weight
retRes.postp = GEBCPostProbs(x,retRes.priorp(1),retRes.priorp(2));

% Estimate the posterior medians
retRes.mu = GEBCPostMeds(x, retRes.postp(:,1),retRes.postp(:,2))*inSD;

%disp('Congratulations! The job is done!');

    
%toc;
