Homework 3

CHE 116: Numerical Methods and Statistics

Prof. Andrew White

Version 1.1 (1/26/2015)


$\DeclareMathOperator{\P}{P}$ $\DeclareMathOperator{\E}{E}$

1. Python Math and Boolean (6 Points)

Answer the following problems in Python

  1. Is $3^{14}$ greater than $10^6$?

  2. Using the frexp function, show how the floating point number $0.4$ is represented and rebuild the number from its pieces. Use base 10

  3. Is $0.4 + 0.2$ exactly equal to $(4.0 + 2.0) / 10.0$ with floats? Why or why not?

  4. The next cell contains a joint probability mass function for $x$ and $y$. $x$ is the first number and $y$ is the second. You may access elements like this: P[0,2]. P[0,2] is the probability that $x=0$ and $y=2$. Demonstrate that the two random variables are not independent.

  5. Calculate the marginal $\P(x=2)$, where $x$ is the first index using the next cell's joint probability mass function.

  6. Calculate the conditional $\P(x=2 | y = 1)$, where $x$ is the first index using the next cell's joint probability mass function.


In [14]:
#This is loading the data for question 1.4 through 1.6
#Execute this cell and use new cells below. Do not answer in this cell!

import numpy as np
P = np.zeros( (3,3) )
P[0,0] = 0.
P[0,1] = 1. / 9
P[0,2] = 1. / 9.
P[1,0] = 1. / 3
P[1,1] = 0.
P[1,2] = 1. / 9
P[2,0] = 1. / 18
P[2,1] = 1. / 9
P[2,2] = 1. / 6

Answers


In [3]:
#Question 1.1
3**14 > 10**6


Out[3]:
True

In [4]:
#Question 1.2
from math import frexp
m,e=frexp(.4)
print '{:4}*2^({:})={}' .format(m,int(e), m * 2**e)


 0.8*2^(-1)=0.4

In [11]:
#Question 1.3
print(0.4 + 0.2, (4.0 + 2.0) / 10.)
print('{:.25f}'.format(0.2))
print('{:.25f}'.format(0.4))
print('0.4 and 0.2 cannot be represented exactly')


0.6000000000000001 0.6
0.2000000000000000111022302
0.4000000000000000222044605
0.4 and 0.2 cannot be represented exactly

In [15]:
#Question 1.4
print(P[1,1], (P[1,1]+P[1,0]+P[1,2])*(P[0,1]+P[1,1]+P[2,1]))
print('Since these are not equal, P(X=1, Y=1) != P(X=1) P(Y=1), which violates independence')


0.0 0.0987654320988
Since these are not equal, P(X=1, Y=1) != P(X=1) P(Y=1), which violates independence

In [17]:
#Question 1.5
p_x2 = 0
for px in P[2,:]:
    p_x2 += px
print(p_x2)


0.333333333333

2. Slicing Lists (6 Points)

Using this sentence: "This pangram contains four As, one B, two Cs, one D, thirty Es, six Fs, five Gs, seven Hs, eleven Is, one J, one K, two Ls, two Ms, eighteen Ns, fifteen Os, two Ps, one Q, five Rs, twenty-seven Ss, eighteen Ts, two Us, seven Vs, eight Ws, two Xs, three Ys, & one Z", Create slices of the string to answer the following questions. Note that character/element mean the same thing, so that every element in the sentence is a character. Answer in Python

  1. What is the first characeter?
  2. What is the sentence without the last 10 characeter?
  3. What are the first 5 characeters?
  4. What is the first half of the sentence?
  5. What is the second half?
  6. What is every 8th character, starting from the 16th?

In [18]:
#Question 2.1
string = 'This pangram contains four As, one B, two Cs, one D, thirty Es, six Fs, five Gs, seven Hs, eleven Is, one J, one K, two Ls, two Ms, eighteen Ns, fifteen Os, two Ps, one Q, five Rs, twenty-seven Ss, eighteen Ts, two Us, seven Vs, eight Ws, two Xs, three Ys, & one Z'
print('The first element is "{}"' .format(string[0]))


The first element is "T"

In [19]:
#Question 2.2
string[:-10]


Out[19]:
'This pangram contains four As, one B, two Cs, one D, thirty Es, six Fs, five Gs, seven Hs, eleven Is, one J, one K, two Ls, two Ms, eighteen Ns, fifteen Os, two Ps, one Q, five Rs, twenty-seven Ss, eighteen Ts, two Us, seven Vs, eight Ws, two Xs, three Y'

In [22]:
#Question 2.3
string[:5]


Out[22]:
'This '

In [27]:
#question 2.4
from math import floor
string[:floor(len(string)/2)]


Out[27]:
'This pangram contains four As, one B, two Cs, one D, thirty Es, six Fs, five Gs, seven Hs, eleven Is, one J, one K, two Ls, two Ms, '

In [29]:
#Question 2.5
string[floor(len(string)/2) + 1:]


Out[29]:
'ighteen Ns, fifteen Os, two Ps, one Q, five Rs, twenty-seven Ss, eighteen Ts, two Us, seven Vs, eight Ws, two Xs, three Ys, & one Z'

In [30]:
#Question 2.6
string[15::8]


Out[30]:
'noowni  ,Hene  h,noeeeeiTUngtt,Z'

