# [pstricks] plotting a piecewise constant function (step function)

Leon Free leon.free at free.fr
Thu Oct 21 09:52:30 CEST 2010

Hi pstricks list !

Is there an **easy** way to make latex+pstrick plot a function similar to :
http://en.wikipedia.org/wiki/File:Binomial_distribution_cdf.svg ?

"easy" means : passing some suitable options to the \psplot or \dataplot
function

I want to plot the binomial (n,p) cumulative distribution function (cdf)
which is a right continuous function with jumps at each integer between
0 and n (or I want to plot any such function). I call this function
cdf(x) with x begin any real number.

I managed to get the graph I want for this function relying heavily on
FP commands provided by the fp.sty package. Here is my code :

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\FPeval{\n}{10}    % nb of replications
\FPeval{\p}{0.3}   % prob of succes
\FPeval{\q}{1-\p}  % prob of failure
\FPeval{\bc}{1}    % initiate the binomial coefficients
\FPeval{\N}{\n+1}  % size of the x axis
\psset{algebraic}
\begin{psgraph}[Dy=0.1](0,0)(\N,1.1){7cm}{5cm}
\FPeval{\cdf}{\q^\n}             % compute CDF(0) and initiate CDF
\psline[linecolor=red](-1,0)(0,0)    % draw 1rst step of CDF
\psline[linecolor=red](0,\cdf)(1,\cdf)

\multido{\I=0+1}{\n}{
\FPeval{\i}{\I+1}                 % nb of success, starting at 1
% (the proba of 0 success has
% been  computed as the
% initialization step)
\FPeval{\m}{\n-\i}                % nb of failures
\FPeval{\BC}{\bc*(\n-\I)/(\I+1)}  % binomial coefficient
% (recurrence relation)
\FPeval{\ps}{\BC*exp(\i*ln(\p))*exp(\m*ln(q))} % prob of \i
% success out of
% \n replications
\FPeval{\CDF}{\ps+\cdf}           % CDF(\i) = prob of \i success +
% CDF(\i-1)
\FPeval{\bc}{\BC}                 % set the binomial coeff
% for the next iteration
\FPeval{\cdf}{\CDF}               % now set cdf to CDF(\i) for
% the next iteration
\FPeval{\J}{\i+1}                 % the CDF remains constant
% over the interval [\i,\i+1[
% = [\i,\J[
\psline[linecolor=red]{*-}(\i,\cdf)(\J,\cdf)
% \FPeval{\j}{0.1*\I} \rput(\i,\j){\cdf} % prints the computed
% values of cdf to check
% their accuracy
% \psline[linecolor=red](\i,0)(\i,\ps) % if we want to plot the
% density
}
\end{psgraph}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

However I think this code is quite ugly, and I think it would be nice to
have some way of plotting any sequence of data points as a staircase
function using something that could be close to :

\dataplot[linestyle=staircase, leftendline=*, rightendline=o]{\mydata}

where \mydata is read in an external file that contains the [x cdf(x)]
values to be plotted (the * and o are the way arrow extremities are defined)

Is this available somewhere in the whole pstricks pst-xxx bundle ?
(couldnt find it after 2 days looking for some solution :( )

The main advantage of this solutions would be that the values of the
function are computed more efficiently (and more easily) by an external
program (scilab, bc, xcas,...) using for instance

\makeatletter
\newcommand{\executScilab}{
\immediate\write18{scilab -f #1} }
\makeatother

and compiling with the --shell-escape option

Thanks to all
LF