Skip to content

Instantly share code, notes, and snippets.

View brandonwillard's full-sized avatar

Brandon T. Willard brandonwillard

View GitHub Profile
@brandonwillard
brandonwillard / sequence_probabilities.py
Last active November 17, 2023 23:55
Computing sequence probabilities in Outlines
import torch
import outlines.models as models
from outlines.text.generate.regex import choice
from outlines.text.generate.continuation import continuation
from outlines.text.generate.sample import greedy
def make_greedy_tracker(generator):
@brandonwillard
brandonwillard / lift_adv_indices_concat.py
Last active December 23, 2021 01:20
Lift advanced indices through concatenate/stack
from typing import Tuple, Union
import numpy as np
def is_basic_idx(x):
return isinstance(x, (slice, type(None)))
def expand_indices(
@brandonwillard
brandonwillard / numba-ufunc-reduce.py
Created October 26, 2021 15:50
Calling `PyUFuncObject`'s `reduce` method from Numba
import numba as nb
import numpy as np
from numba.core.datamodel.models import StructModel
from numba.core.datamodel.registry import register_default
from numba.core.extending import intrinsic, overload, overload_method
@nb.njit
def add(x, y):
return x + y
@brandonwillard
brandonwillard / aeppl-time-varying-hmm.py
Created October 15, 2021 23:46
An HMM with time-varying transitions matrices constructed in AePPL
import aesara
import aesara.tensor as at
import numpy as np
from aeppl.joint_logprob import factorized_joint_logprob
# aesara.config.compute_test_value = "warn"
srng = at.random.RandomStream(seed=2320)
N = 100
@brandonwillard
brandonwillard / numpy-extension-testing.org
Last active October 15, 2021 17:46
Custom NumPy C API extension testing setup (look at the raw document)

NumPy C API Testing

@brandonwillard
brandonwillard / both-opt-max-reduce-axis-1.pdf
Last active May 14, 2024 22:40
Numba CAReduce Performance MWE
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@brandonwillard
brandonwillard / aesara-remove-constants.py
Created August 30, 2021 22:20
Some preliminary work to remove "constant" sub-graphs with minimal/no changes to multiplication order
from collections import defaultdict
import aesara.tensor as aet
from aesara import config
from aesara.compile import optdb
from aesara.graph.basic import ancestors, applys_between, graph_inputs
from aesara.graph.fg import FunctionGraph
from aesara.graph.opt import (
EquilibriumOptimizer,
@brandonwillard
brandonwillard / hs-gibbs-under-determined.py
Last active August 27, 2021 22:41
Normal Regression with Horseshoe prior Gibbs Sampling for an Under-determined Model
import time
from datetime import date, timedelta
import arviz as az
import formulaic
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy
@brandonwillard
brandonwillard / scan-log-likelihood.org
Created August 24, 2021 19:25
Illustrations of `theano.scan` structure and manipulations for log-likelihood generation

Computing log-likehood graphs for src_python[:eval never]{Scan}s

@brandonwillard
brandonwillard / svd-gaussian-posterior.py
Last active July 27, 2021 00:29
SVD-based Gaussian conjugate update
import numpy as np
sigma2_true = 2.0
def simulate_regression(N=100, M=1000):
X = np.random.normal(size=(N, M))
beta_true = np.random.normal(size=(M,))
y = X.dot(beta_true) + np.random.normal(0, np.sqrt(sigma2_true), size=(N,))