Git Product home page Git Product logo

dsc-probability-simulations-lab-online-ds-ft-051319's Introduction

Simulations with Conditional and Total Probability - Lab

Introduction

In this lab, we shall run simulations for simple total probability problems. We shall solve these problems by hand and also perform random sampling from a defined probability distribution repeatedly to see if our calculated results match the results of random simulations.

Objectives

You will be able to:

  • Use knowledge of conditional probabilities, total probabilities, and the product rule to run random simulations using numpy

Exercise 1

Part 1

Suppose you have two bags of marbles. The first bag contains 6 white marbles and 4 black marbles. The second bag contains 3 white marbles and 7 black marbles. Now suppose you put the two bags in a box. Now if you close your eyes, grab a bag from the box, and then grab a marble from the bag, what is the probability that it is black?

# Your answer here

Part 2

Run a simple simulation to estimate the probability of drawing a marble of a particular color. Run the code and verify that it agrees with your computation done earlier.

Perform following tasks:

  • Create dictionaries for bag1 and bag2 holding marble color and probability values:

    • bag1 = {'marbles' : np.array(["color1", "color2"]), 'probs' : np.array([P(color1), P(color2)])}
  • Create a dictionary for the box holding the bags and their probability values:

    • box = {'bags' : np.array([bag1, bag2]), 'probs' : np.array([P(bag1),P(bag2)])}
  • Show the content of your dictionaries

import numpy as np
bag1 = None
bag2 = None
box  = None

bag1, bag2, box

# ({'marbles': array(['black', 'white'], dtype='<U5'),
#   'probs': array([0.4, 0.6])},

#  {'marbles': array(['black', 'white'], dtype='<U5'),
#   'probs': array([0.7, 0.3])},

#  {'bags': array([{'marbles': array(['black', 'white'], dtype='<U5'), 'probs': array([0.4, 0.6])},
#          {'marbles': array(['black', 'white'], dtype='<U5'), 'probs': array([0.7, 0.3])}],
#         dtype=object), 'probs': array([0.5, 0.5])})

Create a function sample_marble(box) that randomly chooses a bag from the box and then randomly chooses a marble from the bag

def sample_marble(box):
    # randomly choose a bag 
   
    # randomly choose a marble 
    return none

#sample_marble(box)
# 'black' OR 'white'

Create another function probability_of_colors(color, box, num_samples) that gets a given number of samples from sample_marbles() and computes the fraction of marbles of a desired color

def probability_of_color(color, box, num_samples=1000):
    # get a bunch of marbles 
    # compute fraction of marbles of desired color 
    return none

Now let's run our function in line with our original problem, i.e. the probability of seeing a black marble by sampling form the box 100000 times.

# probability_of_color("black", box, num_samples=100000)


# your answer should be very close to 0.55

Exercise 2

Suppose now we add a third color of marble to the mix. Bag 1 now contains 6 white marbles, 4 black marbles, and 5 gray marbles. Bag 2 now contains 3 white marbles, 7 black marbles, and 5 gray marbles.

The probability of grabbing the first bag from the box is now TWICE the probability of grabbing the second bag.

What is the probability of drawing a gray marble from the bag according to law of total probabilities?

Copy and paste the code from the exercise above and modify it to estimate the probability that you just computed and check your work.

# Change above code here 
# probability_of_color("gray", box, num_samples=100000)



# Very close to 0.33

Summary

In this lab, you looked at some more examples of simple problems using the law of total probability. You also ran some simulations to solve these problems using continuous random sampling. You learned that you get a result very close to the mathematical solution when using random sampling. This difference is due to randomness, and as your sample size grows you'll see that the difference becomes very small! For more complex problems with larger datasets, having an understanding of the underlying probabilities can help you solve a lot of optimization problems as you'll learn later.

dsc-probability-simulations-lab-online-ds-ft-051319's People

Contributors

cheffrey2000 avatar lmcm18 avatar loredirick avatar mas16 avatar shakeelraja avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.