## Copyright (C) 2012 Alexander Klein
## 
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## plot_deviation_from_uniformcdf

## Author: Alexander Klein <alexander.klein@math.uni-giessen.de>
## Created: 2012-08-20

function plot_deviation_from_uniform_cdf ( p, n )

	sp = sort ( p );

	logm = logspace ( -n, -1, n ); 
	ticks = [ logm, 0.5, 1 - fliplr( logm ) ];

	logg = [ logspace( -n, -1, 3 * n ), logspace( -1, log10 ( 0.5 ), 10 ) ];
	limits = [ logg, 0.5, 1 - fliplr( logg ) ];
	at = atanh ( 2 * limits - 1 );

	l = length ( limits );

	P_emp = zeros ( 1, l );

	for k = 1 : l

		P_emp ( k ) = lookup ( sp, limits ( k ) );
	end

	P_emp /= length ( p );

	subplot ( 2, 1, 1 );

	plot ( at, P_emp - limits );

	xlim ( [ at( 1 ), at( end ) ] );
	set ( gca(), "xtick", atanh ( 2 * ticks - 1 ) );
	formstr = ( strcat ( "%1.", num2str ( n ), "g" ) );
	set ( gca(), "xticklabel", cellstr(num2str(ticks', formstr ) ) );
	xlabel ( "Probability" );
	ylabel ( "Absolute Deviation" );
	set ( get ( gca (), "ylabel" ), "rotation", 90 );


	subplot ( 2, 1, 2 );

	plot ( at, P_emp ./ limits );

	xlim ( [ at( 1 ), at( end ) ] );
	set ( gca(), "xtick", atanh ( 2 * ticks - 1 ) );
	formstr = ( strcat ( "%1.", num2str ( n ), "g" ) );
	set ( gca (), "xticklabel", cellstr(num2str(ticks', formstr ) ) );
	xlabel ( "Probability" );
	ylabel ( "Relative Deviation" );
	set ( get ( gca, "ylabel" ), "rotation", 90 );

endfunction
