From ab7f1237d1f24bc0fcf5ddfe8cdec4e988ef3506 Mon Sep 17 00:00:00 2001 From: ligerfotis Date: Tue, 28 Mar 2023 21:48:15 +0200 Subject: [PATCH] bonus unsolved --- assignment 3/bonus_unsloved.ipynb | 185 ++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 assignment 3/bonus_unsloved.ipynb diff --git a/assignment 3/bonus_unsloved.ipynb b/assignment 3/bonus_unsloved.ipynb new file mode 100644 index 0000000..a10c05a --- /dev/null +++ b/assignment 3/bonus_unsloved.ipynb @@ -0,0 +1,185 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "source": [ + "## Bonus Exercise: Bayes' Rule\n", + "### Description\n", + "In this exercise, you will implement Bayes' rule to solve a simple card game." + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": 29, + "outputs": [], + "source": [ + "import random\n", + "\n", + "class CardDeck:\n", + " def __init__(self):\n", + " # Initialize the deck of cards\n", + " self.cards = ['R', 'R', 'B', 'B']\n", + " # Shuffle the deck of cards\n", + " random.shuffle(self.cards)\n", + " # Initialize the prior probability\n", + " \n", + "\n", + " def draw_card(self, index):\n", + " \"\"\"\n", + " Draw a card from the deck\n", + " :param index: the index of the card to be drawn\n", + " :return: the card and its color\n", + " \"\"\"\n", + " card = self.cards.pop(index)\n", + " color = 'Red' if card == 'R' else 'Black'\n", + " return (card, color)\n", + "\n", + " def get_card_color(self, index):\n", + " \"\"\"\n", + " Get the color of a card\n", + " :return: the color of the card\n", + " \"\"\"\n", + " card = self.cards[index]\n", + " color = 'Red' if card == 'R' else 'Black'\n", + " return color\n", + "\n", + " def get_remainder(self):\n", + " \"\"\"\n", + " Get the remainder deck\n", + " :return: the remainder deck\n", + " \"\"\"\n", + " return self.cards\n", + "\n", + " \"\"\"\n", + " Update the prior probability based on Bayes' rule\n", + " function name: update_probabilities\n", + " \"\"\"\n", + " ### function missing here\n", + " \n", + " \"\"\"\n", + " Calculate the posterior probability based on the prior probability, likelihood, and marginal likelihood\n", + " function name: get_posterior\n", + " \"\"\"\n", + " ### function missing here\n", + " \n", + "\n", + " def play_game(self):\n", + " while self.cards:\n", + " for i, card in enumerate(self.cards):\n", + " print(f'{i}: Unknown')\n", + " # only one card left\n", + " if len(self.cards) == 1:\n", + " # code missing here\n", + " \n", + " self.cards.pop()\n", + " else:\n", + " index = int(input(f\"Remainder deck: {self.get_remainder()}\\nEnter the index of the card you want to draw:\"))\n", + " card, color = self.draw_card(index-1)\n", + " print(f'You drew a {color} card with value {card}')\n", + "\n", + " # Calculate the likelihood and marginal likelihood based on the color of the drawn card\n", + " \n", + " # Update the prior probability based on Bayes' rule\n", + " \n", + " # Print the posterior probabilities\n", + " print(f\"After drawing a {card} card:\")\n", + " print(f\"The probability that the other card is red: {posterior_red:.2f}\")\n", + " print(f\"The probability that the other card is black: {posterior_black:.2f}\")\n", + " print(\"The deck is empty.\")\n", + "\n", + "\n" + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "markdown", + "source": [ + "play the game by running the following code:\n" + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0: Unknown\n", + "1: Unknown\n", + "2: Unknown\n", + "3: Unknown\n", + "You drew a Red card with value R\n", + "After drawing a R card:\n", + "The probability that the other card is red: 0.33\n", + "The probability that the other card is black: 0.67\n", + "0: Unknown\n", + "1: Unknown\n", + "2: Unknown\n", + "You drew a Black card with value B\n", + "After drawing a B card:\n", + "The probability that the other card is red: 0.33\n", + "The probability that the other card is black: 0.67\n", + "0: Unknown\n", + "1: Unknown\n" + ] + } + ], + "source": [ + "deck = CardDeck()\n", + "deck.play_game()\n" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "is_executing": true + } + } + }, + { + "cell_type": "markdown", + "source": [], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [], + "metadata": { + "collapsed": false + } + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}