function value = FitGEVLeftTail(beta, a0, a1, Ka0, Ka1, fKa0, fKa1)
% function that fits left tail of distribution to generalizex extreme value
% distribution

% a0: higher value (0.05)
% a1: lower value (0.02)
% Ka0: quantile of higher value
% Ka1: quantile of smaller value
% fKa0: prob of higher value
% fKa1: probability of smaller value


Mean = beta(1);
sigma = beta(2);
phi = beta(3);

% before Feb 13:

% value = (CDF_GEV(Ka0,Mean,sigma,phi)-a0)^2 + ...
%     (PDF_GEV(Ka0,Mean,sigma,phi)-fKa0)^2 + ...
%     (PDF_GEV(Ka1,Mean,sigma,phi)-fKa1)^2;

%% Feb 13, attempt to adjust the optimization to friglewski paper and his 
% comment on adjusting GEV optimization to left hand

w1 = 1e3;
w2 = 1e7;
w3 = 1e7;

% value = w1*(CDF_GEV(-Ka0,Mean,sigma,phi) - (1-a0))^2 + ...
%     w2*(PDF_GEV(-Ka0,Mean,sigma,phi) - fKa0)^2 + ...
%     w3*(PDF_GEV(-Ka1,Mean,sigma,phi) - fKa1)^2;

value = w1*(gevcdf(-Ka0,phi,sigma,Mean) - (1-a0))^2 + ...
    w2*(gevpdf(-Ka0,phi,sigma,Mean) - fKa0)^2 + ...
    w3*(gevpdf(-Ka1,phi,sigma,Mean) - fKa1)^2;


if sigma < 0 | isreal(value) == 0
    value = 1e100;
end


