Repository navigation
Expand file tree
/
Copy pathtest_probabilistic_programming.py
More file actions
55 lines (42 loc) · 2.18 KB
/
Copy pathtest_probabilistic_programming.py
File metadata and controls
55 lines (42 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
from aima.probabilistic_programming import *
def test_om_is_about_ten_to_the_a():
rng = np.random.default_rng(0)
assert 500 < np.median([om(3, 1, rng) for _ in range(2000)]) < 2000
def test_citation_world():
researchers, papers, citations = generate_citation_world(20, np.random.default_rng(1))
assert len(citations) == 20 and papers
assert all(any(c['cited_paper'] is p for p in papers) for c in citations)
assert all(c['cited_paper']['author'] in researchers for c in citations)
def test_seismic_world():
stations = [(0.0, 0.0), (45.0, 90.0)]
events, detections = generate_seismic_world(stations, T=36000.0, rng=np.random.default_rng(2))
assert events and detections
for d in detections:
if d['event'] is not None:
# the wave needs time to travel, up to the Laplace noise
assert d['onset_time'] > d['event']['time'] - 20
assert any(d['event'] is None for d in detections)
def test_radar_world():
T = 30
aircraft, blips = generate_radar_world(T, rng=np.random.default_rng(3))
assert aircraft and blips
assert all(0 <= b['time'] < T for b in blips)
for a in aircraft:
times = sorted(a['track'])
assert times[0] == a['entry_time'] and times == list(range(times[0], times[-1] + 1))
assert any(b['source'] is None for b in blips) and any(b['source'] is not None for b in blips)
def test_render_and_noisy_image():
clean = render(['l'], 32, 128)
assert clean.shape == (32, 128)
assert clean[10:15, 10:13].sum() == FONT['l'].count('1') and clean.sum() == FONT['l'].count('1')
rng = np.random.default_rng(4)
letters, image = generate_image(rng)
assert image.shape == (32, 128)
# the noise has zero mean: the difference from the clean image averages out
assert abs((image - render(letters, 32, 128)).mean()) < 0.05
def test_markov_letters_follow_the_bigrams():
initial, transition = letter_bigram_model(['ab', 'ab'])
assert initial == {'a': 1.0} and transition == {'a': {'b': 1.0}}
letters = generate_markov_letters(20, initial, transition, np.random.default_rng(5))
assert letters == ['a', 'b'] * (len(letters) // 2) + ['a'] * (len(letters) % 2)