Skip to content

Instantly share code, notes, and snippets.

@omarfsosa
omarfsosa / binomial.py
Last active September 30, 2022 12:50
Binomial Asset Pricing (Shreve's chapter 1)
"""
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)
@omarfsosa
omarfsosa / training.py
Created September 5, 2022 08:51
Custom tqdm progress bar in pytorch training loop
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:
@omarfsosa
omarfsosa / install-numpyro.sh
Last active February 21, 2022 06:33
Install Python via Pyenv and Pyenv-Virtualenv on an Ubuntu EC2 (AWS)
#!/bin/bash
pip install matplotlib numpy pandas scipy scikit-learn
pip install jax numpyro
@omarfsosa
omarfsosa / temme.py
Last active December 28, 2021 23:26
Temme approximation for Poisson inverse CDF
"""
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
@omarfsosa
omarfsosa / basic-ds-setup.sh
Last active August 4, 2022 21:18
Set up data science tools
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
"""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.
4.69
5.85
7.28
3.49
3.26
5.69
12.13
10.42
5.28
5.88
@omarfsosa
omarfsosa / truncated_poisson_data.stan
Created February 4, 2021 21:54
Modelling poisson data with selection bias.
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;
}
}
@omarfsosa
omarfsosa / jupyter_env.bash
Created January 28, 2021 18:09
Make python environment available in jupyter notebook
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)"
@omarfsosa
omarfsosa / neg_binomial_2_rng.py
Created January 27, 2021 16:06
Sample from a negative binomial in terms of mean and precision parameters
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)