Add a "clone" method

Issue #48 closed
Pierre Denis repo owner created an issue

Currently, Lea instances cannot be duplicated while keeping their dependency relationships. Consider for example the following CPT, where obs1 is an observation that depends on an unknown hypothesis, among H1 or H2:

hyp = pmf({'H1': 0.50, 'H2': 0.50})
obs1 = hyp.switch({ 'H1': pmf({'A': 0.50, 'B': 0.50}),
                    'H2': pmf({'A': 0.75, 'B': 0.25})} )

We want to see how the likelihood of each hypothesis evolves as new observation obs2 is made. The approach obs2 = obs1.new() is wrong because it makes the marginalization and build a new independent random variable. Currently, the only way to define obs2 is to redo the CPT construct:

obs2 = hyp.switch({ 'H1': pmf({'A': 0.50, 'B': 0.50}),
                    'H2': pmf({'A': 0.75, 'B': 0.25})} )

By doing so, obs1 and obs2 share the same hypothesis hyp. We can then make make valid queries as:

print (hyp.given(obs1=='A',obs2=='A'))
# -> H1 : 0.3076923076923077
     H2 : 0.6923076923076923 

To avoid redoing the same statement over and over, it could be nice to have a clone method, which makes the job:

obs2 = obs1.clone((hyp,))

The argument is an iterable specifying the variables that we want to share between the orignal instance and the cloned instance. This has the advantage of having an explicit statement of the dependencies between the two variables, which makes the model more understandable.

Similarly to the new method, an optional argument should allow building up n clones at a time:

obs1to10 = obs1.clone((hyp,),10)

Comments (9)

  1. Log in to comment