function [prob,died]=physionet2012(tm,category,val)
% [prob,SAPS_SCORE,died]=physionet2012(tm,category,val)
%
% Sample Submission for the PhysioNet 2012 Challenge. Variables our:
%
% tm - (Nx1 Cell Array) Cell array containing time of measurement
% category- (Nx1 Cell Array) Cell array containing type (category)
% measurement
% value - (Nx1 Cell Array) Cell array containing value of measurement
%
%
% prob - (Scalar) Probability value of the patient dying in the hospital.
% died - (Logical)Binary classification if the patient is going to die
% on the hospital (1 - Died, 0 - Survived)
%
% Example:
% [prob,died]=physionet2012(tm,category,val)
%
% Written by Ikaro Silva, 2012
%
% Version 1.0
%
%The sample submission calculates the in hospital death
%probability based on Bayesian Rule conditioned on SAPS_SCORE
%and PDF fitting based on training Set A.
%
% P(dying | SAPS_SCORE) = P(dying)*P(SAPS_SCORE|Dying)/
% (P(dying)*P(SAPS_SCORE|dying) + P(living)*P(SAPS_SCORE|living))
%
%Calculate likelihood, P(SAPS_SCORE|Dying), based polynomial fitting of the CDF
%of the training data A conditioned on those that died (values of the fit
%are hardcoded below)
%
% saps_died=SAPS_SCORE(ihd==1); %ihd is logical vector where 1 = in hospital death
% MX_SAPS=4*14;
% [Ndied,xx]=hist(saps_died,[0:MX_SAPS]);
% md=nanmean(saps_died);
% st_d=nanstd(saps_died)
% pdf_died=normpdf(xx,md,st_d);
% plot(xx,Ndied./sum(Ndied));hold on;grid on;plot(xx,pdf_died,'r') %Check fit
%
% Repeat to get conditional probability on those that lived using Extreme
% Value Distribution
% saps_alive=SAPS_SCORE(ihd==0); %ihd is logical vector where 1 = in hospital death
% [Nalive,xx]=hist(saps_alive,[0:MX_SAPS]);
% parmhat = evfit(saps_alive(~isnan(saps_alive)));
% pdf_alive=evpdf(xx,parmhat(1),parmhat(2));
% figure
% %This fitting is not very good, but for the purpose of an example will do
% plot(xx,Nalive./sum(Nalive));hold on;grid on;plot(xx,pdf_alive,'r')
TH=0.2038; %Threshold for classifying the patient as non-survivor in the hospital
%Determined through maximization of the min(PPV,Sensitivity) on
%training Set A
%Calculate SAPS SCORE
truncated=1; %Attempt to calculate SAPS even if some variables are missing
[SAPS_SCORE]=saps_score(tm,category,val,truncated);
%Hard-coded values based on the analysis commented above
md=017.644699140401148;
st_d=4.677514903313259;
likelihood_saps_dying=normpdf(SAPS_SCORE,md,st_d);
parmhat =[15.927414293093566 4.930958270510013];
likelihood_saps_living=evpdf(SAPS_SCORE,parmhat(1),parmhat(2));
pdf_dying= 0.1385;%Based on set A
%Calcuate the Posteriori Probability given the SAPS_SCORE
posteriori_num= (pdf_dying*likelihood_saps_dying);
posteriori_den= (pdf_dying*likelihood_saps_dying + (1-pdf_dying)*likelihood_saps_living);
prob=posteriori_num/posteriori_den;
died=prob>=TH;