Skip to content

Commit

Permalink
added a rudimentary walktrough of some maths.
Browse files Browse the repository at this point in the history
  • Loading branch information
percolator committed Dec 2, 2024
1 parent 481136c commit 9c5e8dc
Show file tree
Hide file tree
Showing 3 changed files with 331 additions and 0 deletions.
4 changes: 4 additions & 0 deletions dsbook/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ parts:
chapters:
- file: network/pathway.ipynb
- file: network/gsea.ipynb
- caption: Appendix
numbered: True
chapters:
- file: appendix/math.ipynb
174 changes: 174 additions & 0 deletions dsbook/appendix/math.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "c3a1dbd7",
"metadata": {},
"source": [
"# Appendix: Some Maths\n",
"\n",
"## Row Vector\n",
"\n",
"A **row vector** is a 1-dimensional array consisting of a single row of elements. For example, a row vector with three elements can be written as:\n",
"\n",
"$$\n",
"\\mathbf{r} = [r_1, r_2, r_3]\n",
"$$\n",
"\n",
"## Column Vector\n",
"\n",
"A **column vector** is a 1-dimensional array consisting of a single column of elements. It can be thought of as a matrix with one column. For instance, a column vector with three elements appears as:\n",
"\n",
"$$\n",
"\\mathbf{c} = \\begin{bmatrix}\n",
"c_1 \\\\\n",
"c_2 \\\\\n",
"c_3\n",
"\\end{bmatrix}\n",
"$$\n",
"\n",
"## Matrix\n",
"\n",
"A **matrix** is a rectangular array of numbers arranged in rows and columns. For example, a matrix with two rows and three columns is shown as:\n",
"\n",
"$$\n",
"\\mathbf{A} = \\begin{bmatrix}\n",
"a_{11} & a_{12} & a_{13} \\\\\n",
"a_{21} & a_{22} & a_{23}\n",
"\\end{bmatrix}\n",
"$$\n",
"\n",
"## Transpose Operator\n",
"\n",
"The **transpose** of a matrix is obtained by swapping its rows with its columns. The transpose of matrix $\\mathbf{A}$ is denoted $\\mathbf{A}^T$. For the given matrix $\\mathbf{A}$, the transpose is:\n",
"\n",
"$$\n",
"\\mathbf{A}^T = \\begin{bmatrix}\n",
"a_{11} & a_{21} \\\\\n",
"a_{12} & a_{22} \\\\\n",
"a_{13} & a_{23}\n",
"\\end{bmatrix}\n",
"$$\n",
"\n",
"## Multiplication between Vectors\n",
"\n",
"**Vector multiplication** can result in either a scalar or a matrix:\n",
"\n",
"- **Dot product**: Multiplication of the transpose of a column vector $\\mathbf{a}$ with another column vector $\\mathbf{b}$ results in a scalar. This is also known as the inner product:\n",
"\n",
"$$\n",
"\\mathbf{a}^T \\mathbf{b} = a_1b_1 + a_2b_2 + a_3b_3\n",
"$$\n",
"\n",
"- **Outer product**: The multiplication of a column vector $\\mathbf{a}$ by the transpose of another column vector $\\mathbf{b}$ results in a matrix:\n",
"\n",
"$$\n",
"\\mathbf{a} \\mathbf{b}^T = \\begin{bmatrix}\n",
"a_1b_1 & a_1b_2 & a_1b_3 \\\\\n",
"a_2b_1 & a_2b_2 & a_2b_3 \\\\\n",
"a_3b_1 & a_3b_2 & a_3b_3\n",
"\\end{bmatrix}\n",
"$$\n",
"\n",
"## Matrix Multiplication\n",
"\n",
"The product of two matrices $\\mathbf{A}$ and $\\mathbf{B}$ is a third matrix $\\mathbf{C}$. Each element $c_{ij}$ of $\\mathbf{C}$ is computed as the dot product of the $i$-th row of $\\mathbf{A}$ and the $j$-th column of $\\mathbf{B}$:\n",
"\n",
"$$\n",
"c_{ij} = \\sum_{k} a_{ik} b_{kj}\n",
"$$\n",
"\n",
"## Projection\n",
"\n",
"The **projection** of vector $\\mathbf{u}$ onto vector $\\mathbf{v}$ is given by:\n",
"\n",
"$$\n",
"\\text{proj}_{\\mathbf{v}} \\mathbf{u} = \\frac{\\mathbf{u}^T\\mathbf{v}}{\\mathbf{v}^T\\mathbf{v}} \\mathbf{v}\n",
"$$\n",
"\n",
"This represents the orthogonal projection of $\\mathbf{u}$ in the direction of $\\mathbf{v}$."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18f60404",
"metadata": {
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"u = np.array([1, 3])\n",
"v = np.array([3, 1])\n",
"\n",
"# Recalculate the projection of u onto the new v\n",
"proj_u_on_v = np.dot(u, v) / np.dot(v, v) * v\n",
"orthogonal_component = u - proj_u_on_v\n",
"\n",
"# Update plot with new vector v and its projection\n",
"plt.figure(figsize=(6, 6))\n",
"plt.quiver(0, 0, u[0], u[1], angles='xy', scale_units='xy', scale=1, color='r', label=r'$\\mathbf{u}=(1,3)^T$')\n",
"plt.quiver(0, 0, v[0], v[1], angles='xy', scale_units='xy', scale=1, color='b', label=r'$\\mathbf{v}=(3,1)^T$')\n",
"plt.quiver(0, 0, proj_u_on_v[0], proj_u_on_v[1], angles='xy', scale_units='xy', scale=1, color='g', label='proj$_{\\mathbf{v}} \\mathbf{u}$')\n",
"\n",
"# Plot orthogonal line as a dotted line segment\n",
"end_point = proj_u_on_v + orthogonal_component\n",
"plt.plot([proj_u_on_v[0], end_point[0]], [proj_u_on_v[1], end_point[1]], 'purple', linestyle='dotted', label='Orthogonal Component')\n",
"\n",
"# Set plot limits and aspect\n",
"plt.xlim(0, 4)\n",
"plt.ylim(0, 4)\n",
"plt.gca().set_aspect('equal', adjustable='box')\n",
"\n",
"# Add a grid, legend, and labels\n",
"plt.grid(True)\n",
"plt.legend()\n",
"plt.title('Projection of Vector $\\mathbf{u}$ onto Vector $\\mathbf{v}$')\n",
"plt.xlabel('X axis')\n",
"plt.ylabel('Y axis')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "cacd922c",
"metadata": {},
"source": [
"## Eigenvalue and Eigenvector\n",
"\n",
"An **eigenvalue** $\\lambda$ and its corresponding **eigenvector** $\\mathbf{v}$ of a matrix $\\mathbf{A}$ satisfy the equation:\n",
"\n",
"$$\n",
"\\mathbf{A} \\mathbf{v} = \\lambda \\mathbf{v}\n",
"$$\n",
"\n",
"## Gradient\n",
"\n",
"The **gradient** of a multivariable function $f(\\mathbf{x})$ is a vector of partial derivatives, which points in the direction of the steepest ascent of $f$:\n",
"\n",
"$$\n",
"\\nabla f(\\mathbf{x}) = \\begin{bmatrix}\n",
"\\frac{\\partial f}{\\partial x_1} \\\\\n",
"\\frac{\\partial f}{\\partial x_2} \\\\\n",
"\\vdots \\\\\n",
"\\frac{\\partial f}{\\partial x_n}\n",
"\\end{bmatrix}\n",
"$$"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
153 changes: 153 additions & 0 deletions dsbook/appendix/math.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
---
kernelspec:
display_name: Python 3
language: python
name: python3
jupytext:
formats: md:myst
text_representation:
extension: .md
format_name: myst
---

# Appendix: Some Maths

## Row Vector

A **row vector** is a 1-dimensional array consisting of a single row of elements. For example, a row vector with three elements can be written as:

$$
\mathbf{r} = [r_1, r_2, r_3]
$$

## Column Vector

A **column vector** is a 1-dimensional array consisting of a single column of elements. It can be thought of as a matrix with one column. For instance, a column vector with three elements appears as:

$$
\mathbf{c} = \begin{bmatrix}
c_1 \\
c_2 \\
c_3
\end{bmatrix}
$$

## Matrix

A **matrix** is a rectangular array of numbers arranged in rows and columns. For example, a matrix with two rows and three columns is shown as:

$$
\mathbf{A} = \begin{bmatrix}
a_{11} & a_{12} & a_{13} \\
a_{21} & a_{22} & a_{23}
\end{bmatrix}
$$

## Transpose Operator

The **transpose** of a matrix is obtained by swapping its rows with its columns. The transpose of matrix $\mathbf{A}$ is denoted $\mathbf{A}^T$. For the given matrix $\mathbf{A}$, the transpose is:

$$
\mathbf{A}^T = \begin{bmatrix}
a_{11} & a_{21} \\
a_{12} & a_{22} \\
a_{13} & a_{23}
\end{bmatrix}
$$

## Multiplication between Vectors

**Vector multiplication** can result in either a scalar or a matrix:

- **Dot product**: Multiplication of the transpose of a column vector $\mathbf{a}$ with another column vector $\mathbf{b}$ results in a scalar. This is also known as the inner product:

$$
\mathbf{a}^T \mathbf{b} = a_1b_1 + a_2b_2 + a_3b_3
$$

- **Outer product**: The multiplication of a column vector $\mathbf{a}$ by the transpose of another column vector $\mathbf{b}$ results in a matrix:

$$
\mathbf{a} \mathbf{b}^T = \begin{bmatrix}
a_1b_1 & a_1b_2 & a_1b_3 \\
a_2b_1 & a_2b_2 & a_2b_3 \\
a_3b_1 & a_3b_2 & a_3b_3
\end{bmatrix}
$$

## Matrix Multiplication

The product of two matrices $\mathbf{A}$ and $\mathbf{B}$ is a third matrix $\mathbf{C}$. Each element $c_{ij}$ of $\mathbf{C}$ is computed as the dot product of the $i$-th row of $\mathbf{A}$ and the $j$-th column of $\mathbf{B}$:

$$
c_{ij} = \sum_{k} a_{ik} b_{kj}
$$

## Projection

The **projection** of vector $\mathbf{u}$ onto vector $\mathbf{v}$ is given by:

$$
\text{proj}_{\mathbf{v}} \mathbf{u} = \frac{\mathbf{u}^T\mathbf{v}}{\mathbf{v}^T\mathbf{v}} \mathbf{v}
$$

This represents the orthogonal projection of $\mathbf{u}$ in the direction of $\mathbf{v}$.

```{code-cell}ipython3
:tags: [hide-input]
import numpy as np
import matplotlib.pyplot as plt
u = np.array([1, 3])
v = np.array([3, 1])
# Recalculate the projection of u onto the new v
proj_u_on_v = np.dot(u, v) / np.dot(v, v) * v
orthogonal_component = u - proj_u_on_v
# Update plot with new vector v and its projection
plt.figure(figsize=(6, 6))
plt.quiver(0, 0, u[0], u[1], angles='xy', scale_units='xy', scale=1, color='r', label=r'$\mathbf{u}=(1,3)^T$')
plt.quiver(0, 0, v[0], v[1], angles='xy', scale_units='xy', scale=1, color='b', label=r'$\mathbf{v}=(3,1)^T$')
plt.quiver(0, 0, proj_u_on_v[0], proj_u_on_v[1], angles='xy', scale_units='xy', scale=1, color='g', label='proj$_{\mathbf{v}} \mathbf{u}$')
# Plot orthogonal line as a dotted line segment
end_point = proj_u_on_v + orthogonal_component
plt.plot([proj_u_on_v[0], end_point[0]], [proj_u_on_v[1], end_point[1]], 'purple', linestyle='dotted', label='Orthogonal Component')
# Set plot limits and aspect
plt.xlim(0, 4)
plt.ylim(0, 4)
plt.gca().set_aspect('equal', adjustable='box')
# Add a grid, legend, and labels
plt.grid(True)
plt.legend()
plt.title('Projection of Vector $\mathbf{u}$ onto Vector $\mathbf{v}$')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
```

## Eigenvalue and Eigenvector

An **eigenvalue** $\lambda$ and its corresponding **eigenvector** $\mathbf{v}$ of a matrix $\mathbf{A}$ satisfy the equation:

$$
\mathbf{A} \mathbf{v} = \lambda \mathbf{v}
$$

## Gradient

The **gradient** of a multivariable function $f(\mathbf{x})$ is a vector of partial derivatives, which points in the direction of the steepest ascent of $f$:

$$
\nabla f(\mathbf{x}) = \begin{bmatrix}
\frac{\partial f}{\partial x_1} \\
\frac{\partial f}{\partial x_2} \\
\vdots \\
\frac{\partial f}{\partial x_n}
\end{bmatrix}
$$

0 comments on commit 9c5e8dc

Please sign in to comment.