HackMD
  • Beta
    Beta  Get a sneak peek of HackMD’s new design
    Turn on the feature preview and give us feedback.
    Go → Got it
      • Create new note
      • Create a note from template
    • Beta  Get a sneak peek of HackMD’s new design
      Beta  Get a sneak peek of HackMD’s new design
      Turn on the feature preview and give us feedback.
      Go → Got it
      • Sharing Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • More (Comment, Invitee)
      • Publishing
        Please check the box to agree to the Community Guidelines.
        Everyone on the web can find and read all notes of this public team.
        After the note is published, everyone on the web can find and read this note.
        See all published notes on profile page.
      • Commenting Enable
        Disabled Forbidden Owners Signed-in users Everyone
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Invitee
      • No invitee
      • Options
      • Versions and GitHub Sync
      • Transfer ownership
      • Delete this note
      • Template
      • Save as template
      • Insert from template
      • Export
      • Dropbox
      • Google Drive Export to Google Drive
      • Gist
      • Import
      • Dropbox
      • Google Drive Import from Google Drive
      • Gist
      • Clipboard
      • Download
      • Markdown
      • HTML
      • Raw HTML
    Menu Sharing Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Versions and GitHub Sync Transfer ownership Delete this note
    Export
    Dropbox Google Drive Export to Google Drive Gist
    Import
    Dropbox Google Drive Import from Google Drive Gist Clipboard
    Download
    Markdown HTML Raw HTML
    Back
    Sharing
    Sharing Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Note Permission
    Read
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    More (Comment, Invitee)
    Publishing
    Please check the box to agree to the Community Guidelines.
    Everyone on the web can find and read all notes of this public team.
    After the note is published, everyone on the web can find and read this note.
    See all published notes on profile page.
    More (Comment, Invitee)
    Commenting Enable
    Disabled Forbidden Owners Signed-in users Everyone
    Permission
    Owners
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Invitee
    No invitee
       owned this note    owned this note      
    Published Linked with GitHub
    Like1 BookmarkBookmarked
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Monty Hall Problem The Monty Hall problem is a brain teaser, in the form of a probability puzzle, loosely based on the American television game show *Let's Make a Deal* and named after its original host, Monty Hall. - There are 3 doors, behind which are two goats and a car. - You pick a door (call it door A). You're hoping for the car of course. - Monty Hall, the game show host, examines the other doors (B & C) and opens one with a goat. (If both doors have goats, he picks randomly.) ## Define a Monty Hall game as a Python function - **input**: your initial choice [0, 1, 2]; and your decision ['stay', 'switch'] - **outcome**: win or loss ```python def MHGame(door, d): print('You select door: %d' %door) ## initialize the setting out = np.zeros(3) ## generate a door with a car door0 = random.randint(0, 2) out[door0] = 1. ## Monty opens a door with a goat res_door = set([0,1,2]) - set([door0, door]) open_door = random.choice(list(res_door)) print('Monty open the door: %d' %open_door) if d == 'stay': final_door = door print('You decide to stay...') final_out = out[door] elif d == 'switch': print('You decide to switch...') final_door = list(set([0,1,2]) - set([door, open_door]))[0] final_out = out[final_door] else: print('please input a correct decision: stay or switch') print('The true door is: %d; your final choice is: %d' %(door0, final_door)) if final_out == 1: print('You win!') else: print('Sorry, You lose!') return final_out ``` ```python ## test your function MHGame(door=1, d='stay') ``` > You select door: 1 > Monty open the door: 2 > You decide to stay... > The true door is: 1; your final choice is: 1 > You win! ```python ## test your function MHGame(door=2, d='switch') ``` > You select door: 2 > Monty open the door: 1 > You decide to switch... > The true door is: 0; your final choice is: 0 > You win! ## Statistical simulation - Run `MHGame()` 1000 times with different decisions, to see which one is better ```python res = {'door': [], 'decision': [], 'out': []} n_trial = 1000 for i in range(n_trial): for d in ['switch', 'stay']: for door in range(3): out_tmp = MHGame(door=2, d=d) res['door'].append(door) res['decision'].append(d) res['out'].append(out_tmp) ``` ```python res = pd.DataFrame(res) ``` ## Analyze the result: - Compute the conditional probability - Visualize the results ```python for d in ['stay', 'switch']: freq_tmp = res[res['decision'] == d]['out'].mean() print('The freq of %s to win the game is: %.3f' %(d, freq_tmp)) ``` The freq of stay to win the game is: 0.342 The freq of switch to win the game is: 0.656 ```python sum_res = res.groupby(['door', 'decision']).mean().reset_index() ``` | door | decision | out | | ----------- | ----------- | --- | |0 | stay | 0.337 | |0 | switch | 0.662 | |1 | stay | 0.361 | |1 | switch | 0.648 | |2 | stay | 0.328 | |2 | switch | 0.657 | ### Histgram: decision matters? ```python import seaborn as sns sns.set_theme(style="white", palette='pastel') sns.displot(res, x="out", col="decision", hue='decision', stat="probability") ``` ![](https://i.imgur.com/jax00Uy.png) ### Histgram: initial door matters? ```python sns.displot(res, x="out", col="door", hue='door', stat="probability") ``` ![](https://i.imgur.com/2yrdEXC.png) ```python sns.catplot(data=sum_res, x="door", y="out", hue="decision", kind='bar') # sns.catplot(data=df, x="age", y="class", kind="box") ``` ![](https://i.imgur.com/xWuXSPc.png) ## Probabilistic interpretation[^1] [^1]: `latex`([tutorial](https://www.youtube.com/watch?v=zqQM66uAig0)) is required, which is optional (but highly recommended) for our course. A statistical perspective: we want to make a decision $D$: "stay" (=0) or "switch" (=1), to maximize the probability, that is, $$ \max_{d = 0, 1} \mathbb{P}\big( Y = 1 \big| D = d \big). $$ Thus, the primary goal is to compute the conditional probability. Clearly, $Y = 1$ is highly depended on your choice in the first stage, let's denote $X \in \{0, 1\}$ as your initial selection with or w/o car. Then, we have $$ \mathbb{P}\big( Y = 1 | D =d \big) = \mathbb{P} \big( Y = 1, X = 0 | D =d \big) + \mathbb{P} \big( Y = 1, X = 1 | D =d \big) \\ = \mathbb{P} \big( Y = 1, | X = 0, D =d \big) \mathbb{P}(X = 0) + \mathbb{P} \big( Y = 1 | X=1, D =d \big) \mathbb{P}(X = 1) $$ where the first equality follows from the relation between marginal and joint probabilities, and the second equality follows Bayes' theorem. Note that $$ \mathbb{P}(X = 1) = \mathbb{P}(\text{initially select car}) = 1/3, \ \ \mathbb{P}(X = 0) = \mathbb{P}(\text{initially select goat}) = 2/3. $$ Now, we compare the difference in the conditional probabilities when making different decisions. - ==D = 'stay'== $$ \mathbb{P}( Y = 1 | D = 0 \big) = \frac{2}{3} \mathbb{P} \big( Y = 1 | X = 0, D=0 \big) + \frac{1}{3} \mathbb{P} \big( Y = 1 | X=1, D=0 \big) = \frac{1}{3}. $$ - ==D = 'switch'== $$ \mathbb{P}( Y = 1 | D = 0 \big) = \frac{2}{3} \mathbb{P} \big( Y = 1 | X = 0, D=1 \big) + \frac{1}{3} \mathbb{P} \big( Y = 1 | X=1, D=1 \big) = \frac{2}{3}. $$

    Import from clipboard

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lost their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template is not available.


    Upgrade

    All
    • All
    • Team
    No template found.

    Create custom template


    Upgrade

    Delete template

    Do you really want to delete this template?

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Tutorials

    Book Mode Tutorial

    Slide Mode Tutorial

    YAML Metadata

    Contacts

    Facebook

    Twitter

    Feedback

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions

    Versions and GitHub Sync

    Sign in to link this note to GitHub Learn more
    This note is not linked with GitHub Learn more
     
    Add badge Pull Push GitHub Link Settings
    Upgrade now

    Version named by    

    More Less
    • Edit
    • Delete

    Note content is identical to the latest version.
    Compare with
      Choose a version
      No search result
      Version not found

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub

        Please sign in to GitHub and install the HackMD app on your GitHub repo. Learn more

         Sign in to GitHub

        HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Available push count

        Upgrade

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Upgrade

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully