# Single layer models

In this lab we will implement a single-layer network model consisting of solely of an affine transformation of the inputs. The relevant material for this was covered in [the slides of the first lecture](http://www.inf.ed.ac.uk/teaching/courses/mlp/2016/mlp01-intro.pdf).

We will first implement the forward propagation of inputs to the network to produce predicted outputs. We will then move on to considering how to use gradients of an error function evaluated on the outputs to compute the gradients with respect to the model parameters to allow us to perform an iterative gradient-descent training procedure. In the final exercise you will use an interactive visualisation to explore the role of some of the different hyperparameters of gradient-descent based training methods.

#### A note on random number generators

It is generally a good practice (for machine learning applications **not** for cryptography!) to seed a pseudo-random number generator once at the beginning of each experiment. This makes it easier to reproduce results as the same random draws will produced each time the experiment is run (e.g. the same random initialisations used for parameters). Therefore generally when we need to generate random values during this course, we will create a seeded random number generator object as we do in the cell below.

```python
import numpy as np
seed = 27092016
rng = np.random.RandomState(seed)
```

## Exercise 1: linear and affine transforms

Any *linear transform* (also called a linear map) of a finite-dimensional vector space can be parametrised by a matrix. So for example if we consider $\boldsymbol{x} \in \mathbb{R}^{D}$ as the input space of a model with $D$ dimensional real-valued inputs, then a matrix $\mathbf{W} \in \mathbb{R}^{K\times D}$ can be used to define a prediction model consisting solely of a linear transform of the inputs

\begin{equation}
    \boldsymbol{y} = \mathbf{W} \boldsymbol{x}
    \qquad
    \Leftrightarrow
    \qquad
    y_k = \sum_{d=1}^D \left( W_{kd} x_d \right) \quad \forall k \in \left\lbrace 1 \dots K\right\rbrace
\end{equation}

with here $\boldsymbol{y} \in \mathbb{R}^K$ the $K$-dimensional real-valued output of the model. Geometrically we can think of a linear transform doing some combination of rotation, scaling, reflection and shearing of the input.

An *affine transform* consists of a linear transform plus an additional translation parameterised by a vector $\boldsymbol{b} \in \mathbb{R}^K$. A model consisting of an affine transformation of the inputs can then be defined as

\begin{equation}
    \boldsymbol{y} = \mathbf{W}\boldsymbol{x} + \boldsymbol{b}
    \qquad
    \Leftrightarrow
    \qquad
    y_k = \sum_{d=1}^D \left( W_{kd} x_d \right) + b_k \quad \forall k \in \left\lbrace 1 \dots K\right\rbrace
\end{equation}

In machine learning we will usually refer to the matrix $\mathbf{W}$ as a *weight matrix* and the vector $\boldsymbol{b}$ as a *bias vector*.

Generally rather than working with a single data vector $\boldsymbol{x}$ we will work with batches of datapoints $\left\lbrace \boldsymbol{x}^{(b)}\right\rbrace_{b=1}^B$. We could calculate the outputs for each input in the batch sequentially

\begin{align}
    \boldsymbol{y}^{(1)} &= \mathbf{W}\boldsymbol{x}^{(1)} + \boldsymbol{b}\\
    \boldsymbol{y}^{(2)} &= \mathbf{W}\boldsymbol{x}^{(2)} + \boldsymbol{b}\\
    \dots &\\
    \boldsymbol{y}^{(B)} &= \mathbf{W}\boldsymbol{x}^{(B)} + \boldsymbol{b}\\
\end{align}

by looping over each input in the batch and calculating the output. However in general loops in Python are slow (particularly compared to compiled and typed languages such as C). This is due at least in part to the large overhead in dynamically inferring variable types. In general therefore wherever possible we want to avoid having loops in which such overhead will become the dominant computational cost.

For array based numerical operations, one way of overcoming this bottleneck is to *vectorise* operations. NumPy `ndarrays` are typed arrays for which operations such as basic elementwise arithmetic and linear algebra operations such as computing matrix-matrix or matrix-vector products are implemented by calls to highly-optimised compiled libraries. Therefore if you can implement code directly using NumPy operations on arrays rather than by looping over array elements it is often possible to make very substantial performance gains.

As a simple example we can consider adding up two arrays `a` and `b` and writing the result to a third array `c`. First lets initialise `a` and `b` with arbitrary values by running the cell below.

```python
size = 1000
a = np.arange(size)
b = np.ones(size)
```

Now let's time how long it takes to add up each pair of values in the two array and write the results to a third array using a loop-based implementation. We will use the `%%timeit` magic briefly mentioned in the previous lab notebook specifying the number of times to loop the code as 100 and to give the best of 3 repeats. Run the cell below to get a print out of the average time taken.

```python
%%timeit -n 100 -r 3
c = np.empty(size)
for i in range(size):
    c[i] = a[i] + b[i]
```

    2.05 ms ± 148 µs per loop (mean ± std. dev. of 3 runs, 100 loops each)

And now we will perform the corresponding summation with the overloaded addition operator of NumPy arrays. Again run the cell below to get a print out of the average time taken.

```python
%%timeit -n 100 -r 3
c = a + b
```

    3.01 µs ± 1.53 µs per loop (mean ± std. dev. of 3 runs, 100 loops each)

The first loop-based implementation should have taken on the order of milliseconds ($10^{-3}$s) while the vectorised implementation should have taken on the order of microseconds ($10^{-6}$s), i.e. a $\sim1000\times$ speedup. Hopefully this simple example should make it clear why we want to vectorise operations whenever possible!

Getting back to our affine model, ideally rather than individually computing the output corresponding to each input we should compute the outputs for all inputs in a batch using a vectorised implementation. As you saw last week, data providers return batches of inputs as arrays of shape `(batch_size, input_dim)`. In the mathematical notation used earlier we can consider this as a matrix $\mathbf{X}$ of dimensionality $B \times D$, and in particular

\begin{equation}
    \mathbf{X} = \left[ \boldsymbol{x}^{(1)} ~ \boldsymbol{x}^{(2)} ~ \dots ~ \boldsymbol{x}^{(B)} \right]^\mathrm{T}
\end{equation}

i.e. the $b^{\textrm{th}}$ input vector $\boldsymbol{x}^{(b)}$ corresponds to the $b^{\textrm{th}}$ row of $\mathbf{X}$. If we define the $B \times K$ matrix of outputs $\mathbf{Y}$ similarly as

\begin{equation}
    \mathbf{Y} = \left[ \boldsymbol{y}^{(1)} ~ \boldsymbol{y}^{(2)} ~ \dots ~ \boldsymbol{y}^{(B)} \right]^\mathrm{T}
\end{equation}

then we can express the relationship between $\mathbf{X}$ and $\mathbf{Y}$ using [matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication) and addition as

\begin{equation}
    \mathbf{Y} = \mathbf{X} \mathbf{W}^\mathrm{T} + \mathbf{B}
\end{equation}

where $\mathbf{B} = \left[ \boldsymbol{b} ~ \boldsymbol{b} ~ \dots ~ \boldsymbol{b} \right]^\mathrm{T}$ i.e. a $B \times K$ matrix with each row corresponding to the bias vector. The weight matrix needs to be transposed here as the inner dimensions of a matrix multiplication must match i.e. for $\mathbf{C} = \mathbf{A} \mathbf{B}$ then if $\mathbf{A}$ is of dimensionality $K \times L$ and $\mathbf{B}$ is of dimensionality $M \times N$ then it must be the case that $L = M$ and $\mathbf{C}$ will be of dimensionality $K \times N$.

The first exercise for this lab is to implement *forward propagation* for a single-layer model consisting of an affine transformation of the inputs in the `fprop` function given as skeleton code in the cell below. This should work for a batch of inputs of shape `(batch_size, input_dim)` producing a batch of outputs of shape `(batch_size, output_dim)`.

You will probably want to use the NumPy `dot` function and [broadcasting features](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) to implement this efficiently. If you are not familiar with either / both of these you may wish to read the [hints](#Hints:-Using-the-dot-function-and-broadcasting) section below which gives some details on these before attempting the exercise.

```python
def fprop(inputs, weights, biases):
    """Forward propagates activations through the layer transformation.

    For inputs `x`, outputs `y`, weights `W` and biases `b` the layer
    corresponds to `y = W x + b`.

    Args:
        inputs: Array of layer inputs of shape (batch_size, input_dim).
        weights: Array of weight parameters of shape
            (output_dim, input_dim).
        biases: Array of bias parameters of shape (output_dim, ).

    Returns:
        outputs: Array of layer outputs of shape (batch_size, output_dim).
    """
    return inputs.dot(weights.T) + biases
```

Once you have implemented `fprop` in the cell above you can test your implementation by running the cell below.

```python
inputs = np.array([[0., -1., 2.], [-6., 3., 1.]])
weights = np.array([[2., -3., -1.], [-5., 7., 2.]])
biases = np.array([5., -3.])
true_outputs = np.array([[6., -6.], [-17., 50.]])

if not np.allclose(fprop(inputs, weights, biases), true_outputs):
    print('Wrong outputs computed.')
else:
    print('All outputs correct!')
```

    All outputs correct!

### Hints: Using the `dot` function and broadcasting

For those new to NumPy below are some details on the `dot` function and broadcasting feature of NumPy that you may want to use for implementing the first exercise. If you are already familiar with these and have already completed the first exercise you can move on straight to [second exercise](#Exercise-2:-visualising-random-models).

#### `numpy.dot` function

Matrix-matrix, matrix-vector and vector-vector (dot) products can all be computed in NumPy using the [`dot`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html) function. For example if `A` and `B` are both two dimensional arrays, then `C = np.dot(A, B)` or equivalently `C = A.dot(B)` will both compute the matrix product of `A` and `B` assuming `A` and `B` have compatible dimensions. Similarly if `a` and `b` are one dimensional arrays then `c = np.dot(a, b)` (which is equivalent to `c = a.dot(b)`) will compute the [scalar / dot product](https://en.wikipedia.org/wiki/Dot_product) of the two arrays. If `A` is a two-dimensional array and `b` a one-dimensional array `np.dot(A, b)` (which is equivalent to `A.dot(b)`) will compute the matrix-vector product of `A` and `b`. Examples of all three of these product types are shown in the cell below:

```python
# Initiliase arrays with arbitrary values
A = np.arange(9).reshape((3, 3))
B = np.ones((3, 3)) * 2
a = np.array([-1., 0., 1.])
b = np.array([0.1, 0.2, 0.3])
print(A.dot(B))  # Matrix-matrix product
print(B.dot(A))  # Reversed product of above A.dot(B) != B.dot(A) in general
print(A.dot(b))  # Matrix-vector product
print(b.dot(A))  # Again A.dot(b) != b.dot(A) unless A is symmetric i.e. A == A.T
print(a.dot(b))  # Vector-vector scalar product
```

    [[  6.   6.   6.]
     [ 24.  24.  24.]
     [ 42.  42.  42.]]
    [[ 18.  24.  30.]
     [ 18.  24.  30.]
     [ 18.  24.  30.]]
    [ 0.8  2.6  4.4]
    [ 2.4  3.   3.6]
    0.2

#### Broadcasting

Another NumPy feature it will be helpful to get familiar with is [broadcasting](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html). Broadcasting allows you to apply operations to arrays of different shapes, for example to add a one-dimensional array to a two-dimensional array or multiply a multidimensional array by a scalar. The complete set of rules for broadcasting as explained in the official documentation page just linked to can sound a bit complex: you might find the [visual explanation on this page](http://www.scipy-lectures.org/intro/numpy/operations.html#broadcasting) more intuitive. The cell below gives a few examples:

```python
# Initiliase arrays with arbitrary values
A = np.arange(6).reshape((3, 2))
b = np.array([0.1, 0.2])
c = np.array([-1., 0., 1.])
print(A + b)  # Add b elementwise to all rows of A
print((A.T + c).T)  # Add b elementwise to all columns of A
print(A * b)  # Multiply each row of A elementise by b
```

    [[ 0.1  1.2]
     [ 2.1  3.2]
     [ 4.1  5.2]]
    [[-1.  0.]
     [ 2.  3.]
     [ 5.  6.]]
    [[ 0.   0.2]
     [ 0.2  0.6]
     [ 0.4  1. ]]

## Exercise 2: visualising random models

In this exercise you will use your `fprop` implementation to visualise the outputs of a single-layer affine transform model with two-dimensional inputs and a one-dimensional output. In this simple case we can visualise the joint input-output space on a 3D axis.

For this task and the learning experiments later in the notebook we will use a regression dataset from the [UCI machine learning repository](http://archive.ics.uci.edu/ml/index.html). In particular we will use a version of the [Combined Cycle Power Plant dataset](http://archive.ics.uci.edu/ml/datasets/Combined+Cycle+Power+Plant), where the task is to predict the energy output of a power plant given observations of the local ambient conditions (e.g. temperature, pressure and humidity).

The original dataset has four input dimensions and a single target output dimension. We have preprocessed the dataset by [whitening](https://en.wikipedia.org/wiki/Whitening_transformation) it, a common preprocessing step. We will only use the first two dimensions of the whitened inputs (corresponding to the first two principal components of the inputs) so we can easily visualise the joint input-output space.

The dataset has been wrapped in the `CCPPDataProvider` class in the `mlp.data_providers` module and the data included as a compressed file in the data directory as `ccpp_data.npz`. Running the cell below will initialise an instance of this class, get a single batch of inputs and outputs and import the necessary `matplotlib` objects.

```python
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mlp.data_providers import CCPPDataProvider
%matplotlib notebook

data_provider = CCPPDataProvider(
    which_set='train',
    input_dims=[0, 1],
    batch_size=5000,
    max_num_batches=1,
    shuffle_order=False
)

input_dim, output_dim = 2, 1

inputs, targets = data_provider.next()
```

Here we used the `%matplotlib notebook` magic command rather than the `%matplotlib inline` we used in the previous lab as this allows us to produce interactive 3D plots which you can rotate and zoom in/out by dragging with the mouse and scrolling the mouse-wheel respectively. Once you have finished interacting with a plot you can close it to produce a static inline plot using the <i class="fa fa-power-off"></i> button in the top-right corner.

Now run the cell below to plot the predicted outputs of a randomly initialised model across the two dimensional input space as well as the true target outputs. This sort of visualisation can be a useful method (in low dimensions) to assess how well the model is likely to be able to fit the data and to judge appropriate initialisation scales for the parameters. Each time you re-run the cell a new set of random parameters will be sampled

Some questions to consider:

  * How do the weights and bias initialisation scale affect the sort of predicted input-output relationships?
  * Does the linear form of the model seem appropriate for the data here?

```python
weights_init_range = 0.5
biases_init_range = 0.1

# Randomly initialise weights matrix
weights = rng.uniform(
    low=-weights_init_range,
    high=weights_init_range,
    size=(output_dim, input_dim)
)

#  Randomly initialise biases vector
biases = rng.uniform(
    low=-biases_init_range,
    high=biases_init_range,
    size=output_dim
)
# Calculate predicted model outputs
outputs = fprop(inputs, weights, biases)

# Plot target and predicted outputs against inputs on same axis
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
ax.plot(inputs[:, 0], inputs[:, 1], targets[:, 0], 'r.', ms=2)
ax.plot(inputs[:, 0], inputs[:, 1], outputs[:, 0], 'b.', ms=2)
ax.set_xlabel('Input dim 1')
ax.set_ylabel('Input dim 2')
ax.set_zlabel('Output')
ax.legend(['Targets', 'Predictions'], frameon=False)
fig.tight_layout()
```

    <IPython.core.display.Javascript object>

## Exercise 3: computing the error function and its gradient

Here we will consider the task of regression as covered in the first lecture slides. The aim in a regression problem is given inputs $\left\lbrace \boldsymbol{x}^{(n)}\right\rbrace_{n=1}^N$ to produce outputs $\left\lbrace \boldsymbol{y}^{(n)}\right\rbrace_{n=1}^N$ that are as 'close' as possible to a set of target outputs $\left\lbrace \boldsymbol{t}^{(n)}\right\rbrace_{n=1}^N$. The measure of 'closeness' or distance between target and predicted outputs is a design choice.

A very common choice is the squared Euclidean distance between the predicted and target outputs. This can be computed as the sum of the squared differences between each element in the target and predicted outputs. A common convention is to multiply this value by $\frac{1}{2}$ as this gives a slightly nicer expression for the error gradient. The error for the $n^{\textrm{th}}$ training example is then

\begin{equation}
    E^{(n)} = \frac{1}{2} \sum_{k=1}^K \left\lbrace \left( y^{(n)}_k - t^{(n)}_k \right)^2 \right\rbrace.
\end{equation}

The overall error is then the *average* of this value across all training examples

\begin{equation}
    \bar{E} = \frac{1}{N} \sum_{n=1}^N \left\lbrace E^{(n)} \right\rbrace.
\end{equation}

*Note here we are using a slightly different convention from the lectures. There the overall error was considered to be the sum of the individual error terms rather than the mean. To differentiate between the two we will use $\bar{E}$ to represent the average error here as opposed to sum of errors $E$ as used in the slides with $\bar{E} = \frac{E}{N}$. Normalising by the number of training examples is helpful to do in practice as this means we can more easily compare errors across data sets / batches of different sizes, and more importantly it means the size of our gradient updates will be independent of the number of training examples summed over.*

The regression problem is then to find parameters of the model which minimise $\bar{E}$. For our simple single-layer affine model here that corresponds to finding weights $\mathbf{W}$ and biases $\boldsymbol{b}$ which minimise $\bar{E}$.

As mentioned in the lecture, for this simple case there is actually a closed form solution for the optimal weights and bias parameters. This is the linear least-squares solution those doing MLPR will have come across.

However in general we will be interested in models where closed form solutions do not exist. We will therefore generally use iterative, gradient descent based training methods to find parameters which (locally) minimise the error function. A basic requirement of being able to do gradient-descent based training is (unsuprisingly) the ability to evaluate gradients of the error function.

In the next exercise we will consider how to calculate gradients of the error function with respect to the model parameters $\mathbf{W}$ and $\boldsymbol{b}$, but as a first step here we will consider the gradient of the error function with respect to the model outputs $\left\lbrace \boldsymbol{y}^{(n)}\right\rbrace_{n=1}^N$. This can be written

\begin{equation}
    \frac{\partial \bar{E}}{\partial \boldsymbol{y}^{(n)}} = \frac{1}{N} \left( \boldsymbol{y}^{(n)} - \boldsymbol{t}^{(n)} \right)
    \qquad \Leftrightarrow \qquad
    \frac{\partial \bar{E}}{\partial y^{(n)}_k} = \frac{1}{N} \left( y^{(n)}_k - t^{(n)}_k \right) \quad \forall k \in \left\lbrace 1 \dots K\right\rbrace
\end{equation}

i.e. the gradient of the error function with respect to the $n^{\textrm{th}}$ model output is just the difference between the $n^{\textrm{th}}$ model and target outputs, corresponding to the $\boldsymbol{\delta}^{(n)}$ terms mentioned in the lecture slides.

The third exercise is, using the equations given above, to implement functions computing the mean sum of squared differences error and its gradient with respect to the model outputs. You should implement the functions using the provided skeleton definitions in the cell below.

```python
def error(outputs, targets):
    """Calculates error function given a batch of outputs and targets.

    Args:
        outputs: Array of model outputs of shape (batch_size, output_dim).
        targets: Array of target outputs of shape (batch_size, output_dim).

    Returns:
        Scalar error function value.
    """
    return 0.5 * ((outputs - targets)**2).sum() / outputs.shape[0]

def error_grad(outputs, targets):
    """Calculates gradient of error function with respect to model outputs.

    Args:
        outputs: Array of model outputs of shape (batch_size, output_dim).
        targets: Array of target outputs of shape (batch_size, output_dim).

    Returns:
        Gradient of error function with respect to outputs.
        This will be an array of shape (batch_size, output_dim).
    """
    return (outputs - targets) / outputs.shape[0]
```

Check your implementation by running the test cell below.

```python
outputs = np.array([[1., 2.], [-1., 0.], [6., -5.], [-1., 1.]])
targets = np.array([[0., 1.], [3., -2.], [7., -3.], [1., -2.]])
true_error = 5.
true_error_grad = np.array([[0.25, 0.25], [-1., 0.5], [-0.25, -0.5], [-0.5, 0.75]])

if not error(outputs, targets) == true_error:
    print('Error calculated incorrectly.')
elif not np.allclose(error_grad(outputs, targets), true_error_grad):
    print('Error gradient calculated incorrectly.')
else:
    print('Error function and gradient computed correctly!')
```

    Error function and gradient computed correctly!

## Exercise 4: computing gradients with respect to the parameters

In the previous exercise you implemented a function computing the gradient of the error function with respect to the model outputs. For gradient-descent based training, we need to be able to evaluate the gradient of the error function with respect to the model parameters.

Using the [chain rule for derivatives](https://en.wikipedia.org/wiki/Chain_rule#Higher_dimensions) we can write the partial deriviative of the error function with respect to single elements of the weight matrix and bias vector as

\begin{equation}
    \frac{\partial E}{\partial W_{kj}} = \sum_{n=1}^N \left\lbrace \frac{\partial E}{\partial y^{(n)}_k} \frac{\partial y^{(n)}_k}{\partial W_{kj}} \right\rbrace
    \quad \textrm{and} \quad
    \frac{\partial E}{\partial b_k} = \sum_{n=1}^N \left\lbrace \frac{\partial E}{\partial y^{(n)}_k} \frac{\partial y^{(n)}_k}{\partial b_k} \right\rbrace.
\end{equation}

From the definition of our model at the beginning we have

\begin{equation}
    y^{(n)}_k = \sum_{d=1}^D \left\lbrace W_{kd} x^{(n)}_d \right\rbrace + b_k
    \quad \Rightarrow \quad
    \frac{\partial y^{(n)}_k}{\partial W_{kj}} = x^{(n)}_j
    \quad \textrm{and} \quad
    \frac{\partial y^{(n)}_k}{\partial b_k} = 1.
\end{equation}

Putting this together we get that

\begin{equation}
    \frac{\partial E}{\partial W_{kj}} =
    \sum_{n=1}^N \left\lbrace \frac{\partial E}{\partial y^{(n)}_k} x^{(n)}_j \right\rbrace
    \quad \textrm{and} \quad
    \frac{\partial E}{\partial b_{k}} =
    \sum_{n=1}^N \left\lbrace \frac{\partial E}{\partial y^{(n)}_k} \right\rbrace.
\end{equation}

Although this may seem a bit of a roundabout way to get to these results, this method of decomposing the error gradient with respect to the parameters in terms of the gradient of the error function with respect to the model outputs and the derivatives of the model outputs with respect to the model parameters, will be key when calculating the parameter gradients of more complex models later in the course.

Your task in this exercise is to implement a function calculating the gradient of the error function with respect to the weight and bias parameters of the model given the already computed gradient of the error function with respect to the model outputs. You should implement this in the `grads_wrt_params` function in the cell below.

```python
def grads_wrt_params(inputs, grads_wrt_outputs):
    """Calculates gradients with respect to model parameters.

    Args:
        inputs: array of inputs to model of shape (batch_size, input_dim)
        grads_wrt_to_outputs: array of gradients of with respect to the model
            outputs of shape (batch_size, output_dim).

    Returns:
        list of arrays of gradients with respect to the model parameters
        `[grads_wrt_weights, grads_wrt_biases]`.
    """
    grads_wrt_weights = grads_wrt_outputs.T.dot(inputs)
    grads_wrt_biases = grads_wrt_outputs.sum(0)
    return [grads_wrt_weights, grads_wrt_biases]
```

Check your implementation by running the test cell below.

```python
inputs = np.array([[1., 2., 3.], [-1., 4., -9.]])
grads_wrt_outputs = np.array([[-1., 1.], [2., -3.]])
true_grads_wrt_weights = np.array([[-3., 6., -21.], [4., -10., 30.]])
true_grads_wrt_biases = np.array([1., -2.])

grads_wrt_weights, grads_wrt_biases = grads_wrt_params(
    inputs, grads_wrt_outputs)

if not np.allclose(true_grads_wrt_weights, grads_wrt_weights):
    print('Gradients with respect to weights incorrect.')
elif not np.allclose(true_grads_wrt_biases, grads_wrt_biases):
    print('Gradients with respect to biases incorrect.')
else:
    print('All parameter gradients calculated correctly!')
```

    All parameter gradients calculated correctly!

## Exercise 5: wrapping the functions into reusable components

In exercises 1, 3 and 4 you implemented methods to compute the predicted outputs of our model, evaluate the error function and its gradient on the outputs and finally to calculate the gradients of the error with respect to the model parameters. Together they constitute all the basic ingredients we need to implement a gradient-descent based iterative learning procedure for the model.

Although you could implement training code which directly uses the functions you defined, this would only be usable for this particular model architecture. In subsequent labs we will want to use the affine transform functions as the basis for more interesting multi-layer models. We will therefore wrap the implementations you just wrote in to reusable components that we can build more complex models with later in the course.

  * In the [`mlp.layers`](/edit/mlp/layers.py) module, use your implementations of `fprop` and `grad_wrt_params` above to implement the corresponding methods in the skeleton `AffineLayer` class provided.
  * In the [`mlp.errors`](/edit/mlp/errors.py) module use your implementation of `error` and `error_grad` to implement the `__call__` and `grad` methods respectively of the skeleton `SumOfSquaredDiffsError` class provided. Note `__call__` is a special Python method that allows an object to be used with a function call syntax.

Run the cell below to use your completed `AffineLayer` and `SumOfSquaredDiffsError` implementations to train a single-layer model using batch gradient descent on the CCPP dataset.

```python
from mlp.layers import AffineLayer
from mlp.errors import SumOfSquaredDiffsError
from mlp.models import SingleLayerModel
from mlp.initialisers import UniformInit, ConstantInit
from mlp.learning_rules import GradientDescentLearningRule
from mlp.optimisers import Optimiser
import logging

# Seed a random number generator
seed = 27092016
rng = np.random.RandomState(seed)

# Set up a logger object to print info about the training run to stdout
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.handlers = [logging.StreamHandler()]

# Create data provider objects for the CCPP training set
train_data = CCPPDataProvider('train', [0, 1], batch_size=100, rng=rng)
input_dim, output_dim = 2, 1

# Create a parameter initialiser which will sample random uniform values
# from [-0.1, 0.1]
param_init = UniformInit(-0.1, 0.1, rng=rng)

# Create our single layer model
layer = AffineLayer(input_dim, output_dim, param_init, param_init)
model = SingleLayerModel(layer)

# Initialise the error object
error = SumOfSquaredDiffsError()

# Use a basic gradient descent learning rule with a small learning rate
learning_rule = GradientDescentLearningRule(learning_rate=1e-2)

# Use the created objects to initialise a new Optimiser instance.
optimiser = Optimiser(model, error, learning_rule, train_data)

# Run the optimiser for 5 epochs (full passes through the training set)
# printing statistics every epoch.
stats, keys, _ = optimiser.train(num_epochs=10, stats_interval=1)

# Plot the change in the error over training.
fig = plt.figure(figsize=(8, 4))
ax = fig.add_subplot(111)
ax.plot(np.arange(1, stats.shape[0] + 1), stats[:, keys['error(train)']])
ax.set_xlabel('Epoch number')
ax.set_ylabel('Error')
```

    Epoch 1: 0.0s to complete
        error(train)=1.67e-01
    Epoch 2: 0.0s to complete
        error(train)=9.30e-02
    Epoch 3: 0.0s to complete
        error(train)=7.95e-02
    Epoch 4: 0.0s to complete
        error(train)=7.71e-02
    Epoch 5: 0.0s to complete
        error(train)=7.66e-02
    Epoch 6: 0.0s to complete
        error(train)=7.65e-02
    Epoch 7: 0.0s to complete
        error(train)=7.65e-02
    Epoch 8: 0.0s to complete
        error(train)=7.65e-02
    Epoch 9: 0.0s to complete
        error(train)=7.63e-02
    Epoch 10: 0.0s to complete
        error(train)=7.64e-02

    <IPython.core.display.Javascript object>

    <matplotlib.text.Text at 0x7f2e8c192e80>

Using similar code to previously we can now visualise the joint input-output space for the trained model. If you implemented the required methods correctly you should now see a much improved fit between predicted and target outputs when running the cell below.

```python
data_provider = CCPPDataProvider(
    which_set='train',
    input_dims=[0, 1],
    batch_size=5000,
    max_num_batches=1,
    shuffle_order=False
)

inputs, targets = data_provider.next()

# Calculate predicted model outputs
outputs = model.fprop(inputs)[-1]

# Plot target and predicted outputs against inputs on same axis
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
ax.plot(inputs[:, 0], inputs[:, 1], targets[:, 0], 'r.', ms=2)
ax.plot(inputs[:, 0], inputs[:, 1], outputs[:, 0], 'b.', ms=2)
ax.set_xlabel('Input dim 1')
ax.set_ylabel('Input dim 2')
ax.set_zlabel('Output')
ax.legend(['Targets', 'Predictions'], frameon=False)
fig.tight_layout()
```

    <IPython.core.display.Javascript object>

## Exercise 6: visualising training trajectories in parameter space

Running the cell below will display an interactive widget which plots the trajectories of gradient-based training of the single-layer affine model on the CCPP dataset in the three dimensional parameter space (two weights plus bias) from random initialisations. Also shown on the right is a plot of the evolution of the error function (evaluated on the current batch) over training. By moving the sliders you can alter the training hyperparameters to investigate the effect they have on how training procedes.

Some questions to explore:

  * Are there multiple local minima in parameter space here? Why?
  * What happens to learning for very small learning rates? And very large learning rates?
  * How does the batch size affect learning?

**Note:** You don't need to understand how the code below works. The idea of this exercise is to help you understand the role of the various hyperparameters involved in gradient-descent based training methods.

```python
from ipywidgets import interact
%matplotlib inline

def setup_figure():
    # create figure and axes
    fig = plt.figure(figsize=(12, 6))
    ax1 = fig.add_axes([0., 0., 0.5, 1.], projection='3d')
    ax2 = fig.add_axes([0.6, 0.1, 0.4, 0.8])
    # set axes properties
    ax2.spines['right'].set_visible(False)
    ax2.spines['top'].set_visible(False)
    ax2.yaxis.set_ticks_position('left')
    ax2.xaxis.set_ticks_position('bottom')
    ax2.set_yscale('log')
    ax1.set_xlim((-2, 2))
    ax1.set_ylim((-2, 2))
    ax1.set_zlim((-2, 2))
    #set axes labels and title
    ax1.set_title('Parameter trajectories over training')
    ax1.set_xlabel('Weight 1')
    ax1.set_ylabel('Weight 2')
    ax1.set_zlabel('Bias')
    ax2.set_title('Batch errors over training')
    ax2.set_xlabel('Batch update number')
    ax2.set_ylabel('Batch error')
    return fig, ax1, ax2

def visualise_training(n_epochs=1, batch_size=200, log_lr=-1., n_inits=5,
                       w_scale=1., b_scale=1., elev=30., azim=0.):
    fig, ax1, ax2 = setup_figure()
    # create seeded random number generator
    rng = np.random.RandomState(1234)
    # create data provider
    data_provider = CCPPDataProvider(
        input_dims=[0, 1],
        batch_size=batch_size,
        shuffle_order=False,
    )
    learning_rate = 10 ** log_lr
    n_batches = data_provider.num_batches
    weights_traj = np.empty((n_inits, n_epochs * n_batches + 1, 1, 2))
    biases_traj = np.empty((n_inits, n_epochs * n_batches + 1, 1))
    errors_traj = np.empty((n_inits, n_epochs * n_batches))
    # randomly initialise parameters
    weights = rng.uniform(-w_scale, w_scale, (n_inits, 1, 2))
    biases = rng.uniform(-b_scale, b_scale, (n_inits, 1))
    # store initial parameters
    weights_traj[:, 0] = weights
    biases_traj[:, 0] = biases
    # iterate across different initialisations
    for i in range(n_inits):
        # iterate across epochs
        for e in range(n_epochs):
            # iterate across batches
            for b, (inputs, targets) in enumerate(data_provider):
                outputs = fprop(inputs, weights[i], biases[i])
                errors_traj[i, e * n_batches + b] = error(outputs, targets)
                grad_wrt_outputs = error_grad(outputs, targets)
                weights_grad, biases_grad = grads_wrt_params(inputs, grad_wrt_outputs)
                weights[i] -= learning_rate * weights_grad
                biases[i] -= learning_rate * biases_grad
                weights_traj[i, e * n_batches + b + 1] = weights[i]
                biases_traj[i, e * n_batches + b + 1] = biases[i]
    # choose a different color for each trajectory
    colors = plt.cm.jet(np.linspace(0, 1, n_inits))
    # plot all trajectories
    for i in range(n_inits):
        lines_1 = ax1.plot(
            weights_traj[i, :, 0, 0],
            weights_traj[i, :, 0, 1],
            biases_traj[i, :, 0],
            '-', c=colors[i], lw=2)
        lines_2 = ax2.plot(
            np.arange(n_batches * n_epochs),
            errors_traj[i],
            c=colors[i]
        )
    ax1.view_init(elev, azim)
    plt.show()

w = interact(
    visualise_training,
    elev=(-90, 90, 2),
    azim=(-180, 180, 2),
    n_epochs=(1, 5),
    batch_size=(100, 1000, 100),
    log_lr=(-3., 1.),
    w_scale=(0., 2.),
    b_scale=(0., 2.),
    n_inits=(1, 10)
)

for child in w.widget.children:
    child.layout.width = '100%'
```

```python

```

```python

```


# Steady-State Growth and Optimal Resource Allocation
Griffin Chure - January 26, 2021

```python
import numpy as np
import pandas as pd
import altair as alt
import sympy as sp
import panel as pn
import param
import scipy.stats
import diaux.viz
pn.extension('vega')
colors, palette = diaux.viz.altair_style()
```

In this notebook, I derive and explore a simple model which describes steady-state balanced bacterial growth on a single nutrient (i.e. carbon) source.

## Protein Synthesis
We begin by considering the  influence of peptide bond formation (translation or *protein syntehsis*) as the most resource-intensive process involved in bacterial growth. In balanced, exponential steady-state, the cellular composition  at any point in remains the same with the synthesis of new proteins being balanced by dilution via cell division. In this mode of growth, the total protein mass of the cell $M$ grows linearly with the cellularl growth rate $\lambda$,

$$
\frac{dM}{dt} = \lambda M. \tag{1}
$$

Assuming we have an unlimited pool of precursors (meaning, tRNAs charged with amino acids) at our disposal, we can write Equation 1 in terms of the total number of translating ribosomes ribosomes $N_R^{(active)}$ as

$$
\frac{dM}{dt} = N_R^{(active)} k_R, \tag{2}
$$

where the rate $k_R$ is the effective translation rate per active ribosome with dimensions of [MT$^{-1}$]. Here, we cast this in terms of *actively translating ribosomes*, but that does not reflect the *total* number of ribosomes. There will always exist some minimum number of ribosomes $N_R^\text{(inactive)}$ that are not actively translating. Together, the sum of the actively translating and inactive ribosome pools yields the total ribosome copy number,

$$
N_R = N_R^\text{(active)} + N_R^\text{(inactive)}. \tag{3}
$$

To speak in terms of total ribosome copy number $N_R$, we can restate Equation 2 using the conservation of mass in Equation 3 to yield

$$
\frac{dM}{dt} = (N_R - N_R^\text{(inactive)}) k_R. \tag{4}.
$$

By Equation 1, we can relate this to the growth rate $\lambda$ by simple rearrangement to yield

$$
\lambda = \frac{1}{M}\frac{dM}{dt} = \frac{\left(N_R - N_R^\text{(inactive)}\right) k_R}{M}. \tag{4}
$$

Rather than working in terms of ribosome copy numbers (which can be difficult to measure), we can define the fraction of the entire protein mass that is ribosomal $\phi_R$, defined as

$$
\phi_R = \frac{M_R}{M}, \tag{5}
$$

where $M_R$ is the the mass of *all* ribosomes in the cell. We state that each ribosome has a proteinaceous mass of $m_R$, the total ribosomal mass $M_R$ can be calculated as $M_R = m_R N_R$. Thus, by computing the mass fraction, we can resetate Equation 5 as

$$
\lambda = \frac{\left(M_R - M_R^\text{(inactive)}\right)}{m_R}\frac{k_R}{M} = \frac{k_R}{m_R}\left(\phi_R - \phi_R^\text{(inactive)}\right) = \gamma\left(\phi_R - \phi_R^\text{(inactive)}\right), \tag{6}
$$

where we have introduced the term $\gamma = \frac{k_R}{m_R}$. This term, commonly referred to as the *translational efficiency* is the ratio of the average translation rate $k_R$ and the mass of a single ribosome $m_R$ and thus has dimensions of [T$^{-1}$]. This term capture what fraction of a ribosomes mass synthesized per unit time, or alternatively, the inverse $\frac{1}{\gamma}$ is the length of time it takes to synthesize one ribosomes worth of peptide mass.  For *E. coli*, a sisngle ribosome consisits of ≈ 7500 amino acids and has a maximal translation rate of ≈ 17 amino acids / second, yielding

$$
\gamma = {k_R}{m_R} \approx \frac{17\,\text{amino acids / s}}{7500\,\text{amino acids}} \approx 2.2 \times 10^{-3} \text{ribosomes / sec}; \frac{1}{\gamma} \approx 400 \frac{\text{s}}{\text{ribosome}}.
$$.

Equation 6 provides a means to calculate the cellular growth rate so long as one knows the  translational efficiency $\gamma$ and the proteome mass fraction $\phi_R$. Alternatively, knowing the growth rate $\lambda$ and having measured the proteome mass fraction $\phi_R$, one can easily infer the inactive ribosomal mass fraction and the translational efficiency $\gamma$ by performing a simple linear regression of

$$
\phi_R = \phi_R^\text{(inactive)} + \frac{\lambda}{\gamma}. \tag{7}
$$

Below, we load data from a variety of proteomic and other studies where the ribosomal mass fraction (either by RNA/protein ratio or via proteomics) and the growth rate were measured and perform this simple inference.

```python
# Load the data
mass_fraction = pd.read_csv('../../data/other/mass_fraction_compiled.csv')

# Do the simple inference of the parameters
popt = scipy.stats.linregress(mass_fraction['growth_rate_hr'], mass_fraction['mass_fraction'])

# Compute the trendline
growth_rate = np.linspace(0, 2, 100)
trend = popt[1] + popt[0] * growth_rate
fit_df = pd.DataFrame(np.array([growth_rate, trend]).T,
                      columns=['growth_rate_hr', 'mass_fraction'])

fit = alt.Chart(fit_df).mark_line(color=colors['black'], size=2).encode(
                x=alt.X(field='growth_rate_hr', type='quantitative', title='growth rate [hr\u207b\u00B9]'),
                y=alt.Y(field='mass_fraction', type='quantitative', title='ribosomal mass fraction'))
# Set up the plot
base = alt.Chart(mass_fraction).encode(
                x=alt.X(field='growth_rate_hr', type='quantitative', title='growth rate [hr\u207B\u00B9]'),
                y=alt.Y(field='mass_fraction', type='quantitative', title='ribosomal mass fraction'),
                color=alt.Color(field='source', type='nominal', title='data source'),
                shape=alt.Shape(field='source', type='nominal'))
points = base.mark_point(size=100,opacity=0.75)

print(f"""
Estimated Parameters
---------------------
γ ≈ {1/popt[0]:0.0f} hr^-1
Φ_R (inactive) = {popt[1]:0.2f}
""")
(points + fit)
```

    Estimated Parameters
    ---------------------
    γ ≈ 10 hr^-1
    Φ_R (inactive) = 0.04

The linear relation given in Equation 7, and subsequent parameteter estimates, provide some more context for what the  parameter $\phi_R$  $^\text{(inactive)}$ means. When the growth rate is 0, protein synthesis is no longer proceeding meaning that *all* ribosomes in the cell are inactive. Thus, this implies that the parameter $\phi_R^\text{(inactive)}$ represents the *minimum* fraction of the proteome that can be occupied by ribosomal mass. Thus, going forward, we will make the definition

$$
\phi_R^\text{(inactive)} \equiv \phi_R^\text{(min)}. \tag{8}
$$

such that the meaning of $\phi_R^\text{(min)}$ is clear.

## Nutrient Transport and General Metabolism
Thus far we have only considered the synthesis of proteis as the rate-limiting step of growth (sometimes referred to as *translation limited growth*). In doing so, we have made the assumption *a priori* that the concentration of precursors (think amino acids or charged tRNA) are in sufficient abundance. Here, we extend the model to consider the transport or synthesis of nutrients, which we will approximate as being a type of amino acid.

Consider some amino acid $a$ (not specificially *alanine*), whose total mass in the cell is $M_a$. As the cell grows, will need to transport or synthesize at some flux $J_a$ to at least match the rate at which it is being consumed. The dynamics of this process can be written as

$$
\frac{dM_a}{dt} = J_a - \beta \frac{dM}{dt}, \tag{9}
$$

where $\beta$, defined on the range [0, 1], corresponds to the frequency at which $a$ is being incorporated into the new cell mass. For example, if we consider that all 20 amino acids are used in equal measure, the parameter $\beta = \frac{1}{20}$ to indicate that one out of every 20 amino acids is the nutrient $a$. In addition, we can stipulate that there must be a residual pool of mass $M_a^\text{(residual)}$ such that the kinetics of the translation isn't slowed. Noting that rather than working in the space of absolute mass $M_a$, we can again work in the language of mass fraction by defining

$$
\theta_a = \frac{M_a}{M}, \tag{10}
$$

where we have chosen $\theta$ as to note that it is possible for $\theta > 1$ where as it is not the case for $\phi$. Thus, to impose the limit that there must be a residual concentration of $a$ at a given mass fraction $\theta_a$, we can amend Equation 9 to read

$$
\frac{dM_a}{dt} = J_a - \beta \frac{dM}{dt} - \theta_a \frac{dM}{dt} = J_a - \left( \beta + \theta_a\right)\frac{dM}{dt}. \tag{11}
$$

We can express the entire dynamics enumerated in Equation 11 in terms of the nutrient mass fraction $\theta_a$ by dividing Equation 11 by $M$, yielding
$$
\frac{d\theta_a}{dt} = \frac{J_a}{M} - \frac{\beta +  \theta_a}{M}\frac{dM}{dt}. \tag{12}
$$

Using the relation given in Equation 1, we can state Equation 12 in terms of the growth rate as
$$
\frac{d\theta_a}{dt} = \frac{J_a}{M} - \lambda\left(\beta + \theta_a\right). \tag{13}
$$

In steady-state, the mass fraction of the nutrient $\theta_a$ is constant, meaning that Equation 13 is equal to zero. Therefore, we can state that

$$
\lambda = \frac{J_a}{M\left(\beta + \theta_a\right)}. \tag{14}
$$

The flux or synthesis parameter $J_a$ reflects the concerted action of an array of metabolic proteins involving enzymatic pathways and transporters. We can abstract this complicated process by stating that all of the metabolic proteins involved in this synthesis and transport have a combined mass $M_A$, proceeding with a generalized rate $k_A$ which has units of nutrient mass produced/transported per unit mass of metabolic protein. This allows us to define the flux as

$$
J_a = k_P M_P. \tag{15}
$$

Plugging Equation 15 into Equation 14 yields a complete expression for the growth rate,

$$
\lambda = \frac{k_P}{\beta + \theta_a} \frac{M_P}{M} = \frac{k_P}{\beta + \theta_a} \phi_P. \tag{16}
$$

where $\phi_P$ is the mass fraction of the proteome occupied by metabolic proteins. In our section on *Protein Synthesis* we noted that there existed a maximum and minimum fraction fo teh total cellular proteome that can be occupied by ribosomes, $\phi_R^\text{(min)}$ and $\phi_R^\text{(max)}$. In order for the ribosomal mass fraction of the proteome to be able to change, there must be a comensurate change in the mass fraction in the rest of the proteome. We can state this mathematically by noting that the mass of the proteome must be partitioned between metabolic and ribosomal proteins,

$$
\phi_P + \phi_R = \phi_R^\text{(max)}. \tag{17}
$$

Thus, as $\phi_P$ gets smaller and smaller, the fraction of the proteome occupied by the ribosomes grows larger. Using this simple constraint, we can define expression for the growth rate given by Equation 16 in terms of the ribosomal mass fraction as

$$
\lambda = \frac{k_P}{\beta + \theta_a} \left(\phi_R^\text{(max)} - \phi_R\right). \tag{18}
$$

In standard physiological conditions, the standing mass fraction pool of amino acids is small compared to the number being incorporated into protein at any point in time. In this regime, we can make the mathematical approximation that

$$
\beta + \theta_a \approx \beta, \tag{19}
$$

allowing us to simplify Equation 18 to

$$
\lambda = \nu\left(\phi_R^\text{(max)} - \phi_R\right), \tag{20}
$$

where we have introduced $\nu = \frac{k_P}{\beta}$ which defines the synthesis/transport rate of nutrients to their frequency of usage. As we did in the previous section, we can rewrite Equation 20 to define the ribosomal mass fraction $\phi_R$ as a function of the growth rate as

$$
\phi_R = \phi_R^\text{(max)} - \frac{\lambda}{\nu}. \tag{21}
$$

Equation 21 immediately illustratese that, for a fixed value of $\nu$, the ribosomal mass fraction decreases relative as the growth rate increases. In their seminal 2010 work, Matthew Scott and company decreased the growth rate of *E. coli* in a single growth medium by titrating in different amounts of chloramphenicol, an antibiotic which reversibly inactivates ribosomes. Thus, as $\nu$ is dictated by the identity of the growth medium, adding chloramphenicol acts as a means to decrease $\lambda$, theoretically *increasing* the ribosomal mass fraction. Below we plot these data and estimate the relevant parameters $\nu$ and $\phi_R^\text{(max)}$ for each minimal growth medium.

```python
# Load data for Scott et al 2010, Fig 2AC
scott_data = pd.read_csv('../../data/other/Scott2010_chlor_inhibition_minimal.csv')
scott_data['mass_fraction'] = scott_data['RNA_protein_ratio'] * 0.4855 # Converts RNA/Protein to ribosome mass fraction

# Set up the plot of the points
points = alt.Chart(scott_data).mark_point(size=80).encode(
                    x=alt.X(field='growth_rate_hr', type='quantitative', title='growth rate [hr\u207b\u00b9]'),
                    y=alt.Y(field='mass_fraction', type='quantitative', title='ribosomal mass fraction',
                           scale=alt.Scale(domain=[0, 0.4])),
                    color=alt.Color(field='medium', type='nominal',
                                    title='growth medium'),
                    shape=alt.Shape(field='chloramphenicol_conc_uM', type='nominal', title='chloramphenicol [µM]'))

# For each growth medium, do the fit.
fit_dfs = []
growth_rate = np.linspace(0, 1, 100)
print("""
Estimated Parameters
--------------------
""")
for g, d in scott_data.groupby(['medium']):
    popt = scipy.stats.linregress(d['growth_rate_hr'],d['mass_fraction'])
    print(f'Growth medium {g}: Φ_R max ≈ {popt[1]:0.2f}; ν ≈ {-1 * popt[0]**-1:0.1f}')
    _df = pd.DataFrame([])
    _df['growth_rate_hr'] = growth_rate
    _df['mass_fraction'] = popt[1] + popt[0] * growth_rate
    _df['medium'] = g
    fit_dfs.append(_df)
fit_df = pd.concat(fit_dfs, sort=False)
fit_df = fit_df[fit_df['mass_fraction']>=0]

# Generate the plot with the fits.
fit = alt.Chart(fit_df).mark_line(size=2).encode(
                x=alt.X(field='growth_rate_hr', type='quantitative', title='growth rate [hr\u207b\u00b9]'),
                y=alt.Y(field='mass_fraction', type='quantitative', title='ribosomal mass fraction'),
                color=alt.Color(field='medium', type='nominal', title='growth medium')
)
(points + fit)
```

    Estimated Parameters
    --------------------

    Growth medium M63 + glucose: Φ_R max ≈ 0.31; ν ≈ 2.9
    Growth medium M63 + glycerol: Φ_R max ≈ 0.37; ν ≈ 1.4
    Growth medium cAA + glucose: Φ_R max ≈ 0.32; ν ≈ 5.5
    Growth medium cAA + glycerol: Φ_R max ≈ 0.29; ν ≈ 4.3

From this inference, we can come to the conclusion that the maximum ribosomal mass fraction $\phi_R^\text{(max)}$ is largely independent of the growth medium and is around $\approx$ 0.3 - 0.4. The nutritional capacity $\nu$ is (as expected), dependent on the growth medium.

We now have in hand two "growth laws" for exponential steady-state growth. Using Equations 7 and 20, we can enumerate a mathematical expression for the growth rate as a function of $\nu$, $\gamma$, $\phi_R^\text{max}$ and $\phi_R^\text{min}$ as

$$
\lambda = \frac{\phi_R^\text{(max)} - \phi_R^\text{(min)}}{\frac{1}{\nu} + \frac{1}{\gamma}}. \tag{22}
$$

```python
# Define the parameter ranges.
nu_range = np.round(np.linspace(1, 5, 20), decimals=2)
gamma_range = np.linspace(20, 1, 20)

# Define the maximum and minimum bounds of the ribosomal mass fraction
phi_R_max = 0.35
phi_R_min = 0.04

# Compute the grid.
nu_mesh, gamma_mesh = np.meshgrid(nu_range, gamma_range)
growth_rate = (phi_R_max - phi_R_min) / (nu_mesh**-1 + gamma_mesh**-1)

# Assemble teh dataframe
df = pd.DataFrame({'nu': nu_mesh.ravel(),
                   'gamma': gamma_mesh.ravel(),
                   'growth_rate_hr': growth_rate.ravel()})

# Plot the heatmap
heatmap = alt.Chart(df).mark_rect().encode(
                x=alt.X(field='nu', type='ordinal', title='nutritional capacity; ν'),
                y=alt.Y(field='gamma', type='ordinal', title='translational capacity; γ'),
                color=alt.Color(field='growth_rate_hr', type='quantitative',
                                title='growth rate; λ [hr\u207b\u00b9]', scale=alt.Scale(scheme='viridis'))
        )
heatmap.interactive()

```

The above heatmap shows that growth rate monotonically increases as $\nu$ and $\gamma$ get larger. However, this assumes that for any combination of $\gamma$ and $\nu$, their values are fixed and independent of the nutrients in the growth medium, a strict assumption we relax below.

## Michaelis-Menten Kinetics of $\gamma$ and $\nu$
Up to this point, we have considered the translational capacity $\gamma$ and the nutritional capacity $\nu$ to be constants or at least independent of the nutrient concentration $\theta_0$ in the growth medium (as made by the approximation Equation 19). However, it is not unreasonable to assume that these parameters will be dependent on the nutrient concentrations in the cell. Take for example the translational capacity $\gamma$ which is defined as a function of the effective translation rate $k_t$. To proceed at its maximal rate of $k_t \approx 17.1\,$ amino acids / s, charged tRNAs (read, nutrients) need to be in a sufficient excess such that it is the actual enzymatic reaction of forming new peptide bonds that is limiting and not finding the correct tRNA to match the codon. However, as the concentration of charged tRNAs drop, the opposite can be true where the kinetics is determined by the association with the correct tRNA.

Another way to think about this is that the translational capacity is tuned such that the formation of new peptide bonds (consumption of nutrients) does not outpace the synthesis of new charged tRNAs (production of nutrients). In similar situations, it's not unreasonable to model the concentration-dependent rate as following Michaelis-Menten kinetics, mathematized as
$$
\gamma(\theta_a) = \gamma^\text{(max)} \frac{\theta_a}{\theta_a + \theta_{0}} = \frac{\gamma^\text{(max)}}{1 + \frac{\theta_{0}}{\theta_a}}, \tag{23}
$$

where $\theta_{0}$ is the Michaelis-Menten constant and represents the mass fraction of nutrients where the translational capacity is half maximal. Equation 23 states that so long as the mass fraction of nutrients $\theta_a >> \theta_0$, the translational capacity will remain high and growth will be rapid. However, as $\theta_a << \theta_0$, $\gamma$ plummets, slowing translation as to not exhaust nutrients and leave ribosomes stalled, unable to synthesize new proteins.

In principle, we can take a similar approach to modeling the nutrient dependence on the nutritional capacity $\nu$. If nutrient conditions are high  $\theta_a >> \theta_0$, then it is not optimal to devote a large swath of these resources towards making metabolic machinery. Rather, devoting more resources to making more ribosomes would lead to faster growth. Thus, it's reasonalbe to assume that there is a negative dependence of the nutritional capacity $\nu$ on the nutrient mass fraction $\theta_a$, such that $\nu$ is maximal when $\theta_a  << \theta_0$,

$$
\nu(\theta_a) = \frac{\nu^\text{(max)}}{1 + \frac{\theta_a}{\theta_0}}. \tag{24}
$$

Plots of these functions are given below. Of course, Equations 23 and 24 are very simplistic and biological reality may impose more parameterization, such as a cooperativity that we have not considered here.

```python
# Define the parameter ranges.
t_t0 = np.logspace(-3, 3, 200)
gamma_max = [1E-5, 1E-4, 1E-3, 1E-2, 1E-1]
nu_max = [1E-5, 1E-4, 1E-3, 1E-2, 1E-1]

# Compute the equations and add to dataframe
dfs = []
for g, n in zip(gamma_max, nu_max):
    gamma = g / (1 + t_t0**-1)
    nu = n / ( 1  + t_t0)
    _df = pd.DataFrame(np.array([t_t0, gamma, nu]).T,
                        columns=['t_t0', 'gamma', 'nu'])
    _df['gamma_param'] = g
    _df['nu_param'] = n
    dfs.append(_df)
df = pd.concat(dfs, sort=False)

# Set up the plots.
gamma_plot = alt.Chart(df, title='translational capacity, γ', width=300).mark_line(size=2).encode(
                x=alt.X(field='t_t0', type='quantitative', scale=alt.Scale(type='log'),
                        title='θ\u2090 / θ\u2080', axis=alt.Axis(values=[0.001, 0.01, 0.1, 1, 10, 100, 1000])),
                y=alt.Y(field='gamma', type='quantitative', scale=alt.Scale(type='log'),
                        title='γ [T\u207b\u00b9]', axis=alt.Axis(values=[1E-8, 1E-6, 1E-4, 1E-2, 1])),
                color=alt.Color(field='gamma_param', type='quantitative', title='γ max [T\u207b\u00b9]'))

nu_plot = alt.Chart(df, title='nutritional capacity, ν', width=300).mark_line(size=2).encode(
                x=alt.X(field='t_t0', type='quantitative', scale=alt.Scale(type='log'),
                        title='θ\u2090 / θ\u2080', axis=alt.Axis(values=[0.001, 0.01, 0.1, 1, 10, 100, 1000])),
                y=alt.Y(field='nu', type='quantitative', scale=alt.Scale(type='log'),
                        title='ν [T\u207b\u00b9]', axis=alt.Axis(values=[1E-8, 1E-6, 1E-4, 1E-2, 1])),
                color=alt.Color(field='nu_param', type='quantitative', title='ν max'))
gamma_plot | nu_plot

```

## Modeling Steady-State Growth and Nutrient Concentrations
With these theoretical underpinnings in hand, we can now turn to the real meat of this notebook which is presenting a complete model of steady state growth rates as a function of the nutrient concentration, or mass fraction $\theta_a$ as we have done thus far.

Given the equations derived above, we have two expressions for the growth rate as a function of the ribosomal mass fraction $\phi_R$ and the nutrient concentration $\theta_a$,

$$
\lambda = \frac{\gamma^\text{(max)}}{1 + \frac{\theta_0}{\theta_a}}\left(\phi_R - \phi_R^\text{(min)}\right), \tag{25}
$$

and

$$
\lambda = \frac{k_P}{\beta + \theta_a}\left(\phi_R^\text{(max)} - \phi_R\right). \tag{26}
$$

Equation 26 can be rearranged to solve for the nutrient mass fraction pool $\theta_a$ to yield

$$
\theta_a = \beta\left[\frac{\nu}{\lambda}\left(\phi_R^\text{(max)} - \phi_R\right) - 1 \right]. \tag{27}
$$

Substituting Equation 24 for $\nu$ in Equation 27 and solving for $\theta_a$ results in one physically meaningful positive root, whose form I calculated symbollically below. It has an unweildly form which we will neglect enumerating here for now.

However, with a closed-form solution for $\theta_a$ in hand, we can solve Equation 25 analytically for $\lambda$, which again is just a quadratic equation. Computing symbollically results in a single physically meaningful root with a nasty form that we don't present here for clarity. What is more insightful is to choose a set of parameters and plot how the growth rate changes as a function of the parameters, which we implement below

```python
sp.init_printing()

# Define the symbols
nu_max = sp.Symbol('{{\\nu^{(max)}}}')
gamma_max = sp.Symbol('{{\\gamma^{(max)}}}')
theta_0 = sp.Symbol('\\theta_0')
theta_a = sp.Symbol('\\theta_a')
beta = sp.Symbol('\\beta')
lam = sp.Symbol('\\lambda')
phir_max = sp.Symbol('{{(\\phi_R^{(max)} - \\phi_R)}}')
phir_min = sp.Symbol('{{(\\phi_R - \\phi_R^{(min)}}}')

# Define Eq27 with MM nu
eq27 = (beta / lam) * ((nu_max / (1 + (theta_a/theta_0))) * phir_max - lam) - theta_a
theta_a_soln = sp.solve(eq27, theta_a)[0]

# Plug in this solution for theta_a into Equation 25 and solve.
eq25 = (gamma_max * phir_min)/ (1 + theta_0/theta_a_soln) - lam
lam_soln = sp.solve(eq25, lam)
```

```python
# lam_soln[1].simplify().expand().factor()
theta_a_soln
```

```python
# Define the function for the steady state growth rate
def steady_state_growth(phi_R, gamma_max, nu_max, theta_0, beta=1,
                        phi_R_max=0.4, phi_R_min=0.04):
    # Compute the mass fraction differences
    phi_min = phi_R - phi_R_min
    phi_max = phi_R_max - phi_R

    # Compute the numerator piecewise
    prefix = beta * phi_min**2 * gamma_max**2 + 2 * beta * phi_min * phi_max * gamma_max * nu_max
    sqrt = np.sqrt(beta**2 * phi_min**4 * gamma_max**4 + 4 * beta * theta_0 * phi_min **3 * phi_max * gamma_max**3 * nu_max)
    numer = prefix - sqrt

    # Compute the denominator and return
    denom = 2 * (beta * phi_min * gamma_max + beta * phi_max * nu_max - theta_0 * phi_min * gamma_max)
    return numer / denom

def steady_state_theta_a(lam, theta_0, beta, phi_R_max, phi_R):
    phi_max = (phi_R_max - phi_R)
    prefix = -lam * (beta + theta_0)
    sqrt = np.sqrt(lam * (beta**2 * lam - 2 * beta * lam*theta_0 + 4 * beta * theta_0 * phi_max * nu_max + lam * theta_0**2))
    numer = prefix + sqrt
    denom = 2 * lam
    return numer / denom

def nutritional_capacity(nu_max, theta_a, theta_0):
    return nu_max * (1 + theta_a/theta_0)**-1

def translational_capacity(gamma_max, theta_a, theta_0):
    return gamma_max * (1 + theta_0/theta_a)**-1
```

```python
# Define the parameters
# beta = 1
# gamma_max = 10
# nu_max = 5.5
# phi_R_max = 0.5
# phi_R_min = 0.025
# delta = 0.001 # To avoid division by zero
# # Typical AA concentration is ≈ 1mM. Here, we convert that to mass frac
# m_aa = 110 # mass of an aa in Da
# N_aa = 20 * 1E6 # total number of alll free amino acids
# M = 1E9 * m_aa # mass of whole proteome
# theta_0 = (N_aa * m_aa)/M #
# phi_R = np.linspace(phi_R_min+delta, phi_R_max-delta, 100)
# print(theta_0)

# Define the panel parameterized function for plot generation
gamma_slider = pn.widgets.FloatSlider(name='γ max', start=0.1, end=10, step=0.1, value=8)
nu_slider = pn.widgets.FloatSlider(name='ν max', start=0.1, end=10, step=0.1, value=6)
theta_0_slider = pn.widgets.FloatSlider(name='log10 θ\u2080', start=-4, end=0,step=0.01)

@pn.depends(gamma_slider.param.value, nu_slider.param.value, theta_0_slider.param.value)
def generate_plot(gamma_max, nu_max, theta_0, beta=1, phi_R_max=0.5, phi_R_min=0.025):
    theta_0 = 10**theta_0
    # Define the mass fraction range.
    delta = 0.0001
    phi_R_range = np.linspace(phi_R_min + delta, phi_R_max - delta, 200)

    # Compute the quantitites and set up the dataframe
    ss_gr = steady_state_growth(phi_R_range, gamma_max, nu_max, theta_0,
                                phi_R_max=phi_R_max, phi_R_min=phi_R_min)
    ss_ta = steady_state_theta_a(ss_gr, theta_0, beta, phi_R_max, phi_R_range)
    ss_nu = nutritional_capacity(nu_max, ss_ta, theta_0)
    ss_gamma = translational_capacity(gamma_max, ss_ta, theta_0)
    ss_df = pd.DataFrame(np.array([phi_R_range, ss_gr, ss_ta,
                                    ss_nu, ss_gamma]).T,
                            columns=['mass_fraction', 'growth_rate_hr',
                                    'theta_a', 'nu', 'gamma'])
    ss_df['status'] = 'physical'
    ss_df.loc[ss_df['theta_a'] < 0, 'status'] = 'non-physical'
    ss_df['theta_a'] = np.log10(ss_df['theta_a'].values)

    # Set up the plots
    ss_gr_plot = alt.Chart(ss_df, width=200, height=200, title='steady-state growth rate').mark_line().encode(
                x=alt.X(field='mass_fraction', type='quantitative', title='ribosomal mass fraction',
                       scale=alt.Scale(domain=[phi_R_min, phi_R_max])),
                y=alt.Y(field='growth_rate_hr', type='quantitative', title='growth rate [hr\u207b\u00b9]',
                        scale=alt.Scale(domain=[0, 1.5])),
                strokeDash=alt.StrokeDash(field='status', type='nominal', legend=None, sort='descending'))
    ss_ta_plot = alt.Chart(ss_df, width=200, height=200, title='steady-state θ_A').mark_line().encode(
                x=alt.X(field='mass_fraction', type='quantitative', title='ribosomal mass fraction',
                       scale=alt.Scale(domain=[phi_R_min, phi_R_max])),
                y=alt.Y(field='theta_a', type='quantitative', title='log10 nutrient mass fraction; θ_a',
                        scale=alt.Scale(domain=[-8, 1])),
                strokeDash=alt.StrokeDash(field='status', type='nominal', legend=None, sort='descending'))
    ss_gamma_plot = alt.Chart(ss_df, width=200, height=200, title='steady-state translational capacity γ').mark_line().encode(
                x=alt.X(field='mass_fraction', type='quantitative', title='ribosomal mass fraction'),
                y=alt.Y(field='gamma', type='quantitative', title='translational capacity γ [hr\u207b\u00b9]',
                       scale=alt.Scale(domain=[0, 10])),
                strokeDash=alt.StrokeDash(field='status', type='nominal', legend=None, sort='descending'))
    ss_nu_plot = alt.Chart(ss_df, width=200, height=200, title='steady-state nutritional capacity ν').mark_line().encode(
                x=alt.X(field='mass_fraction', type='quantitative', title='ribosomal mass fraction'),
                y=alt.Y(field='nu', type='quantitative', title='nutritional capacity ν'),
                strokeDash=alt.StrokeDash(field='status', type='nominal', legend=None, sort='descending'))

    # Return the plotting canvas
    return (ss_gr_plot | ss_ta_plot | ss_gamma_plot | ss_nu_plot)

# Set up the panel object
widgets = pn.Row(gamma_slider, nu_slider, theta_0_slider)
plots = pn.Column(widgets, generate_plot)
plots
```

```python

```


```python
from sympy import init_printing; init_printing()
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all" # display all expression in one cell instead of the last one
```

## Initialization of Variables

## Utility Functions

```python
def divide_Array_Eq(eq):
    from sympy import Eq
    assert(eq.lhs.shape == eq.rhs.shape)
    eq_shape = eq.lhs.shape
    arr_order = len(eq_shape)
    if arr_order > 1: # Not yet tested for high order tensor
        eq_list = []
        for i in range(eq_shape[0]):
            eq_list.append(
                divide_Array_Eq(Eq(eq.lhs[i], eq.rhs[i]))
            )
        return eq_list
    elif arr_order == 1:
        return [Eq(eq.lhs[i], eq.rhs[i]) for i in range(eq_shape[0])]
    else:# arr_order == 0
        return eq

def find_trig_period(expr, var):
#     from collections.abc import Iterable
#     if isinstance(var, Iterable):
#         return {find_trig_omega(expr, a_var) for a_var in var}
    from sympy.functions.elementary.trigonometric import TrigonometricFunction
    if isinstance(expr, TrigonometricFunction):
        try:
            return {expr.period(symbol=var)}
        except:
            return set()
    else:
        periods = [find_trig_period(sub_expr, var) for sub_expr in expr.args]
        return set().union(*periods)

def pick_min_period(expr, t):
    Ts = find_trig_period(expr, t)
    if len(Ts) == 0:
        raise ValueError("The expression does not contain Trigonometric functions, please decide the T parameter manually.")
    try:
        return min(Ts)
    except TypeError: # If we can not decide which period is the min one, a random one is chosen.
        return next(iter(Ts))

def time_average_of_TrigFunc(expr, t, method="Integrate_Then_Average", T=None):

    if method=="Integrate_Then_Average":
        from sympy import integrate

        if T is None:
            T = pick_min_period(expr, t)
        return integrate(expr, (t, 0, T)) / T
#     elif method=="Expand_Multiple_Angle_Then_Subs":
#         raise NotImplementedError(f"{__name__} not yet implemented for the method '{method}'.")
    else:
        raise ValueError(f"The `method` parameter can only be 'Integrate_Then_Average' right now.")

if __name__ == '__main__': # Code for test
    from sympy import sin, sinh, pi, symbols
    a = symbols("a", positive=True)
    b, t = symbols("b, t", real=True)
    find_trig_period(sin((b**2)*t)+sin(b*t), t)
```

## Set up the Newton Second Law Equation (ode system)

```python

```

### Time Averaging

```python
from sympy import integrate, Eq, symbols, Array, Function
from sympy import sin, cos, sqrt
from sinupy.algebra.tensor import cross

t, x, delta, a_0 = symbols("t, x, delta, a_0", real=True)
r_x, r_y, r_z = symbols("r_x, r_y, r_z", real=True, cls=Function); r = Array([r_x(t), r_y(t), r_z(t)])
p_x, p_y, p_z = symbols("p_x, p_y, p_z", real=True, cls=Function); p = Array([p_x(t), p_y(t), p_z(t)])
phi = symbols("phi", real=True, cls=Function)
A_x, A_y, A_z = symbols("A_x, A_y, A_z", real=True, cls=Function); # A = Array([0, A_y, A_z]) # Array([A_x, A_y, A_z]) or Array([0, A_y, A_z])
```

```python
A = Array([
    0,
    delta* a_0 * cos(phi(t, x)),
    sqrt(1 - delta**2) * a_0 * sin(phi(t, x))
]).subs(phi(t, x), t - x)
```

```python
phi(t, x).diff(t)
phi(t, x).subs(phi(t, x), t-x).diff(t)
```

```python
E = -A.diff(t).simplify()
B = Array([0, -A[2].diff(x), A[1].diff(x)]).simplify()
E; B
```

```python
Newton2 = Eq(p.diff(t), -( E.subs(x, r[0]) +  cross (p/sqrt(1 + p[0]**2 + p[1]**2 + p[2]**2), B.subs(x, r[0])) ) )
Newton2_list = divide_Array_Eq(Newton2)
for i in range(3): Newton2_list[i]
```

```python
import sympy.solvers.ode as ode
p_sol = ode.systems.dsolve_system(Newton2_list)

```

```python
Newton2_list = [Eq(
    p[i] - A[i].subs(x, r[0]),
    p[i].subs(t, 0)) for i in [1, 2]]
alpha = symbols("alpha", real=True)
Newton2_list.append(Eq(
    sqrt(1 + p[0]**2 + p[1]**2 + p[2]**2)-p[0],
    alpha))
for i in range(3): Newton2_list[i]
```

```python
# Set the p_y(0) = p_z(0) = 0
Newton2_list = [Eq(
    p[i] - A[i].subs(x, r[0]),
    0) for i in [1, 2]]
alpha = symbols("alpha", real=True)
Newton2_list.append(Eq(
    sqrt(1 + p[0]**2 + p[1]**2 + p[2]**2)-p[0],
    alpha))
for i in range(3): Newton2_list[i]
```

```python
from sympy import solve
p_sol = solve(Newton2_list, p[0]); p_sol
```

Suppose the electron initially at rest, $p_x(0)=p_y(0)=p_z(0)=0$ when $t=0$. Therefore $\gamma(0)=\sqrt{1+p^2(t=0)/m^2 c^2}=1$ and $\alpha=1$.

```python
import sympy.solvers.ode as ode
Newton2_list_init_rest = [eq.subs(alpha, 1) for eq in Newton2_list]
Newton2_list_init_rest.append(Eq(p[0], sqrt(1+ p[0]**2 + p[1]**2 + p[2]**2) * r[0].diff(t)))

for eq in Newton2_list_init_rest: eq
funcs = [p[i] for i in range(3)]
funcs.append(r[0])
```

```python
import plotly
```

```python
p_sol[0][p[0]] = p_sol[0][p[0]]\
    .subs(p[1], a_0 * delta * cos(t - r[0]))\
    .subs(p[2], a_0 * sqrt(1 - delta**2) * sin(t - r[0]))\
    .subs(alpha, 1).simplify();
from sympy.simplify import collect
p_sol[0][p[0]] = collect(p_sol[0][p[0]], cos(2*t - 2*r[0]))
p_sol[0][p[1]] = a_0 * delta * cos(t - r[0])
p_sol[0][p[2]] = a_0 * sqrt(1 - delta**2) * sin(t - r[0])
for i in range(3): p_sol[0][p[i]]
```

```python
p_sol = ode.systems.dsolve_system(
    Newton2_list_init_rest,
    funcs=funcs, t=t,
    ics={p[0].subs(t,0): 0, p[1].subs(t,0): 0, p[2].subs(t,0): 0, r[0].subs(t,0): 0})
```

```python

```

```python

```

## The Electron Trace Interacting with a Beam of Laser

```python
from scipy.integrate import ode as sciode
import numpy as np

x0, t0 = 0.0, 0.0
a_0, delta = 1.0, 0.2
def f(t, x, a_0, delta):
    numerator = 0.25 * a_0**2 * ( 1 + (2*delta**2-1) * np.cos(2*t-2*x))
    denominator = 1.0 + 0.25 * a_0**2 * ( 1 + (2*delta**2 - 1) * np.cos(2*t-2*x) )
    return numerator / denominator
```

```python
r = sciode(f).set_integrator('vode', method='bdf')
r.set_initial_value(x0, t0).set_f_params(a_0, delta)
t1 = 50
dt = 0.05
num_of_t = int(t1 / dt); num_of_t
x_arr = np.empty((num_of_t)); x_arr[0] = x0
t_arr = np.arange(num_of_t) * dt

i = 0
while r.successful() and i < num_of_t-1:
    i += 1
    x_arr[i] = r.integrate(r.t+dt)

y_arr = delta * a_0 * np.sin(t_arr - x_arr)
z_arr =-np.sqrt(1-delta**2) * a_0 * np.cos(t_arr - x_arr)

x_rest_arr = x_arr - a_0**2 / 4 * (t_arr-x_arr)
px_arr = a_0**2 / 4 * ( 1 + (2* delta**2 - 1) * np.cos(2*t_arr-2*x_arr))
py_arr = delta * a_0 * np.cos(t_arr-x_arr)
pz_arr = a_0 * np.sqrt(1 - delta**2) * np.sin(t_arr-x_arr)
p_arr = np.sqrt(px_arr**2 + py_arr**2 + pz_arr**2)
```

```python
import plotly.graph_objects as go
import numpy as np

fig = go.Figure(data=go.Scatter3d(
    x=x_arr, y=y_arr, z=z_arr,
    marker=dict(
        size=4,
        color=p_arr,
        colorscale='Viridis',
    ),
    line=dict(
        color=p_arr,
        width=2
    )
))

fig.update_layout(
    scene=dict(
        aspectratio = dict( x=1, y=1, z=1.0 ),
        aspectmode = 'data'))
# fig.show()

```

```python
import plotly.graph_objects as go
import numpy as np

fig = go.Figure(data=go.Scatter3d(
    x=x_rest_arr, y=y_arr, z=z_arr,
    marker=dict(
        size=4,
        color=p_arr,
        colorscale='Viridis',
    ),
    line=dict(
        color=p_arr,
        width=2
    )
))

fig.update_layout(
    scene=dict(
        aspectratio = dict( x=1, y=1, z=1.0 ),
        aspectmode = 'data'))
# fig.show()

```

## Fast Lambdify by Cupy

```python
import cupy as cp
```

## Bugs to be reported to Sympy

```python
from sympy import sin, sinh, pi, symbols
a = symbols("a", positive=True)
b, t = symbols("b, t", real=True)
sin(a*t).period(t)
```

```python
sin(-a*t).period(t) # AttributeError: 'Mul' object has no attribute 'period'
sin((-1)*a*t).period(t) # AttributeError: 'Mul' object has no attribute 'period'
```

```python
sin(b*t).period(t)
```

```python

```


# Gas Viscosity

## References:
-  ### Papers
-   Azad Jarrahian, Babak Aghel, Ehsan Heidaryan, __On the viscosity of natural gas__,
Fuel, Vol 150, Pp 609-618, 2015
https://doi.org/10.1016/j.fuel.2015.02.049
-  Lee, A.L., Gonzalez, M.H., and Eakin, B.E. 1966. **The Viscosity of Natural Gases.** J Pet Technol 18 (8): 997–1000. SPE-1340-PA. http://dx.doi.org/10.2118/1340-PA
-  <span style="font-variant:small-caps;"> F.E. Londono, R.A. Archer, and T. A. Blasingame, </span>Texas A&M **Simplified Correlations for Hydrocarbon Gas Viscosity and Gas Density —  [Validation and Correlation of Behavior Using a Large-Scale Database][1]**; *SPE  Gas  Technology  Symposium; Calgary, Alberta, Canada, 30 April–2 May 2002*
-  William Sutherland, __LII. The viscosity of gases and molecular force__, Philosophical Magazine Series 5, 36:223, 507-531, 1893
http://dx.doi.org/10.1080/14786449308620508
-   ### On-line
-   http://excelcalculations.blogspot.com/p/chemical-engineering-calculations.html
-   https://petrowiki.org/Gas_viscosity [see below (Londono et al.) and compare with above]
-   https://www.lmnoeng.com/Flow/GasViscosity.php

[1]: https://www.researchgate.net/publication/242736426_Correlations_for_Hydrocarbon-Gas_Viscosity_and_Gas_Density-Validation_and_Correlation_of_Behaviour_Using_a_Large-Scale_Database

#### Gas viscosity: the constant pressure case -- Sutherland's formula
The physical properties of a gas vary with pressure and temperature in complex and important way.
These variations can be of genuine engineering significance, especially in the petrochemical industry where we are often dealing with
mixtures of long, complex molecules in environments where the phase behaviour, including multi-phase equilibria needs to be accurately understood.
Consequently,there are  a significant number of experimental campaigns and correlations based on these datasets (usually combined with theoretical models).
We discuss some models for gas viscosity here.

One simple correlation is due to Sutherland (1893) who used the kinetic theory of gases, including Van der Waal's forces, to derive a simple relationship
which shows
how the viscosity of a gas varies with temperature __(assuming constant pressure)__:
\begin{align*}
\mu = \mu_0 \left(\frac{T}{T_0}\right)^{3/2} \frac{T_0 + S}{T + S},
\end{align*}
The constant, $S$, can be derived from experimental data and it encapsulates the molecular properties of the gas.
(Compare the derivation of the Peng Robinson model.)

We take the reference temperature to be: $T_0$ = 293.15K, additional data computed at $T$ = 450K

| Fluid   |         | S(approx) | $\mu_0$ (Pa\,s)    |   | Data Point   | Reference |
|---------|---------|-----------|-------------------------|---|--------------|------------|
| CH$_4$     |         | 173       | 0.01103                 |   |              |            |
| Nat Gas | $S_g$ 0.5  | 145.967   | 1.2645714E-5            |   | 1.7720886E-5 | lmnoeng.com |
| Nat Gas | $S_g$ 0.75 | 172.446   | 1.1251429E-5            |   | 1.60066E-5   | lmnoeng.com |
| Nat Gas | $S_g$ 1    | 238.753   | 1.0217143E-5            |   | 1.50066E-5   | lmnoeng.com |
| Air     |         | 120       | 1.822E-5            |   |              |            |
| O$_2$      |         | 140       | 2.0229876E-5            |   |              |            |
| N$_2$      |         | 120       | 1.7470348E-5            |   |              |            |
| CO$_2$     |         | 244       | 1.48E-5                 |   |              |            |
| CO      |         | 116       | 1.7435044E-5            |   |              |            |
| C$_2$H$_6$    |   | 235.77    | 0.921E-5                |   | 1.351E-5     | engineeringtoolbox.com |
|         |         | 243.86    | 0.921E-5                |   | 2.136E-5     | $\mu$ at (800K)     |
| SO$_2$  |         | 416       |                         |   |              |            |
|         |         |           |                         |   |              |            |
|         |         |           |                         |   |              |            |
|         |         |           |                         |   |              |            |

In the rows which contain entries in the < Data Point > column the value of $S$ has been obtained by applying the correlation and two points and solving for $S$. The variation in values for __ethane__ show the accuracy of this approach.

[Data taken from][2]  www.lmnoeng.com / Crane Tech Paper 410M (2013) / Perry (Chem Eng Handbook)
[Data from][3] www.engineeringtoolbox.com
[2]: https://www.lmnoeng.com/Flow/GasViscosity.php
[3]: https://www.engineeringtoolbox.com/ethane-dynamic-kinematic-viscosity-temperature-pressure-d_2069.html?vA=293.15&degree=K#

```python
#Calculate the Sutherland constant (S in the formula above) using two data points obtained from: https://www.lmnoeng.com/Flow/GasViscosity.php
#Validate against the Crane graphs (Metric version PA-6)
def SutherlandS(mu0, muT, T0 = 293.15, T1 = 450, DEBUG = 0):
    theta = (muT/mu0)*pow(T0/T1,3/2)
    S = (T1*theta - T0)/(1 - theta)
    if DEBUG == 1: print("mu0, muT, T0, T1,theta,S: ",mu0, muT, T0, T1,theta,S)
    return S
def CalcMu(mu0,T1,S,T0=293.15):
    """Compute the viscosity of a gas using Sutherland's Correlation.
    Usage: CalcMu(mu0,T0,T1,S)(mu0 - known viscosity at temperature T0, muS , viscosity at temperature T, S is Sutherland parameter.)
    """
#Example of assert statement for sanity checking!
    assert (T1>0),"Warning T1 is negative"
    muS = mu0*(T1/T0)**(3/2)*(T0+S)/(T1+S)
    return muS
#
print( "Computation of S for various Natural Gas specific gravities) ")
S = SutherlandS(1.2645714E-5,1.7720886E-5, DEBUG=0)
print("Nat Gas $S_g$ = 0.50 - Sutherland S factor: ", S)
S = SutherlandS(1.1251429E-5,1.60066E-5, DEBUG=0)
print("Nat Gas $S_g$ = 0.75 - Sutherland S factor: ", S)
S = SutherlandS(1.0217143E-5 ,1.50066E-5, DEBUG=0)
print("Nat Gas $S_g$ = 1.00 - Sutherland S factor: ", S)
S = SutherlandS(0.921E-5 ,1.351E-5, DEBUG=1)
S = SutherlandS(0.921E-5 ,2.136E-5, T1 = 800, DEBUG=1)
print("Ethane - Sutherland S factor (800): ", S)
#
print("\nValidation:                               muS,         T1,          - Comment")
T1 = 293.15+300
muS = CalcMu(0.01103,T1,173)
print("Sutherland viscosity Methane         : ",'{:12.4e}'.format(muS),'{:12.4e}'.format(T1),"K - agrees with Crane")
#All the arg lists below need changing. then do the push and see if colab has seen it. Then delete this line
muS = CalcMu(1.2645714E-5,T1,145.9674428020306)
print("Sutherland viscosity (NatGas Sg=0.50): ",'{:12.4e}'.format(muS),'{:12.4e}'.format(T1),"K - agrees with Crane")
T1 = 293.15+450
muS = CalcMu(1.1251429E-5,T1,172.44568149915352)
print("Sutherland viscosity (NatGas Sg=0.75): ",'{:12.4e}'.format(muS),'{:12.4e}'.format(T1),"K - agrees with Crane")
T1 = 293.15+550
muS = CalcMu(1.0217143E-5,T1,238.75286871568218)
print("Sutherland viscosity (NatGas Sg=1.00): ",'{:12.4e}'.format(muS),'{:12.4e}'.format(T1),"K - agrees with Crane")
T1 = 293.15+300
muS = CalcMu(1.822E-5,T1,110)
print("Sutherland viscosity Air             : ",'{:12.4e}'.format(muS),'{:12.4e}'.format(T1),"K - agrees with Crane/")
muS = CalcMu(0.921E-5,900,235.77153903863766)
print("Sutherland viscosity           Ethane: ",'{:12.4e}'.format(muS),'{:12.4e}'.format(T1),"K - EngineeringToolBox: 16.97E-6")
muS = CalcMu(0.921E-5,900,243.86)
print("Sutherland viscosity      Ethane(800): ",'{:12.4e}'.format(muS),'{:12.4e}'.format(T1),"K - EngineeringToolBox: 16.97E-6")
```

    Computation of S for various Natural Gas specific gravities)
    Nat Gas $S_g$ = 0.50 - Sutherland S factor:  145.96744280203066
    Nat Gas $S_g$ = 0.75 - Sutherland S factor:  172.44568149915352
    Nat Gas $S_g$ = 1.00 - Sutherland S factor:  238.75286871568218
    mu0, muT, T0, T1,theta,S:  9.21e-06 1.351e-05 293.15 450 0.7712795135536198 235.77153903863766
    mu0, muT, T0, T1,theta,S:  9.21e-06 2.136e-05 293.15 800 0.5144480027024468 243.86348490169044
    Ethane - Sutherland S factor (800):  243.86348490169044

    Validation:                               muS,         T1,          - Comment
    Sutherland viscosity Methane         :    1.9315e-02   5.9315e+02 K - agrees with Crane
    Sutherland viscosity (NatGas Sg=0.50):    2.1623e-05   5.9315e+02 K - agrees with Crane
    Sutherland viscosity (NatGas Sg=0.75):    2.3094e-05   7.4315e+02 K - agrees with Crane
    Sutherland viscosity (NatGas Sg=1.00):    2.4502e-05   8.4315e+02 K - agrees with Crane
    Sutherland viscosity Air             :    3.0066e-05   5.9315e+02 K - agrees with Crane/
    Sutherland viscosity           Ethane:    2.3072e-05   5.9315e+02 K - EngineeringToolBox: 16.97E-6
    Sutherland viscosity      Ethane(800):    2.3259e-05   5.9315e+02 K - EngineeringToolBox: 16.97E-6

### Exercises
*This could go below so that students can choose different formulae and compare results.*
  1. Compute the viscosity of XX (Given this dataM maybe let people find it out for themselves?) Comment on the results.
  1. More questions.
  12. Even more questions.

### Gas viscosity variation with pressure and temperature -- Londono et al.
> **NEEDS VALIDATING AND CONVERTING**

\begin{align}
\mu_g &= 10^{-4}K \exp \left ( X \rho^Y\right )\\
\rho &= \frac{PM_g}{ZRT} = 0.00149406 \frac{PM_g}{ZT}\\
K &= \frac{(9.379 +  0.01607 M_g)T^{1.5}}{209.2+19.26M_g+T}\\
X &= 3.448 + \frac{986.4}{T} + 0.01009 M_g\\
Y&= 2.447 -0.2224X
\end{align}
$\mu_g$: gas viscosity in cp; $\rho$: gas density in g/cm$^3$; $P$: pressure psia; $T$: temperature in $^\circ$R; $M_g$: gas molecular weight ($=28.967\gamma_g$).

-   For the data from which the correlation was developed, the standard deviation in the calculated gas viscosity was 2.7%, and the maximum deviation was 9%.
-   The ranges of variables used in the correlation were 100 psia < p < 8,000 psia, 100 < T (°F) < 340, and 0.90 < $CO_2$ (mol%) < 3.20 and 0.0 < $N_2$ (mol%) < 4.80.
-   In using these equations, it is important either to measure the density or to ensure that the z -factor calculation has included the effect of $N_2$, $4CO_2$, and $H_2 S$ using the method of Wichert and Aziz.[5]
-   The equations of Lee et al.[4] were originally written to give the viscosity in micropoise, but the modified form above gives the viscosity in the more commonly used centipoise. This viscosity unit (cp) is also easily converted to the SI unit of Pa•s by dividing by 1,000

# Mixtures

For many quantities we can use the obvious weighted averages -- aka Kay's rule:
Kay, W. 1936. Gases and Vapors At High Temperature and Pressure - Density of Hydrocarbon. Ind. Eng. Chem. 28 (9): 1014-1019. http://dx.doi.org/10.1021/ie50321a008
(See also __Fugacity__.)

The Piping Calculations Manual (Shashi Menon, 2005) gives the following formula for the vixture of a mixture of components:
$\mu = \frac{\Sigma (\mu_i y_i \sqrt{M_i}}{\Sigma (y_i \sqrt{M_i}}$
#Eqn 7.4 P393, Mendeley

# Equations of State

Many variants of VdW and Peng Robinson.... Where do we get phase information?
Basically we use the generalised gas equation:
$PM = \rho z R T  \hspace{10cm}$[Equation 1]
$Z$, the compressibility accouts for the non-idealized behaviour of the gas/mixture. Using Peng-Robinson for $Z$ is a reasonable approach.
Alternatives are:
-  Soave-Redlich-Kwong (SRK EOS)
-  [Lee Kesler EOS (LK EOS)][1]
-  Benedict-Webb-Rubin EOS (BWR EOS)
-  Benedict-Webb-Rubin-Starling EOS (BWRS EOS)
[1]: https://www.e-education.psu.edu/png520/m7_p3.html

Note that the PR EOS and rhe SRK EOS are derived from van der Waal's equations; these are all cubic equations for the compressibility. There are obviously cases where the equations have three real roots - this reflects that the equilibrium fluid is in two-phase [equilibrium][2] for these conditions.

[2]: http://www.et.byu.edu/~rowley/ChEn273/Topics/Mass_Balances/Single_Phase_Systems/Ideal_Systems_Liquid_&_Solid.htm

To use [Equation 1] we only need to know the [value of $\rho$ for some specific values][3] of $P$ and $T$, and the appropriate coefficients for the compressibility of the gas. [On-line calculators][4] are also available.

We can use the data point that natural gas with a molecular weight of 19.5 has a density in the range: $0.7-0.9 kg m^{-3}$ at standard temperature and pressure (273.15K, 1.01325E5 Pa).

[3]: https://www.engineeringtoolbox.com/gas-density-d_158.html
[4]: https://www.unitrove.com/engineering/tools/gas/natural-gas-density

```python

```

Gas Viscosity and Equations of State.ipynb


# Esta tarea fue realizada en equipo por Betsy Torres, Mariana Briones y Erick Mendoza

## Griegas de un Put option

## Griegas para opción PUT Europea
**Formula de Black \& Scholes para el precio justo de una opción tipo PUT tipo Europea.**

\begin{equation}
\hat{E}[P_{t}^{LP}(t,S_{t})]=k e^{-r(T-t)}N(-d2)- S_{t}N(-d1)
\end{equation}

Consideramos que el portafolio tiene una opción put ${Q_{1}= 1}$ donde:

$$f_{t}=e^{-r(T-t)}\hat{E}[P_{t}^{LP}(t,S_{t})]$$

Si $X=ln(S_t)$:

$$ln(S_t)\sim N[(ln(S_{t}) + (\mu -\frac{\sigma^2}{2})(T-t)) , \sigma^{2} (T-t)]$$

Podemos definir el portafolio de la siguiente manera:

$$V_{t}=e^{-r(T-t)}[\int_{-\infty}^{ln(k)} (k-S_{t})p(x) dx + \int_{ln(k)}^{\infty} (0)p(x) dx ]$$

Remplazando $p(x)$:
$$V_{t}=e^{-r(T-t)}[\int_{-\infty}^{ln(k)} (k-S_{t})[ \frac{1}{\sqrt{2\pi\sigma^{2}(T-t)}}e^{\frac{-1}{2}(\frac{x-[ln(S_{t})+ (r-\frac{\sigma^2}{2})(T-t)]}  {\sigma\sqrt{(T-t)}})^2}] dx$$

### Delta $\frac{\partial V_t}{\partial S_t}= \triangle$

Previamente probamos que el precio justo de una opción Europea put está dada por la ecuación 1.

$$\hat{E}[P_{t}^{LP}(t,S_{t})]=k e^{-r(T-t)}N(-d2)- S_{t}N(-d1)$$

donde:

$$-d1=\frac{-ln(\frac{S_t}{k}) +(r+\frac{\sigma^2}{2})(T-t) }{\sigma\sqrt{(T-t)}}$$

$$-d2=\frac{-ln(\frac{S_t}{k}) +(r-\frac{\sigma^2}{2})(T-t) }{\sigma\sqrt{(T-t)}}$$

$$-d2=-d1-\sigma\sqrt{(T-t)}$$

Podemos expresar el portafolio en términos de -d1:

\begin{equation}
V_{t}=ke^{-r(T-t)} N(-d1-\sigma\sqrt{(T-t)})S_{t}N(-d1)
\end{equation}

Dividimos el portafolio respecto de -d1:

$$\frac{\partial{V_{t}}}{\partial{-d1}}=ke^{-r(T-t)}N'(-d1-\sigma\sqrt{(T-t)})S_{t}N'(-d1)$$

$$0=ke^{-r(T-t)}N'(-d1-\sigma\sqrt{(T-t)})S_{t}N'(-d1)$$

Sabemos que :

$$N'(-d1-\sigma\sqrt{(T-t)})=-d2$$

Por lo tanto,  **propiedad 1**:

$$S_{t}N'(-d1)=ke^{-r(T-t)}N'(-d2)$$

Ahora, si derivamos -d1 y -d2 con respecto de $S_{t}$ tenemos:

$$-d1=\frac{-ln(\frac{S_t}{k}) +(r+\frac{\sigma^2}{2})(T-t) }{\sigma\sqrt{(T-t)}}$$

Obsevamos que $(r+\frac{\sigma^2}{2})(T-t) $no tiene $ S_{t}$

$$\frac{\partial{-d1}}{\partial{S_t}}=\frac{-1}{S_{t}\sigma\sqrt{(T-t)}}$$

$$-d2=\frac{-ln(\frac{S_t}{k}) +(r-\frac{\sigma^2}{2})(T-t) }{\sigma\sqrt{(T-t)}}$$

$$\frac{\partial{-d2}}{\partial{S_{t}}}=\frac{-1}{S_{t}\sigma\sqrt{(T-t)}}$$

Obtenemos **propiedad 2**:
$$\frac{\partial -d1}{\partial S_t}=\frac{\partial-d2}{\partial S_t}$$

Retomando los conceptos anteriores, el valor del portafolio esta dado por la ecuación:

\begin{equation}
V_{t}=ke^{-r(T-t)} N(-d2)S_{t}N(-d1)
\end{equation}

Derivamos con respecto de $S_t$ y usando la regla de la cadena:

$$\frac{\partial{V_{t}}}{\partial{S_{t}}}=(ke^{-r(T-t))} (\frac{\partial{N(-d2)}}{\partial{-d2}}) (\frac{\partial{-d2}}{\partial{S_t}}) - [\frac{\partial{S_t}}{\partial{S_t}} N(-d1) + (S_t)(\frac{\partial N(-d1)}{\partial -d1})(\frac{\partial-d1}{\partial S_t})]$$

$$\frac{\partial{V_{t}}}{\partial{S_{t}}}=(ke^{-r(T-t))}N'(-d2)(\frac{\partial{-d2}}{\partial{S_t}}) - [(1)N(-d1)+ S_{t}N'(-d1)()] (\frac{\partial-d1}{\partial S_t})$$

Por la propiedad 2:
$$\frac{\partial -d1}{\partial S_t}=\frac{\partial -d2}{\partial S_t}$$

Obtenemos lo siguiente:

$$\frac{\partial{V_{t}}}{\partial{S_{t}}}= (\frac{\partial-d1}{\partial S_t})[ke^{-r(T-t))} N'(-d2) - S_{t}N'(-d1)] - N(-d1)$$

Por la propiedad 1:

$$S_{t}N'(-d1)=ke^{-r(T-t)}N'(-d2)$$

$$\frac{\partial{V_{t}}}{\partial{S_{t}}}= (\frac{\partial-d1}{\partial S_t})[ke^{-r(T-t))} N'(-d2) - ke^{-r(T-t))} - N'(-d2)]-N-(d1)$$

$$\frac{\partial{V_{t}}}{\partial{S_{t}}}= (\frac{\partial-d1}{\partial S_t})[0]-N-(d1)$$

$$\frac{\partial{V_{t}}}{\partial{S_{t}}}= -N-(d1) - \triangle$$

### Gamma $\frac{\partial {^{2}} V_t}{\partial S^{2}_t}= \Gamma$

Veamos que pasa con la segunda derivada para -d1 y -d2 ...

Sabemos que : $$\frac{\partial -d1}{\partial S_t} = -\frac{1}{S_{t} \sigma \sqrt{(T-t)}} = \frac{\partial -d2}{\partial S_t} $$

Por lo que:

$$\frac{\partial^{2}-d1}{\partial S^{2}_t} = \frac{1}{S^{2}_{t} \sigma \sqrt{(T-t)}} $$ $$\frac{\partial^{2}-d2}{\partial S^{2}_t} = \frac{1}{S^{2}_{t} \sigma \sqrt{(T-t)}} $$

**Propiedad 3**:

$$\frac{\partial^{2}-d1}{\partial S^{2}_t} = \frac{\partial^{2}-d2}{\partial S^{2}_t} $$

Retomando la ecuación 3 que fue el resultado previo de delta:

$$V_{t}=ke^{-r(T-t)} N(-d2)S_{t}N(-d1)
$$

$$\frac{\partial^{2}V_t}{\partial S^{2}_t} = \frac{\partial \triangle}{\partial S_t}= \Gamma$$

Por propiedad 2 $(\frac{\partial -d1}{\partial S_t}=\frac{\partial -d2}{\partial S_t})$:

$$\frac{\partial \triangle}{\partial S_t}= \frac{\partial - N(-d1)}{\partial S_t}= \frac{\partial N(-d1)}{\partial {-d1}} \frac{\partial -d1}{\partial S_t}$$

$$\frac{\partial \triangle}{\partial S_t}= - N'(-d1)(\frac{-1}{S_{t}\sigma\sqrt{(T-t)}})$$

Por lo tanto:

$$\frac{\partial^{2} \triangle}{\partial S^{2}_t}= - N'(-d1)(\frac{-1}{S_{t}\sigma\sqrt{(T-t)}}) = \Gamma$$

### Rho $\frac{\partial  V_t}{\partial r}= \rho$

Derivamos -d1 y -d2 con respecto de r:
$$\frac{\partial -d1}{\partial r}= - \frac{\sqrt{(T-t)}}{\sigma}$$

$$\frac{\partial -d2}{\partial r}= - \frac{\sqrt{(T-t)}}{\sigma}$$

Obtenemos una **cuarta propiedad (4)**:

$$\frac{\partial -d1}{\partial r}=\frac{\partial -d2}{\partial r}$$

Retomando el portafolio con la ecuación 3:

$$V_t=K e^{-r(T-t)}N(-d2)- S_{t}N(-d1)$$

$$\frac{\partial{V_{t}}}{\partial{S_{t}}}= (\frac{\partial ke^{-r(T-t)}}{\partial r}) N(-d2) + (ke^{-r(T-t)})(\frac{\partial N(-d2)}{\partial -d2}) (\frac{\partial -d2}{\partial r}) - [S_{t} (\frac{ \partial N'(-d1)}{\partial -d1}) (\frac{\partial -d1}{\partial r})] $$

$$\frac{\partial{V_{t}}}{\partial{S_{t}}}= - (T-t)ke ^{-r(T-t)} N(-d2) + ke ^{-r(T-t)} N'(-d2) \frac{\partial -d2}{\partial r} - [S_{t} N'(-d1) \frac{\partial -d1}{\partial r}]$$

Por propiedad 4 y factor común:

$$\frac{\partial{V_{t}}}{\partial{S_{t}}}= - (T-t)ke ^{-r(T-t)} N(-d2) + \frac{\partial -d1}{\partial r}[ke ^{-r(T-t)} N'(-d2)-S_{t} N'(-d1)]$$

Por propiedad 1:
$$\frac{\partial{V_{t}}}{\partial{S_{t}}}= - (T-t)ke ^{-r(T-t)} N(-d2) + \frac{\partial -d1}{\partial r}[ke ^{-r(T-t)} N'(-d2)- ke ^{-r(T-t)} N(-d2)  ]$$

$$\frac{\partial{V_{t}}}{\partial{S_{t}}}= - (T-t)ke ^{-r(T-t)} N(-d2) + \frac{\partial -d1}{\partial r}[0]$$

Al finalizar tenemos que:
$$\frac{\partial{V_{t}}}{\partial{S_{t}}}= - (T-t)ke ^{-r(T-t)} N(-d2) = \rho$$

### Theta $\frac{\partial V_t}{\partial \tau}= \Theta$

Por sencillez, definimos que $(T-t)= \tau$

Es necesario obtener la segunda derivada de d1 y d2 con respecto de $\tau$

$$-d1=- \frac{ln(\frac{S_t}{k}) + (r + \frac{\sigma^2}{2})(\tau)}{\sigma\sqrt{\tau}}$$

$$\frac{\partial -d1}{\partial \tau}= \frac{1}{2} (\frac{ln(\frac{S_t}{k})}{\sigma \tau\sqrt{\tau}}) - \frac{1}{2} (\frac{r+ \frac{\sigma^2}{2}}{\sigma \sqrt{\tau}})$$

$$\frac{\partial -d1}{\partial \tau}= \frac{1}{2\sigma\sqrt{\tau}}[\frac{ln(\frac{S_t}{k})}{\tau} - (r + \frac{\sigma^2}{2})]$$

$$-d2= - \frac{ln(\frac{S_t}{k})- (r+ \frac{\sigma^2}{2})(\tau)}{\sigma\sqrt{\tau}}$$

$$-d2 =-d1 -\sigma\sqrt{\tau} \rightarrow \frac{\partial(-d1-\sigma\sqrt{\tau})}{\partial \tau}=\frac{\partial -d1}{\partial \tau} - \frac{1}{2 } \frac{\sigma}{\sqrt{\tau}}$$

**Propiedad 5**:

$$\frac{\partial -d1}{\partial \tau} = - [\frac{\partial -d2}{\partial \tau} + \frac{1}{2}\frac{\sigma}{\sqrt{\tau}}]$$

Retomando el valor del portafolio (ecuación 3):

$$V_t=K e^{-r(T-t)}N(-d2)- S_{t}N(-d1)$$

$$\frac{\partial V_t}{\partial\tau}= \frac{\partial(ke^{-r(\tau)})}{\partial \tau}N(-d2) + ke^{-r(\tau)} \frac{\partial N(-d2)}{\partial -d2} \frac{\partial -d2}{\partial \tau} - [S_{t} \frac{\partial N(-d1)}{\partial -d1} \frac{\partial -d1}{\partial \tau}]$$

$$\frac{\partial V_t}{\partial\tau}=-rke^{-r(\tau)} N(-d2) + ke^{-r(\tau)}N(-d2) \frac{\partial -d2}{\partial \tau} -[S_{t} \frac{\partial N(-d1)}{\partial -d1} \frac{\partial -d1}{\partial \tau}]$$

Por propiedad 5 y factor común:

$$\frac{\partial V_t}{\partial\tau}=-rke^{-r(\tau)} N(-d2) - \frac{1}{2} \frac{\sigma}{\sqrt{\tau}}[-S_{t}N'(-d1)] - \frac{\partial -d2}{\partial \tau}[-S_{t}N'(d1) + ke^{-r(\tau)}N'(-d2)]$$

Por propiedad 1:

$$\frac{\partial V_t}{\partial\tau}=-rke^{-r(\tau)} N(-d2) + \frac{\sigma}{2\sqrt{\tau}}S_{t}N'(d1) -\frac{\partial -d2}{\partial \tau} [-ke^{-r(\tau)}N'(-d2) + ke^{-r(\tau)}N'(-d2)]$$

$$\frac{\partial V_t}{\partial\tau}= -rke^{-r(\tau)}N(-d2) + \frac{\sigma}{2\sqrt{\tau}}S_{t}N'(-d1) - \frac{\partial -d2}{\partial \tau}[0] $$

Por naturaleza de $\tau$:

$$\frac{\partial V_t}{\partial\tau} \rightarrow \frac{-\partial V_t}{\partial\tau} \leq 0 = \Theta$$

Por lo tanto:

$$\Theta = -\frac{\partial V_t}{\partial\tau} =rke^{-r(\tau)} N(-d2) - \frac{\sigma}{2\sqrt{\tau}} S_{t}N'(-d1) $$

### Vega $\frac{\partial V_t}{\partial \sigma}= \nu$

Obtenemos la derivada de -d1 y -d2 con respecto de $\sigma$:

$$\frac{\partial -d1}{\partial \sigma} = \frac{ln(\frac{S_t}{k})}{\sigma^{2}\sqrt{(T-t)}} + \frac{r (T-t)}{\sigma^{2}\sqrt{(T-t)}} - \frac{(T-t)}{\sqrt{(T-t)}}$$

$$-d2= -d1 - \sigma\sqrt{(T-t)} \rightarrow \frac{\partial -d2}{\partial\sigma}= \frac{\partial -d1}{\partial \sigma}-\sqrt{(T-t)}$$

**Propiedad 6**:

$$\frac{\partial -d1}{\partial \sigma}=-[\frac{\partial -d2}{\partial\sigma} + \sqrt{(T-t)}]$$

Retomando el valor del portafolio (ecuación 3):

$$V_t=K e^{-r(T-t)}N(-d2)- S_{t}N(-d1)$$

$$\frac{\partial V_t}{\partial\sigma}= ke^{-r(T-t)} \frac{\partial N(-d2)}{\partial-d2} \frac{\partial -d2}{\partial\sigma} - S_{t} \frac{\partial N(-d1)}{\partial-d1} \frac{\partial -d1}{\partial\sigma}$$

$$\frac{\partial V_t}{\partial\sigma}= ke^{-r(T-t)} N'(-d2) \frac{\partial -d2}{\partial\sigma} - S_{t} N'(-d1) \frac{\partial -d1}{\partial\sigma}$$

Por propiedad 6 y factor común:

$$\frac{\partial V_t}{\partial\sigma}= - \frac{\partial -d2}{\partial\sigma}[ke^{-r(T-t)} N'(-d2)  - S_{t} N'(-d1)] - [S_{t} N'(-d1) (-\sqrt{(T-t})]$$

Por propiedad 1:

$$\frac{\partial V_t}{\partial\sigma}= - \frac{\partial -d2}{\partial\sigma}[ke^{-r(T-t)} N'(-d2)  -  ke^{-r(T-t)} N'(-d2)] + S_{t} N'(-d1) (\sqrt{(T-t})$$

$$\frac{\partial V_t}{\partial\sigma}= - \frac{\partial -d2}{\partial\sigma}[0] + S_{t} N'(-d1) (\sqrt{(T-t})$$

$$\frac{\partial V_t}{\partial\sigma}=  [S_{t} N'(-d1) (\sqrt{(T-t})] = \nu$$

```python

```