Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'd like to draw a Student's t-distribution with five degrees of freedom using TikZ, then another with 10 degrees of freedom, etc.

In the program I am working in the degrees of freedom will be a random number from 1 to 20, so I need a t-distribution for the degrees of freedom assigned by Perl randomization.

share|improve this question
9  
Please show some attempt you've made in achieving this and specify what you're struggling with. The point of this Q&A site is to solve specific problems, not to have other people do your work. –  doncherry Nov 4 '12 at 5:11
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Qrrbrbirlbel Sep 3 '13 at 17:33
add comment

5 Answers

This is a pgfplots/gnuplot solution.

For \addplot gnuplot {…} to work you need to have a working installation of gnuplot on your machine and have to call pdflatex with write18 enabled (i.e. --shell-escape or --enable-write18).
How pgfplots and gnuplot interact can be studied in the pgfplots manual in subsection 4.2.5 “Computing Coordinates With Mathematical Expressions (gnuplot)”.

There are two foreach loops in this code. One that loops over tikzpicture and gives you one plot per picture, the other one loops over \addplot so that you will get one picture with nineteen plots.

Edit: Apparently gnuplot sees /2 as an integer rather than a floating point division.
The function is therefore:

gamma((\n+1)/2.)/(sqrt(\n*pi)*gamma(\n/2.))*((1+(x*x)/\n)^(-(\n+1)/2.))% or
gamma((\n+1)/2.)/(sqrt(\n*pi)*gamma(\n/2.))/((1+(x*x)/\n)^((\n+1)/2.))% or
gamma(.5*(\n+1))/(sqrt(\n*pi)*gamma(.5*\n))*((1+(x*x)/\n)^(-.5*(\n+1)))% or
gamma(.5*(\n+1))/(sqrt(\n*pi)*gamma(.5*\n))/((1+(x*x)/\n)^(.5*(\n+1)))%

Code

\documentclass[tikz,border=2pt]{standalone}
\usepackage{pgfplots}
\def\basefunc{%
    gamma(.5*(\n+1))/(sqrt(\n*pi)*gamma(.5*\n))*((1+x^2/\n)^(-.5*(\n+1)))%
}
\begin{document}
\foreach \n in {1,...,20}{
    \begin{tikzpicture}
        \begin{axis}[
            ymin=0,
            ymax=.41,
        ]
            \addplot gnuplot [
                smooth,
                no marks,
                domain={-6:+6},
                ]{\basefunc};
            \legend{$n = \n$}
        \end{axis}
    \end{tikzpicture}
}
\begin{tikzpicture}
    \begin{axis}[
        ymin=0,
        ymax=.41,
    ]
    \foreach \n in {2,...,20}{
        \addplot gnuplot [
            very thin,
            smooth,
            no marks,
            domain={-6:+6},
            ]{\basefunc};
        }
    \end{axis}
\end{tikzpicture}
\end{document}

Animated output of the first tikzpictures

Output of the first animated tikzpicture

Output of the second tikzpicture

Output of the second tikzpicture

share|improve this answer
1  
Very helpful.Thanks. –  David Nov 4 '12 at 15:25
add comment

Run it with xelatex or the sequence latex->dvips->ps2pdf:

\documentclass{article}
\usepackage{pst-func}
\begin{document}

\psset{xunit=1.25cm,yunit=10cm}
\begin{pspicture*}(-6,-0.1)(6,0.5)
\psaxes[Dy=0.1]{->}(0,0)(-5,0)(5.5,0.5)
\psTDist[linewidth=1pt,plotpoints=100,linecolor=red,
  fillstyle=solid,fillcolor=red!50,opacity=0.4, nue=3]{-5}{5}
\rput(3,0.3){$\nu=3$}
\end{pspicture*}

\end{document}

output

And for the values nue = 1,2,5,1000:

\documentclass{article}
\usepackage{pst-func}
\begin{document}

\psset{xunit=1cm,yunit=10cm}
\begin{pspicture*}(-6,-0.1)(6,0.5)
\psaxes[Dy=0.1]{->}(0,0)(-5,0)(5.5,0.5)
\psset{linewidth=1pt,plotpoints=100}
\psTDist[linecolor=red,nue=1]{-5}{5}
\psTDist[linecolor=green,nue=2]{-5}{5}
\psTDist[linecolor=blue,nue=5]{-5}{5}
\psTDist[linestyle=dashed,nue=1000]{-5}{5}
\end{pspicture*}

