Homework 5

CHE 116: Numerical Methods and Statistics

2/15/2018


1 Combinations (18 Points)

  1. [2 points] A family may have up to 3 cars. On a street with 5 families, how many possibilities of car ownership among the 5 families can their be? Assume the families can be distinguished.

  2. [2 points] You are playing starcraft matches against your friend. You have won 7 out of 10 matches. How many sequences of win/lose could arrive at such a score of 7 - 3 (7 wins, 3 losses). For example, one sequence is Win, Lose, Win, Win, Win, Win, Lose, Lose, Win, Win. Hint: Try thinking about this as a rearranging letters problem

  3. [2 points] You have a sequences of 0s and 1s. Let $k$ represent the number of 1s and $n$ be the length of the sequence. For example, $01110$ has $k = 3$ and $n = 5$. How many sequences are there for a fixed $n$ and $k$?

  4. [4 points] The equation from problem 1.3 has been seen in unit 2 in a different context. What was it used to calculate? What is different about its use there and the examples from problem 1.3 and 1.2?

  5. [5 points] Using your information from 1.2 through 1.4 reason about the purpose of the binomial coefficient in the formula for the binomial probability distribution.

  6. [2 points] You have 8 textbooks but you can only carry 3 at a time. How many possible combinations of textbooks can you carry?

1.1

Can assume families can own 0 cars or must own 1 car. $$ 3^{5} = 243\,, 4^5 = 1024 $$

1.2

Imagine the word 'WWWLLLLLLL'. Following our lecture examples, the number of ways to arrange these letters is:

$$ \frac{10!}{7!3!} = 120 $$

1.3

$$ \frac{n!}{k!(n - k)!} $$

1.4

It is the equation for the number of size $k$ combinations with repeats composed of $n$ objects. Here we are exploring permutations where we have a fixed number of one object (success) and a fixed size.

1.5

It is the number of permutations for a given number of success and failures. This accounts for the multiple ways to get 7 successes in 10 trials, for example.

2 Working with Probability Distributions (20 Points)

Consider a probability distribution with a 5 element sample space: $Q = \{-3, 0, 2, 3, 6\}$ whose probabilities are $\{1 / 10,\, 4 / 10, \,1/ 10, \,2 / 10,\, 2 / 10\}$ respectively. For example, $P(3) = 2 / 10$. Answer the following questions below:

  1. [5 points] Show that the probability distribution is normalized. Use a for loop and the += operator

  2. [5 points] What is the distribution's expected value? Use a for loop and the += operator Hint: try iterating over the index of the array to visit elements in both arrayss using this syntax: for i in range(len(Q)):, where Q is a list containing the elements in the sample space. Try Python tutor if needed.

  3. [5 points] What is the distribution's variance? Use numpy to evaluate this, but do not use the built in var or std functions. You can reuse value for $E[X]$ from 2.2. *Hint: convert your lists to np arrays using the np.array(Q) syntax. You should only need the np.sum and no other numpy functions.

  4. [5 points] Define the random variable $S(x) = x^2$. What is $\textrm{E}[S]$. Use a for loop


In [1]:
#2.1

#enter data
Q = [-3, 0, 2, 3, 6]
P = [1/10, 4/10, 1/10, 2/10, 2/10]

#initialize psum
psum = 0
#iterate over index i
for i in range(len(Q)):
    #add up probabilities
    psum += P[i]
#print result
print(psum)


1.0

In [2]:
#2.2

#initialize expectation value
ex = 0
#iterate over index i
for i in range(len(Q)):
    #add up product of probability and element in sample space
    ex += P[i] * Q[i]
#print result
print(ex)


1.7000000000000002

In [3]:
#2.3

import numpy as np

#convert to numpy arrays
Parray = np.array(P)
Qarray = np.array(Q)

ex2 = np.sum(Parray * Qarray**2)

var = ex2 - ex**2

print(var)


7.41

In [4]:
#initialize expectation value
exS = 0
#iterate over index i
for i in range(len(Q)):
    #add up product of probability and element in sample space
    exS += P[i] * Q[i]**2
#print result
print(exS)


10.3

3 Standard Probility Distributions (7 Points)

Choose below the probability distribution that best fits the described process. Choose only from Bernoulli, Geometric, Binomial, Poisson, Exponential, and Normal. 1 Points each.

  1. Number of lottery winners
  2. Number of students who completed their homework in a class with 25 students.
  3. The speed of a car on the highway
  4. The number of collisions before a chemical reaction happens between two molecules
  5. Whether or not your front door is open
  6. The time between seeing your best friend
  7. The number of occupied tables in a restaurant with 18 tables.

3.1

Poisson

3.2

Binomial

3.3

Normal

3.4

Geometric

3.5

Bernoulli

3.6

Exponential

3.7

Binomial

4 Working with Probability Distributions (19 Points + 5 Extra Credit)

For the following problems, choose and state an appropriate distribution, write out its parameters based on the problem statement and then compute the requested quantity. Write the distribution information in Markdown/LaTeX and compute answers in Python. Use for loops for computing expected value and prediction intervals. Do not use numpy or the formulas given in lecture notes. For the geometric distribution or Poisson distribution, replace the $\infty$ with a large number like 500. Assume in all these examples that multiple trials are independent.

  1. [2 points] You go from store to store trying to find a special toy for your dog. Stores have the toy with probability 0.3. What's the expected number of store you must visit before obtaining the toy? Remember to read the instruction above!!

  2. [2 points] When you open the door for your dog, there is a 0.38 probability of him going outside. What is the probability of the dog not going outside?

  3. [2 points] You are playing fetch with your dog. He retrieves the ball which you throw with probability 0.8. After 5 throws, what's the probability of him returning the ball only once?

  4. [2 points] You give your dog 4 treats per day on average. What is the probability of your dog receives 10 treats?

  5. [4 points] Your dog is running laps around the house where the time between the laps is 30 seconds. What's the probability of the dog completing a lap less than 10 seconds? *Hint: compute the integral analytically in LaTeX, then evaluate the upper/lower terms in Python.

  6. [5 points] You are hunting for dog poop in the yard. You check the dog's 10 favorite places to poop. Each can hold 1 or 0 poops. If the probability of your dog pooping in a spot is 0.3, what is the probability of finding more than 6 poops?

  7. [2 points] Your dog is "one in a million". Given that there are 73 million dogs in the US, what is the probability your dog is truly unique. In other words, what is the probability that there are 0 dogs like yours.

  8. [Extra Credit 5 Points] Repeat problem 4.1 but find how many stores you need to visit to have a 90% chance of finding the toy.

4.1

Geometric. $p=0.3$


In [5]:
#set parameter
p = 0.3

#init sum for expectation
ex = 0
#for loop over sample space
for i in range(1, 500):
    # P(i) * i
    ex += ((1 - p)**(i - 1) * p) * i
print(ex)


3.3333333333333326

4.2

Bernoulli. $p=0.38$


In [6]:
1 - 0.38


Out[6]:
0.62

4.3

Binomial. $p = 0.8$, $N = 5$


In [7]:
from scipy.special import comb

# set parametrs
p = 0.8
N = 5
#binomial dist equation
prob = comb(N, 1) * (1 - p)**(N - 1) * p**(1)
print(prob)


0.0064

4.4

Poisson. $\mu = 4$


In [8]:
import math

#set parameters
mu = 4
x = 10
#equation for Poisson
prob = math.exp(-mu) * mu ** x / (math.factorial(x))
print(prob)


0.0052924766764201195

4.5

Exponential. $\lambda = 1 / 30$

$$ P(x < 10) = \int_0^{10} \lambda e^{-\lambda x}\,dx = \left. -e^{-\lambda x}\right]_0^{10} = 1 - e^{-\lambda 10} $$

In [9]:
1 - math.exp(-10 / 30)


Out[9]:
0.28346868942621073

4.6

Binomial. $N = 10$, $p = 0.3$


In [10]:
# set parametrs
p = 0.3
N = 10

#summing over possible numbers of poops 
psum = 0
for i in range(7, N + 1):
    #binomial dist equation
    psum += comb(N, i) * (1 - p)**(N - i) * p**(i)
print(psum)


0.0105920784

Poisson. $p = 1 / 10^6$, $N = 73 \times 10^6$, $\mu = 73$. Can choose $x = 0$ or $x = 1$, as "no other dogs like yours" or "your dog is one of a kind" are both valid interpretations.


In [11]:
#set parameters
mu = 73
x = 0
#equation for Poisson
prob = math.exp(-mu) * mu ** x / (math.factorial(x))
print(prob)


1.9792598779469045e-32

4.7


In [12]:
#set parameter
p = 0.3

#init sum for prob
psum = 0
#for loop over sample space
for i in range(1, 500):
    # P(i)
    psum += ((1 - p)**(i - 1) * p)
    if psum >= 0.9:
        break
print(i, 'stores')


7 stores