<jupyter_start><jupyter_text>연습문제 6.5.6<jupyter_code>py = JPD(['Y'], [2], np.array([10, 10]) / 20)
print(py)<jupyter_output>+------+--------+
| Y    |   P(Y) |
+======+========+
| Y(0) | 0.5000 |
+------+--------+
| Y(1) | 0.5000 |
+------+--------+<jupyter_text>연습문제 6.5.7<jupyter_code>pxy2 = JPD(['X', 'Y'], [2, 2], [0.3, 0.3, 0.2, 0.2])
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>주변확률분포, 주변화<jupyter_code>pxy = JPD(['X', 'Y'], [2, 2], np.array([3, 9, 7, 1]) / 20)
print(pxy)
pmx = pxy.marginal_distribution(['X'], inplace=False)
print(pmx)
pmx = pxy.marginalize(['Y'], inplace=False)
print(pmx)<jupyter_output>+------+--------+
| X    |   P(X) |
+======+========+
| X(0) | 0.6000 |
+------+--------+
| X(1) | 0.4000 |
+------+--------+<jupyter_text>조건부확률<jupyter_code>py_on_x1 = pxy.conditional_distribution([('X', 1)], inplace=False)
print(py_on_x1)<jupyter_output>+------+--------+
| Y    |   P(Y) |
+======+========+
| Y(0) | 0.8750 |
+------+--------+
| Y(1) | 0.1250 |
+------+--------+<jupyter_text>두 변수간의 독립여부 확인<jupyter_code>pxy.check_independence(['X'], ['Y'])<jupyter_output><empty_output><jupyter_text>두 변수가 독립인지 사실여부 체크<jupyter_code>print(px * py)
print(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>pmx2 = pxy2.marginal_distribution(['X'], inplace=False)
print(pmx)
pmx2 = pxy2.marginalize(['Y'], inplace=False)
print(pmx)
py_on_x1 = pxy2.conditional_distribution([('X', 1)], inplace=False)
print(py_on_x1)
pxy2.check_independence(['X'], ['Y'])<jupyter_output><empty_output><jupyter_text>베이즈 정리<jupyter_code>from pgmpy.factors.discrete import TabularCPD

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)<jupyter_output>+------+------+------+
| X    | X(0) | X(1) |
+------+------+------+
| Y(0) | 0.95 | 0.01 |
+------+------+------+
| Y(1) | 0.05 | 0.99 |
+------+------+------+<jupyter_text>BayesianModel<jupyter_code>from pgmpy.models import BayesianModel

model = BayesianModel([('X', 'Y')])
model.add_cpds(cpd_X, cpd_Y_on_X)
model.check_model()<jupyter_output><empty_output><jupyter_text>VariableElimination 클래스의 query() 메서드를 활용한 사후확률 계산<jupyter_code>from pgmpy.inference import VariableElimination

inference = VariableElimination(model)
posterior = inference.query(['X'], evidence={'Y': 1})
print(posterior['X'])<jupyter_output>Finding Elimination Order: : : 0it [00:00, ?it/s]
0it [00:00, ?it/s]
{'fraction_non_alphanumeric': 0.3946745562130177, 'fraction_numerical': 0.06479289940828402, 'mean_word_length': 2.3114593535749264, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '656918', 'license': 'MIT', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1646, 'n_words': 198, 'path': 'statistics/200427-stat.ipynb', 'repo_name': 'Yeongmin1992/Python-Statistics', 'size': '3380'}