**1**. Making wallpaper with `fromfunction`

Adapted from [Circle Squared](http://igpphome.ucsd.edu/~shearer/COMP233/SciAm_Mandel.pdf)

Create a $400 \times 400$ array using the function `lambda i, j:  0.27**2*(i**2 + j**2) % 1.5`. Use `imshow` from `matplotlib.pyplot` with `interpolation='nearest'` and the `YlOrBr` colormap to display the resulting array as an image.


```python
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
```


```python
xy = np.fromfunction(lambda i, j: 0.27**2*(i**2 + j**2) % 1.5, (400, 400))
plt.figure(figsize = (8, 8))
plt.imshow(xy, interpolation = 'nearest', cmap = plt.cm.YlOrBr)
```

**2**. Find t least squares solution for $\beta_0, \beta_1, \beta_2$ using the normal equations $\hat{\beta} = (X^TX)^{-1}x^Ty$.

\begin{align}
10 &= \beta_0 + 3 \beta_1  + 7 \beta_2 \\
11 &= \beta_0 + 2 \beta_1  + 8 \beta_2 \\
9 &= \beta_0 + 3 \beta_1 + 7 \beta_2 \\
10 &= \beta_0 + 1 \beta_1 + 9 \beta_2 \\
\end{align}

You can find the inverse of a matrix by using `np.linalg.inv` and the transpose with `X.T`


```python
X = np.c_[np.ones(4), [3,2,3,1], [7,8,7,9]]
y = np.array([10,11,9,10]).reshape(4,1)
```


```python
b = np.linalg.inv(X.T @ X) @ X.T @ y
b
```




    array([[-1280.],
           [    0.],
           [   29.]])




```python
b2 = np.linalg.solve(X.T @ X,  X.T @ y)
b2
```




    array([[ 9.10878011],
           [-0.19269619],
           [ 0.17094017]])




```python
np.c_[y, X @ b]
```




    array([[   10., -1077.],
           [   11., -1048.],
           [    9., -1077.],
           [   10., -1019.]])




```python
np.c_[y, X @ b2]
```




    array([[10.        ,  9.72727273],
           [11.        , 10.09090909],
           [ 9.        ,  9.72727273],
           [10.        , 10.45454545]])



632249008fe1ff6912ef387469cd955401e4499b
ipynb
Jupyter Notebook
notebook/T03_Exercises_Answer.ipynb
ywang512/sta663
722c73a9b549b3bbbc1ec2ddd8d7ba4750023b92
BSD-3-Clause
2020-02-18T10:06:51.000Z
2020-02-18T10:06:51.000Z
notebook/T03_Exercises_Answer.ipynb
ywang512/sta663
722c73a9b549b3bbbc1ec2ddd8d7ba4750023b92
BSD-3-Clause
notebook/T03_Exercises_Answer.ipynb
ywang512/sta663
722c73a9b549b3bbbc1ec2ddd8d7ba4750023b92
BSD-3-Clause
2021-03-15T08:38:14.000Z
2021-03-15T08:38:14.000Z
Qwen/Qwen-72B
1. YES
2. YES