<a href="https://colab.research.google.com/github/BrittonWinterrose/AB-Demo/blob/master/module2-intermediate-linear-algebra/Intermediate_Linear_Algebra_Assignment.ipynb" target="_parent"></a>

# Statistics


```
import math
import pandas as pd
import numpy as np
import statistics as stats
import matplotlib.pyplot as plt
```

## 1.1 Sales for the past week was the following amounts: [3505, 2400, 3027, 2798, 3700, 3250, 2689]. Without using library functions, what is the mean, variance, and standard deviation of of sales from last week? (for extra bonus points, write your own function that can calculate these two values for any sized list)


```
sales = [3505, 2400, 3027, 2798, 3700, 3250, 2689]

def mean(numbers):
    total = 0
    count = 0
    for number in numbers:
        total = total + number
        count = count + 1     
    return total/count

def var(numbers):
    return (sum([(number - mean(numbers))**2 for number in numbers]) / (len(numbers) -1))

def stdev(numbers):
    return var(numbers)**(1/2)

print(f"Mean: {mean(sales)}, Var: {var(sales)}, & Standard Deviation: {stdev(sales)}")
```

    Mean: 3052.714285714286, Var: 214387.90476190473, & Standard Deviation: 463.0204150595357


## 1.2 Find the covariance between last week's sales numbers and the number of customers that entered the store last week: [127, 80, 105, 92, 120, 115, 93] (you may use librray functions for calculating the covariance since we didn't specifically talk about its formula)


```
customers = [127, 80, 105, 92, 120, 115, 93]
np.cov(sales, customers)
```




    array([[214387.9047619 ,   7604.35714286],
           [  7604.35714286,    290.95238095]])



## 1.3 Find the standard deviation of customers who entered the store last week. Then, use the standard deviations of both sales and customers to standardize the covariance to find the correlation coefficient that summarizes the relationship between sales and customers. (You may use library functions to check your work.)


```
cstdev = stdev(customers)
sstdev = stdev(sales)
print (f"Last weeks Customers Standard Deviation: {cstdev}\n")
# divide np.cor by the stdev of sales&custs to "standardize"
print (f"Correlation Coefficient:\n {np.divide(np.cov(sales, customers),[[sstdev**2, sstdev*cstdev],[sstdev*cstdev, cstdev**2]])}")

print (f"\nCheck:\n{np.corrcoef(sales, customers)}")
```

    Last weeks Customers Standard Deviation: 17.057326313123664
    
    Correlation Coefficient:
     [[1.         0.96283398]
     [0.96283398 1.        ]]
    
    Check:
    [[1.         0.96283398]
     [0.96283398 1.        ]]


## 1.4 Use pandas to import a cleaned version of the titanic dataset from the following link: [Titanic Dataset](https://raw.githubusercontent.com/Geoyi/Cleaning-Titanic-Data/master/titanic_clean.csv)

## Calculate the variance-covariance matrix and correlation matrix for the titanic dataset's numeric columns. (you can encode some of the categorical variables and include them as a stretch goal if you finish early)


```
df = pd.read_csv("https://raw.githubusercontent.com/Geoyi/Cleaning-Titanic-Data/master/titanic_clean.csv",index_col=[0])
df.cov()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>pclass</th>
      <th>survived</th>
      <th>age</th>
      <th>sibsp</th>
      <th>parch</th>
      <th>fare</th>
      <th>body</th>
      <th>has_cabin_number</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>pclass</th>
      <td>0.701969</td>
      <td>-0.127248</td>
      <td>-3.954605</td>
      <td>0.053090</td>
      <td>0.013287</td>
      <td>-24.227788</td>
      <td>-2.876653</td>
      <td>-0.249992</td>
    </tr>
    <tr>
      <th>survived</th>
      <td>-0.127248</td>
      <td>0.236250</td>
      <td>-0.314343</td>
      <td>-0.014088</td>
      <td>0.034776</td>
      <td>6.146023</td>
      <td>0.000000</td>
      <td>0.061406</td>
    </tr>
    <tr>
      <th>age</th>
      <td>-3.954605</td>
      <td>-0.314343</td>
      <td>165.850021</td>
      <td>-2.559806</td>
      <td>-1.459378</td>
      <td>114.416613</td>
      <td>81.622922</td>
      <td>1.463138</td>
    </tr>
    <tr>
      <th>sibsp</th>
      <td>0.053090</td>
      <td>-0.014088</td>
      <td>-2.559806</td>
      <td>1.085052</td>
      <td>0.336833</td>
      <td>8.641768</td>
      <td>-8.708471</td>
      <td>-0.003946</td>
    </tr>
    <tr>
      <th>parch</th>
      <td>0.013287</td>
      <td>0.034776</td>
      <td>-1.459378</td>
      <td>0.336833</td>
      <td>0.749195</td>
      <td>9.928031</td>
      <td>4.237190</td>
      <td>0.013316</td>
    </tr>
    <tr>
      <th>fare</th>
      <td>-24.227788</td>
      <td>6.146023</td>
      <td>114.416613</td>
      <td>8.641768</td>
      <td>9.928031</td>
      <td>2678.959738</td>
      <td>-179.164684</td>
      <td>10.976961</td>
    </tr>
    <tr>
      <th>body</th>
      <td>-2.876653</td>
      <td>0.000000</td>
      <td>81.622922</td>
      <td>-8.708471</td>
      <td>4.237190</td>
      <td>-179.164684</td>
      <td>9544.688567</td>
      <td>3.625689</td>
    </tr>
    <tr>
      <th>has_cabin_number</th>
      <td>-0.249992</td>
      <td>0.061406</td>
      <td>1.463138</td>
      <td>-0.003946</td>
      <td>0.013316</td>
      <td>10.976961</td>
      <td>3.625689</td>
      <td>0.174613</td>
    </tr>
  </tbody>
</table>
</div>



# Orthogonality

## 2.1 Plot two vectors that are orthogonal to each other. What is a synonym for orthogonal?


```
print("Perpendicular")

