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

# Statistics

## 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)


```
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import random
from math import sqrt


sales = [3505, 2400, 3027, 2798, 3700, 3250, 2689]
s = len(sales)
mean = sum(sales) / len(sales)
# mean calculation
sales.sort()
if s % 2 == 0:
  median1 = sales[s//2]
  median2 = sales[s//2 - 1]
  median = (median1 + median2)/2
else:
  median = sales[s//2]
print("Sales median is:" + str(median))

#calculating variance
sales_var = sum((xi - median) ** 2 for xi in sales) / (len(sales) -1)
print("Sales Variance is: " + str(sales_var))

#calculating standard deviation
# sales_std = sqrt(sum([(number - mean(sales)) ** 2 for number in sales]) / (len(sales) - 1))
# print("The standard deviation is: " + str(sales_std))
# print("The standard deviation is: " + str(np.std(sales)))

sales_std = np.std(sales, ddof=1)
sales_std
```

    Sales median is:3027
    Sales Variance is: 215159.33333333334





    463.02041505953576



## 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]
```


```
numer = np.cov(sales, customers)
numer
```




    array([[214387.9047619 ,   -869.64285714],
           [  -869.64285714,    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.)


```
df = pd.DataFrame({
    'sales' : sales, 
    'customers' : customers
    })
# df['customers'] = pd.Series[customers]
df.head()
```




<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>sales</th>
      <th>customers</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>2400</td>
      <td>127</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2689</td>
      <td>80</td>
    </tr>
    <tr>
      <th>2</th>
      <td>2798</td>
      <td>105</td>
    </tr>
    <tr>
      <th>3</th>
      <td>3027</td>
      <td>92</td>
    </tr>
    <tr>
      <th>4</th>
      <td>3250</td>
      <td>120</td>
    </tr>
  </tbody>
</table>
</div>




```
customer_std = np.std(customers)
print(customer_std)
```

    15.792015549069118



```
r = numer / (sales_std * customer_std)
r
```




    array([[29.31990623, -0.11893323],
           [-0.11893323,  0.03979094]])




```
np.corrcoef(sales, customers)
```




    array([[ 1.        , -0.11011078],
           [-0.11011078,  1.        ]])




```
df.corr()
```




<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>sales</th>
      <th>customers</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>sales</th>
      <td>1.000000</td>
      <td>-0.110111</td>
    </tr>
    <tr>
      <th>customers</th>
      <td>-0.110111</td>
      <td>1.000000</td>
    </tr>
  </tbody>
</table>
</div>



## 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')
df.head()
```




<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>Unnamed: 0</th>
      <th>pclass</th>
      <th>survived</th>
      <th>name</th>
      <th>sex</th>
      <th>age</th>
      <th>sibsp</th>
      <th>parch</th>
      <th>ticket</th>
      <th>fare</th>
      <th>cabin</th>
      <th>embarked</th>
      <th>boat</th>
      <th>body</th>
      <th>home.dest</th>
      <th>has_cabin_number</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>1.0</td>
      <td>1.0</td>
      <td>Allen, Miss. Elisabeth Walton</td>
      <td>female</td>
      <td>29.0000</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>24160</td>
      <td>211.3375</td>
      <td>B5</td>
      <td>S</td>
      <td>2</td>
      <td>NaN</td>
      <td>St Louis, MO</td>
      <td>1</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2</td>
      <td>1.0</td>
      <td>1.0</td>
      <td>Allison, Master. Hudson Trevor</td>
      <td>male</td>
      <td>0.9167</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>113781</td>
      <td>151.5500</td>
      <td>C22 C26</td>
      <td>S</td>
      <td>11</td>
      <td>NaN</td>
      <td>Montreal, PQ / Chesterville, ON</td>
      <td>1</td>
    </tr>
    <tr>
      <th>2</th>
      <td>3</td>
      <td>1.0</td>
      <td>0.0</td>
      <td>Allison, Miss. Helen Loraine</td>
      <td>female</td>
      <td>2.0000</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>113781</td>
      <td>151.5500</td>
      <td>C22 C26</td>
      <td>S</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>Montreal, PQ / Chesterville, ON</td>
      <td>1</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4</td>
      <td>1.0</td>
      <td>0.0</td>
      <td>Allison, Mr. Hudson Joshua Creighton</td>
      <td>male</td>
      <td>30.0000</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>113781</td>
      <td>151.5500</td>
      <td>C22 C26</td>
      <td>S</td>
      <td>NaN</td>
      <td>135.0</td>
      <td>Montreal, PQ / Chesterville, ON</td>
      <td>1</td>
    </tr>
    <tr>
      <th>4</th>
      <td>5</td>
      <td>1.0</td>
      <td>0.0</td>
      <td>Allison, Mrs. Hudson J C (Bessie Waldo Daniels)</td>
      <td>female</td>
      <td>25.0000</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>113781</td>
      <td>151.5500</td>
      <td>C22 C26</td>
      <td>S</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>Montreal, PQ / Chesterville, ON</td>
      <td>1</td>
    </tr>
  </tbody>