\end{document}

output

share|improve this answer
    
This is very nice. I would be happ(y/ier) if I could see the code for the drawings above. –  Svend Tveskæg Nov 6 '12 at 2:04
    
@SvendMortensen: see my edit with the code –  Herbert Nov 6 '12 at 7:21
    
Great! Just to be "difficult": Can I see the code for the animation too? :) –  Svend Tveskæg Nov 6 '12 at 10:04
    
click on the image: tug.org/PSTricks/main.cgi?file=Examples/Gallery/Gallery You need to convert the pdf width convert into an animated gif –  Herbert Nov 6 '12 at 10:13
    
Very nice. Thank you. –  Svend Tveskæg Nov 6 '12 at 10:18
add comment

If you want to plot the function without using gnuplot, you can define the approximation for the gamma function using declare function and use that for plotting the t-distribution.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}[
    declare function={gamma(\z)=
    2.506628274631*sqrt(1/\z)+ 0.20888568*(1/\z)^(1.5)+ 0.00870357*(1/\z)^(2.5)- (174.2106599*(1/\z)^(3.5))/25920- (715.6423511*(1/\z)^(4.5))/1244160)*exp((-ln(1/\z)-1)*\z;},
    declare function={student(\x,\n)= gamma((\n+1)/2.)/(sqrt(\n*pi) *gamma(\n/2.)) *((1+(\x*\x)/\n)^(-(\n+1)/2.));}
]

\begin{axis}[
    axis lines=left,
    enlargelimits=upper,
    samples=50
]
\pgfplotsinvokeforeach{1,2,5,100}{
    \addplot [thick, smooth, domain=-6:6] {student(x,#1)} node [pos=0.5, anchor=mid west, xshift=2em, append after command={(\tikzlastnode.west) edge [thin, gray] +(-2em,0)}] {$n=#1$};
}
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
add comment

This is also possible with tkz-fct and gnuplot. It's possible to use a scope to keep the color red local for the curve. Without the scope I need to to indicate the black color to draw the axes. I used the function given by Qrrbrbirlbel. I fixed \n but it's possible to use \foreach.

Update

\documentclass[11pt]{scrartcl} 
\usepackage[utf8]{inputenc}     
\usepackage{tkz-fct}
\begin{document}  

\begin{tikzpicture}[scale=.6,font=\small]
 \tkzInit[xmin=-5,xmax=5,ymin=-0,ymax=.5,ystep=.05] 
 \tkzGrid
\foreach \n in {2,4,...,20} {% 
\pgfmathsetmacro\col{3*\n+20}
     \tkzFct[color=red!\col!blue,domain=-5:5]{%
         gamma((\n+1)/2.)/(sqrt(\n*pi)*gamma(\n/2.))*(1+x**2./\n)**(-(\n+1)/2.)}  
} 
 \def\n{10} 
 \pgfmathsetmacro\col{3*\n+20} 
   \tkzFct[color=red!\col!blue,domain=-5:5]{%
       gamma((\n+1)/2.)/(sqrt(\n*pi)*gamma(\n/2.))*(1+x**2./\n)**(-(\n+1)/2.)}
   \tkzDrawArea[opacity=.3,color=red!30,domain = -6:6]   
 \tkzAxeXY[color=black]     
\end{tikzpicture}  

\end{document}  

enter image description here

share|improve this answer
    
As edited to my answer and commented by the OP gnuplot needs /2. instead of /2. –  Qrrbrbirlbel Nov 4 '12 at 18:57
    
@Qrrbrbirlbel Yes you are right. I used your function without checking its validity. Sorry ! –  Alain Matthes Nov 6 '12 at 8:00
add comment

To complete the overkill of the number of solution here a version with gnuplottex.

\documentclass{standalone}
\usepackage{gnuplottex}
\begin{document}

\begin{gnuplot}[terminal=epslatex,terminaloptions=color solid]
    unset key
    set samples 1000
    set format '$%g$'
    set xrange [-6:6]
    set yrange [0:0.41]
    f(n,x) = gamma(.5*(n+1))/(sqrt(n*pi)*gamma(.5*n))*((1+x**2/n)**(-.5*(n+1)))
    plot for[i=1:20] f(i,x)
\end{gnuplot}

\end{document}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.