fig = plt.figure()
plt.arrow(0,0,.75,0, head_width=0.03, head_length=0.03, color="red")
plt.arrow(0,0,0,.75, head_width=0.03, head_length=0.03, color="purple")

plt.xlim(-.25,1)          
plt.ylim(-.25,1)

plt.show()

```

## 2.2 Are the following vectors orthogonal? Why or why not?

\begin{align}
a = \begin{bmatrix} -5 \\ 3 \\ 7 \end{bmatrix}
\qquad
b = \begin{bmatrix} 6 \\ -8 \\ 2 \end{bmatrix}
\end{align}


```
a = np.matrix('-5 3 7')
b = np.matrix('6 -8 2').T
np.dot(a,b)

print (f"Not orthogonal because the dot product = {np.dot(a,b)}, not Zero. ")
```

    Not orthogonal because the dot product = [[-40]], not Zero. 


## 2.3 Compute the following values: What do these quantities have in common?

## What is $||c||^2$? 

## What is $c \cdot c$? 

## What is $c^{T}c$?

\begin{align}
c = \begin{bmatrix} 2 & -15 & 6 & 20 \end{bmatrix}
\end{align}


```
c = np.array([2,-15,6,20])
print(f'||C||^2: {np.linalg.norm(c)**2}')
print(f'c . c: {np.dot(c,c)}')
print(f'c.T x c: {np.matmul(c.T, c)}')
print(f"These all equal {np.dot(c,c)}")

```

    ||C||^2: 665.0
    c . c: 665
    c.T x c: 665
    These all equal 665


# Unit Vectors

## 3.1 Using Latex, write the following vectors as a linear combination of scalars and unit vectors:

\begin{align}
d = \begin{bmatrix} 7 \\ 12 \end{bmatrix}
\qquad
e = \begin{bmatrix} 2 \\ 11 \\ -8  \end{bmatrix}
\end{align}

Answer 
\begin{align} d = 7\begin{bmatrix} 1 \\ 0 \end{bmatrix} + 12\begin{bmatrix} 0 \\ 1 \end{bmatrix}  \end{align}


\begin{align} d = 7\ \hat{i} + 12 \ \hat{j} \end{align}

\begin{align}\end{align}
\begin{align} e = 2\begin{bmatrix} 1 \\ 0 \\ 0 \end{bmatrix} + 11\begin{bmatrix} 0 \\ 1 \\ 0\end{bmatrix} - 8\begin{bmatrix} 0 \\ 0 \\ 1\end{bmatrix} \end{align}

\begin{align} e = 2\ \hat{i} + 11\ \hat{j} - 8\ \hat{k} \end{align}

## 3.2 Turn vector $f$ into a unit vector:

\begin{align}
f = \begin{bmatrix} 4 & 12 & 11 & 9 & 2 \end{bmatrix}
\end{align}


```
#!pip install transformations
f = np.array([4,12,11,9,2])
f_u = np.divide(f,np.linalg.norm(f))
print(f_u)

import transformations
print(transformations.unit_vector(f, axis=0))

```

    [0.20908335 0.62725005 0.57497921 0.47043754 0.10454167]
    [0.20908335 0.62725005 0.57497921 0.47043754 0.10454167]


# Linear Independence / Dependence 

## 4.1 Plot two vectors that are linearly dependent and two vectors that are linearly independent (bonus points if done in $\mathbb{R}^3$).


```
print("Linearly Independent")

fig = plt.figure()
plt.arrow(0,0,.75,.3, head_width=0.03, head_length=0.03, color="red")
plt.arrow(0,0,.3,.75, head_width=0.03, head_length=0.03, color="purple")

plt.xlim(-.25,1)          
plt.ylim(-.25,1)

plt.show()
```


```
print("Linearly Dependent")

