[pstricks] plotting a piecewise constant function (step function)
Leon Free
leon.free at free.fr
Thu Oct 21 12:56:12 CEST 2010
Thanks Herbert for that answer (and for the wonderful work you're doing
on pstricks)
Am I right saying that the \psGaussI integrates the Gauss function on a
given interval ?
What if I want to draw a function such as
$f(x) = \sum_{i}^n g(x_{i-1}) I(x \in [x_{i-1} , x_i))$
where $g$ can be any given function, $I(A) = 1$ if condition $A$ is
verified and 0 otherwise, and the $x_i$'s are given real values ?
(binomial cdf is just one such function taken as a starting point)
I guess the "postscript side" would be a better way in that case. But
unfortunately, I dont speak postscript, although your suggestion to my
binomial issue is a good tutorial.
As far as I understand the postscript code, it does reproduce almost
word for a word the FP computations I initially made.
Now what if I dont know what the function $g$ is and the $n+1$ values
$g(x_0),\ldots,g(x_n)$ are stored in an external file ? (the goal being
plotting $f$ as a step function with *- horizontal lines on each
interval $[x_{i-1},x_i)$)
> Date: Thu, 21 Oct 2010 09:52:30 +0200
> From: Leon Free <leon.free at free.fr>
> To: pstricks at tug.org
> Subject: [pstricks] plotting a piecewise constant function (step
> function)
> Message-ID: <4CBFF13E.8050104 at free.fr>
> Content-Type: text/plain; charset=ISO-8859-1
>
> 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 :
>
>
> %needs fp, pstricks, pstricks-add
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> \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}[1]{
> \immediate\write18{scilab -f #1} }
> \makeatother
>
> and compiling with the --shell-escape option
>
> Thanks to all
> LF
>
>
> ------------------------------
>
> Message: 2
> Date: Thu, 21 Oct 2010 11:52:01 +0200
> From: Herbert Voss <Herbert.Voss at FU-Berlin.DE>
> To: Graphics with PSTricks <pstricks at tug.org>
> Subject: Re: [pstricks] plotting a piecewise constant function (step
> function)
> Message-ID: <4CC00D41.1060904 at FU-Berlin.DE>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Am 21.10.2010 09:52, schrieb Leon Free:
>
>> 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 :
>
> everything kann be done more easily on PostScript side. However,
> with the pst-func.tex from http://texnik.dante.de/tex/generic/ you can also
> use the \psGaussI (see attched figure, created with the commented
> two lines in the example)
>
> \documentclass{article}
>
> \usepackage{pst-func}
> \makeatletter
> \@namedef{psds at dotline}{\pst at gdot{ 0 0 DS \tx at SD 0 0 moveto 12 0 lineto
> stroke}}
> \makeatother
>
> \begin{document}
>
> \begin{psgraph}[Dy=0.2,Dx=2](0,0)(10,1.1){7cm}{5cm}
> % \rput[lb](2,0){\psGaussI[xunit=2.72,plotpoints=11,
> % plotstyle=dots,dotstyle=dotline,linecolor=red]{-1}{3}}%
> \psline[linecolor=red](-1,0)(0,0) % draw 1rst step of CDF
> \psplot[plotpoints=10,plotstyle=dots,
> dotstyle=dotline,linecolor=red]{0}{9}%
> [/N 10 def % predefined values
> /p 0.3 def
> /q 1 p sub def
> /bc 1 def
> /cdf q N exp def
> ]{% x value is on stack
> /x1 x 1 add def
> /M N x1 sub def
> /BC bc N x sub mul x 1 add div def
> /ps BC Euler x1 p ln mul exp mul Euler M q ln mul exp mul def
> /CDF ps cdf add def
> /bc BC def
> /cdf CDF def
> cdf
> }
> \end{psgraph}
>
> \end{document}
>
> Herbert
>
>
> -------------- next part --------------
> A non-text attachment was scrubbed...
> Name: x.png
> Type: image/png
> Size: 2640 bytes
> Desc: not available
> URL: <http://tug.org/pipermail/pstricks/attachments/20101021/81cad66a/attachment-0001.png>
>
> ------------------------------
More information about the PSTricks
mailing list