clear all;
load('results/v25_reservevariation_stats');
categories={'moments','pctls','freqs','freqs_range','pctls_values','pricestat','costs'};
% for i=1:size(tcpy,2)
% 	%eval(tcpy(i),'_res'))=moments;
%     tcpy{i}
%     [isstr(tcpy{i}) iscell(tcpy{i})]
% end;
reserve_moments=moments;
reserve_pctls=pctls;
reserve_freqs=freqs;
reserve_pctls_values=pctls_values;
reserve_freqs_range=freqs_range;
reserve_costs=costs;
yaxisp=(1:20)*0.005;
reserve_yaxisp=yaxisp;
reserve_xaxisp=xaxisp;

load('results/v25_defaultvalues_stats');
scenarios_moments=moments;
scenarios_pctls=pctls;
scenarios_freqs=freqs;
scenarios_pctls_values=pctls_values;
scenarios_freqs_range=freqs_range;
scenarios_costs=costs;


load('results/v25_privstorsubsidies_stats');
privstor_moments=moments;
privstor_pctls=pctls;
privstor_freqs=freqs;
privstor_pctls_values=pctls_values;
privstor_freqs_range=freqs_range;
privstor_costs=costs;
privstor_xaxisp=xaxisp;

figure;
contourf(reserve_yaxisp,reserve_xaxisp,reserve_pctls(:,:,7,11)');
contourf(reserve_yaxisp,reserve_xaxisp,reserve_costs');

figure;
contourf(reserve_yaxisp,reserve_xaxisp,reserve_moments(:,:,5,1)');
xlabel('Reserve capacity');%ylabel('Trigger Price');
c=colorbar;

for i=1:30
    value=(i)*0.01/100;
    [~, xindex(i,:)] = min(abs(reserve_costs(:,:) - value));
%     [~, xindex] = min(min(abs(reserve_costs(:,:) - i/100)));
%     yindex=yindex(xindex);
%     reserve_costs(yindex,xindex)
%     reserve_costs(7:11,7:11)*100
    for i2=1:21
        constantcostlineforp99(i2)=reserve_pctls(xindex(i,i2),i2,7,11);
        constantcostlineforp90(i2)=reserve_pctls(xindex(i,i2),i2,7,9);
    end;
    [p99forp99(i),yindexforp99(i)]=min(constantcostlineforp99);
    [p90forp90(i),yindexforp90(i)]=min(constantcostlineforp90);
    xindex2forp99(i)=xindex(i,yindexforp99(i));
    p90forp99(i)=reserve_pctls(xindex2forp99(i),yindexforp99(i),7,9);
    p999forp99(i)=reserve_pctls(xindex2forp99(i),yindexforp99(i),7,12);
    p75forp99(i)=reserve_pctls(xindex2forp99(i),yindexforp99(i),7,8);
    p50forp99(i)=reserve_pctls(xindex2forp99(i),yindexforp99(i),7,7);
    p10forp99(i)=reserve_pctls(xindex2forp99(i),yindexforp99(i),7,5);
    
    xindex2forp90(i)=xindex(i,yindexforp90(i));
    p99forp90(i)=reserve_pctls(xindex2forp90(i),yindexforp90(i),7,11);
    p999forp90(i)=reserve_pctls(xindex2forp90(i),yindexforp90(i),7,12);
    p75forp90(i)=reserve_pctls(xindex2forp90(i),yindexforp90(i),7,8);
    p50forp90(i)=reserve_pctls(xindex2forp90(i),yindexforp90(i),7,7);
    p10forp90(i)=reserve_pctls(xindex2forp90(i),yindexforp90(i),7,5);
    
    rv_costs(i)=value;
end;
% size(reserve_pctls(:,:,7,11)')
% size(reserve_pctls(:,xindex(i,:),7,11)')
% [t1,t2]=min(reserve_pctls(:,xindex(20,:),7,11)')


% figure;plot(privstor_xaxisp,costs)
figure;
plot(100*privstor_costs,privstor_pctls(1,:,7,12),'b');
hold on;
plot(100*privstor_costs,privstor_pctls(1,:,7,11),'g');
plot(100*privstor_costs,privstor_pctls(1,:,7,9),'r');
% plot(xaxisp,pctls(1,:,7,7),'m');
plot(100*privstor_costs,privstor_pctls(1,:,7,8),'k');
plot(100*privstor_costs,privstor_pctls(1,:,7,7),'c');
plot(100*privstor_costs,privstor_pctls(1,:,7,5),'m');

plot([0 rv_costs*100],[1.904 p999forp99],'b:');
plot([0 rv_costs*100],[1.572 p99forp99],'g:');
plot([0 rv_costs*100],[1.231 p90forp99],'r:');
plot([0 rv_costs*100],[1.071 p75forp99],'k:');
plot([0 rv_costs*100],[0.9577 p50forp99],'c:');
plot([0 rv_costs*100],[0.8667 p10forp99],'m:');


plot([0 rv_costs*100],[1.904 p999forp90],'b--');
plot([0 rv_costs*100],[1.572 p99forp90],'g--');
plot([0 rv_costs*100],[1.231 p90forp90],'r--');
plot([0 rv_costs*100],[1.071 p75forp90],'k--');
plot([0 rv_costs*100],[0.9577 p50forp90],'c--');
plot([0 rv_costs*100],[0.8667 p10forp90],'m--');
hold off;
ylabel('Price');xlabel('Costs (%)');

export_fig 'figures/comparison_reserve_subsidy' -pdf -nocrop -transparent

%%  Create minimal reserve figure


figure;
% plot(100*privstor_costs,privstor_pctls(1,:,7,12),'b');

plot(100*privstor_costs,privstor_pctls(1,:,7,11),'g');
hold on;
% plot(100*privstor_costs,privstor_pctls(1,:,7,9),'r');
% plot(xaxisp,pctls(1,:,7,7),'m');
plot(100*privstor_costs,privstor_pctls(1,:,7,8),'k');
% plot(100*privstor_costs,privstor_pctls(1,:,7,7),'c');
plot(100*privstor_costs,privstor_pctls(1,:,7,5),'m');

% plot([0 rv_costs*100],[1.904 p999forp99],'b:');
plot([0 rv_costs*100],[1.572 p99forp99],'g:');
% plot([0 rv_costs*100],[1.231 p90forp99],'r:');
plot([0 rv_costs*100],[1.071 p75forp99],'k:');
% plot([0 rv_costs*100],[0.9577 p50forp99],'c:');
plot([0 rv_costs*100],[0.8667 p10forp99],'m:');


% plot([0 rv_costs*100],[1.904 p999forp90],'b--');
% plot([0 rv_costs*100],[1.572 p99forp90],'g--');
% plot([0 rv_costs*100],[1.231 p90forp90],'r--');
% plot([0 rv_costs*100],[1.071 p75forp90],'k--');
% plot([0 rv_costs*100],[0.9577 p50forp90],'c--');
% plot([0 rv_costs*100],[0.8667 p10forp90],'m--');
hold off;
ylabel('Price (relative to avg. price)');xlabel('Fiscal costs (% of agricultural GDP)');

export_fig 'figures/comparison_reserve_subsidy_minimal' -pdf -nocrop -transparent
