Skip to content

Instantly share code, notes, and snippets.

View davipatti's full-sized avatar

David Pattinson davipatti

  • UW-Madison
  • New York, USA
View GitHub Profile
@davipatti
davipatti / pymc-ordered-distributions.ipynb
Created August 27, 2024 20:20
Investigating the behaviour of pymc.distributions.transforms.ordered
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@davipatti
davipatti / find_maximal_subsets.py
Created August 14, 2024 16:43
Find maximal subsets
#!/usr/bin/env python3
from collections import defaultdict
from itertools import chain
def find_maximal_subsets(sets: list[set]) -> set[frozenset]:
"""
Find the maximal subsets of items that always appear together in a group of sets.
@davipatti
davipatti / mean-set-containment.py
Created August 14, 2024 13:27
[mean set containment] What is the probability a set of size N contains its mean?
#!/usr/bin/env python3
import random
import matplotlib.pyplot as plt
numbers = tuple(range(100))
def trial(N: int) -> bool:
S = random.choices(numbers, k=N)
@davipatti
davipatti / assign_coords.py
Last active August 2, 2024 18:41
[Rename coordinates using assign_coords] How to rename coordinates in an xarray dataset #pymc #arviz #xarray
def rename_coordinates(dataset: "xarray.Dataset", dim: str, mapping: dict[str, str]) -> "xarray.Dataset":
return dataset.assign_coords(**{dim: [mapping[k] for k in dataset.coords[dim].values]})
@davipatti
davipatti / prior-dists-dont-need-to-resemble-the-posterior.ipynb
Created November 7, 2023 19:21
[Bayesian prior distributions don't need to look like what you think the posterior distribution will look like]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@davipatti
davipatti / matplotlib-transforms.py
Last active September 20, 2023 20:54
[Useful matplotlib transforms] #matplotlib
import matplotlib.pyplot as plt
ax = plt.gca()
axis_to_data = ax.transAxes + ax.transData.inverted()
data_to_axis = axis_to_data.inverted()
@davipatti
davipatti / aesara-test.py
Last active June 2, 2023 20:33
[aesara scan IndexError] Using aesara.scan to make values 0 if any three preceding values in a column are non-zero. Output is as expected, but errors are logged, and IndexError gets raised. #aesara
import aesara as ae
import numpy as np
print("aesara version", ae.__version__)
np.random.seed(42)
m, n = 10, 12
arr = np.random.choice([0, 1], p=[0.75, 0.25], size=m * n).reshape(m, n)
print("\ninput:")
@davipatti
davipatti / make_trace.py
Created May 16, 2023 17:42
Try to load a pymc trace from a path. If the path doesn't exist then generate the trace by calling fun.
import arviz as az
def make_trace(path: str, fun: Callable, *args, **kwargs):
"""
Try to load a pymc trace from a path. If the path doesn't exist then generate
the trace by calling fun.
Args:
path: Path to Netcdf file.
fun: Callable that returns a PyMC trace.
@davipatti
davipatti / hide-code-demo.ipynb
Created April 19, 2023 18:04
[ipynb toggle code demo] #jupyter #ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@davipatti
davipatti / hide_code_cells.py
Last active April 19, 2023 17:57
[jupyter notebook toggle code cells] Snippet for hiding code cells in a jupyter notebook #jupyter #ipynb #python
from IPython.display import HTML
HTML("""
<script>
code_show = true;
function code_toggle() {
if (code_show) {
$('div.input').hide();
} else {
$('div.input').show();
} code_show = !code_show