Skip to content

Commit 0751076

Browse files
Fix examples to use AutogradPotential and also take all outputs of hmc_slow
1 parent c45f971 commit 0751076

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

README.md

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ The API of `minimc` is mimicked in `minimc.minimc_slow`, which returns trajector
4343
```python
4444
import autograd.numpy as np
4545
from minimc import neg_log_normal, hamiltonian_monte_carlo
46+
from minimc.autograd_interface import AutogradPotential
4647

47-
samples = hamiltonian_monte_carlo(2_000, neg_log_normal(0, 0.1),
48-
initial_position=0.)
48+
neg_log_p = AutogradPotential(neg_log_normal(0, 0.1))
49+
samples = hamiltonian_monte_carlo(2_000, neg_log_p, initial_position=0.)
4950

5051
100%|███████████████████████████████████████████| 2500/2500 [00:04<00:00, 615.91it/s]
5152
```
@@ -55,9 +56,9 @@ samples = hamiltonian_monte_carlo(2_000, neg_log_normal(0, 0.1),
5556
```python
5657
from minimc.minimc_slow import hamiltonian_monte_carlo as hmc_slow
5758

58-
samples, positions, momentums, accepted = hmc_slow(50, neg_log_normal(0, 0.1),
59-
initial_position=0.,
60-
step_size=0.01)
59+
samples, positions, momentums, accepted, p_accepts = hmc_slow(50, neg_log_p,
60+
initial_position=0.,
61+
step_size=0.01)
6162

6263
100%|███████████████████████████████████████████| 50/50 [00:00<00:00, 52.72it/s]
6364
```
@@ -68,7 +69,7 @@ from minimc import neg_log_mvnormal
6869

6970
mu = np.zeros(2)
7071
cov = np.array([[1.0, 0.8], [0.8, 1.0]])
71-
neg_log_p = neg_log_mvnormal(mu, cov)
72+
neg_log_p = AutogradPotential(neg_log_mvnormal(mu, cov))
7273

7374
samples = hamiltonian_monte_carlo(1000, neg_log_p, np.zeros(2))
7475

@@ -78,9 +79,10 @@ samples = hamiltonian_monte_carlo(1000, neg_log_p, np.zeros(2))
7879
<img src="examples/plot3.png" width="400">
7980

8081
```python
81-
samples, positions, momentums, accepted = hmc_slow(10, neg_log_p, np.zeros(2),
82-
path_len=4,
83-
step_size=0.01)
82+
samples, positions, momentums, accepted, p_accepts = hmc_slow(10, neg_log_p,
83+
np.zeros(2),
84+
path_len=4,
85+
step_size=0.01)
8486

8587
100%|███████████████████████████████████████████| 10/10 [00:01<00:00, 9.06it/s]
8688
```
@@ -92,7 +94,7 @@ from minimc import mixture
9294

9395
neg_log_probs = [neg_log_normal(1.0, 0.5), neg_log_normal(-1.0, 0.5)]
9496
probs = np.array([0.2, 0.8])
95-
neg_log_p = mixture(neg_log_probs, probs)
97+
neg_log_p = AutogradPotential(mixture(neg_log_probs, probs))
9698
samples = hamiltonian_monte_carlo(2000, neg_log_p, 0.0)
9799

98100
neg_log_probs = [
@@ -101,7 +103,7 @@ neg_log_probs = [
101103
neg_log_normal(1.0, 0.3),
102104
]
103105
probs = np.array([0.1, 0.5, 0.4])
104-
neg_log_p = mixture(neg_log_probs, probs)
106+
neg_log_p = AutogradPotential(mixture(neg_log_probs, probs))
105107
samples = hamiltonian_monte_carlo(2_000, neg_log_p, 0.)
106108

107109
100%|███████████████████████████████████████████| 2000/2000 [00:09<00:00, 261.17it/s]
@@ -110,7 +112,9 @@ samples = hamiltonian_monte_carlo(2_000, neg_log_p, 0.)
110112
<img src="examples/plot5.png" width="400">
111113

112114
```python
113-
samples, positions, momentums, accepted = hmc_slow(100, neg_log_p, 0.0, step_size=0.01)
115+
samples, positions, momentums, accepted, p_accepts = hmc_slow(100, neg_log_p,
116+
0.0,
117+
step_size=0.01)
114118

115119
100%|███████████████████████████████████████████| 100/100 [00:07<00:00, 14.04it/s]
116120
```
@@ -128,14 +132,14 @@ cov2 = 0.2 * np.array([[1.0, -0.6],
128132
mu3 = np.array([-1.0, 2.0])
129133
cov3 = 0.3 * np.eye(2)
130134

131-
neg_log_p = mixture(
135+
neg_log_p = AutogradPotential(mixture(
132136
[
133137
neg_log_mvnormal(mu1, cov1),
134138
neg_log_mvnormal(mu2, cov2),
135139
neg_log_mvnormal(mu3, cov3),
136140
],
137141
[0.3, 0.3, 0.4],
138-
)
142+
))
139143

140144
samples = hamiltonian_monte_carlo(2000, neg_log_p, np.zeros(2))
141145

@@ -145,9 +149,10 @@ samples = hamiltonian_monte_carlo(2000, neg_log_p, np.zeros(2))
145149
<img src="examples/plot7.png" width="400">
146150

147151
```python
148-
samples, positions, momentums, accepted = hmc_slow(20, neg_log_p, np.zeros(2),
149-
path_len=3,
150-
step_size=0.01)
152+
samples, positions, momentums, accepted, p_accepts = hmc_slow(20, neg_log_p,
153+
np.zeros(2),
154+
path_len=3,
155+
step_size=0.01)
151156

152157
100%|███████████████████████████████████████████| 20/20 [00:08<00:00, 2.31it/s]
153158
```

0 commit comments

Comments
 (0)