</table>
</div>




```
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>Unnamed: 0</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>Unnamed: 0</th>
      <td>143117.500000</td>
      <td>284.357034</td>
      <td>-53.967125</td>
      <td>-1442.939812</td>
      <td>25.828746</td>
      <td>1.172783</td>
      <td>-9410.735123</td>
      <td>591.579132</td>
      <td>-95.438885</td>
    </tr>
    <tr>
      <th>pclass</th>
      <td>284.357034</td>
      <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>-53.967125</td>
      <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>-1442.939812</td>
      <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>25.828746</td>
      <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>1.172783</td>
      <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>-9410.735123</td>
      <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>591.579132</td>
      <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>-95.438885</td>
      <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?

A synonym for orthogonal is perpindicular


```
vector_1 = [0, 3]
vector_2 = [3, 0]

plt.arrow(0, 0, vector_1[0], vector_1[1], color='red')
plt.arrow(0, 0, vector_2[0], vector_2[1], color='green')
plt.xlim(-2, 5)
plt.ylim(-2, 5)
plt.title("Yeet")
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.array([
              [-5],
              [3],
              [7]
              ])

b = np.array([
              [6],
              [-8],
              [2]
              ])

# a = [-5, 3, 7]


np.dot(a.T, b)
```




    array([[-40]])



#Not orthogonal since dotproduct !=0

## 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])

magnitude_c = np.linalg.norm(c)
mag_sqrd_c = magnitude_c**2
print('The magnitude of c squared is: ' + str(mag_sqrd_c))
```

    The magnitude of c squared is: 665.0



```
print(np.dot(c, c))
```

    665



```
c_tpose = c.T
c_tpose * c
```




    array([  4, 225,  36, 400])



#They all add up to 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}
e = \begin{bmatrix} 2 \\ 11 \\ -8  \end{bmatrix}
\end{align}

#Linear Combinations:
\begin{align}
d = \begin{bmatrix} 7 \\ 12 \end{bmatrix} = 7\begin{bmatrix} 1 \\ 0 \end{bmatrix} + 12\begin{bmatrix} 0 \\ 1 \end{bmatrix} = 7\hat{i} + 3\hat{j}
\end{align}

\begin{align}
e = \begin{bmatrix} 2 \\ 11 \\ -8  \end{bmatrix} = 2\begin{bmatrix} 1 \\ 0 \\ 0 \end{bmatrix} + 11\begin{bmatrix} 0 \\ 1 \\ 0 \end{bmatrix} + (-8)\begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix} = 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}


