function [Fscore, Precision, Recall] = validate(ypred,yval)

if size(yval)~=size(ypred)
    error('yval and ypred must be the same size');
end

Fscore = zeros(size(yval,1),1);
Precision = zeros(size(yval,1),1);
Recall = zeros(size(yval,1),1);
for i=1:size(yval,1)
    Precision(i) = sum((yval(i,:).*ypred(i,:))>0.5)/sum(ypred(i,:));
    Recall(i) = sum((yval(i,:).*ypred(i,:))>0.5)/sum(yval(i,:));
    Fscore(i) = 2*(Precision(i)*Recall(i))/(Precision(i)+Recall(i));
    if isnan(Precision(i))
        Precision(i) = 0;
    end
    if isnan(Recall(i))
        Recall(i) = 0;
    end
    if isnan(Fscore(i))
        Fscore(i) = 0;
    end
end

Precision = mean(Precision);
Recall = mean(Recall);
Fscore = mean(Fscore);

fprintf('Precision: %.3f, Recall: %.3f, F-score: %.3f\n',...
    mean(Precision), mean(Recall), mean(Fscore));

end

