14

I have tried without success to plot the curve of the chi-squared distribution. Is there a generous soul who can come to my rescue.

3
  • 15
    Show what you've tried so far in form of a minimal example and add this to your question. Jan 12, 2012 at 20:33
  • Thanks for the gnuplot and PSTricks based solution. But I am still looking for a TikZ based solution. Anyone to help?
    – hmhsl
    Jun 29, 2015 at 6:40
  • This is trivial using either pgfplots or even the plot option in tikz. I don't have access to a computer right now, but since you have the function to be plotted in closed form, using pgfplots (which uses tikz), it would require an \addplot[samples=200,domain=0.001:5]{function goes here}; or something like that.
    – JPi
    Jun 29, 2015 at 7:07

5 Answers 5

24

If you can access gnuplot, you can try this. This is an adapted version of a gnuplot demo file.

enter image description here

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[%
    xlabel = $x$,
    ylabel = {Probability density},
    samples = 200,
    restrict y to domain = 0:0.5,
    domain = 0.01:15]
    \foreach \k in {1,...,8} {%
      \addplot+[mark={}] gnuplot[raw gnuplot] {%
        isint(x) = (int(x)==x);
        log2 = 0.693147180559945;
        chisq(x,k)=k<=0||!isint(k)?1/0:x<=0?0.0:exp((0.5*k-1.0)*log(x)-0.5*x-lgamma(0.5*k)-k*0.5*log2);
        set xrange [1.00000e-5:15.0000];
        set yrange [0.00000:0.500000];
        samples=200;
        plot chisq(x,\k)};
    \addlegendentryexpanded{$k = \k$}}
  \end{axis}
\end{tikzpicture}
\end{document}
2
  • To see a solution with LuaLaTeX, see here.
    – Azoun
    Jan 15, 2012 at 1:07
  • 1
    The curve for the case $k=0$ is missing.This isn't exactly a surprise, because the chi-squared distribution isn't defined properly for $k=0$... I suggest you update the plot to drop the case $k=0$ from the legend.
    – Mico
    Jan 14, 2015 at 8:28
20

if you can use PSTricks, then it is easy. Run the example with xelatex

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

\psset{xunit=1.2cm,yunit=10cm,plotpoints=200}
\begin{pspicture*}(-0.75,-0.05)(9.5,.65)
\multido{\rnue=0.5+0.5,\iblue=0+10}{10}{%
  \psChiIIDist[linewidth=1pt,linecolor=blue!\iblue,nue=\rnue]{0.01}{9}}
\psaxes[Dy=0.1,ticksize=0 3pt]{->}(0,0)(9.5,.6)
\end{pspicture*}

\end{document}

enter image description here

4
  • Thanks for taking the time to answer my request but when I run the code I have this message of error: ! Package xkeyval Error: plotpoints' undefined in families ,pstricks,pst-grad ,pst-slpe,pst-node,pst-fun'. See the xkeyval package documentation for explanation. Type H <return> for immediate help. ... l.5 \psset{xunit=1.2cm,yunit=10cm,plotpoints=200} ? x Any help would be appreciated. Sincerly.
    – Zbigniew
    Jan 18, 2012 at 20:10
  • best result with texlive Unlike MiKTteX. Thank you to everyone.
    – Zbigniew
    Jan 18, 2012 at 20:21
  • You may have an outdated version of pstricks. Since you're using TeX Live, call tlmgr update --self --all from your terminal (command prompt).
    – Werner
    Jan 18, 2012 at 20:46
  • add \usepackage{pst-plot} to the preamble
    – user2478
    Jan 18, 2012 at 20:57
9

Here is a sketch of pure TikZ solution. The idea is that Gamma function is not available in tikz (tex), but the values of Gamma(k/2) for k=1,...,8 are simple. So we can "hardcode" them.

If somebody wants to put axes and legend, feel free to edit this answer ;)

\documentclass[tikz,border=7mm]{standalone}
\begin{document}
  \begin{tikzpicture}[domain=.001:15,samples=200,thick]
    \clip (-1,-1) rectangle (15,10);
    \foreach[count=\k,evaluate={\z=\k>2?"(0,0)--":"";\c=10*\k}] 
      \g in {sqrt(pi),1,sqrt(pi)/2,1,3/4*sqrt(pi),2,15/8*sqrt(pi),6}
        \draw[color=blue!\c!red,yscale=30] \z 
          plot (\x,{exp(ln(\x/2)*\k/2-ln(\x)-\x/2-ln(\g))});
  \end{tikzpicture}
\end{document}

enter image description here

0

Here is my "lazy" solution:

  1. generate the curve using Octave/MATLAB, then save the points in a CSV file:

    x = .1:.1:8;
    pdf1 = chi2pdf(x,1);
    pdf2 = chi2pdf(x,2);
    m = [x' pdf1' pdf2' pdf3' pdf4' pdf5' pdf6' pdf7' pdf8'];
    csvwrite ('chisquare.csv', m);
    
  2. import the data in LaTeX:

    `\pgfplotstableread[col sep=comma]{chisquare.csv}\dataChiSquare`
    
  3. Draw the plots:

    \pgfplotsinvokeforeach{1,2,3,4,5,6,7,8}{
     \addplot table[x = x, y = pdf#1] from \dataChiSquare;
    }
    

You can find the full working code and the already-generated .csv file in this question.

0

There is a nice solution of the Gamma distribution by User 22986

The Chi-squared pdf is a particular case of the gamma pdf with $\theta=2$, and $k \to k/2$.

Here is a modification of that script from the gamma PDF to to the chi-squared pdf.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\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={gammapdf(\x,\k,\theta) = \x^(\k-1)*exp(-\x/\theta) / (\theta^\k*gamma(\k));}
]

\begin{axis}[
    axis lines=left,
    enlargelimits=upper,
    samples=50,
    legend entries={$k=2$,$k=4$,$k=6$,$k=8$}
]
\addplot [smooth, domain=0:20,line width=1] {gammapdf(x,1,2)};
\addplot [smooth, domain=0:20, red, line width=1 ] {gammapdf(x,2,2)};
\addplot [smooth, domain=0:20, green, line width=1] {gammapdf(x,3,2)};
\addplot [smooth, domain=0:20, blue, line width=1] {gammapdf(x,4,2)};
\end{axis}
\end{tikzpicture}
\end{document}


   

Here the result: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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