Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
| lehrkraefte:blc:math-2021hw:pi-wuerfeln-mit-python [2024/10/30 08:18] – Ivo Blöchliger | lehrkraefte:blc:math-2021hw:pi-wuerfeln-mit-python [2024/10/30 08:40] (current) – Ivo Blöchliger | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== $\pi$ würfeln ====== | ||
| + | Idee: Man wählt wiederholt zufällig einen Punkt $P$ im Einheitsquadrat (achsenparalleles Quadrat mit Ecken $(0,0)$ und $(1,1)$). | ||
| + | |||
| + | Man zählt, wie oft der Punkt im Einheitskreis liegt. Der erwartete Anteil entspricht dem Flächenverhältnis zwischen Viertelkreis und Quadrat, also $\frac{\pi}{4}$. | ||
| + | |||
| + | WebTigerPython: | ||
| + | |||
| + | ===== Ein Punkt ===== | ||
| + | |||
| + | <code python> | ||
| + | from random import random | ||
| + | |||
| + | def punkt(): | ||
| + | x = random() | ||
| + | y = random() | ||
| + | if ........ | ||
| + | return 1 # Resultat ist 1 | ||
| + | return 0 # Resultat ist 0 | ||
| + | |||
| + | # Test-Code | ||
| + | for i in range(20): | ||
| + | print(f" | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== $n$ Punkte ===== | ||
| + | |||
| + | <code python> | ||
| + | def experiment(n): | ||
| + | summe = 0 | ||
| + | for i in range(n): | ||
| + | summe += punkt() | ||
| + | # Mittelwert | ||
| + | xbar = summe/n | ||
| + | # Standardabweichung | ||
| + | s = (1/ | ||
| + | return [xbar, s] | ||
| + | |||
| + | # Test-Code | ||
| + | print(experiment(1000)) | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ===== 95% Vertrauensintervall ===== | ||
| + | <code python> | ||
| + | def intervall(n): | ||
| + | e = experiment(n) | ||
| + | etwapi = e[0]*4 | ||
| + | s = e[1]*4 | ||
| + | sbar = s/(n**0.5) | ||
| + | return [etwapi - 2*sbar, etwapi + 2*sbar] | ||
| + | | ||
| + | # Test-Code | ||
| + | for i in range(10): | ||
| + | print(intervall(100)) | ||
| + | </ | ||
| + | | ||
| + | |||
| + | ===== Intervalle testen ===== | ||
| + | <code python> | ||
| + | import math | ||
| + | def vertrauensfrage(n, | ||
| + | ok = 0 | ||
| + | for i in range(k): | ||
| + | bereich = intervall(n) | ||
| + | if (math.pi> | ||
| + | ok += 1 | ||
| + | return ok/k | ||
| + | |||
| + | # Test-Code | ||
| + | print(vertrauensfrage(100, | ||
| + | </ | ||
| + | |||
| + | ===== Vertrauensintervall für die Vertrauensintervallswahrscheinlichkeitsabschätzung ===== | ||
| + | In der Funktion '' | ||
| + | |||
| + | Wie verhält sich das Intervall für kleine $n$? Siehe auch Tabelle einiger t-Quantile: https:// | ||