<jupyter_start><jupyter_text>Dynamic Bayesian NetworkConsider the following configuration:![problem](../references/problem.png)The robot starts in square 'C' at time 0. It can only move clockwise. There is a 50% chance the robot moves to the next square, and there is a 50% chance the robot remains in its current square. We can only measure the robot's location with a sensor. The sensor returns the robot's true location with 60% accuracy. Otherwise, it returns uniformly distributed noise. This notebook will model this state estimation problem with a dynamic bayesian network.<jupyter_code># import libraries
import pyAgrum as gum
import pyAgrum.lib.notebook as gnb
import pyAgrum.lib.dynamicBN as gdyn
%matplotlib inline
# initialize model
dbn = gum.BayesNet()
# initialize environment
locations = ['A', 'B', 'C', 'D']
opts = len(locations)
# make nodes: cardinality -> 4 options per node
nodes = [gum.LabelizedVariable(s, s, opts) for s in ['L0', 'S0', 'Lt', 'St']]
for node in nodes:
    for i in range(opts):
        node.changeLabel(i, locations[i])
# add nodes to network
L0, S0, Lt, St = [dbn.add(node) for node in nodes]
# make connections
dbn.addArc(L0, S0)
dbn.addArc(L0, Lt)
dbn.addArc(Lt, St)
# make conditional probability tables
dbn.cpt(L0).fillWith([0, 0, 1.0, 0])
dbn.cpt(S0)[{'L0': 'A'}] = [0.7, 0.1, 0.1, 0.1]
dbn.cpt(S0)[{'L0': 'B'}] = [0.1, 0.7, 0.1, 0.1]
dbn.cpt(S0)[{'L0': 'C'}] = [0.1, 0.1, 0.7, 0.1]
dbn.cpt(S0)[{'L0': 'D'}] = [0.1, 0.1, 0.1, 0.7]

dbn.cpt(Lt)[{'L0': 'A'}] = [0.5, 0.5, 0, 0]
dbn.cpt(Lt)[{'L0': 'B'}] = [0, 0.5, 0, 0.5]
dbn.cpt(Lt)[{'L0': 'C'}] = [0.5, 0, 0.5, 0]
dbn.cpt(Lt)[{'L0': 'D'}] = [0, 0, 0.5, 0.5]

dbn.cpt(St)[{'Lt': 'A'}] = [0.7, 0.1, 0.1, 0.1]
dbn.cpt(St)[{'Lt': 'B'}] = [0.1, 0.7, 0.1, 0.1]
dbn.cpt(St)[{'Lt': 'C'}] = [0.1, 0.1, 0.7, 0.1]
dbn.cpt(St)[{'Lt': 'D'}] = [0.1, 0.1, 0.1, 0.7]
# preview cpts: initial location is known
dbn.cpt(L0)
# sensor is 60% accurate + 40% uniform noise
dbn.cpt(S0)
# robot has a 50/50 of moving or staying
dbn.cpt(Lt)
# sensor cpt does not change
dbn.cpt(St)
# preview model
gnb.showBN(dbn)
# show time slices
gdyn.showTimeSlices(dbn)
# show propagated model
T = 5 # seconds
ext_dbn = gdyn.unroll2TBN(dbn, T)
gdyn.showTimeSlices(ext_dbn, size=str(T))
# compute posterior: P(L|S) after 1 update
gnb.showPosterior(dbn, target='Lt', evs={'St': 'C'})
# show posterior changing over time
for i in range(T):
    # conditional evidence
    if i % 2 == 1:
        # sensor returns C every other second
        evidence = {'S{}'.format(i): 'C'}
    else:
        # no sensor return
        evidence = {}
        
    print('Available evidence: {}'.format(evidence))
    print('Most likely location...')
    gnb.showPosterior(ext_dbn, target='L{}'.format(i), evs=evidence)
# show evolution of posteriors over T with set evidence
gdyn.plotFollow(['L'], dbn, T=T, evs={'S1': 'C', 'S3': 'D'})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18717683557394002, 'fraction_numerical': 0.047569803516028956, 'mean_word_length': 4.046956521739131, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '582938', 'license': 'MIT', 'n_tokens_mistral': 1274, 'n_tokens_neox': 1196, 'n_words': 353, 'path': 'notebooks/dynamic_bayes_network.ipynb', 'repo_name': 'Joshua-Robison/ASU', 'size': '2901'}