
正規分布とスチューデント T 分布のさまざまな図を描いています。以下に示すのは、両方の分布を示す簡単な図の MWE です。スチューデント T 計算を直接使用して垂直線を描画すると、問題なく動作します。同じ計算を pgfmathsetmacro に入れると、「! 次元が大きすぎます」というメッセージが表示されて失敗します。
私の実際のアプリケーションでは、rgtCH の値が複数の場所で使用されています。計算には TeX でしばらく時間がかかるため、毎回再計算するのではなく、計算を一度だけ実行して結果を変数に入れて再利用するようにしました。私の実際のアプリケーションでは、これはすべて高度にパラメータ化されています。MWE をできるだけシンプルにするために、パラメータ化をできるだけ削除しました。私は pgf の初心者であることは認めますが、私が間違っている点について理解するのを手伝ってくれる人がいれば、とても助かります。
\documentclass[11pt]{article}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{DistAxis/.style={%
no markers,
domain=-4:4, % Only display z values between -4 and 4.
samples=100,
xlabel=\textbf{t},
every axis x label/.style={at={(axis description cs:1.0, 0.0)}, anchor=west},
height=5cm, width=12cm,
xtick=\empty, ytick=\empty,
enlargelimits=false,
clip=false,
axis on top=true,
hide y axis,
axis x line*=middle,
axis line style ={thick,latex-latex}}
}
\pgfmathdeclarefunction{std_norm}{1}{%
\pgfmathparse{1/(sqrt(2*pi))*exp(-((#1)^2)/(2))}%
}
\pgfmathdeclarefunction{gamma}{1}{%
\pgfmathparse{2.506628274631*sqrt(1/#1) + 0.20888568*(1/#1)^(1.5) + %
0.00870357*(1/#1)^(2.5) - (174.2106599*(1/#1)^(3.5))/25920 - %
(715.6423511*(1/#1)^(4.5))/1244160)*exp((-ln(1/#1)-1)*#1}%
}
\pgfmathdeclarefunction{std_stud}{2}{%
\pgfmathparse{gamma(.5*(#1+1))/(sqrt(#1*pi)*gamma(.5*#1))*((1+(#2*#2)/#1)^(-.5*(#1+1)))}%
}
\begin{document}
\begin{tikzpicture}[scale=0.6]
\begin{axis}[DistAxis]
% If you comment the first and uncomment the second, this fails to compile. Why?
\pgfmathsetmacro{\rgtCH}{ 0.075+std_norm(1.0))};
% \pgfmathsetmacro{\rgtCH}{ 0.075+std_stud(9, 1.0))};
\draw [thick,magenta,dashed] (axis cs:1.0, -0.02) -- (axis cs:1.0, {0.075+std_stud(9,1.0)});
\draw [thick,magenta,dashed] (axis cs:1.0, -0.02) -- (axis cs:1.0, {\rgtCH});
\addplot [very thick,blue] {std_norm(x)};
\addplot [very thick,red] {std_stud(9,x)};
\end{axis}
\end{tikzpicture}
\end{document}
以下は、正規分布とスチューデント T 分布の計算が両方とも機能していることを示す画像です。
答え1
デフォルトでは\pgfmathsetmacro
fpu
オフになっています。オンにするとdimension too large
エラーは消えます。
\documentclass[11pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\pgfplotsset{DistAxis/.style={%
no markers,
domain=-4:4, % Only display z values between -4 and 4.
samples=100,
xlabel=\textbf{t},
every axis x label/.style={at={(axis description cs:1.0, 0.0)}, anchor=west},
height=5cm, width=12cm,
xtick=\empty, ytick=\empty,
enlargelimits=false,
clip=false,
axis on top=true,
hide y axis,
axis x line*=middle,
axis line style ={thick,latex-latex}}
}
\newcommand{\pgfmathparseFPU}[1]{\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathparse{#1}%
\pgfmathsmuggle\pgfmathresult\endgroup}
\pgfmathdeclarefunction{std_norm}{1}{%
\pgfmathparseFPU{1/(sqrt(2*pi))*exp(-((#1)^2)/(2))}%
}
\pgfmathdeclarefunction{gamma}{1}{%
\pgfmathparseFPU{2.506628274631*sqrt(1/#1) + 0.20888568*(1/#1)^(1.5) + %
0.00870357*(1/#1)^(2.5) - (174.2106599*(1/#1)^(3.5))/25920 - %
(715.6423511*(1/#1)^(4.5))/1244160)*exp((-ln(1/#1)-1)*#1}%
}
\pgfmathdeclarefunction{std_stud}{2}{%
\pgfmathparseFPU{gamma(.5*(#1+1))/(sqrt(#1*pi)*gamma(.5*#1))*((1+(#2*#2)/#1)^(-.5*(#1+1)))}%
}
\begin{document}
\begin{tikzpicture}[scale=0.6]
\begin{axis}[DistAxis]
% If you comment the first and uncomment the second, this fails to compile. Why?
\pgfmathsetmacro{\rgtCH}{ 0.075+std_norm(1.0))};
\pgfmathsetmacro{\rgtCH}{ 0.075+std_stud(9, 1.0))};
\draw [thick,magenta,dashed] (axis cs:1.0, -0.02) -- (axis cs:1.0, {0.075+std_stud(9,1.0)});
\draw [thick,magenta,dashed] (axis cs:1.0, -0.02) -- (axis cs:1.0, {\rgtCH});
\addplot [very thick,blue] {std_norm(x)};
\addplot [very thick,red] {std_stud(9,x)};
\end{axis}
\end{tikzpicture}
\end{document}