Factor product failing
GabrielAzevedoFerreiraQB opened this issue · comments
Subject of the issue
Describe your issue here.
import pandas as pd
from pgmpy.factors import factor_product
from pgmpy.factors.discrete import TabularCPD
import numpy as np
aux=np.random.rand(3,3 *3 *3)
cpd1 = TabularCPD(
variable='z',
variable_card=3,
values=np.divide(aux,aux.sum(axis=0)),
evidence=['p_0', 'p_1','p_2'],
evidence_card=[3, 3, 3],
)
aux=np.random.rand(3,3)
cpd2 = TabularCPD(
variable='c_0',
variable_card=3,
values=np.divide(aux,aux.sum(axis=0)),
evidence=['z'],
evidence_card=[3],
)
aux=np.random.rand(3,3)
cpd3 = TabularCPD(
variable='c_1',
variable_card=3,
values=np.divide(aux,aux.sum(axis=0)),
evidence=['z'],
evidence_card=[3],
)
factor_product(cpd1, cpd2, cpd3)
error message:
/lib/python3.8/site-packages/pgmpy/factors/discrete/DiscreteFactor.py in __init__(self, variables, cardinality, values, state_names)
97
98 if len(set(variables)) != len(variables):
---> 99 raise ValueError("Variable names cannot be same")
100
101 if not isinstance(state_names, dict):
ValueError: Variable names cannot be same
Versions:
pgmpy==0.1.20
python 3.8
Linux and Mac
Your environment
- pgmpy version
- Python version
- Operating System
Steps to reproduce
Tell us how to reproduce this issue. Please provide a minimal reproducible code of
the issue you are facing if possible.
Expected behaviour
Tell us what should happen
Actual behaviour
Tell us what happens instead
@GabrielAzevedoFerreiraQB The factor_product
method expects the arguments to be DiscreteFactor
objects, so you will need to explicitly convert the CPDs to factor objects. Something like this works:
In [20]: factor_product(cpd1.to_factor(), cpd2.to_factor(), cpd3.to_factor())
Out[20]: <DiscreteFactor representing phi(c_1:3, c_0:3, z:3, p_0:3, p_1:3, p_2:3) at 0x7f09ed840130>