我想绘制具有不同形状和尺度参数的伽马分布。以下 MWE 显示错误。错误信息是:
以下是 MWE:
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\pgfmathdeclarefunction{gammaPDF}{2}{
\pgfmathparse{1/(#2^#1*gamma(#1))*x^(#1-1)*exp(-x/#2)}
}
\begin{tikzpicture}
\begin{axis}[
width=4cm,
height=3cm,
axis x line=bottom,
axis y line=left
]
\addplot[
blue!50!white,
mark=none,
domain=0:20,
samples=100,
smooth] {\gammaPDF(9,0.5)};
\end{axis}
\end{tikzpicture}
答案1
以下是使用 Wolfram Alpha 生成的伽马函数近似得到的伽马 PDF:
\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={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=1\quad \theta=2$,$k=2\quad \theta=2$, $k=9\quad \theta=0.5$}
]
\addplot [smooth, domain=0:20] {gammapdf(x,1,2)};
\addplot [smooth, domain=0:20, red] {gammapdf(x,2,2)};
\addplot [smooth, domain=0:20, blue] {gammapdf(x,9,0.5)};
\end{axis}
\end{tikzpicture}
\end{document}