Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3774706
D2263.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
D2263.diff
View Options
diff --git a/roles/devserver/userland-home/files/dereckson/bin/hypergeometric_distribution b/roles/devserver/userland-home/files/dereckson/bin/hypergeometric_distribution
new file mode 100755
--- /dev/null
+++ b/roles/devserver/userland-home/files/dereckson/bin/hypergeometric_distribution
@@ -0,0 +1,58 @@
+#!/usr/bin/env python3
+
+import math
+import sys
+
+
+def binomial_coefficient(n, k):
+ f = math.factorial
+ return f(n) / f(k) / f(n - k)
+
+
+def compute(population_size, success_states_count, draws_count, observed_successes):
+ N = population_size
+ K = success_states_count
+ n = draws_count
+ k = observed_successes
+
+ bc = binomial_coefficient
+
+ return bc(K, k) * bc(N - K, n - k) / bc(N, n)
+
+
+def usage():
+ print(f"Usage: {sys.argv[0]} <population size> <number of success states> <number of draws> <number of observed successes>", file=sys.stderr)
+
+
+def help():
+ usage()
+ print()
+ print("Example:")
+ print()
+ print("In a Magic the Gathering deck of 60 crads, you've put 23 lands.")
+ print("To compute the probability to get 3 lands in your opening hand (7 cards):")
+ print(f" {sys.argv[0]} 60 23 7 3")
+
+
+def has_help_arg():
+ help_args = ["-h", "--help", "/?", "/help"]
+ return any([help_arg in sys.argv[1:] for help_arg in help_args])
+
+
+if __name__ == "__main__":
+ if has_help_arg():
+ help()
+ sys.exit(0)
+
+ if len(sys.argv) != 5:
+ usage()
+ sys.exit(1)
+
+ try:
+ numbers = [float(arg) for arg in sys.argv[1:]]
+ probability = compute(*numbers)
+ print(probability)
+ except ValueError as e:
+ print(e)
+ usage()
+ sys.exit(2)
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 25, 13:38 (21 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2262567
Default Alt Text
D2263.diff (1 KB)
Attached To
Mode
D2263: Provision hypergeometric distribution script
Attached
Detach File
Event Timeline
Log In to Comment