summaryrefslogtreecommitdiffstats
path: root/hw2/p1.py
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2015-10-08 22:39:22 -0400
committerThibaut Horel <thibaut.horel@gmail.com>2015-10-08 22:39:22 -0400
commitc1e4e0e41442c087284f34d7585cf9f551e52c2f (patch)
tree5154724e4f018128edaffc3b58392bf22188d613 /hw2/p1.py
parent0f9fc1bdb1755f5677e3201bcd35706e09235844 (diff)
downloadcs281-c1e4e0e41442c087284f34d7585cf9f551e52c2f.tar.gz
[hw2]
Diffstat (limited to 'hw2/p1.py')
-rw-r--r--hw2/p1.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/hw2/p1.py b/hw2/p1.py
new file mode 100644
index 0000000..682abc1
--- /dev/null
+++ b/hw2/p1.py
@@ -0,0 +1,37 @@
+from scipy.special import gamma
+import numpy as np
+from math import exp
+import matplotlib.pyplot as plt
+from scipy.stats import rv_continuous
+
+
+def pdf(u, D):
+ return 1 / (2 ** ((D / 2.) - 1.)
+ * gamma(D / 2.)) * u ** (D - 1.) * exp(-u ** 2. / 2.)
+
+
+class my(rv_continuous):
+
+ def _pdf(self, u, D):
+ return pdf(u, D)
+
+
+def dist():
+ a = np.linspace(0, 20, 1000)
+ plt.figure(figsize=(9, 6))
+ for D in [1, 2, 5, 10, 20, 50, 100]:
+ b = [pdf(u, D) for u in a]
+ plt.plot(a, b, label="D=" + str(D))
+ plt.legend()
+ plt.savefig("dist1.pdf")
+
+
+def cdf():
+ a = my(a=0, b=float("inf"))
+ px = np.linspace(5, 15, 100)
+ plt.figure(figsize=(9, 6))
+ plt.plot(px, a.cdf(px, 100))
+ plt.savefig("cumul.pdf")
+
+if __name__ == "__main__":
+ cdf()