import bnlearn as bn
from pgmpy.factors.discrete import TabularCPD
flu = TabularCPD(variable='flu', variable_card=2, values=[[0.95], [0.05]])
fever = TabularCPD(variable='fever', variable_card=2,
values=[[0.8, 0.1],
[0.2, 0.9]],
evidence=['flu'], evidence_card=[2])
edges = [('flu', 'fever')]
DAG = bn.make_DAG(edges)
DAG = bn.make_DAG(DAG, CPD=[flu, fever])
# bn.print_CPD(DAG)
# Probability of flu when fever?
bn.inference.fit(DAG, variables=['flu'], evidence={'fever': 1})
from pomegranate import *
flu = DiscreteDistribution({True: 0.05, False: 0.95})
fever = ConditionalProbabilityTable([[True, True, 0.9],
[True, False, 0.1],
[False, True, 0.2],
[False, False, 0.9]],
[flu])
s_flu = State(flu, name='flu')
s_fever = State(fever, name='fever')
model = BayesianNetwork()
model.add_states(s_flu, s_fever)
model.add_edge(s_flu, s_fever)
model.bake()
# Probability of flu when fever?
model.predict_proba({'fever': True})
model.predict_proba([[None, True]])
import bnlearn as bn
from pgmpy.factors.discrete import TabularCPD
rain = TabularCPD(variable='rain', variable_card=2, values=[[0.7], [0.3]])
sprinkler = TabularCPD(variable='sprinkler', variable_card=2,
values=[[0.2, 0.9],
[0.8, 0.1]],
evidence=['rain'], evidence_card=[2])
wet = TabularCPD(variable='wet', variable_card=2,
values=[[0.99, 0.1, 0.3, 0.05],
[0.01, 0.9, 0.7, 0.95]],
evidence=['rain', 'sprinkler'],
evidence_card=[2, 2])
edges = [('rain', 'sprinkler'),
('rain', 'wet'),
('sprinkler', 'wet')]
DAG = bn.make_DAG(edges)
DAG = bn.make_DAG(DAG, CPD=[rain, sprinkler, wet])
# bn.print_CPD(DAG)
# Probability of wet when no rain
bn.inference.fit(DAG, variables=['wet'], evidence={'rain': 0})
# Probability of wet when no rain and no sprinkler
bn.inference.fit(DAG, variables=['wet'], evidence={'rain': 0, 'sprinkler': 0})
# Probability of rain when wet
bn.inference.fit(DAG, variables=['rain'], evidence={'wet': 1})
# Probability of rain and/or sprinkler when wet
bn.inference.fit(DAG, variables=['rain', 'sprinkler'], evidence={'wet': 1})
from pomegranate import *
rain = DiscreteDistribution({True: 0.7, False: 0.3})
sprinkler = ConditionalProbabilityTable([[True, True, 0.1],
[True, False, 0.9],
[False, True, 0.8],
[False, False, 0.2]],
[rain])
wet = ConditionalProbabilityTable(
[[True, True, True, 0.95],
[True, True, False, 0.05],
[True, False, True, 0.7],
[True, False, False, 0.3],
[False, True, True, 0.9],
[False, True, False, 0.1],
[False, False, True, 0.01],
[False, False, False, 0.99]],
[rain, sprinkler])
s_rain = State(rain, name='rain')
s_sprinkler = State(sprinkler, name='sprinkler')
s_wet = State(wet, name='wet')
model = BayesianNetwork()
model.add_states(s_rain, s_sprinkler, s_wet)
model.add_edge(s_rain, s_sprinkler)
model.add_edge(s_rain, s_wet)
model.add_edge(s_sprinkler, s_wet)
model.bake()
# Probability of wet when no rain
model.predict_proba({'rain': False})
# Probability of wet when no rain and no sprinkler
model.predict_proba({'rain': False, 'sprinkler': False})
import bnlearn as bn
from pgmpy.factors.discrete import TabularCPD
burglary = TabularCPD(variable='burglary', variable_card=2, values=[[0.999], [0.001]])
earthquake = TabularCPD(variable='earthquake', variable_card=2, values=[[0.998], [0.002]])
alarm = TabularCPD(variable='alarm', variable_card=2,
values=[[0.999, 0.71, 0.06, 0.05],
[0.001, 0.29, 0.94, 0.95]],
evidence=['burglary', 'earthquake'],
evidence_card=[2, 2])
john = TabularCPD(variable='john', variable_card=2,
values=[[0.95, 0.1],
[0.05, 0.9]],
evidence=['alarm'], evidence_card=[2])
mary = TabularCPD(variable='mary', variable_card=2,
values=[[0.99, 0.3],
[0.01, 0.7]],
evidence=['alarm'], evidence_card=[2])
edges = [('burglary', 'alarm'),
('earthquake', 'alarm'),
('alarm', 'john'),
('alarm', 'mary')]
DAG = bn.make_DAG(edges)
DAG = bn.make_DAG(DAG, CPD=[burglary, earthquake, alarm, john, mary])
# bn.print_CPD(DAG)
# Diagnostyczne Magda zadzwoniła Czy było włamanie?
bn.inference.fit(DAG, variables=['burglary'], evidence={'mary': 1})
# Predykcyjne Włączył się alarm Czy zadzwoni Jan lub Magda?
bn.inference.fit(DAG, variables=['john', 'mary'], evidence={'alarm': 1})
# Wyjaśniające Włączył się alarm i było trzęsienie z. Czy nie było również włamania?
bn.inference.fit(DAG, variables=['burglary'], evidence={'alarm': 1, 'earthquake': 1})
# Mieszane Było trzęsienie ziemi i zadzwonił Jan Czy włączył się też alarm?
bn.inference.fit(DAG, variables=['alarm'], evidence={'earthquake': 1, 'john': 1})
# Jakie p-stwo ze Jan zadzwoni
bn.inference.fit(DAG, variables=['john'], evidence={})
from pomegranate import *
burglary = DiscreteDistribution({True: 0.001, False: 0.999})
earthquake = DiscreteDistribution({True: 0.002, False: 0.998})
alarm = ConditionalProbabilityTable([[True, True, True, 0.95],
[True, True, False, 0.05],
[True, False, True, 0.94],
[True, False, False, 0.06],
[False, True, True, 0.29],
[False, True, False, 0.71],
[False, False, True, 0.001],
[False, False, False, 0.999]],
[burglary, earthquake])
john = ConditionalProbabilityTable(
[[True, True, 0.9],
[True, False, 0.1],
[False, True, 0.05],
[False, False, 0.95]],
[alarm])
mary = ConditionalProbabilityTable(
[[True, True, 0.7],
[True, False, 0.3],
[False, True, 0.01],
[False, False, 0.99]],
[alarm])
s_burglary = State(burglary, name='burglary')
s_earthquake = State(earthquake, name='earthquake')
s_alarm = State(alarm, name='alarm')
s_john = State(john, name='john')
s_mary = State(mary, name='mary')
model = BayesianNetwork()
model.add_states(s_burglary, s_earthquake, s_alarm, s_john, s_mary)
model.add_edge(s_burglary, s_alarm)
model.add_edge(s_earthquake, s_alarm)
model.add_edge(s_alarm, s_john)
model.add_edge(s_alarm, s_mary)
model.bake()
# Diagnostyczne Magda zadzwoniła Czy było włamanie?
model.predict_proba({'mary': True})[0].items()[0]
# Predykcyjne Włączył się alarm Czy zadzwoni Jan lub Magda?
sum(model.probability([[None, None, True, True, False],
[None, None, True, False, True],
[None, None, True, True, True]]))
# Wyjaśniające Włączył się alarm i było trzęsienie z. Czy nie było również włamania?
model.predict_proba({'alarm': True, 'earthquake': True})[0]
# Mieszane Było trzęsienie ziemi i zadzwonił Jan Czy włączył się też alarm?
model.predict_proba({'earthquake': True, 'john': True})[2]