|
16 | 16 | { |
17 | 17 | "data": { |
18 | 18 | "application/vnd.jupyter.widget-view+json": { |
19 | | - "model_id": "fd9e14d3-276a-42e7-a4ee-d0b721a148aa", |
| 19 | + "model_id": "a4d84936-b337-4880-963c-b61be6b8c440", |
20 | 20 | "version_major": 2, |
21 | 21 | "version_minor": 0 |
22 | 22 | }, |
|
400 | 400 | }, |
401 | 401 | { |
402 | 402 | "cell_type": "code", |
403 | | - "execution_count": 12, |
| 403 | + "execution_count": 7, |
404 | 404 | "metadata": {}, |
405 | 405 | "outputs": [ |
406 | 406 | { |
407 | 407 | "name": "stdout", |
408 | 408 | "output_type": "stream", |
409 | 409 | "text": [ |
410 | | - "Joint Distribution = <0.108, 0.012, 0.016, 0.064, 0.072, 0.008, 0.144, 0.576>\n" |
| 410 | + "Joint Distribution = <0.108, 0.012, 0.016, 0.064, 0.072, 0.008, 0.144, 0.576>\n", |
| 411 | + "Probability of having cavity and toothache but not of catch = 0.012\n" |
411 | 412 | ] |
412 | 413 | }, |
413 | 414 | { |
414 | | - "ename": "ERROR", |
415 | | - "evalue": " java.lang.IllegalArgumentException", |
416 | | - "output_type": "error", |
417 | | - "traceback": [ |
418 | | - "\u001b[1;31mERROR: java.lang.IllegalArgumentException: Assignments passed in is not the same size as variables making up probability table.\u001b[0;0m" |
419 | | - ] |
| 415 | + "data": { |
| 416 | + "text/plain": [ |
| 417 | + "null" |
| 418 | + ] |
| 419 | + }, |
| 420 | + "execution_count": 7, |
| 421 | + "metadata": {}, |
| 422 | + "output_type": "execute_result" |
420 | 423 | } |
421 | 424 | ], |
422 | 425 | "source": [ |
|
458 | 461 | "System.out.println(\"Joint Distribution = \"+jointDistribution.toString());\n", |
459 | 462 | "\n", |
460 | 463 | "// We can then ask about any subset of the jointDistribution\n", |
461 | | - "// Lets ask about the probability of having a toothache and cavity\n", |
| 464 | + "// Lets ask about the probability of having a toothache and cavity but not of catch\n", |
462 | 465 | "// We will use the AssignmentProposition class for assigning the values to the random variables.\n", |
463 | 466 | "AssignmentProposition toothache = new AssignmentProposition(toothacheRv,true);\n", |
464 | 467 | "AssignmentProposition cavity = new AssignmentProposition(cavityRv,true);\n", |
465 | | - "System.out.println(\"Probability of having cavity and toothache = \" + jointDistribution.getValue(toothache));" |
| 468 | + "AssignmentProposition catchVal = new AssignmentProposition(catchRv,false);\n", |
| 469 | + "System.out.println(\"Probability of having cavity and toothache but not of catch = \" +\n", |
| 470 | + " jointDistribution.getValue(toothache,cavity,catchVal));" |
| 471 | + ] |
| 472 | + }, |
| 473 | + { |
| 474 | + "cell_type": "markdown", |
| 475 | + "metadata": {}, |
| 476 | + "source": [ |
| 477 | + "From the preceding definition of possible worlds, it follows that a probability model is\n", |
| 478 | + "completely determined by the joint distribution for all of the random variables—the so-called\n", |
| 479 | + "**full joint probability distribution**. For example, if the variables are Cavity, Toothache ,\n", |
| 480 | + "and Weather , then the full joint distribution is given by P(Cavity, Toothache , Weather ).\n", |
| 481 | + "This joint distribution can be represented as a 2 × 2 × 4 table with 16 entries. the above example represents the full joint distribution of a similar case. To encode a probability model using a full joint distribiution we can use the `FullJointDistributionModel` class from the code repo as follows :" |
| 482 | + ] |
| 483 | + }, |
| 484 | + { |
| 485 | + "cell_type": "code", |
| 486 | + "execution_count": 16, |
| 487 | + "metadata": {}, |
| 488 | + "outputs": [ |
| 489 | + { |
| 490 | + "name": "stdout", |
| 491 | + "output_type": "stream", |
| 492 | + "text": [ |
| 493 | + "The random variables in the model = [Toothache, Cavity, Catch]\n", |
| 494 | + "The prior probability of having a toothache = 0.2\n", |
| 495 | + "The prior probability of having a cavity = 0.2\n", |
| 496 | + "The probability of having a cavity and toothache simultaneously is = 0.12\n", |
| 497 | + "The probability of having a toothache given that the person has a cavity(causal direction) is = 0.6\n", |
| 498 | + "The probability of having a cavity given that the person is experiencing toothache(diagnostic direction) is = 0.6\n" |
| 499 | + ] |
| 500 | + }, |
| 501 | + { |
| 502 | + "data": { |
| 503 | + "text/plain": [ |
| 504 | + "null" |
| 505 | + ] |
| 506 | + }, |
| 507 | + "execution_count": 16, |
| 508 | + "metadata": {}, |
| 509 | + "output_type": "execute_result" |
| 510 | + } |
| 511 | + ], |
| 512 | + "source": [ |
| 513 | + "package aima.notebooks.quantifyinguncertainty;\n", |
| 514 | + "\n", |
| 515 | + "import aima.core.probability.domain.*;\n", |
| 516 | + "import aima.core.probability.*;\n", |
| 517 | + "import aima.core.probability.util.*;\n", |
| 518 | + "import aima.core.probability.proposition.*;\n", |
| 519 | + "import aima.core.probability.full.*;\n", |
| 520 | + "\n", |
| 521 | + "// First we define the random variables\n", |
| 522 | + "RandVar cavityRv = new RandVar(\"Cavity\", new BooleanDomain());\n", |
| 523 | + "RandVar toothacheRv = new RandVar(\"Toothache\", new BooleanDomain());\n", |
| 524 | + "RandVar catchRv = new RandVar(\"Catch\", new BooleanDomain());\n", |
| 525 | + "\n", |
| 526 | + "\n", |
| 527 | + "// Create the full joint distribution from the table given above.\n", |
| 528 | + "FullJointDistributionModel model = new FullJointDistributionModel(\n", |
| 529 | + " new double[] {\n", |
| 530 | + "\t\t\t\t// Toothache = true, Cavity = true, Catch = true\n", |
| 531 | + "\t\t\t\t0.108,\n", |
| 532 | + "\t\t\t\t// Toothache = true, Cavity = true, Catch = false\n", |
| 533 | + "\t\t\t\t0.012,\n", |
| 534 | + "\t\t\t\t// Toothache = true, Cavity = false, Catch = true\n", |
| 535 | + "\t\t\t\t0.016,\n", |
| 536 | + "\t\t\t\t// Toothache = true, Cavity = false, Catch = false\n", |
| 537 | + "\t\t\t\t0.064,\n", |
| 538 | + "\t\t\t\t// Toothache = false, Cavity = true, Catch = true\n", |
| 539 | + "\t\t\t\t0.072,\n", |
| 540 | + "\t\t\t\t// Toothache = false, Cavity = true, Catch = false\n", |
| 541 | + "\t\t\t\t0.008,\n", |
| 542 | + "\t\t\t\t// Toothache = false, Cavity = false, Catch = true\n", |
| 543 | + "\t\t\t\t0.144,\n", |
| 544 | + "\t\t\t\t// Toothache = false, Cavity = false, Catch = false\n", |
| 545 | + "\t\t\t\t0.576 },\n", |
| 546 | + " toothacheRv,cavityRv,catchRv);\n", |
| 547 | + "// Let's define a few assignments\n", |
| 548 | + "AssignmentProposition toothache = new AssignmentProposition(toothacheRv,true);\n", |
| 549 | + "AssignmentProposition cavity = new AssignmentProposition(cavityRv,true);\n", |
| 550 | + "// Now let's have a look at what we can do with the model.\n", |
| 551 | + "// To print the random variables in the model\n", |
| 552 | + "System.out.println(\"The random variables in the model = \" + model.getRepresentation());\n", |
| 553 | + "// We can calculate the prior probabilities of a variety of combinations of random variables\n", |
| 554 | + "System.out.println(\"The prior probability of having a toothache = \"+ model.prior(toothache));\n", |
| 555 | + "System.out.println(\"The prior probability of having a cavity = \"+ model.prior(cavity));\n", |
| 556 | + "System.out.println(\"The probability of having a cavity and toothache simultaneously is = \"+ model.prior(toothache, cavity));\n", |
| 557 | + "// We can also calculate a variety of posterior probabilities from the model as follows\n", |
| 558 | + "System.out.println(\"The probability of having a toothache given that the person has a cavity(causal direction) is = \"+ \n", |
| 559 | + " model.posterior(toothache,cavity));\n", |
| 560 | + "System.out.println(\"The probability of having a cavity given that the person is experiencing toothache(diagnostic direction) is = \"\n", |
| 561 | + " + model.posterior(cavity,toothache));\n" |
| 562 | + ] |
| 563 | + }, |
| 564 | + { |
| 565 | + "cell_type": "markdown", |
| 566 | + "metadata": {}, |
| 567 | + "source": [ |
| 568 | + "## Inference using full joint distributions" |
466 | 569 | ] |
467 | 570 | }, |
468 | 571 | { |
|
0 commit comments