function val = FitGPRightTail(beta,a1,a2,Quant_1,Quant_2,f_1,f_2) %,rnd_exceed,strike_exceed)

    %... (beta,a1,a2,Quant_1,Quant_2,f_1,f_2)
% 1-values correspond to first match point (f.ex. 0.05), 2-values correpsond to 2nd point (f.ex. 0.02)
phi = beta(1);
sigma = beta(2);

multiplicator = 1-a1;

% only probability matching
%val = (multiplicator*gppdf(Quant_1,phi,sigma,Quant_1) - f_1)^2 + ...
%    (multiplicator*gppdf(Quant_2,phi,sigma,Quant_1) - f_2)^2; 


val = (gppdf(Quant_1,phi,sigma,Quant_1) - f_1)^2 + ...
    (gppdf(Quant_2,phi,sigma,Quant_1) - f_2)^2; 

% probability and cdf matching of second point
% val = (multiplicator*gppdf(Quant_2,phi,sigma,Quant_1) - f_2)^2 + ...
%     (gpcdf(Quant_2,phi,sigma,Quant_1) - (a2-a1)/multiplicator)^2;

% use all empirical RND points and minimize distance
% vec = nan(length(rnd_exceed),1);
% for i = 1:length(rnd_exceed)
%     vec(i) = (0.05*gppdf(strike_exceed(i),phi,sigma,Quant_1) - rnd_exceed(i))^2;
% end
% val = sum(vec);
    
if sigma < 0 | isreal(val) == 0
    val = 1e100;
end

