|
16 | 16 |
|
17 | 17 | class TestMCMCMethod(unittest.TestCase): |
18 | 18 | def test_exa_1901(self): |
19 | | - pass |
| 19 | + def f(x): |
| 20 | + return np.exp(-x**2/2) |
| 21 | + |
| 22 | + def p(x): |
| 23 | + return 1 |
| 24 | + |
| 25 | + x = np.random.uniform(0, 1, 10) |
| 26 | + y = f(x) |
| 27 | + x_raw = np.arange(0, 1, 0.01) |
| 28 | + y_raw = f(x_raw) |
| 29 | + print(x.shape, y.shape) |
| 30 | + data = np.concatenate((x_raw[:, np.newaxis], y_raw[:, np.newaxis]), |
| 31 | + axis=1) |
| 32 | + print(data) |
| 33 | + # data = data[data[:, 0].argsort()] |
| 34 | + rst = np.mean(y) |
| 35 | + |
| 36 | + plt.rc('text', usetex=True) |
| 37 | + plt.rc('font', family='serif') |
| 38 | + |
| 39 | + plt.figure(figsize=(5, 5)) |
| 40 | + plt.scatter(x, y, marker='*') |
| 41 | + plt.plot(data[:, 0], data[:, 1], alpha=0.3) |
| 42 | + bbox = dict(boxstyle="round", fc="0.8") |
| 43 | + comment = r"$\int_0^1e^{-{x^2}/{2}}\mathrm{d}x=$"+str(round(rst, 2)) |
| 44 | + plt.annotate(comment, (0.2, 0.8), bbox=bbox) |
| 45 | + plt.ylabel(r'$\exp\left(-\frac{1}{2}x^2\right)$') |
| 46 | + plt.ylim(-0.02, 1.02) |
| 47 | + plt.xlim(-0.02, 1.02) |
| 48 | + plt.show() |
20 | 49 |
|
21 | 50 | def test_exa_1902(self): |
22 | | - pass |
| 51 | + def p(x): |
| 52 | + return np.sqrt(2*np.pi)*np.exp(-x**2/2) |
| 53 | + |
| 54 | + def f(x): |
| 55 | + return x |
| 56 | + |
| 57 | + x = np.random.normal(0, 1, 10) |
| 58 | + y = f(x) |
| 59 | + x_raw = np.arange(-2, 2, 0.1) |
| 60 | + y_raw = f(x_raw) |
| 61 | + print(x.shape, y.shape) |
| 62 | + data = np.concatenate((x_raw[:, np.newaxis], y_raw[:, np.newaxis]), |
| 63 | + axis=1) |
| 64 | + print(data) |
| 65 | + # data = data[data[:, 0].argsort()] |
| 66 | + rst = np.mean(y) |
| 67 | + |
| 68 | + plt.rc('text', usetex=True) |
| 69 | + plt.rc('font', family='serif') |
| 70 | + |
| 71 | + plt.figure(figsize=(5, 5)) |
| 72 | + plt.scatter(x, y, marker='*', alpha=0.3) |
| 73 | + plt.plot(data[:, 0], data[:, 1], alpha=0.3) |
| 74 | + bbox = dict(boxstyle="round", fc="0.8") |
| 75 | + comment = r"$\int_{-\infty}^{\infty}x\frac{1}{\sqrt{2\pi}}e^{-{x^2}/{2}}\mathrm{d}x=$"+str(round(rst, 2)) |
| 76 | + plt.annotate(comment, (-2, 2), bbox=bbox) |
| 77 | + |
| 78 | + plt.show() |
23 | 79 |
|
24 | 80 | def test_exa_1903(self): |
25 | 81 | pass |
|
0 commit comments