Skip to content

Instantly share code, notes, and snippets.

@omarfsosa
omarfsosa / multinormal.py
Last active January 26, 2021 17:07
Sample from a multinormal distribution when given the correlation coefficient
import numpy as np
# given
mu_x = 10
mu_y = 20
sigma_x = 2
sigma_y = 3
corr = 0.3
# ---
@omarfsosa
omarfsosa / extend_datetime.py
Created January 26, 2021 18:02
Extend a pandas datetime index
import pandas as pd
def extend_datetime_index(index: pd.DatetimeIndex, end: pd.Timestamp = None, periods: int = None) -> pd.DatetimeIndex:
"""
Minimal function for extending a pandas DateTimeIndex.
Original attributes of the `index` (e.g name or tz) are not kept.
Only the `freq` attribute is preserved.
"""
if (bool(end) == bool(periods)): # xor
@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)
@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 / 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;
}
}
4.69
5.85
7.28
3.49
3.26
5.69
12.13
10.42
5.28
5.88
"""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.
@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
@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 / 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