% !TeX TS-program = pdflatex


\documentclass[french,a4paper,11pt]{article}
\usepackage[margin=1.5cm]{geometry}
\usepackage{amsmath,amssymb}
\usepackage{babel}
\usepackage{siunitx}
\sisetup{locale=FR,output-decimal-marker={,},group-minimum-digits=4}
\usepackage{xintexpr}
\usepackage{xparse}
\usepackage[most]{tcolorbox}
\tcbuselibrary{listings}
\usepackage{ifthen}

%calculs "simples" binom
\newcommand\calcPbinomP[3]{%npk
	\xintFloatToDecimal{\xintfloateval{binomial(#1,#3)*#2^#3*(1-#2)^(#1-#3)}}
}
\newcommand\calcPbinomC[4]{%npab
	\def\BorneInf{#3}\def\BorneSup{#4}
	\ifthenelse{\equal{#3}{*}}%
		{\def\BorneInf{0}}
		{}
	\ifthenelse{\equal{#4}{*}}%
		{\def\BorneSup{#4}}
		{}
	\xintFloatToDecimal{\xintfloateval{add(binomial(#1,i)*#2^i*(1-#2)^(#1-i), i=\BorneInf..\BorneSup)}}
}
%calculs "simples" Poiss
\newcommand\calcPpoissP[2]{%lk
\xintFloatToDecimal{\xintfloateval{exp(-#1)*#1^#2/factorial(#2)}}
}
\newcommand\calcPpoissC[3]{%lab
	\def\BorneInf{#2}\def\BorneSup{#3}
	\ifthenelse{\equal{#2}{*}}%
		{\def\BorneInf{0}}
		{}
	\ifthenelse{\equal{#3}{*}}%
		{\def\BorneSup{10*#1}}
		{}
	\xintFloatToDecimal{\xintfloateval{add(exp(-#1)*#1^i/factorial(i), i=\BorneInf..\BorneSup)}}
}
%utiles
\xintdeffloatvar a_1,a_2,a_3,a_4,a_5,a_6 :=
0.0705230784,0.0422820123,0.0092705272,0.0001520143,0.0002765672,0.0000430638;
\xintdeffloatvar rac2inv := sqrt(2)/2 ;
\xintdeffloatfunc erfc(x) := % uniquement pour x positif
((((((a_6*x+a_5)*x+a_4)*x+a_3)*x+a_2)*x+a_1)*x+1)**-16 ; % fin de définition de erfc(x)
\xintdeffloatfunc Phi(z) := (z>=0)?{1 - 0.5 * erfc(rac2inv * z)}{0.5 * erfc(-rac2inv * z)};
%calculs "simples" norm
\newcommand\calcPnormC[4]{%msab
	%def des bornes de l'intervalle suivant l'absence de a ou de b...
	\def\BorneInf{#3}\def\BorneSup{#4}
	\ifthenelse{\equal{#3}{*}}%
		{\def\BorneInf{#4-10*#2}}
		{}
	\ifthenelse{\equal{#4}{*}}%
		{\def\BorneSup{#3+10*#2}}
		{}
	\xintFloatToDecimal{\xintfloateval{Phi((\BorneSup-#1)/#2)-Phi((\BorneInf-#1)/#2)}}
}
%calculs "simples" fiabilite
\newcommand\calcPexpoC[3]{%lab
	\def\BorneInf{#2}\def\BorneSup{#3}
	\ifthenelse{\equal{#2}{*}}%
		{\def\BorneInf{0}}
		{}
	\ifthenelse{\equal{#3}{*}}%
		{\def\BorneSup{100/#1}}
		{}
	\xintFloatToDecimal{\xintfloateval{exp(-#1*\BorneInf)-exp(-#1*\BorneSup)}}
}
%calculs formatés
\NewDocumentCommand{\numPbinomP}{ s O{3} m m m }{%*=sci,2=prec,3=n,4=p,5=k
	\IfBooleanTF{#1}%
		{%
			\num[exponent-mode=scientific]{\xintFloatToDecimal{\xintfloateval{round(binomial(#3,#5)*#4^#5*(1-#4)^(#3-#5),#2)}}}
		}%
		{%
			\num[minimum-decimal-digits=#2]{\xintFloatToDecimal{\xintfloateval{round(binomial(#3,#5)*#4^#5*(1-#4)^(#3-#5),#2)}}}
		}
}

\NewDocumentCommand{\numPbinomC}{ s O{3} m m m m }{%*=sci,2=prec,3=n,4=p,5=a,6=b
	\def\BorneInf{#5}\def\BorneSup{#6}
	\ifthenelse{\equal{#5}{*}}%
		{\def\BorneInf{0}}
		{}
	\ifthenelse{\equal{#6}{*}}%
		{\def\BorneSup{#6}}
		{}
	\IfBooleanTF{#1}%
	{%
		\num[exponent-mode=scientific]{\xintFloatToDecimal{\xintfloateval{round(add(binomial(#3,i)*#4^i*(1-#4)^(#3-i), i=\BorneInf..\BorneSup),#2)}}}
	}%
	{%
		\num[minimum-decimal-digits=#2]{\xintFloatToDecimal{\xintfloateval{round(add(binomial(#3,i)*#4^i*(1-#4)^(#3-i), i=\BorneInf..\BorneSup),#2)}}}
	}
}

\NewDocumentCommand{\numPpoissP}{ s O{3} m m }{%*=sci,2=prec,3=lbda,4=k
	\IfBooleanTF{#1}%
	{%
		\num[exponent-mode=scientific]{\xintFloatToDecimal{\xintfloateval{round(exp(-#3)*#3^#4/factorial(#4),#2)}}}
	}%
	{%
		\num[minimum-decimal-digits=#2]{\xintFloatToDecimal{\xintfloateval{round(exp(-#3)*#3^#4/factorial(#4),#2)}}}
	}
}

\NewDocumentCommand{\numPpoissC}{ s O{3} m m m }{%*=ing,2=prec,3=lbda,4=a,5=b
	\def\BorneInf{#4}\def\BorneSup{#5}
	\ifthenelse{\equal{#4}{*}}%
		{\def\BorneInf{0}}
		{}
	\ifthenelse{\equal{#5}{*}}%
		{\def\BorneSup{10*#3}}
		{}
	\IfBooleanTF{#1}%
	{%
		\num[exponent-mode=scientific]{\xintFloatToDecimal{\xintfloateval{round(add(exp(-#3)*#3^i/factorial(i), i=\BorneInf..\BorneSup),#2)}}}
	}%
	{%
		\num[minimum-decimal-digits=#2]{\xintFloatToDecimal{\xintfloateval{round(add(exp(-#3)*#3^i/factorial(i), i=\BorneInf..\BorneSup),#2)}}}
	}
}

\NewDocumentCommand{\numPnormC}{ s O{3} m m m m }{%
	%*=ing,2=prec,3=mu,4=sigma,5=a,6=b
	%def des bornes de l'intervalle suivant l'absence de a ou de b...
	\def\BorneInf{#5}\def\BorneSup{#6}
	\ifthenelse{\equal{#5}{*}}%
		{\def\BorneInf{#6-10*#4}}
		{}
	\ifthenelse{\equal{#6}{*}}%
		{\def\BorneSup{#5+10*#4}}
		{}
	\IfBooleanTF{#1}%
	{%
		\num[exponent-mode=scientific]{\xintFloatToDecimal{\xintfloateval{round(Phi((\BorneSup-#3)/#4)-Phi((\BorneInf-#3)/#4),#2)}}}
	}%
	{%
		\num[minimum-decimal-digits=#2]{\xintFloatToDecimal{\xintfloateval{round(Phi((\BorneSup-#3)/#4)-Phi((\BorneInf-#3)/#4),#2)}}}
	}
}

\NewDocumentCommand{\numPexpoC}{ s O{3} m m m }{%*=ing,2=prec,3=lbda,4=a,5=b
	\def\BorneInf{#4}\def\BorneSup{#5}
	\ifthenelse{\equal{#4}{*}}%
		{\def\BorneInf{0}}
		{}
	\ifthenelse{\equal{#5}{*}}%
		{\def\BorneSup{100/#3}}
		{}
	\IfBooleanTF{#1}%
	{%
		\num[exponent-mode=scientific]{\xintFloatToDecimal{\xintfloateval{round(exp(-#3*\BorneInf)-exp(-#3*\BorneSup),#2)}}}
	}%
	{%
		\num[minimum-decimal-digits=#2]{\xintFloatToDecimal{\xintFloatToDecimal{\xintfloateval{round(exp(-#3*\BorneInf)-exp(-#3*\BorneSup),#2)}}}}
	}
}

\begin{document}

\part*{Calculs de probabilités discrètes et continues}

\section{Loi binomiale}

\begin{tcblisting}{colback=red!1,colframe=red!75!black,listing options={style=tcblatex,basicstyle=\ttfamily\tiny}}
$X \hookrightarrow \mathcal{B}(5\,;\,0,4)$ :\\ \\
$P(X=3) \approx \calcPbinomP{5}{0.4}{3} \approx \numPbinomP[3]{5}{0.4}{3}$.\\
$P(X\leqslant1) \approx \calcPbinomC{5}{0.4}{*}{1} \approx \numPbinomC[3]{5}{0.4}{0}{1}$.
\end{tcblisting}

\begin{tcblisting}{colback=red!1,colframe=red!75!black,listing options={style=tcblatex,basicstyle=\ttfamily\tiny}}
$X \hookrightarrow \mathcal{B}(100\,;\,0,02)$ :\\ \\
$P(X=10) \approx \calcPbinomP{100}{0.02}{10} \approx \numPbinomP[7]{100}{0.02}{10} \approx \numPbinomP*[7]{100}{0.02}{10}$.\\
$P(15\leqslant X\leqslant25) \approx \calcPbinomC{100}{0.02}{15}{25} \approx \numPbinomC[10]{100}{0.02}{15}{25} \approx \numPbinomC*[10]{100}{0.02}{15}{25}$.
\end{tcblisting}

\section{Loi de Poisson}

\begin{tcblisting}{colback=red!1,colframe=red!75!black,listing options={style=tcblatex,basicstyle=\ttfamily\tiny}}
$Y \hookrightarrow \mathcal{P}_5$ :\\ \\
$P(Y=3) \approx \calcPpoissP{5}{3} \approx \numPpoissP[4]{5}{3}$.\\
$P(Y\geqslant2) \approx \calcPpoissC{5}{2}{*} \approx \numPpoissC[4]{5}{2}{*}$.
\end{tcblisting}

\section{Test loi Normale}

\begin{tcblisting}{colback=red!1,colframe=red!75!black,listing options={style=tcblatex,basicstyle=\ttfamily\tiny}}
$X \hookrightarrow \mathcal{N}(0\,;\,1)$ :\\ \\
$P(X\leqslant1) \approx \calcPnormC{0}{1}{*}{1} \approx \numPnormC[5]{0}{1}{*}{1}$.\\
$P(-1,96\leqslant Z\leqslant1,96) \approx \calcPnormC{0}{1}{-1.96}{1.96} \approx \numPnormC[5]{0}{1}{-1.96}{1.96}$.
\end{tcblisting}

\begin{tcblisting}{colback=red!1,colframe=red!75!black,listing options={style=tcblatex,basicstyle=\ttfamily\tiny}}
$Y \hookrightarrow \mathcal{N}(550\,;\,30)$ :\\ \\
$P(Y\geqslant600) \approx \calcPnormC{550}{30}{600}{*} \approx \numPnormC[4]{550}{30}{600}{*}$.\\
$P(500\leqslant Y\leqslant600) \approx \calcPnormC{550}{30}{500}{600} \approx \numPnormC[4]{550}{30}{500}{600}$.
\end{tcblisting}

\section{Test loi exponentielle}

\begin{tcblisting}{colback=red!1,colframe=red!75!black,listing options={style=tcblatex,basicstyle=\ttfamily\tiny}}
$Z \hookrightarrow \mathcal{E}_{0,001}$ :\\ \\
$P(Z\geqslant400) \approx \calcPexpoC{0.001}{400}{*} \approx \numPexpoC[6]{0.001}{400}{*}$.\\
$P(300\leqslant Z\leqslant750) \approx \calcPexpoC{0.001}{300}{750} \approx \numPexpoC[6]{0.001}{300}{750}$.
\end{tcblisting}

\end{document}