From 53be1edb660494932e0602237367b9e768a1bc75 Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Thu, 25 Jan 2024 22:50:19 +0100 Subject: [PATCH] Add tram stat problem and python directory --- README.md | 1 + python/README.md | 5 ++++ python/tram-stat-problem/main.py | 40 ++++++++++++++++++++++++++++++ python/tram-stat-problem/shell.nix | 10 ++++++++ 4 files changed, 56 insertions(+) create mode 100644 python/README.md create mode 100644 python/tram-stat-problem/main.py create mode 100644 python/tram-stat-problem/shell.nix diff --git a/README.md b/README.md index efd2ede..3c5b4a6 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,4 @@ The experiments are currently organized based on the language they use: - [Lua](./lua/) - [Idris](./idris/) - [Typst](./typst/) +- [Python](./python/) diff --git a/python/README.md b/python/README.md new file mode 100644 index 0000000..642b86b --- /dev/null +++ b/python/README.md @@ -0,0 +1,5 @@ +# Python + +| Name | Description | +| ---------------------------------------- | ------------------------------------------------------------------ | +| [tram-stat-problem](./tram-stat-problem) | Quick script to visualize a dumb stat problem I was thinking about | diff --git a/python/tram-stat-problem/main.py b/python/tram-stat-problem/main.py new file mode 100644 index 0000000..e6f54fe --- /dev/null +++ b/python/tram-stat-problem/main.py @@ -0,0 +1,40 @@ +import numpy as np +import matplotlib.pyplot as plt + +size = 100000 + +fig, axes = plt.subplots(3, 3, figsize=(15, 15)) +fig.subplots_adjust(hspace=0.5) + + +def check(label, tram, ax): + bugun = np.random.uniform(0, 100, size=size) + tram = np.clip(tram, 0, 100) + + result = (tram - 10 <= bugun) & (bugun < tram) + count = np.sum(result) + + print(f"{label}: {count}") + + # ax.hist(bugun, alpha=0.5, label="Bugun", histtype="step", bins="auto") + # ax.hist(tram, alpha=0.5, label="Tram", histtype="step", bins="auto") + ax.hist(tram - bugun, label="Difference", bins="auto") + ax.set_title(label) + ax.legend() + + +distributions = [ + ("uniform", np.random.uniform(0, 100, size=size)), + ("normal(1)", np.random.normal(50, 1, size=size)), + ("normal(10)", np.random.normal(50, 10, size=size)), + ("normal(30)", np.random.normal(50, 30, size=size)), + ("normal(50)", np.random.normal(50, 50, size=size)), + ("normal(70)", np.random.normal(50, 70, size=size)), + ("normal(100)", np.random.normal(50, 100, size=size)), + ("normal(10000)", np.random.normal(50, 10000, size=size)), +] + +for i in range(len(distributions)): + check(*distributions[i], axes.flat[i]) + +plt.show() diff --git a/python/tram-stat-problem/shell.nix b/python/tram-stat-problem/shell.nix new file mode 100644 index 0000000..32187e5 --- /dev/null +++ b/python/tram-stat-problem/shell.nix @@ -0,0 +1,10 @@ +{ pkgs ? import { } }: +pkgs.mkShell { + packages = [ + (pkgs.python3.withPackages (ps: with ps; [ + numpy + matplotlib + ])) + ]; +} +