# [pstricks] More statistical distributions for pst-func

Herbert Voss Herbert.Voss at fu-berlin.de
Thu Aug 30 22:44:54 CEST 2007

halbard7 at aol.com wrote:
> ?Gamma distribution
> ?Chi-square distribution*
> ?Student's t distribution*
> ?Snedecor's F distribution*
>
> *These should be familiar to anyone who has studied Statistics A2 level.
>
> If you do incorporate these into pst-func, feel free to rename the functions and/or parameters as you think best.
>
Gerry,
the package pst-math knows the PostScript function GAMMALN.
Using this one makes it easier to plot your distributions, which
are then default plots.

Herbert

\documentclass{article}
\usepackage{pst-func,pst-math,pst-xkey}

\makeatletter
\define at key[psset]{pst-func}{alpha}{\def\psk at alpha{#1 }} % gamma
\define at key[psset]{pst-func}{beta}{\def\psk at beta{#1 }} % gamma
\define at key[psset]{pst-func}{nue}{\def\psk at nue{#1 }} % t and F

% The gamma distribution with parameters $\alpha$, $\beta$ has pdf
% $f(x)=\frac{\beta(\beta x)^{\alpha-1}e^{-\beta x}}{\Gamma(\alpha)}$
% for $x>0$ and $\alpha$, $\beta>0$

\pst at killglue%
\psset{plotpoints=200,alpha=0.5,beta=0.5}%
\psset{#1}%
\psplot{#2}{#3}{%
\psk at beta x mul \psk at alpha exp x div
Euler \psk at beta neg x mul \psk at alpha GAMMALN sub exp mul
}%
}\ignorespaces}

% The chi-square distribution with parameter $\nu$ is the same as
% a Gamma distribution with $\alpha=\nu/2$ and $\beta=1/2$

\def\psChiSqdist{\@ifnextchar[{\psChiSqdist at i}{\psChiSqdist at i[]}}
\def\psChiSqdist at i[#1]#2#3{{%
\pst at killglue%
\psset{plotpoints=200,nue=1}%
\psset{#1}%
\psplot{#2}{#3}{%
x 2 div \psk at nue 2 div exp x div
Euler -0.5 x mul \psk at nue 2 div GAMMALN sub exp mul
}%
}\ignorespaces}

% The $t$-distribution with parameter $\nu$ has the density function
% $f(x)=\frac1{\sqrt{\nu\pi}}\times % \frac{\Gamma[(\nu+1)/2]}{\Gamma(\nu/2)}\frac1{[1+(x^2/\nu)]^{(\nu+1)/2}}$
% for $-\infty<x<\infty$ and $\nu>0$

\def\psTdist{\@ifnextchar[{\psTdist at i}{\psTdist at i[]}}
\def\psTdist at i[#1]#2#3{{%
\pst at killglue%
\psset{plotpoints=200,nue=1}%
\psset{#1}%
\psplot{#2}{#3}{%
1 x 2 exp \psk at nue div 1 add \psk at nue 1 add 2 div exp div
\psk at nue Pi mul sqrt div
Euler \psk at nue 1 add 2 div GAMMALN \psk at nue 2 div GAMMALN sub exp mul
}%
}\ignorespaces}

% The F-distribution with parameters $\mu$ and $\nu$ has pdf
% $\frac{\Gamma[(\mu+\nu)/2]}{\Gamma(\mu/2)\Gamma(\nu/2)}\times % (\mu/\nu)^{\mu/2}\frac{x^{(\mu/2)-1}{[1+(\mu x/\nu)]^{(\mu+\nu)/2}}$
% for $x>0$ and $\mu$, $\nu>0$

\def\psFdist{\@ifnextchar[{\psFdist at i}{\psFdist at i[]}}
\def\psFdist at i[#1]#2#3{{%
\pst at killglue%
\psset{plotpoints=200,mue=1,nue=1}%
\psset{#1}%
\psplot{#2}{#3}{%
x \psk at mue mul \psk at nue div dup \psk at mue 2 div exp x div
exch 1 add \psk at mue \psk at nue add 2 div exp div
Euler \psk at mue \psk at nue add 2 div GAMMALN
\psk at mue 2 div GAMMALN sub \psk at nue 2 div GAMMALN sub exp mul
}%
}\ignorespaces}

\makeatother

\def\pshlabel#1{\tiny$#1$}
\let\psvlabel\pshlabel

\begin{document}

% Examples

\begin{pspicture}(-1,-0.1)(6,6)
\psaxes{->}(0,0)(6,6)
\psset{plotpoints=100,linewidth=1pt}
\psplot[linecolor=red]{0.01}{5.5}{ x GAMMALN }
\end{pspicture}

\psset{xunit=1.2cm,yunit=10cm}
\begin{pspicture*}(-1,-0.1)(9.5,.65)
\SpecialCoor
\psaxes{->}(0,0)(-1,0)(9.5,.6)
\end{pspicture*}

\begin{pspicture*}(-1,-0.1)(9.5,.65)
\SpecialCoor
\psaxes[Dy=0.1]{->}(0,0)(-1,0)(9.5,.6)
\psChiSqdist[nue=2]{0.001}{9}
\psChiSqdist[nue=4]{0.001}{9}
\psChiSqdist[nue=6]{0.001}{9}
\end{pspicture*}

The $\chi^2_\nu$-distribution has mode $\nu-2$ for $\nu\geq2$.

\psset{xunit=1cm,yunit=8cm}
\begin{pspicture}(-6,-0.1)(6,.5)
\psaxes[Dy=0.1]{->}(0,0)(-6,0)(6.5,.5)
\psset{linewidth=1pt}
\psGauss[mue=0,sigma=1,linecolor=red]{-4}{4}
\psTdist[nue=2]{-4}{4}
\psTdist[nue=6]{-4}{4}
\end{pspicture}

The $t_\nu$-distribution has mode 0.

\psset{xunit=1.3cm,yunit=10cm}
\begin{pspicture}(-1,-0.1)(9.5,1.1)
\SpecialCoor
\psaxes[Dy=0.1]{->}(0,0)(-1,0)(9.5,1)
\psline[linestyle=dashed](0.5,0)(0.5,1)
\psline[linestyle=dashed](! 2 7 div 0)(! 2 7 div 1)
\psset{linewidth=1pt}
\psFdist[mue=12,nue=3,linecolor=red]{0.001}{9}
\psFdist[mue=3,nue=12,linecolor=blue]{0.001}{9}
\end{pspicture}

The $F_{\mu,\nu}$-distribution has mode
$\frac{(\mu-2)\nu}{\mu(\nu+2)}$ for $\mu\geq2$.

\end{document}