Giter Site home page Giter Site logo

dsc-permutations-and-factorials-lab-online-ds-ft-051319's Introduction

Permutations and Factorials - Lab

Introduction

Before, we saw how the creation of a sample space is crucial in finding probabilities. The issue, however is that, when the sample space grows bigger, it is not straightforward to manually compute the size of sample sets anymore.

Luckily, probability theory provides us with several formulas that can help us out. One set of formulas is known as permutations. This lab will help you get a better understanding of permutations, and provide practice!

Objectives

You will be able to:

  • Mathematically derive how many permutations there are for large sets
  • Calculate permutations of a subset
  • Calculate permutations with repetition and replacement

A Note on Factorials

In the last lesson, we talked about permutations in the context of a coverband creating a setlist. We wanted to calculate how many ways we can order 3 songs in their setlist. We can use factorials for that. For 3 songs, this boils down to

setlist = 3*2*1

Now, writing this out is not an issue when $n$ is small. What if $n$ grows though? Imagine there are 10 songs in the setlist

setlist = 10*9*8*7*6*5*4*3*2*1

You wouldn't want to repeat this for 25 songs... Let's create a function for this!

What you'll do below is:

  • create a function that takes one argument, $n$
  • initialize prod as 1
  • next, use prod in a while loop (what is the stopping criterion?)
  • update $n$ so it decreases with value 1 each iteration. This way you essentially calculate $n*(n-1)(n-2)\ldots*(1)$
def factorial(n):
    None

Now, test your function with n=20

None

Just so you know, Python has a built-in function factorial in the math library as well! Let's use our own function in this lab, but just use the math function once to check your previous answer!

import math 

None

Some Practice on Permutations

Let's go back to the appointments exercise from the last lab. A teaching assistant is holding office hours so students can make appointments. She has 6 appointments scheduled today, 3 by male students and 3 by female students. How many ways are there to order the appointments, based on gender of the students? Just to clarify, we're looking for size of the sample space that lists possible orders like this:

FMFMFM
MMMFFF
FMFMMF
...

From what you learned in the permutations lecture, you now have a more structured way of getting to the whole sample space!

Hint: a permutation with repetition is needed here, with formula $\dfrac{n!}{n_1!n_2!\ldots n_k!}$. Think carefully of what needs to go in the denominator and the numerator respectively.

app_num = None
app_num
app_denom = None
app_denom
app_total = None
app_total

Permutations: Hack a Phone

You misplaced your iPhone and are afraid it was stolen. Luckily, your iPhone needs a 4-digit code in order to get in. Imagine that a potential thief can do five attempts at getting the code right before the phone is permanently locked, how big is the chance the thief unlocks the phone?

Think about the sample space and the event space separately. You'll use the formula $P(E) = \dfrac{|E|}{|S|}$ here.

So what should go in the denominator?

denom_phone = None
denom_phone

And the numerator?

numer_phone = None
numer_phone
prob_unlock = None
prob_unlock

Right before you lost your phone you ate a pretzel, and you are pretty sure a grease pattern was left on the four crucial digits of your screen. The four letters in your access code are 3,4,7 and 8, and you realize that this information can increase the thief's chances massively. Assuming the thief interprets the smudgemarks in an intelligent way, what are the chances that the phone will be unlocked successfully?

denom_phone_smudge = None #or math.factorial(4)
denom_phone_smudge
numer_phone_smudge = None
numer_phone_smudge
prob_unlock_smudge = None
prob_unlock_smudge

Now, imagine you chose an iphone access code containing 3 different numbers, with numbers 2,7 and 8 (the code is still 4 digits). Now, the thief knows 1 number was reused (permutations with repetition!), but he doesn't know which one. what is the probability now that the phone will be unlocked successfully?

  • For the denominator here, use a permutation with repetition, along with the fact that you don't know which one is repeated. Hint: you'll have to multiply your final "permutation with repetition"-result to account for that.
  • For the numerator, use the numerator you used before: the number of trials the thief can try before the phone access is blocked.
denom_phone_smudge_2 = None #or use math.factorial(4)
denom_phone_smudge_2
numer_phone_smudge_2 = None 
numer_phone_smudge_2
prob_unlock_smudge_2 = None
prob_unlock_smudge_2

Permutations to Find the Sample and Event Space

What are the odds of throwing a "full house" when throwing 5 dice? Recall, a full house means that you'd throw a three of a certain number along with a pair of a different number.

a) Sample space

First, calculate the sample space. Recall that replacement is possible here.

sample_space_fh = None
sample_space_fh

b) Event space

Next, calculate the event space. The best way to think of the event space here is to split it up in 2 parts:

  • first, try to constrain your problem to a more specific problem, let's say, how many ways can we throw a full house if we have a pair of 4s and three 6s?
  • next, extend your problem by asking yourself how many different full houses are possible.
  • multiply the two!
ways_to_throw_given_fh= None # permutation with repetitions
ways_to_throw_given_fh
diff_fhses = None
diff_fhses

Then the event space is

event_space_fh =  None
event_space_fh

c) Probability of full house

prob_fh = None

prob_fh

Summary

Great job! You got quite some practice with permutations and factorials, and were even able to use it to calculate probability. Now, we'll move over to another concept in combinatorics: combinations.

dsc-permutations-and-factorials-lab-online-ds-ft-051319's People

Contributors

loredirick avatar alexgriff avatar peterbell avatar mas16 avatar lmcm18 avatar

Watchers

James Cloos avatar  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.