[Tex/LaTex] How to plot two curves with the same area under

pgfplotstikz-pgf

I doubt this is doable in tikz/pgfplots but maybe some expert could give me an idea.

I would like the area under the second graph to be the same as the one under the first graph (they are probability density function, the area under them should be equal to 1).

Moreover, I would like the gray zones to have the same area.

The second graph can have a slightly different shape, of course, the important thing is it has a "fat" right tail.

\documentclass[11pt, border=3cm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc, intersections}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}
\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}[
    declare function={gammapdf(\x,\alfa,\lambda)=
      (\lambda^\alfa)*(\x^(\alfa-1))*exp(-\lambda*\x)/factorial(\alfa-1);}
      ]
\begin{axis}[
    axis lines=middle,
    axis line style={-Stealth},
    ticks=none,
    samples=300,clip=false,
    enlarge x limits={value=.04,upper},
    enlarge y limits=.12,
    name=gamma
]
\addplot[thick, smooth, domain=-9:9, name path=curva] {gammapdf((x+10),8,1)};
\begin{scope}[on background layer]
\addplot [fill=gray!30, draw=none, domain=3:9] {gammapdf((x+10),8,1)} \closedcycle;
\end{scope}
\draw (3,-.005) node[below] {$q_\alpha$} |- (axis cs: 3, {gammapdf(13,8,1)});
\node (unomenoalfa) at (axis cs:6.5,0.04)  {$1-\alpha$}; 
\draw[-Stealth] (unomenoalfa) -- (axis cs:4,0.007);
\node (unomenoalfa) at (axis cs:-2.5,0.04)  {$\alpha$}; 
\end{axis}
\begin{axis}[
    axis lines=middle,
    axis line style={-Stealth},
    ticks=none,
    samples=300,clip=false,
    enlarge x limits={value=.04,upper},
    enlarge y limits=.12,
    at=(gamma.below south west),
    anchor=north west,
]
\addplot[thick, smooth, domain=-9:9, name path=curvastrana] coordinates {
    (-9,0) (-5,1) (.5,.1) (7,.1) (9,0)
    }; 
\draw (3,-.005) node[below] {$q_\alpha$} -- (3,1);
\begin{scope}[on background layer]
\clip (axis cs: 3, 0) rectangle (axis cs: 9, 3);
\addplot [fill=gray!30, smooth, draw=none]  coordinates {
    (-9,0) (-5,1) (.5,.1) (7,.1) (9,0)
    } \closedcycle;
\end{scope}
\node (unomenoalfa) at (axis cs:6.5,0.4)  {$1-\alpha$}; 
\draw[-Stealth] (unomenoalfa) -- (axis cs:7,0.03);
\node (unomenoalfa) at (axis cs:-4,0.4)  {$\alpha$}; 
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Best Answer

You can deform functions without changing their normalization by convoluting them. Unfortunately pgfplots cannot do integrals, so all I can offer is a poor cat's convolution. I just multiply the function by a linear combination of functions where one has the peaks at the "right" place and then adjust the coefficients in such a way that the integrals fulfill your requirements. The integrals are done by Mathematica. Different functions will lead to different plots. The following also uses the fillbetween library, which allows you to avoid having to plot the functions twice.

\documentclass[11pt, border=3cm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{calc}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}
\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}[
    declare function={gammapdf(\x,\alfa,\lambda)=
      (\lambda^\alfa)*(\x^(\alfa-1))*exp(-\lambda*\x)/factorial(\alfa-1);
      h(\x)=1.62*pow(sin(\x*18),2)-0.07*\x;}
      ]
\begin{axis}[
    axis lines=middle,
    axis line style={-Stealth},
    ticks=none,
    samples=300,clip=false,
    enlarge x limits={value=.04,upper},
    ymax=0.28,
    %enlarge y limits=.12,
    name=gamma
]
\addplot[thick, smooth, domain=-9:9, name path=curva] {gammapdf((x+10),8,1)};
\path [name path=B] (-10,0) -- (10,0);
\addplot [gray!30] fill between [of=curva and B,
        soft clip={domain=3:9}];
% \begin{scope}[on background layer]
% \addplot [fill=gray!30, draw=none, domain=3:9] {gammapdf((x+10),8,1)} \closedcycle;
% \end{scope}
\draw (3,-.005) node[below] {$q_\alpha$} |- (axis cs: 3, {gammapdf(13,8,1)});
\node (unomenoalfa) at (axis cs:6.5,0.04)  {$1-\alpha$}; 
\draw[-Stealth] (unomenoalfa) -- (axis cs:4,0.007);
\node (unomenoalfa) at (axis cs:-2.5,0.04)  {$\alpha$}; 
\end{axis}
\begin{axis}[
    axis lines=middle,
    axis line style={-Stealth},
    ticks=none,
    samples=300,clip=false,
    enlarge x limits={value=.04,upper},
    ymax=0.28,
    %enlarge y limits=.12,
    at=(gamma.below south west),
    anchor=north west,
]
\addplot[thick, smooth, domain=-9:9, name path=curvastrana] {h(x)*gammapdf((x+10),8,1)};
\draw (3,-.005) node[below] {$q_\alpha$} -- (3,{h(3)*gammapdf((3+10),8,1)});
\path [name path=B] (-10,0) -- (10,0);
\addplot [gray!30] fill between [of=curvastrana and B,
        soft clip={domain=3:9}];
\node (unomenoalfa) at (axis cs:6.5,0.04)  {$1-\alpha$}; 
\draw[-Stealth] (unomenoalfa) -- (axis cs:4,0.007);
\node (unomenoalfa) at (axis cs:-4,0.1)  {$\alpha$}; 
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here