D-K-E / graphical-models

Probabilistic Graphical Models from Scratch with support for LWF Chain Graphs
GNU General Public License v3.0
4 stars 2 forks source link

code is not functionally pure #16

Open murphyk opened 11 months ago

murphyk commented 11 months ago

Every time you ask for a query, it mutates the state of the model, so asking the same question twice causes a crash. For example, consider https://github.com/D-K-E/graphical-models/blob/master/examples/LWFChainGraphs.ipynb. Now simply re-execute the final 2 lines

final_factor, a = cowell.cond_prod_by_variable_elimination(
    set([Bronchitis]), evidences
)
round(FactorOps.phi_normal(final_factor, set([("Bronchitis", True)])), 4)

You wll get the error

TypeError                                 Traceback (most recent call last)
[<ipython-input-9-9383f3297c21>](https://localhost:8080/#) in <cell line: 5>()
      3 )
      4 
----> 5 round(FactorOps.phi_normal(final_factor, set([("Bronchitis", True)])), 4)
      6 # 0.60

[/content/graphical-models/pygmodels/factor/factorf/factorops.py](https://localhost:8080/#) in phi_normal(f, scope_product)
    399         """
    400         Z = f.partition_value(FactorOps.factor_domain(f, D=f.scope_vars()))
--> 401         return f.phi(scope_product) / Z
    402 
    403     @staticmethod

TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'
D-K-E commented 11 months ago

Thanks for the report. I was able to reproduce the problem. I think it comes from how random variables are handling queries. Contrary to previous issue, its fix would require a bit more time as it also overlaps with the refactoring. I'll add a test for this case, and keep this issue open, until the test pass.