```
f = np.array([4, 12, 11, 9, 2])

norm = np.linalg.norm(f)
norm
f / norm
```




    array([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$).


```
from mpl_toolkits.mplot3d import Axes3D

# Axis Bounds
plt.xlim(-1,15)          
plt.ylim(-1,15)



# dependent Vectors
i_hat = [6, 0, 0]


j_hat = np.multiply(3, i_hat)


l_hat = [6, 12, 4]
m_hat = [4, 3, 2]
# Fix Axes
plt.axes().set_aspect('equal')

# PLot Vectors
plt.arrow(0, 0, i_hat[0], i_hat[1], linewidth=3, head_width=.05, head_length=0.05, color ='red')
plt.arrow(0, 0, j_hat[0], j_hat[1], linewidth=3, head_width=.05, head_length=0.05, color ='blue')
plt.arrow(0, 0, l_hat[0], l_hat[1], linewidth=3, head_width=.05, head_length=0.05, color ='black')
plt.arrow(0, 0, m_hat[0], m_hat[1], linewidth=3, head_width=.05, head_length=0.05, color ='green')
plt.title("Vectors, man", color='white')
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}


```
g = [1, 2]
h = [4, 8]
array = np.array([g + h])
np.linalg.matrix_rank(array)
```




    1



## 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}


```
l = [1, 2, 3]
m = [-1, 0, 7]
n = [4, 8, 2]

array = np.array([l + m + n]).reshape(3, 3).T
array
np.linalg.matrix_rank(array)
```




    3



# Basis

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




```
# Plot Linearly Dependent Vectors

# Axis Bounds
plt.xlim(-2,3.5)          
plt.ylim(-1,3)

# Original Vector
a = [-1.5,.5] 
b = [3, 1]

# Plot Vectors
plt.arrow(0,0, a[0], a[1], linewidth=3, head_width=.05, head_length=0.05, color ='blue')
plt.arrow(0,0, b[0], b[1], linewidth=3, head_width=.05, head_length=0.05, color ='red')

plt.title("Vectors that form a basis for R**2")
plt.show()
```

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

To form a basis means that the vectors have a point in which they are [0,0]


# 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.array([
              [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?

The rank of a matrix tells us the scaler by which the matrix can be multiplied and reach all points within the 2d array 

# 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)


```
# Axis Bounds
plt.xlim(-1.1,4)          
plt.ylim(-1.1,4)

# Original Vector
v = [1,3] 
w = [-1,2]
proj = np.multiply(,v)
x_minus_proj = w-proj

# Set axes
axes = plt.gca()
plt.axes().set_aspect('equal')

# Get Vals for L
x_vals = np.array(axes.get_xlim())
y_vals = 1/2*x_vals

# Plot Vectors and L
plt.plot(x_vals, y_vals, '--', color='b', linewidth=1)
plt.arrow(0, 0, proj[0], proj[1], linewidth=3, head_width=.05, head_length=0.05, color ='gray')
plt.arrow(0, 0, v[0], v[1], linewidth=3, head_width=.05, head_length=0.05, color ='green')
plt.arrow(0, 0, w[0], w[1], linewidth=3, head_width=.05, head_length=0.05, color ='red')
plt.arrow(proj[0], proj[1], x_minus_proj[0], x_minus_proj[1], linewidth=3, head_width=.05, head_length=0.05, color = 'yellow')

plt.title("non x-axis projection")
plt.show()
```

# 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.


```
import pandas as pd
import matplotlib.pyplot as plt

# Creating a dataframe for you to work with -Feel free to not use the dataframe if you don't want to.
x_values = [1, 4, 7, 3, 9, 4, 5 ]
y_values = [4, 2, 5, 0, 8, 2, 8]

data = {"x": x_values, "y": y_values}

df = pd.DataFrame(data)

df.head()

plt.scatter(df.x, df.y)
plt.show()
```


```

```

99b34a85d0595e51f90ae60e0ab105f284954203
ipynb
Jupyter Notebook
Intermediate_Linear_Algebra_Assignment.ipynb
moviedatascience/AB-Demo
1a858a62c12b8246dac92238ef9791610b1b6eac
MIT
Intermediate_Linear_Algebra_Assignment.ipynb
moviedatascience/AB-Demo
1a858a62c12b8246dac92238ef9791610b1b6eac
MIT
Intermediate_Linear_Algebra_Assignment.ipynb
moviedatascience/AB-Demo
1a858a62c12b8246dac92238ef9791610b1b6eac
MIT
Qwen/Qwen-72B
1. YES
2. YES