<jupyter_start><jupyter_text>* 결합확률 모형`JointProbabiltiyDistribution(variables, cardinality, values)`<jupyter_code>from pgmpy.factors.discrete import JointProbabilityDistribution as JPD

px = JPD(['X'], [2], np.array([12, 8]) / 20)
print(px)
py = JPD(['Y'], [2], np.array([10, 10])/20)
print(py)
pxy = JPD(['X', 'Y'], [2, 2], np.array([3, 9, 7, 1])/20)
print(pxy)
pxy2 = JPD(['X', 'Y'], [2, 2], np.array([6, 6, 4, 4])/20)
print(pxy2)<jupyter_output>+-----+-----+----------+
| X   | Y   |   P(X,Y) |
+=====+=====+==========+
| X_0 | Y_0 |   0.3000 |
+-----+-----+----------+
| X_0 | Y_1 |   0.3000 |
+-----+-----+----------+
| X_1 | Y_0 |   0.2000 |
+-----+-----+----------+
| X_1 | Y_1 |   0.2000 |
+-----+-----+----------+<jupyter_text>__주변확률 계산__- `marginal_distribution(values, inplace=False)`     - values: 주변확률을 구할 확률변수 이름 문자열 리스트- `marginalize(values, inplace=False)`    - values: 주변확률을 구하기 위해 없앨 확률변수 이름 문자열 리스트    __조건부확률__- `conditional_distribution(values, inplace=False)`    - values: 주변확률을 구할 확률변수 이름 문자열과 값을 묶은 튜플의 리스트[()]<jupyter_code># 경우1
pmx = pxy.marginal_distribution(['X'], inplace=False)
print(pmx)
pmy = pxy.marginalize(['X'], inplace=False)
print(pmy)
# 사건 A에 대한 조건부확률
py_on_x0 = pxy.conditional_distribution([('X', 0)], inplace=False)
print(py_on_x0)
py_on_x1 = pxy.conditional_distribution([('X', 1)], inplace=False)
print(py_on_x1)
px_on_y0 = pxy.conditional_distribution([('Y', 0)], inplace=False)
print(px_on_y0)
px_on_y1 = pxy.conditional_distribution([('Y', 1)], inplace=False)
print(px_on_y1)
pxy.check_independence(['X'], ['Y'])
# False: 두 확률변수 X와 Y는 독립이 아님
print(px*py) 
# 두 객체가 독립이라는 가정 하에 결합확률을 구함. 
print('\n', pxy)<jupyter_output>+-----+-----+----------+
| X   | Y   |   P(X,Y) |
+=====+=====+==========+
| X_0 | Y_0 |   0.3000 |
+-----+-----+----------+
| X_0 | Y_1 |   0.3000 |
+-----+-----+----------+
| X_1 | Y_0 |   0.2000 |
+-----+-----+----------+
| X_1 | Y_1 |   0.2000 |
+-----+-----+----------+

 +-----+-----+----------+
| X   | Y   |   P(X,Y) |
+=====+=====+==========+
| X_0 | Y_0 |   0.1500 |
+-----+-----+----------+
| X_0 | Y_1 |   0.4500 |
+-----+-----+----------+
| X_1 | Y_0 |   0.3500 |
+-----+-----+----------+
| X_1 | Y_1 |   0.0500 |
+-----+-----+----------+<jupyter_text>연습문제 6.5.8<jupyter_code># 경우 2
print(pxy2) 결합확률
px2 = JPD(['X'], [2], np.array([12, 8])/20)
print(px2)
# 성별
py2 = JPD(['Y'], [2], np.array([10, 10])/20)
print(py2)
# 머리길이
pmx2 = pxy2.marginalize(['Y'], inplace=False)
print(pmx2)
pmy2 = pxy2.marginal_distribution(['Y'], inplace=False)
print(pmy2)
py2_on_x0 = pxy2.conditional_distribution([('X', 0)], inplace=False)
print(py2_on_x0)
py2_on_x1 = pxy2.conditional_distribution([('X', 1)], inplace=False)
print(py2_on_x1)
px2_on_y0 = pxy2.conditional_distribution([('Y', 0)], inplace=False)
print(px2_on_y0)
px2_on_y1 = pxy2.conditional_distribution([('Y', 1)], inplace=False)
print(px2_on_y1)
pxy2.check_independence(['X'], ['Y'])
print(px2 * py2)
print('\n', pxy2)<jupyter_output>+-----+-----+----------+
| X   | Y   |   P(X,Y) |
+=====+=====+==========+
| X_0 | Y_0 |   0.3000 |
+-----+-----+----------+
| X_0 | Y_1 |   0.3000 |
+-----+-----+----------+
| X_1 | Y_0 |   0.2000 |
+-----+-----+----------+
| X_1 | Y_1 |   0.2000 |
+-----+-----+----------+

 +-----+-----+----------+
| X   | Y   |   P(X,Y) |
+=====+=====+==========+
| X_0 | Y_0 |   0.3000 |
+-----+-----+----------+
| X_0 | Y_1 |   0.3000 |
+-----+-----+----------+
| X_1 | Y_0 |   0.2000 |
+-----+-----+----------+
| X_1 | Y_1 |   0.2000 |
+-----+-----+----------+<jupyter_text>**베이즈정리*** 조건부확률 구현`TaubularCPD(variable, variable_card, value, evidence=None, evidence_card=None)`<jupyter_code>from pgmpy.factors.discrete import TabularCPD

# P(D; X=1)와 P(X=0)
cpd_X = TabularCPD('X', 2, [[1 - 0.002, 0.002]])
print(cpd_X)
cpd_Y_on_X = TabularCPD('Y', 2, np.array([[0.95, 0.01], [0.05, 0.99]]), evidence=['X'], evidence_card=[2])
print(cpd_Y_on_X)
np.array([[0.95, 0.01], [0.05, 0.99]])<jupyter_output><empty_output><jupyter_text>* 확률변수가 어떻게 결합되어 있는지`BayesianModel(variables)`- add_cpds(): 조건부확률 추가- check_model(): 모형이 정상적인지 확인* `VariableElimination` 클래스로 변수제거법을 사용한 추정 객체를 만들고, 이 객체의 `query(variables, evidences)`메서드를 사용하여 사후확률 계산<jupyter_code>from pgmpy.models import BayesianModel

model = BayesianModel([('X', 'Y')])
model.add_cpds(cpd_X, cpd_Y_on_X)
model.check_model()
from pgmpy.inference import VariableElimination

inference = VariableElimination(model)
posterior = inference.query(['X'], evidence={'Y':1})
print(posterior['X'])<jupyter_output>+-----+----------+
| X   |   phi(X) |
+=====+==========+
| X_0 |   0.9618 |
+-----+----------+
| X_1 |   0.0382 |
+-----+----------+
{'fraction_non_alphanumeric': 0.33030892201339385, 'fraction_numerical': 0.06286454957874271, 'mean_word_length': 2.7248592115848753, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '627843', 'license': 'MIT', 'n_tokens_mistral': 2416, 'n_tokens_neox': 2404, 'n_words': 343, 'path': 'math/0206-math review.ipynb', 'repo_name': 'pearl-lee/TIL', 'size': '4629'}