function [D,t,df,pvalue,CI]=Stud2sample(X,Y,alpha)
% Student't 2-sample equal variance t test.
% [D,t,df,pvalue,CI]=Stud2sample(X,Y,alpha)
% Input: X,Y two samples
%        alpha level for CI, optional, 95% if not specified
% Output: D=mean(X)-mean(Y);
%         t=Student's t statistic
%         df=degrees of freedom for t statistic
%         p value, 2-sided, for t Statisitc, with df degrees of freedom
%         CI for 1-alpha CI, [L U];95% CI if alpha not specified.
% Based on Theorem 9.2.2, page 487 Larsen & Marx
% uses pt.m & qt.m from Anders Holtsberg's stixbox toolbox
% Written by E. Gallagher;
% see also stud1sample, binom2sample,
X=X(:);Y=Y(:);
n=length(X);
m=length(Y);
D=mean(X)-mean(Y);
% Estimated pooled standard deviation (Theorem 9.2.1)
Sp=sqrt(    ((n-1)*var(X)+(m-1)*var(Y))...
                /  (n+m-2) );
t=D/(Sp*sqrt(1/n+1/m));
% Use Matlab's tcdf for p values
df=n+m-2;
if t>=0
    pvalue = 2*(1-tcdf(t,df));
else
    pvalue=2*tcdf(t,df);
end
if nargin<3;
    alpha=0.05;
end
% Use tinv.m from Matlab's statistics toolbox
HalfCI=tinv(1-alpha/2,df)*Sp*sqrt(1/n+1/m);
CI=[D-HalfCI D+HalfCI];