Add gen_histogram.py and finish theory for exercise 2

This commit is contained in:
Andreas Tsouchlos 2026-01-22 13:35:38 +01:00
parent 0716f02766
commit 3fc312ba9d
2 changed files with 271 additions and 225 deletions

View File

@ -0,0 +1,40 @@
import argparse
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import binom
def array_to_pgfplots_table_string(a):
return " ".join([f"({k}, {val})" for (k, val) in enumerate(a)]) + f" ({len(a)}, 0)"
def P_binom(N, p, k):
return binom(N, k) * p**k * (1 - p) ** (N - k)
def main():
# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("-N", type=np.int32, required=True)
parser.add_argument("-p", type=np.float32, required=True)
parser.add_argument("--show", "-s", action="store_true")
args = parser.parse_args()
# Generate and show data
N = args.N
p = args.p
bars = np.array([P_binom(N, p, k) for k in range(N + 1)])
print(array_to_pgfplots_table_string(bars))
if args.show:
plt.stem(bars)
plt.show()
if __name__ == "__main__":
main()

View File

@ -669,7 +669,80 @@
\begin{frame} \begin{frame}
\frametitle{Erinnerung: Rechnen mit Normalverteilungen} \frametitle{Erinnerung: Rechnen mit Normalverteilungen}
% TODO: Write \vspace*{-21mm}
\begin{itemize}
\item Die Normalverteilung
\end{itemize}
\vspace*{-5mm}
\begin{gather*}
f_X(x) = \frac{1}{\sqrt{2\pi \sigma^2}} \exp\left(\frac{(x -
\mu)^2}{2 \sigma^2} \right)
\hspace{20mm}
F_X(x) =
\vcenter{\hbox{\scalebox{1.5}[2.6]{\vspace*{3mm}$\displaystyle\int$}}}_{\hspace{-0.5em}-\infty}^{\,x}
\frac{1}{\sqrt{2\pi
\sigma^2}} \exp\left(\frac{(u - \mu)^2}{2 \sigma^2} \right) du
\end{gather*}
\vspace*{-2mm}
\begin{itemize}
\item Die Standardnormalverteilung
\end{itemize}
\vspace{-5mm}
\begin{minipage}{0.48\textwidth}
\centering
\begin{gather*}
X \sim \mathcal{N} (0,1) \\[4mm]
\Phi(x) := F_X(x) = P(X \le x) \\
\Phi(-x) = 1 - \Phi(x)
\end{gather*}
\end{minipage}%
\begin{minipage}{0.48\textwidth}
\centering
\begin{tabular}{|c|c||c|c||c|c|}
\hline
$x$ & $\Phi(x)$ & $x$ & $\Phi(x)$ & $x$ & $\Phi(x)$ \\
\hline
\hline
$0{,}00$ & $0{,}500000$ & $0{,}10$ & $0{,}539828$ &
$0{,}20$ & $0{,}579260$ \\
$0{,}02$ & $0{,}507978$ & $0{,}12$ & $0{,}547758$ &
$0{,}22$ & $0{,}587064$ \\
$0{,}04$ & $0{,}515953$ & $0{,}14$ & $0{,}555670$ &
$0{,}24$ & $0{,}594835$ \\
$0{,}06$ & $0{,}523922$ & $0{,}16$ & $0{,}563559$ &
$0{,}26$ & $0{,}602568$ \\
$0{,}08$ & $0{,}531881$ & $0{,}18$ & $0{,}571424$ &
$0{,}28$ & $0{,}610261$ \\
\hline
\end{tabular}\\
\end{minipage}
\begin{itemize}
\item Standardisierung einer ZV
\vspace*{-2mm}
\begin{gather*}
\widetilde{X} = \frac{X - E(X)}{\sqrt{V(X)}}
= \frac{X - \mu}{\sigma}
\end{gather*}
\end{itemize}
\vspace*{1mm}
\begin{lightgrayhighlightbox}
\vspace{-4mm}
Rechenbeispiel
\begin{gather*}
X \sim \mathcal{N}(\mu = 1, \sigma^2 = 0{,}5^2) \\[2mm]
P\left(X \le 1{,}12 \right)
= P\left(\frac{X - 1}{0{,}5} \le \frac{1{,}12 - 1}{0{,}5}\right)
= P\big(\underbrace{\widetilde{X}}_{\sim
\mathcal{N}(0,1)} \le 0{,}24\big)
= \Phi\left(0{,}24\right) = 0{,}594835
\end{gather*}
\vspace{-10mm}
\end{lightgrayhighlightbox}
\end{frame} \end{frame}
\begin{frame} \begin{frame}
@ -790,6 +863,7 @@
\end{itemize} \end{itemize}
\pause \pause
\vspace*{2mm}
\begin{minipage}[t]{0.32\textwidth} \begin{minipage}[t]{0.32\textwidth}
\centering \centering
\begin{figure}[H] \begin{figure}[H]
@ -799,74 +873,41 @@
\begin{axis}[ \begin{axis}[
width=10cm, width=10cm,
height=5cm, height=5cm,
scatter/classes={
a={mark=*, blue}
},
xtick=\empty, xtick=\empty,
ytick=\empty, ytick=\empty,
xlabel = $x$, xlabel = $k$,
ylabel = $y$, ylabel = $P_{S_N}(k)$,
xmin=-4,xmax=4, area style,
ymin=-1.5,ymax=2,
] ]
\addplot+[ \addplot+[scol0,fill=scol3,ybar interval,mark=no]
scol1, plot coordinates
scatter, { (0,0.125) (1,0.375) (2,0.375) (3,0.125) (4,0) };
only marks, \end{axis}
scatter src=explicit symbolic, \end{tikzpicture}
\end{figure}
\end{minipage}%
\begin{minipage}[t]{0.32\textwidth}
\centering
\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}[
width=10cm,
height=5cm,
xtick=\empty,
ytick=\empty,
xlabel = $k$,
ylabel = $P_{S_N}(k)$,
area style,
] ]
table[row sep=crcr] { \addplot+[scol0,fill=scol3,ybar interval,mark=no]
x y \\ plot coordinates
0.9782846466992505 1.3425401677691273 \\ {
-0.3342085827306991 -0.3478699656733771 \\ (0,0.0009765625) (1,0.009765625) (2,0.0439453125)
1.0329768177464096 0.906099042791728 \\ (3,0.1171875) (4,0.205078125) (5,0.246 09375)
0.4032837175133078 0.09609805659133519 \\ (6,0.205078125) (7,0.1171875) (8,0.0439453125)
-0.47995152749835157 -0.5885801242458046 \\ (9,0.009765625) (10,0.0009765625) (11,0)
-0.39301528503877914 0.5165601264867574 \\
-0.3016076234682761 0.3555224809310629 \\
-1.283841439924361 -1.092505952596916 \\
0.6394093134607625 0.7760543139022245 \\
-1.3930746204117168 -1.2539179604346171 \\
0.7066349809976303 0.26736104561273705 \\
-0.32358511023766134 -0.4974120460927544 \\
0.5697159086054595 0.7427982778218153 \\
0.7810330322454977 1.021722205669364 \\
-1.05027750818351 -1.088249765156553 \\
-0.18753992607010203 -0.4808932985122092 \\
0.9163016000620543 1.1130761981874584 \\
-0.16588501836421943 -0.5254281720340348 \\
1.7319708376031673 1.2174504869365954 \\
-0.5732884092151935 -0.4923142548003758 \\
1.2626814172655978 1.1468156532099922 \\
-1.1007514357735002 -0.626200459957605 \\
0.40631320003662 -0.3705703698506922 \\
-2.221684738838144 -2.739364284431091 \\
1.1309626619949467 1.1940429603335854 \\
0.3055128861785891 0.529524240616076 \\
-0.22522789028651527 -0.5082861632170081 \\
0.2726524372676921 -0.2466404699684424 \\
0.7078557266441373 0.8428284296154347 \\
-1.4402649481540337 -0.9344326515164862 \\
1.129522000340855 0.4510295424893529 \\
0.5870764491195138 0.5669363454321612 \\
2.3539677525351856 2.2253385575502285 \\
0.2028654829519406 0.24539632425150296 \\
-0.20861363707807395 -0.26125228812993867 \\
0.6187802012217948 -0.2685299708916181 \\
-0.2659232421672081 -0.22662166465228362 \\
1.2403675794143405 1.0157380953006032 \\
-0.0391562905128538 -0.6304153520459441 \\
0.9833408241524402 1.06523679491654 \\
0.5231710701516994 0.44339120385526315 \\
-3.2527645820047146 -2.955881198077996 \\
0.08993024102635327 0.6534559407213543 \\
0.4076640826339743 0.5075313685387366 \\
0.7431965606838403 0.04225691288802064 \\
2.0420226454403996 2.411788877111026 \\
-0.3652226483952774 -0.5846718876921133 \\
0.478643720906727 0.7267990110235567 \\
1.18457297115014 1.7548366300308906 \\
0.05462743086401826 -0.02632310517996274\\
}; };
\end{axis} \end{axis}
\end{tikzpicture} \end{tikzpicture}
@ -881,187 +922,152 @@
\begin{axis}[ \begin{axis}[
width=10cm, width=10cm,
height=5cm, height=5cm,
scatter/classes={
a={mark=*, blue}
},
xtick=\empty, xtick=\empty,
ytick=\empty, ytick=\empty,
xlabel = $x$, xlabel = $k$,
ylabel = $y$, ylabel = $P_{S_N}(k)$,
xmin=-2,xmax=1.7, area style,
ymin=-6,ymax=6,
] ]
\addplot+[ \addplot+[scol0,fill=scol3,ybar interval,mark=no]
scol1, plot coordinates
scatter, {
only marks, (0,8.881784197001252e-16)
scatter src=explicit symbolic, (1,4.440892098500626e-14)
] (2,1.0880185641326534e-12)
table[row sep=crcr] { (3,1.7408297026122455e-11)
x y \\ (4,2.0454749005693884e-10)
1.474648977967909 1.4746489779679088 \\ (5,1.8818369085238373e-09)
0.16441809142886987 0.16441809142886984 \\ (6,1.411377681392878e -08)
-2.031822997202981 -2.0318229972029807 \\ (7,8.871516854469519e-08)
1.2182520939353458 1.2182520939353456 \\ (8,4.768440309277366e-07)
-0.5531479118291289 -0.5531479118291288 \\ (9,2.2252721443294377e-06)
-1.47499216570319 -1.4749921657031897 \\ (10,9.123615791750694e-06)
0.6306758881059998 0.6306758881059997 \\ (11,3.317678469727525e-05)
-1.5640176651059605 -1.56401766510596 \\ (12,0.00010782455026614456)
-1.3859450407248939 -1.3859450407248937 \\ (13,0.00031517945462411495)
-1.0665944205353617 -1.0665944205353615 \\ (14,0.0008329742729351608)
0.7492473817601838 0.7492473817601837 \\ (15,0.001999138255044386)
0.9499783172548493 0.949978317254849 \\ (16,0.004373114932909594)
0.34018738058932396 0.3401873805893239 \\ (17,0.00874622986581919)
-1.0096550061469416 -1.0096550061469414 \\ (18,0.016034754754001845)
-0.6754142249990085 -0.6754142249990084 \\ (19,0.02700590274358206)
-0.7514107790472649 -0.7514107790472647 \\ (20,0.04185914925255218)
0.7509769467458492 0.750976946745849 \\ (21,0.059798784646503136)
0.646133492401253 0.6461334924012528 \\ (22,0.0788256706703905)
-1.745989051249313 -1.7459890512493126 \\ (23,0.09596168603351884)
-0.29616542476720953 -0.2961654247672095 \\ (24,0.10795689678770869)
0.845676968386932 0.8456769683869317 \\ (25,0.11227517265921708)
-1.5863350761719144 -1.586335076171914 \\ (26,0.10795689678770869)
-0.42188175903423886 -0.42188175903423875 \\ (27,0.09596168603351884)
-1.6501793388980994 -1.650179338898099 \\ (28,0.0788256706703905)
-0.5605813083780707 -0.5605813083780706 \\ (29,0.059798784646503136)
0.9976042516699373 0.997604251669937 \\ (30,0.04185914925255218)
-1.4071020277527053 -1.407102027752705 \\ (31,0.02700590274358206)
1.2028353996291026 1.2028353996291024 \\ (32,0.016034754754001845)
1.0123659351742527 1.0123659351742524 \\ (33,0.00874622986581919)
1.3398753611390357 1.3398753611390355 \\ (34,0.004373114932909594)
-1.8353880184343434 -1.835388018434343 \\ (35,0.001999138255044386)
0.20942595245430015 0.2094259524543001 \\ (36,0.0008329742729351608)
-0.8821105204722243 -0.882110520472224 \\ (37,0.00031517945462411495)
0.2823899445624717 0.28238994456247163 \\ (38,0.00010782455026614456)
-0.6518146330435214 -0.6518146330435213 \\ (39,3.317678469727525e-05)
0.48917046669684383 0.4891704666968437 \\ (40,9.123615791750694e-06)
0.8356612510810297 0.8356612510810295 \\ (41,2.2252721443294377e-06)
-0.33074175460821187 -0.3307417546082118 \\ (42,4.768440309277366e-07)
0.2947454674362563 0.29474546743625624 \\ (43,8.871516854469519e-08)
-1.2159944359594155 -1.2159944359594153 \\ (44,1.411377681392878e-08)
-0.21681449173721903 -0.21681449173721898 \\ (45,1.8818369085238373e-09)
-0.7152726760226845 -0.7152726760226844 \\ (46,2.0454749005693884e-10)
1.3162334939362095 1.3162334939362093 \\ (47,1.7408297026122455e-11)
0.2527835241858269 0.25278352418582684 \\ (48,1.0880185641326534e-12)
-0.9986661858536788 -0.9986661858536786 \\ (49,4.440892098500626e-14)
-0.9568391971929098 -0.9568391971929096 \\ (50,8.881784197001252e-16)
-0.5474319285933091 -0.547431928593309 \\ (51,0)
-2.30328755152605 -2.3032875515260494 \\
-1.2929155901531633 -1.292915590153163 \\
-0.8530475462391427 -0.8530475462391425\\
};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{minipage}%
\begin{minipage}[t]{0.32\textwidth}
\centering
\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}[
width=10cm,
height=5cm,
scatter/classes={
a={mark=*, blue}
},
xtick=\empty,
ytick=\empty,
xlabel = $x$,
ylabel = $y$,
xmin=-4,xmax=4,
ymin=-2,ymax=2,
]
\addplot+[
scol1,
scatter,
only marks,
scatter src=explicit symbolic,
]
table[row sep=crcr] {
x y \\
-1.7077500965534018 0.9715072286946655 \\
1.1148806736392152 0.9230117606631614 \\
0.11058932943085453 -0.5157596318522968 \\
0.08527262614233909 -0.9720863462538933 \\
-1.4204389641047823 -0.9712150414232805 \\
-0.6705061079694784 -0.061860055599544606 \\
-0.6212830814536863 1.2589504540208847 \\
1.4236240086652356 -1.302789472184279 \\
-1.0975355477486632 -0.886909899437918 \\
1.5752231220795536 1.2260114881873 \\
1.2049717160174165 1.0705757620706944 \\
-1.7929521084203113 -1.0124364432205855 \\
1.1345482934601252 -0.7213210134187505 \\
0.06993810174580865 -1.5278087661910722 \\
0.50560442840041 1.1191719084519776 \\
-0.814167507403749 0.2021470144855546 \\
2.03061011925002 0.08990067866176893 \\
0.7257818062658367 0.22602273591014058 \\
0.5036942935085902 0.2520250465804246 \\
0.5973644458579076 -0.2093760967114109 \\
1.1104283164930224 1.5071527221448955 \\
-0.052216510646198096 -0.5465573566030532 \\
0.423205976943666 -0.21077815853809784 \\
0.2982451040844636 -1.3591258564459687 \\
0.539438662504297 -0.780387830281188 \\
0.08417174139937453 0.2725275842632153 \\
0.05733773656028022 0.8226842222044897 \\
0.12184004421107687 -1.0962860273484687 \\
3.0973129011059326 0.13325075656192403 \\
1.464718817591499 -2.0541680373660234 \\
0.6017327837974983 -0.43330515099025413 \\
-1.6527036180073127 -0.04153499563379528 \\
1.3583641617521591 -0.9127837751641491 \\
-0.2808122864213532 0.6566355071818034 \\
0.36085503878766245 -0.2372816111687184 \\
-0.7808961491915221 -0.4569496546349541 \\
-0.08144830754364803 0.5297194167082963 \\
-0.3832453043478111 0.695158762430314 \\
-0.3021005547959829 -0.7515146005101381 \\
0.0832540012145203 -1.6257847886861803 \\
-0.08783078629061673 0.48401963778829576 \\
0.5098330610876248 0.3327688893197499 \\
0.4804292632122983 -0.5397408326625166 \\
0.3612454424603153 -0.2728088913965057 \\
0.8706855868841972 1.8337909595106936 \\
-0.7868151662161218 1.643221471861054 \\
-0.5629480754112661 0.16190044666568626 \\
0.9623486507086952 -0.06821392925238735 \\
-0.390445497949156 -1.4902360360777431 \\
2.239228377147278 -0.2037307482272916\\
}; };
\end{axis} \end{axis}
\end{tikzpicture} \end{tikzpicture}
\end{figure} \end{figure}
\end{minipage}% \end{minipage}%
\vspace*{3mm} \vspace*{-1mm}
\begin{minipage}[t]{0.32\textwidth} \begin{minipage}[t]{0.32\textwidth}
\centering \centering
$N = 10$ $N = 4, p=0{,}5$
\end{minipage}% \end{minipage}%
\begin{minipage}[t]{0.32\textwidth} \begin{minipage}[t]{0.32\textwidth}
\centering \centering
$N = 100$ $N = 10, p=0{,}5$
\end{minipage}% \end{minipage}%
\begin{minipage}[t]{0.32\textwidth} \begin{minipage}[t]{0.32\textwidth}
\centering \centering
$N = 1000$ $N = 50, p=0{,}5$
\end{minipage}% \end{minipage}%
\end{frame} \end{frame}
\begin{frame} \begin{frame}
\frametitle{Zusammenfassung} \frametitle{Zusammenfassung}
\begin{itemize} \vspace*{-25mm}
\item Tabelle
\item $\Phi(-x) = 1 - \Phi(x)$
\item ZGWS: Approx von Binom.
\end{itemize}
% TODO: Write \begin{columns}[t]
\column{\kitthreecolumns}
\centering
\begin{greenblock}{Standardnormalverteilung}
\vspace*{-10mm}
\begin{gather*}
X \sim \mathcal{N} (0,1) \\[4mm]
\Phi(x) := F_X(x) = P(X \le x) \\
\Phi(-x) = 1 - \Phi(x)
\end{gather*}
\end{greenblock}
\begin{greenblock}{Standardisierung}
\vspace*{-10mm}
\begin{gather*}
\widetilde{X} = \frac{X - E(X)}{\sqrt{V(X)}}
= \frac{X - \mu}{\sigma}
\end{gather*}
\end{greenblock}
\column{\kitthreecolumns}
\centering
\begin{greenblock}{Approximation einer Binom.vert. mit dem ZGWS}
\vspace*{-10mm}
\begin{gather*}
\text{Bedingung: } Np(1-p) \ge 9
\end{gather*}
\vspace*{-7mm}
\begin{align*}
P_X(a < S_N \le b) &= \nsum_{k=a}^{b} \binom{N}{k}
p^k(1-p)^{N-k} \\
& \approx
\Phi\left(\frac{b - Np}{\sqrt{Np(1-p)}}\right) -
\Phi\left(\frac{a - Np}{\sqrt{Np(1-p)}}\right)
\end{align*}
\end{greenblock}
\end{columns}
\vspace*{5mm}
\begin{table}
\centering
\begin{tabular}{|c|c||c|c||c|c|}
\hline
$x$ & $\Phi(x)$ & $x$ & $\Phi(x)$ & $x$ & $\Phi(x)$ \\
\hline
\hline
$1{,}60$ & $0{,}945201$ & $2{,}00$ & $0{,}977250$ &
$2{,}40$ & $0{,}991802$ \\
$1{,}62$ & $0{,}947384$ & $2{,}02$ & $0{,}978308$ &
$2{,}42$ & $0{,}992240$ \\
$1{,}64$ & $0{,}949497$ & $2{,}04$ & $0{,}979325$ &
$2{,}44$ & $0{,}992656$ \\
$1{,}66$ & $0{,}951543$ & $2{,}06$ & $0{,}980301$ &
$2{,}46$ & $0{,}993053$ \\
$1{,}68$ & $0{,}953521$ & $2{,}08$ & $0{,}981237$ &
$2{,}48$ & $0{,}993431$ \\
\hline
\end{tabular}
\end{table}
\end{frame} \end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -1072,8 +1078,8 @@
Im Werk einer Zahnradfabrik werden verschiedene Im Werk einer Zahnradfabrik werden verschiedene
Präzisionsmetallteile gefertigt. Während einer Präzisionsmetallteile gefertigt. Während einer
Schicht werden 5000 Stück eines Typs A hergestellt. Bei der Schicht werden $5000$ Stück eines Typs A hergestellt. Bei der
Qualitätskontrolle werden 3% dieser Qualitätskontrolle werden $3\%$ dieser
Teile als defekt klassifiziert und aussortiert. Teile als defekt klassifiziert und aussortiert.
% tex-fmt: off % tex-fmt: off