This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#P(B | S) セキュリティ会社に連絡がいったとき、泥棒が入った確率 | |
from pgmpy.inference import VariableElimination | |
# 推論エンジンの作成 | |
infer = VariableElimination(model) | |
# P(B | S)を求める | |
result = infer.query(variables=['B'], evidence={'S': 1}) | |
probability_b_given_s = result.values[1] # B = 1の確率を取得 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pgmpy.models import BayesianNetwork | |
from pgmpy.factors.discrete import TabularCPD | |
# ベイジアンネットワークのモデルを作成 | |
model = BayesianNetwork() | |
# ノードを追加 | |
model.add_nodes_from(['B', 'E', 'A', 'P', 'S']) | |
# エッジを追加 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
import matplotlib.pyplot as plt | |
# ベイジアンネットワークの有向グラフを作成 | |
G2 = nx.DiGraph() | |
# ノードを追加 | |
G2.add_node("B") | |
G2.add_node("E") | |
G2.add_node("A") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#P99 確率的推論(事後確率) P(W = 1 | G = 1) 芝が濡れているとき、晴れである確率は? | |
# P(W = 1 | G = 1)を求める | |
result = infer.query(variables=['W'], evidence={'G': 1}) | |
probability_w_given_g_1 = result.values[1] # W = 1の確率を取得 | |
print("P(W = 1 | G = 1):", probability_w_given_g_1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#P101 (1) P(G = 1 | W = 1)を計算:天気が晴れという条件のもとで、芝が濡れている確率 | |
from pgmpy.inference import VariableElimination | |
# 推論エンジンの作成 | |
infer = VariableElimination(model) | |
# P(G = 1 | W = 1)を求める | |
result = infer.query(variables=['G'], evidence={'W': 1}) | |
print(result) | |
probability_g_given_w_1 = result.values[1] # G = 1の確率を取得 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pgmpy.models import BayesianNetwork | |
from pgmpy.factors.discrete import TabularCPD | |
# ベイジアンネットワークのモデルを作成 | |
model = BayesianNetwork() | |
# ノードを追加 | |
model.add_nodes_from(['W', 'S', 'R', 'G']) | |
# エッジを追加 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
import matplotlib.pyplot as plt | |
# ベイジアンネットワークの有向グラフを作成 | |
G = nx.DiGraph() | |
# ノードを追加 | |
G.add_node("W") | |
G.add_node("S") | |
G.add_node("R") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymc as pm | |
import arviz as az | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from scipy.stats import gamma | |
# 観測データ | |
x = daily_sales.values # 観測データの例 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from scipy.stats import nbinom | |
# 新しいデータ点 x_new が取り得る値の範囲 | |
x_new_values = range(0, daily_sales.max()) | |
# 事後予測分布を計算(負の二項分布を使用) | |
p_pred = [nbinom.pmf(k=x_new, n=alpha_posterior, p=beta_posterior/(1+beta_posterior)) for x_new in x_new_values] | |
# 結果をプロット |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculate the mode (MAP estimate) of the posterior gamma distribution | |
if alpha_posterior > 1: | |
lambda_map_estimate = (alpha_posterior - 1) / beta_posterior | |
else: | |
lambda_map_estimate = 0 | |
lambda_map_estimate |
NewerOlder