bonus unsolved
This commit is contained in:
parent
61a038e980
commit
ab7f1237d1
185
assignment 3/bonus_unsloved.ipynb
Normal file
185
assignment 3/bonus_unsloved.ipynb
Normal file
@ -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",
|
||||
" <code>\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",
|
||||
" <code>\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",
|
||||
" <code>\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",
|
||||
" <code>\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",
|
||||
" <code>\n",
|
||||
" # Update the prior probability based on Bayes' rule\n",
|
||||
" <code>\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
|
||||
}
|
Loading…
Reference in New Issue
Block a user