3. Loops (20 Points)

  1. [4 points] Using a for loop, compute $10!$.
  2. [4 points] Using a for loop compute $$\sum_{n=0}^{25}\frac{1}{n!}$$ What famous number is that?
  3. [8 points] Now that you know what famous number that is, how many terms are required in the sum before it is accurate to 5 decimal places? Your code should print out the number of terms. Think carefully about how to turn "accurate to 5 decimal places" into a python expression.

In [32]:
#Question 3.1
from math import factorial
prod = 1
for i in range(1,11):
    prod *= i
print(prod, factorial(10))


3628800 3628800

In [37]:
#Question 3.2
prod = 1
sum = 0
for i in range(0, 26):
    prod *= i
    #account for the fact that 0! = 1
    if prod == 0:
        prod = 1
    sum += 1.0 / prod
print('The number is approach e: {}'.format(sum))


The number is approach e: 2.7182818284590455

In [41]:
#Question 3.3
from math import e, fabs
prod = 1
sum = 0
for i in range(0, 26):
    prod *= i
    #account for the fact that 0! = 1
    if prod == 0:
        prod = 1
    sum += 1.0 / prod
    #compare sum with e
    if fabs(sum - e) < 10**-5:
        print('{} terms are required'.format(i + 1))
        break


9 terms are required

Numpy (6 Points)

  1. Create a numpy array containing a set of points between 5 and 10 spaced apart by 0.025. This should be done with a numpy function.
  2. Using a numpy array, compute: $$\sum_{i=1}^{100} \cos{i}$$ you should not use a for loop.
  3. Demonstrate 3 methods for creating a numpy array

In [44]:
#Question 4.1
import numpy as np
np.arange(5,10,0.025)


Out[44]:
array([ 5.   ,  5.025,  5.05 ,  5.075,  5.1  ,  5.125,  5.15 ,  5.175,
        5.2  ,  5.225,  5.25 ,  5.275,  5.3  ,  5.325,  5.35 ,  5.375,
        5.4  ,  5.425,  5.45 ,  5.475,  5.5  ,  5.525,  5.55 ,  5.575,
        5.6  ,  5.625,  5.65 ,  5.675,  5.7  ,  5.725,  5.75 ,  5.775,
        5.8  ,  5.825,  5.85 ,  5.875,  5.9  ,  5.925,  5.95 ,  5.975,
        6.   ,  6.025,  6.05 ,  6.075,  6.1  ,  6.125,  6.15 ,  6.175,
        6.2  ,  6.225,  6.25 ,  6.275,  6.3  ,  6.325,  6.35 ,  6.375,
        6.4  ,  6.425,  6.45 ,  6.475,  6.5  ,  6.525,  6.55 ,  6.575,
        6.6  ,  6.625,  6.65 ,  6.675,  6.7  ,  6.725,  6.75 ,  6.775,
        6.8  ,  6.825,  6.85 ,  6.875,  6.9  ,  6.925,  6.95 ,  6.975,
        7.   ,  7.025,  7.05 ,  7.075,  7.1  ,  7.125,  7.15 ,  7.175,
        7.2  ,  7.225,  7.25 ,  7.275,  7.3  ,  7.325,  7.35 ,  7.375,
        7.4  ,  7.425,  7.45 ,  7.475,  7.5  ,  7.525,  7.55 ,  7.575,
        7.6  ,  7.625,  7.65 ,  7.675,  7.7  ,  7.725,  7.75 ,  7.775,
        7.8  ,  7.825,  7.85 ,  7.875,  7.9  ,  7.925,  7.95 ,  7.975,
        8.   ,  8.025,  8.05 ,  8.075,  8.1  ,  8.125,  8.15 ,  8.175,
        8.2  ,  8.225,  8.25 ,  8.275,  8.3  ,  8.325,  8.35 ,  8.375,
        8.4  ,  8.425,  8.45 ,  8.475,  8.5  ,  8.525,  8.55 ,  8.575,
        8.6  ,  8.625,  8.65 ,  8.675,  8.7  ,  8.725,  8.75 ,  8.775,
        8.8  ,  8.825,  8.85 ,  8.875,  8.9  ,  8.925,  8.95 ,  8.975,
        9.   ,  9.025,  9.05 ,  9.075,  9.1  ,  9.125,  9.15 ,  9.175,
        9.2  ,  9.225,  9.25 ,  9.275,  9.3  ,  9.325,  9.35 ,  9.375,
        9.4  ,  9.425,  9.45 ,  9.475,  9.5  ,  9.525,  9.55 ,  9.575,
        9.6  ,  9.625,  9.65 ,  9.675,  9.7  ,  9.725,  9.75 ,  9.775,
        9.8  ,  9.825,  9.85 ,  9.875,  9.9  ,  9.925,  9.95 ,  9.975])

In [45]:
#Question 4.2
indices = np.arange(1, 101)
coses = np.cos(indices)
sum = np.sum(coses)
print(sum)


-0.53228860823

In [47]:
#Question 4.3
np.arange(3)
np.zeros(3)
np.ones(3)
np.linspace(1,2,3)
np.empty(3)
np.array([1,2,3])


Out[47]:
array([1, 2, 3])

In [ ]: