如何定义 Beta 分布 pdf 上的区间?

如何定义 Beta 分布 pdf 上的区间?

在此处输入图片描述

我有一条 Beta 分布曲线,需要完成代码来定义并突出显示曲线上的一个区域,如图所示。

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\newcommand*\GnuplotDefs{
    Binv(p,q)=exp(lgamma(p+q)-lgamma(p)-lgamma(q));
    beta(x,p,q)=p<=0||q<=0?1/0:x<0||x>1?0.0:Binv(p,q)*x**(p-1.0)*(1.0-x)**(q-1.0);
}
\begin{document}
\begin{tikzpicture}
    \pgfmathsetmacro{\xmin}{0}
    \pgfmathsetmacro{\xmax}{1}
\begin{axis}[
    xmin=\xmin,
    xmax=\xmax,
    no markers,
]
    \addplot gnuplot [raw gnuplot] {
        \GnuplotDefs
        plot [x=\xmin:\xmax] beta(x,7,5);
    };
\end{axis}
\end{tikzpicture}
\end{document}

答案1

正如 John Kormylo 在在问题下方评论可以借助fillbetweenPGFPlots 库来填充曲线(部分)之间的区域。有关其工作原理的更多详细信息,请查看代码中的注释。

% used PGFPlots v1.14
% (code modified from <https://tex.stackexchange.com/a/368152/95441>)
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % load the `fillbetween' library to fill (parts) of the area under a curve
    \usetikzlibrary{
        pgfplots.fillbetween,
    }
    \pgfplotsset{
        % use this `compat' level or higher so there is no need to prepend
        % every TikZ coordinate with `axis cs:'
        compat=1.11,
    }
    % define a command which stores all commands that are needed for every
    % `raw gnuplot' call
    \newcommand*\GnuplotDefs{
        % set number of samples
        set samples 51;
        %
        % define beta distribution function
        % (copied from <http://gnuplot.sourceforge.net/demo/prob.5.gnu>)
        Binv(p,q)=exp(lgamma(p+q)-lgamma(p)-lgamma(q));
        beta(x,p,q)=p<=0||q<=0?1/0:x<0||x>1?0.0:Binv(p,q)*x**(p-1.0)*(1.0-x)**(q-1.0);
    }
\begin{document}
\begin{tikzpicture}
        % define macros which are needed for the axis limits as well as for
        % setting the domain of calculation
        \pgfmathsetmacro{\xmin}{0}
        \pgfmathsetmacro{\xmax}{1}
    \begin{axis}[
        xmin=\xmin,
        xmax=\xmax,
        ymin=0,
        no markers,
        % to make the plot look nicer
        smooth,
    ]
        % (invisible) path at y = 0 which is later used for `fill between'
        \path [name path=origin]
            (\pgfkeysvalueof{/pgfplots/xmin},0) --
            (\pgfkeysvalueof{/pgfplots/xmax},0);

        % draw the Beta function
        \addplot+ [
            name path=Beta,
            thick,
        ] gnuplot [raw gnuplot] {
            % first call all the "common" definitions
            \GnuplotDefs
            % and then create the data tables
            % in GnuPlot `x` key is identical to PGFPlots `domain` key
            %
            % "plot" beta function
            plot [x=\xmin:\xmax] beta(x,7,5);
        };

        % fill the area under the Beta function in the given interval
            % define the boundaries of the interval
            \pgfmathsetmacro{\Lower}{0.4}
            \pgfmathsetmacro{\Upper}{0.8}
        \addplot [
            orange,
        ] fill between [
            of=origin and Beta,
            soft clip={domain=\Lower:\Upper},
        ];

        % because there is no real intersection between the two given
        % `name path's, we cannot use the `intersection segments' feature of
        % the fill between library, that is, why we invisibly draw the Beta
        % function again, but this time only using two samples at the start
        % and end of the interval ...
        \addplot+ [
            draw=none,
        ] gnuplot [raw gnuplot] {
            \GnuplotDefs
            set samples 2;
            %
            plot [x=\Lower:\Upper] beta(x,7,5);
        }
            % ... and add coordinates to the two points.
            coordinate [at start] (lower boundary)
            coordinate [at end]   (upper boundary)
        ;
        % These coordinates can now be used to draw the vertical lines
        \draw [red,very thick]
            (lower boundary) -- (lower boundary |- 0,0)
            (upper boundary) -- (upper boundary |- 0,0)
        ;
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容