function [alpha] = cronbach_alpha(X)
% CRONBACH_ALPHA Cronbach's Alpha
% [alpha] = cronbach_alpha(X) calculates Cronbach's alpha of the data
% set X.
%
% Cronbach's Alpha is a statistical coefficient that is assumed to measure internal consistency of test items
% or even reliability of a test (refer to "Classical Test Theory"). 
%
% X: data set
%    - X is a 2-dimensional matrix with at least 2 columns and 2
%      rows
%    - colums are items (of the test), rows are objects (evaluated by the test)
%    - X must not include missing values (NaN's) 
%
% alpha: Cronbach's alpha (refer to "Cronbach, Lee J. (1951): Coefficient alpha and the internal structure of
%    tests. Psychometrika 16(3), p. 297-334")
%
%
% Markus Brückl
% TU Berlin
%
% e-mail: makUs@brYkl.de
%
% September 20, 2008


N = size(X,2);                                       % number of items                                                
s2 = var(X,1);                                       % variance of items
objectsum = sum(X')';                                % sum of item-values per object
s2_tot = var(objectsum,1);                           % variance of object sums
alpha = (N/(N-1)) * (1 - ( sum(s2) / s2_tot ));      % Cronbach's Alpha