Defining Bayesian Networks
A Bayesian network is a type of probabilistic graphical model that represents a set of random variables and their conditional dependencies using a directed acyclic graph (DAG). In this graph, each node corresponds to a random variable, and each directed edge encodes a direct probabilistic influence from one variable to another. The strength and nature of these dependencies are captured by conditional probability tables (CPTs), with each node's CPT specifying the probability of that variable given its parent variables in the graph.
12345678910111213141516171819202122232425262728293031323334# Define the structure of the Bayesian network # Variables: Rain, Sprinkler, WetGrass # Parents for each variable parents = { "Rain": [], "Sprinkler": ["Rain"], "WetGrass": ["Rain", "Sprinkler"] } # Conditional probability tables (CPTs) CPT = { # P(Rain) "Rain": { True: 0.2, False: 0.8 }, # P(Sprinkler | Rain) "Sprinkler": { (True,): {True: 0.01, False: 0.99}, # Rain = True (False,): {True: 0.4, False: 0.6} # Rain = False }, # P(WetGrass | Rain, Sprinkler) "WetGrass": { (True, True): {True: 0.99, False: 0.01}, (True, False): {True: 0.8, False: 0.2}, (False, True): {True: 0.9, False: 0.1}, (False, False): {True: 0.0, False: 1.0} } } # Example: how to access the probability that WetGrass is True given Rain=True and Sprinkler=False prob = CPT["WetGrass"][(True, False)][True] print(f"P(WetGrass=True | Rain=True, Sprinkler=False) = {prob}")
In the code above, the parents dictionary encodes the structure of the Bayesian network as a directed acyclic graph: Rain has no parents, Sprinkler depends on Rain, and WetGrass depends on both Rain and Sprinkler. The CPT dictionary specifies the conditional probability tables for each variable. For instance, CPT["Rain"] gives the prior probability of rain, while CPT["Sprinkler"] defines the probability that the sprinkler is on, conditioned on whether it is raining. CPT["WetGrass"] lists the probability that the grass is wet for each possible combination of rain and sprinkler states. Each CPT entry maps a tuple of parent values to a dictionary of probabilities for the variable's possible states, allowing you to quickly look up the relevant probability for any configuration in the network.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Can you explain how to compute the probability of Rain given that WetGrass is True?
How would I extend this Bayesian network to include more variables?
Can you show how to perform inference using this Bayesian network?
Génial!
Completion taux amélioré à 10
Defining Bayesian Networks
Glissez pour afficher le menu
A Bayesian network is a type of probabilistic graphical model that represents a set of random variables and their conditional dependencies using a directed acyclic graph (DAG). In this graph, each node corresponds to a random variable, and each directed edge encodes a direct probabilistic influence from one variable to another. The strength and nature of these dependencies are captured by conditional probability tables (CPTs), with each node's CPT specifying the probability of that variable given its parent variables in the graph.
12345678910111213141516171819202122232425262728293031323334# Define the structure of the Bayesian network # Variables: Rain, Sprinkler, WetGrass # Parents for each variable parents = { "Rain": [], "Sprinkler": ["Rain"], "WetGrass": ["Rain", "Sprinkler"] } # Conditional probability tables (CPTs) CPT = { # P(Rain) "Rain": { True: 0.2, False: 0.8 }, # P(Sprinkler | Rain) "Sprinkler": { (True,): {True: 0.01, False: 0.99}, # Rain = True (False,): {True: 0.4, False: 0.6} # Rain = False }, # P(WetGrass | Rain, Sprinkler) "WetGrass": { (True, True): {True: 0.99, False: 0.01}, (True, False): {True: 0.8, False: 0.2}, (False, True): {True: 0.9, False: 0.1}, (False, False): {True: 0.0, False: 1.0} } } # Example: how to access the probability that WetGrass is True given Rain=True and Sprinkler=False prob = CPT["WetGrass"][(True, False)][True] print(f"P(WetGrass=True | Rain=True, Sprinkler=False) = {prob}")
In the code above, the parents dictionary encodes the structure of the Bayesian network as a directed acyclic graph: Rain has no parents, Sprinkler depends on Rain, and WetGrass depends on both Rain and Sprinkler. The CPT dictionary specifies the conditional probability tables for each variable. For instance, CPT["Rain"] gives the prior probability of rain, while CPT["Sprinkler"] defines the probability that the sprinkler is on, conditioned on whether it is raining. CPT["WetGrass"] lists the probability that the grass is wet for each possible combination of rain and sprinkler states. Each CPT entry maps a tuple of parent values to a dictionary of probabilities for the variable's possible states, allowing you to quickly look up the relevant probability for any configuration in the network.
Merci pour vos commentaires !