Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa1fc91

Browse files
committed
Delete redundant (and buggy) weighted_sample_with_replacement; add a little testing for it.
1 parent a9bc226 commit aa1fc91

File tree

2 files changed

+5
-20
lines changed

2 files changed

+5
-20
lines changed

probability.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -650,23 +650,5 @@ def particle_filtering(e, N, HMM):
650650
w[i] = float("{0:.4f}".format(w[i]))
651651

652652
# STEP 2
653-
s = weighted_sample_with_replacement(N, s, w)
653+
s = weighted_sample_with_replacement(s, w, N)
654654
return s
655-
656-
657-
def weighted_sample_with_replacement(N, s, w):
658-
"""
659-
Performs Weighted sampling over the paricles given weights of each particle.
660-
We keep on picking random states unitll we fill N number states in new distribution
661-
"""
662-
s_wtd = []
663-
cnt = 0
664-
while (cnt <= N):
665-
# Generate a random number from 0 to N-1
666-
i = random.randint(0, N-1)
667-
if (probability(w[i])):
668-
s_wtd.append(s[i])
669-
cnt += 1
670-
return s_wtd
671-
672-

tests/test_probability.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,11 @@ def test_particle_filtering():
146146
umbrella_transition = [[0.7, 0.3], [0.3, 0.7]]
147147
umbrella_sensor = [[0.9, 0.2], [0.1, 0.8]]
148148
umbrellaHMM = HiddenMarkovModel(umbrella_transition, umbrella_sensor)
149+
s = particle_filtering(umbrella_evidence, N, umbrellaHMM)
150+
assert len(s) == N
151+
assert all(state in 'AB' for state in s)
152+
# XXX 'A' and 'B' are really arbitrary names, but I'm letting it stand for now
149153

150-
assert particle_filtering(umbrella_evidence, N, umbrellaHMM)
151154

152155
# The following should probably go in .ipynb:
153156

0 commit comments

Comments
 (0)