Exemplo n.º 1
0
        [0.1, 0.2, 1, 1, 0.8, 0.9, 1, 1]
    ],  #p(~G)
    evidence=[
        'BrokeElectionLaw', 'PoliticallyMotivatedProsecutor', 'Indicted'
    ],
    evidence_card=[2, 2, 2])

cpd_j = TabularCPD(variable='Jailed',
                   variable_card=2,
                   values=[[0.9, 0.0], [0.1, 1.0]],
                   evidence=['FoundGuilty'],
                   evidence_card=[2])

#Associar os model aos nodos
election_model.add_cpds(cpd_b, cpd_i, cpd_m, cpd_g, cpd_j)

#Verificar as independencias
print(election_model.get_independencies())

samples = BayesianModelSampling(election_model).forward_sample(size=int(1e5))
samples.head()

#Mostrar estimativas
mle = MaximumLikelihoodEstimator(model=election_model, data=samples)
print("\nEstimating the CPD for a single node.\n")
print(mle.estimate_cpd(node='BrokeElectionLaw'))
print(mle.estimate_cpd(node='PoliticallyMotivatedProsecutor'))
print(mle.estimate_cpd(node='Indicted'))
print(mle.estimate_cpd(node='FoundGuilty'))
print(mle.estimate_cpd(node='Jailed'))
#Step 1: Generate some data
# Use the alarm model to generate data from it.

from pgmpy.utils import get_example_model
from pgmpy.sampling import BayesianModelSampling


alarm_model = get_example_model('alarm')
samples = BayesianModelSampling(alarm_model).forward_sample(size=int(1e5))
print(samples.head())

#Step 2: Define a model structure
# Defining the Bayesian Model structure

from pgmpy.models import BayesianModel

model_struct = BayesianModel(ebunch=alarm_model.edges())
print(model_struct.nodes())

#Step 3: Learning the model parameters
# Fitting the model using Maximum Likelihood Estimator

from pgmpy.estimators import MaximumLikelihoodEstimator

mle = MaximumLikelihoodEstimator(model=model_struct, data=samples)

# Estimating the CPD for a single node.
print(mle.estimate_cpd(node='FIO2'))
print(mle.estimate_cpd(node='CVP'))

# Estimating CPDs for all the nodes in the model