fig = plt.figure()
plt.arrow(0,0,.5,.5, head_width=0.03, head_length=0.03, color="red")
plt.arrow(0,0,.25,.25, head_width=0.03, head_length=0.03, color="purple")

plt.xlim(-.25,1)          
plt.ylim(-.25,1)

plt.show()
```

# Span

## 5.1 What is the span of the following vectors?

\begin{align}
g = \begin{bmatrix} 1 & 2 \end{bmatrix}
\qquad
h = \begin{bmatrix} 4 & 8 \end{bmatrix}
\end{align}

4/4 = 1,  8/4 = 2.  

These vectors are colinear and hence span the same space. 

*Utilizing the fact that A and rref(A) has the same row space, check if the two matrices (with the given vectors as row vectors) have the same Reduced Row Echelon Form.*

## 5.2 What is the span of $\{l, m, n\}$?

\begin{align}
l = \begin{bmatrix} 1 & 2 & 3 \end{bmatrix}
\qquad
m = \begin{bmatrix} -1 & 0 & 7 \end{bmatrix}
\qquad
n = \begin{bmatrix} 4 & 8  & 2\end{bmatrix}
\end{align}

There are no common variants of reduced row echelon form so these vectors are not colinear and do not span the same space. 

# Basis

## 6.1 Graph two vectors that form a basis for $\mathbb{R}^2$




```
print("Basis for R2")

fig = plt.figure()
plt.arrow(0,0,.75,.3, head_width=0.03, head_length=0.03, color="red")
plt.arrow(0,0,.3,.75, head_width=0.03, head_length=0.03, color="purple")

plt.xlim(-.25,1)          
plt.ylim(-.25,1)

plt.show()
```

## 6.2 What does it mean to form a basis?

A basis of a vector space is defined as a subset v 1 , v 2 , . . . , v n of vectors in that are linearly independent and span vector space. (thanks google)



# Rank

## 7.1 What is the Rank of P?

\begin{align}
P = \begin{bmatrix} 
1 & 2 & 3 \\
 -1 & 0 & 7 \\
4 & 8  & 2
\end{bmatrix}
\end{align}


```
P = np.matrix("1 2 3; -1 0 7; 4 8 2")
np.linalg.matrix_rank(P)
```




    3



## 7.2 What does the rank of a matrix tell us?

Rank (linear algebra) In linear algebra, the rank of a matrix A is the dimension of the vector space generated (or spanned) by its columns. (Wikipedia 2k19)

# Linear Projections

## 8.1 Line $L$ is formed by all of the vectors that can be created by scaling vector $v$ 
\begin{align}
v = \begin{bmatrix} 1 & 3 \end{bmatrix}
\end{align}

\begin{align}
w = \begin{bmatrix} -1 & 2 \end{bmatrix}
\end{align}

## find $proj_{L}(w)$

## graph your projected vector to check your work (make sure your axis are square/even)


```
v = np.array([1,3])
w = np.array([-1,2])
q = np.dot(w,v)*v / np.dot(v,v)
q
```




    array([0.5, 1.5])




```
print("Checking my projection work")

fig = plt.figure()
plt.arrow(0,0,q[0],q[1], head_width=0.1, head_length=0.1, color="red")
plt.arrow(0,0,v[0],v[1], head_width=0.1, head_length=0.1, color="purple")
plt.arrow(0,0,w[0],w[1], head_width=0.1, head_length=0.1, color="blue")

plt.xlim(-1.5,1.5)          
plt.ylim(-.25,4)

plt.show()
print ('Looks great!')

print ("And it's correct because the projection (red) onto purple is co-linear.")
```

# Stretch Goal

## For vectors that begin at the origin, the coordinates of where the vector ends can be interpreted as regular data points. (See 3Blue1Brown videos about Spans, Basis, etc.)

## Write a function that can calculate the linear projection of each point (x,y) (vector) onto the line y=x. run the function and plot the original points in blue and the new projected points on the line y=x in red. 

## For extra points plot the orthogonal vectors as a dashed line from the original blue points to the projected red points.

8bd716416582a9d24bf5d2ed729baa1c3112fd0b
ipynb
Jupyter Notebook
module2-intermediate-linear-algebra/Intermediate_Linear_Algebra_Assignment.ipynb
BrittonWinterrose/AB-Demo
98342a3fc2fad46bcbbaa1850e5b746095d807e3
MIT
module2-intermediate-linear-algebra/Intermediate_Linear_Algebra_Assignment.ipynb
BrittonWinterrose/AB-Demo
98342a3fc2fad46bcbbaa1850e5b746095d807e3
MIT
module2-intermediate-linear-algebra/Intermediate_Linear_Algebra_Assignment.ipynb
BrittonWinterrose/AB-Demo
98342a3fc2fad46bcbbaa1850e5b746095d807e3
MIT
Qwen/Qwen-72B
1. YES
2. YES