Homework 9 Key

CHE 116: Numerical Methods and Statistics

4/2/2020


1. Creating Matrices (6 Points)

Create the following matrices using the given constraints. 2 Points each.

  1. In 2 lines of python (not including prints/imports), create a 6x6 matrix whose second column (where we count "first", "second", etc.) is 3's and all other elements in the matrix are 1.
  2. Using a list comprehension, show how to get a compute a list containing the first 10 powers of pi.
  3. Use an f-string to print out exp(0.5) to 2 significant figures. Do the computation inside the {} in the f-string.

1.1


In [1]:
import numpy as np

m = np.ones( (6,6) )
m[:,1] = 3
print(m)


[[1. 3. 1. 1. 1. 1.]
 [1. 3. 1. 1. 1. 1.]
 [1. 3. 1. 1. 1. 1.]
 [1. 3. 1. 1. 1. 1.]
 [1. 3. 1. 1. 1. 1.]
 [1. 3. 1. 1. 1. 1.]]

1.2


In [2]:
[np.pi**i for i in range(10)]


Out[2]:
[1.0,
 3.141592653589793,
 9.869604401089358,
 31.006276680299816,
 97.40909103400242,
 306.0196847852814,
 961.3891935753043,
 3020.2932277767914,
 9488.53101607057,
 29809.0993334462]

1.3


In [18]:
f'{np.exp(0.5):.2}'


Out[18]:
'1.6'

2. Matrix Calculations (10 Points)

Use the two matrices given to answer the following problems. Answer in Python.

$$A = \left[\begin{array}{lcr} 4 & -3 & 8\\ -3 & 4 & -4\\ 8 & -4 & 10\\ \end{array}\right]$$$$B = \left[\begin{array}{lcr} 1 & 5 & -1\\ 23 & -1 & -2\\ 1 & 2 & 3\\ \end{array}\right]$$
  1. [1 points] Report the rank of matrix $\mathbf{B}$ using Python.
  2. [1 points] Compute $\mathbf{BAB}$
  3. [2 points] What is the second eigenvalue of $\mathbf{B}$?
  4. [2 points] Solve $\mathbf{B}\vec{x} = \vec{b}$ where $\vec{b} = \left[11, 84, 17\right]$
  5. [1 points] Demonstrate your answer is correct for 2.4
  6. [2 points] Solve $\mathbf{A}\vec{x} = c\vec{x}$ for $x$ where $c$ is an arbitrary scalar.
  7. [1 points] Demonstrate your answer is correct for 2.6

2.1


In [4]:
from numpy import linalg

A = np.array([[4, -3, 8], [-3, 4, -4], [8, -4, 10]])
B = np.array([[1, 5, -1], [23, -1, -2], [1, 2, 3]])

In [5]:
linalg.matrix_rank(B)


Out[5]:
3

2.2


In [6]:
B @ A @ B


Out[6]:
array([[  442,  -160,   -89],
       [-1248,   796,   555],
       [ -109,   177,    82]])

2.3


In [7]:
f'{linalg.eig(B)[0][1]:.2f}'


Out[7]:
'10.07'

2.4


In [8]:
b = [11, 84, 17]

In [9]:
x = linalg.inv(B) @ b
print(x)


[4. 2. 3.]

2.5


In [10]:
B @ x


Out[10]:
array([11., 84., 17.])

2.6


In [11]:
# can use any of the eigenvectors here
eig_s, eig_v = linalg.eig(A)
v = eig_v[:,0]
print(v)


[ 0.53655851 -0.3492683   0.76819048]

2.7


In [12]:
print(A @ v, '==', v * eig_s[0])


[ 9.33956281 -6.07951066 13.37144613] == [ 9.33956281 -6.07951066 13.37144613]

3. Hypothesis Testing

  1. [1 point] If your p-value is 0.075 and $\alpha = 0.1$, should you reject the null hypothesis?
  2. [1 point] What is your p-value if your $T$-value is -3 in the two-tailed/two-sided $t$-test with a DOF of 5?
  3. [4 points] For a one-sample $zM$ test, what is the minimum number of standard deviations away from the population mean a sample should be to reject the null hypothesis with $\alpha = 0.05$? Set-up the equation for this (2 points) and compute the answer (2 points). Hint: your equation should have an integral on one side and you'll need to use ppf to compute
  4. [3 points]For an N-sample $zM$ test, what is the minimum number of standard deviations away from the population mean a sample should be to reject the null hypothesis with $\alpha = 0.05$ in terms of $N$? Report this answer with one digit of precisions. Modify your equation from 3.3 (2 points) and compute the answer (2 points). This is an important expression to memorize
  5. [1 point] In a Poisson hypothesis test (single-sided), what is the p-value if $\mu = 3$ and the sample value is 20? Hint: think about what you would expect the p-value to be
  6. [1 point] What is the p-value for a student t-test with $\bar{x} = 2$, $\sigma_x = 0.8$, $\mu=2.4$, and $N = 11$?

3.1

yes

3.2


In [13]:
import scipy.stats as ss
2 * ss.t.cdf(-3, df=5)


Out[13]:
0.03009924789746257

3.3

$$ 1 - \int_{-Z}^Z p(x - \mu)\,dx = \alpha $$$$ \int_{-\infty}^{-Z} p(x - \mu)\,dx = \frac{\alpha}{2} $$$$ Z = \frac{\mu - x}{\sigma} \Rightarrow \mu - x = Z\sigma $$

In [14]:
print('{:.2f} standard deviations'.format(-ss.norm.ppf(0.025)))


1.96 standard deviations

3.4

$$ Z = \frac{\mu - x}{\sigma / \sqrt{N}} \Rightarrow \mu - x = \frac{Z}{\sqrt{N}}\sigma = $$

In [15]:
print('{:.1f} / sqrt(N) standard deviations'.format(-ss.norm.ppf(0.025)))


2.0 / sqrt(N) standard deviations

3.5


In [16]:
# want extreme high values, so take 1 - CDF up to 10
# However we want 20 to be in our interval, so I add it back
# only subtract 1 point for missing that
1 - ss.poisson.cdf(20, 3) + ss.poisson.pmf(20, 3)


Out[16]:
8.314424518698823e-11

3.6


In [17]:
T = (2 - 2.4) / (0.8 / np.sqrt(11))
print(2 * ss.t.cdf(T, df=10))


0.12824599426481303