Homework 4

CHE 116: Numerical Methods and Statistics

Prof. Andrew White

Version 1.3 (2/9/2015)


1. Plotting Practice (6 Points)

Create a plot of $\sin(2x)$ and $\sin(x)$ from $0$ to $2\pi$ with the following properties:

  1. In Seaborn in the "Poster" context
  2. LaTeX equations labeling the two curves
  3. A Vertical line at $\pi$ that is the color of the first sine wave, $\sin(2x)$
  4. A legend
  5. An $x$ and $y$ label
  6. A title that says "I love Python"

2. Working with Data (5 Points)

Make sure you have downloaded the Excel spreadsheet for this week's homework and it is the same folder as this worksheet. The data in the spreadsheet is from a 4-plate hydrogen fuel cell. The cell below loads the data. Run the cell and then write your own python code in subsequent cells.

  1. Using a for loop, calculate the sample mean of Power of the fuel cell.
  2. Using the numpy mean function, calculate the sample mean of the Power of the fuel cell.
  3. Use a for loop to calculate the standard deviation of the Power of the fuel cell.
  4. Using the numpy var function, calculate the standard deviation of the Power of the fuel cell.
  5. Your answers between 2.3 and 2.4 should be slightly different. Why?

In [2]:
%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn
seaborn.set_context("poster")

#load the fuel cell data
fuel_data = pd.read_excel('fuel_cell.xlsx')

#show the columns
for i in fuel_data.columns:
    print '\'{}\''.format(i)


'Time'
'Voltage'
'Current'
'Power'
'Efficiency'
'Resistance'
'Control Sig'
'Fan %'
'H2 In'
'H2 Out'
'Cell 1'
'Cell 2'
'Cell 3'
'Cell 4'
'RH %'
'Temperature'

In [4]:
#This is how you access data
print fuel_data['Time']


0     3691.651
1     3693.891
2     3696.715
3     3699.508
4     3702.460
5     3705.275
6     3707.923
7     3710.187
8     3712.795
9     3715.428
10    3718.260
11    3721.035
12    3723.707
13    3725.963
14    3728.203
...
662    5496.243
663    5499.492
664    5502.652
665    5505.819
666    5508.987
667    5512.171
668    5514.691
669    5516.948
670    5519.284
671    5522.412
672    5525.595
673    5528.739
674    5531.987
675    5535.188
676    5538.324
Name: Time, Length: 677, dtype: float64

3. Watching Youtube with the Geometric Distribution (3 Points)

Write what quantity the question asks for symbolically, write the equation you need to compute symbolically (if necessary) and compute your answer in Python.

  1. You accidentally open youtube while doing your homework. After watching one video, you find another interesting video you must watch with probability 75%. What is the probability you return to your homework after 1 video?

  2. What is the probability you return to your homework after exactly 5 videos?

  3. What is the expected number of videos you will watch? You may use any method to compute this.

4. Python Practice (10 Points)

Answer the following problems in Python

  1. Create an array containing the integers from 1 to 100 squared. Note: do not include 0.
  2. Using a for loop, sum the squared integers and break if your sum is greater than 1000. Print the integer which causes your sum to exceed 1000.
  3. Take the geometric distribution from Problem 3 and sum the probability of from 1 to 100 videos using a for loop. It should be 1 out to many decimal places.
  4. Add a break to your for loop from 4.3 and print the number of videos when the sum is greater 0.95. What does that represent?
  5. [3 Points] Turn your for loop into while loop, so that you do not need to guess how big your array will need to be and turn it into a function. Your function should take two arguments: the probability of success and the confidence level (default 5%) for a geometric distribution. It should return a number of trials for which there is $>=$ 95% probability of success before reaching that trial number. Hint: Test your function with problem 4.4
  6. [3 Points] Given a normal distribution with $\mu=5$, $\sigma = 2.5$, what is $P(-5 < x < 1)$?

5. Laughing at Tweets with Poisson Distribution (3 Points)

Write what quantity the question asks for symbolically, write the equation you need to compute symbolically and compute your answer in Python.

  1. The probability of laughing at a tweet is 0.1%. If you browse 500 tweets, what's the probability that you laugh three times?
  2. What is the probability you laugh less than three times?
  3. What is the probability you laugh more than three times? Hint: The three questions' answers should sum to 1.

6. Weekend Dating with Binomial Distribution (4 Points)

  1. The probability that you go on a second date is 10%. If you go on 8 first dates this weekend, what's the probability you'll have 3 second dates next weekend? State the distribution, its parameters and the question being asked symbolically and answer the problem in Python.

  2. What is the probability of having more than 3 second dates for next weekend? State the question being asked symbolically and answer the problem in Python. Hint: consider using the np.sum function to make it easy, but to answer 6.4 you might want to try it with a for loop.

  3. To better understand your dating agenda, make a plot of the probability of the number of second dates. It should span the entire sample space.

  4. Caclulate the expected number of second dates with the parameters above using a for loop. Do not just use the expected value equation for the binomial distribution.


In [ ]: