%takes prediction list of the form [data(i) pred(i)], starting initpoint+1
%plotoption - set to 'p' if want to plot histogram and qq plot
%this function finds the and returns the standard deviation of the 
%differences between predictions and data with 5% truncated at both sides of the tail
function out = normalstdev(pdlist,plotoption)

binnum = ceil(length(pdlist(:,1))*.1);
diff = pdlist(:,1)-pdlist(:,2);
totdev = std(diff);
[diff_sort,index] = sort(diff);
tail_size = floor(length(diff_sort)*.05);
if tail_size ~= 0
    tream_index = index(tail_size:length(diff)-tail_size);
sort(tream_index)
    diff = diff(sort(tream_index))
end;
stdev = std(diff);
if plotoption == 'p'
    figure;
    subplot(2,2,1);
    histfit(diff,binnum);  %plot histogram
    hold on;
    x = -5*10^(-3):5*10^(-3)/50:5*10^(-3);
    y = normpdf(x,0,stdev);
    plot(x,y,'black');
    hold off;
    xlabel('differences');
    %legend('\sigma_{diff}=',num2str(stdev));
    title('Histogram of the Differences');
    subplot(2,2,2);
    normplot(diff); %plot qq plot
normfit(diff)
    subplot(2,2,[3,4]);
    plot(pdlist(:,1)-pdlist(:,2));
    ylabel('differences data-prediction');
    xlabel('day number');
    
end;
out = 3 * stdev;