This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Binomial Asset Pricing Model | |
---------------------------- | |
These are some class implementations I'm using to replicate in code | |
some of the examples presented in Shreve's book on Stochastic Calculus | |
part 1. | |
Example usage (example 1.3.1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tqdm.auto import tqdm | |
# The example below is for a classification model | |
# with the last layer of the model producing the logits | |
# --i.e. without a softmax layer | |
def train_sinle_epoch(dataloader, model, loss_fn, optimizer): | |
correct_total = 0 | |
size_total = 0 | |
with tqdm(dataloader, unit="batch") as tepoch: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
pip install matplotlib numpy pandas scipy scikit-learn | |
pip install jax numpyro |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Approximate inverse CDF for the poisson distribution in Jax. | |
Based on the approximation method proposed in [1]. | |
References | |
---------- | |
[1]: https://people.maths.ox.ac.uk/gilesm/codes/poissinv/paper.pdf | |
""" | |
import jax | |
import jax.numpy as jnp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python -m pip install --upgrade pip | |
pip install cython pybind11 pythran numpy | |
OPENBLAS=$(brew --prefix openblas) CFLAGS="-falign-functions=8 ${CFLAGS}" pip install --no-use-pep517 scipy==1.7.0 | |
pip install pandas matplotlib jupyter jupyterlab | |
OPENBLAS=$(brew --prefix openblas) CFLAGS="-falign-functions=8 ${CFLAGS}" pip install --no-use-pep517 scikit-learn==1.0.1 | |
jupyter nbextension enable --py widgetsnbextension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Visualization tools""" | |
import matplotlib.pyplot as plt | |
import numpy as np | |
def spaghetti_plot( | |
x, y, n_samples=20, indices=None, ax=None, plot_kwargs=None | |
): | |
""" | |
Plots x against a few picked examples of y. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4.69 | |
5.85 | |
7.28 | |
3.49 | |
3.26 | |
5.69 | |
12.13 | |
10.42 | |
5.28 | |
5.88 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
functions { | |
real normalisation(real lam, int lb, int ub) { | |
real result = 0; | |
for (x in lb:ub) { | |
result += (lam^x)/tgamma(x+1); | |
} | |
result *= exp(-lam); | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
conda create -n myenv python=3.6 | |
conda activate myenv | |
pip install ipykernel | |
python -m ipykernel install --user --name myenv --display-name "Python 3.6 (myenv)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def neg_binomial_2_rng(mu, phi, size=None): | |
""" | |
RNG for the negative binomial parametrised in terms | |
of the mean and the precision. | |
mean = mu | |
variance = mu + (mu**2)/phi | |
""" | |
tau = np.random.gamma(phi, mu/phi, size=size) |
NewerOlder