Skip to content

Instantly share code, notes, and snippets.

View albertbuchard's full-sized avatar

Albert Buchard albertbuchard

View GitHub Profile
@albertbuchard
albertbuchard / PSM Bootstrap.py
Last active September 15, 2025 09:29
PSM Bootstrap
from typing import Optional
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import warnings
import statsmodels.formula.api as smf
from statsmodels.tools.sm_exceptions import (
ConvergenceWarning,
@albertbuchard
albertbuchard / entropy_balanced_ipaw.py
Created May 25, 2025 01:20
Entropy-Balanced IPAW: Efficient implementation of ridge-penalised entropy balancing applied to inverse probability of attrition weights (IPAW), ensuring finite, non-negative weights for longitudinal studies.
import numpy as np
import pandas as pd
import cvxpy as cp
from typing import Sequence, Union
def entropy_balance_ipaw(
df: pd.DataFrame,
*,
@albertbuchard
albertbuchard / test_preweight_equivalence_full.py
Last active May 15, 2025 18:40
O3 Answers: Pre-weighting ≡ IPAW (in lmer)
"""
This example demonstrates that fitting a weighted linear mixed-effects model using lmer(weights = …)
is algebraically equivalent to pre-multiplying the outcome and all predictors (including intercepts)
by the square root of the weights and fitting an unweighted model on the transformed data.
The test validates that this "pre-weight and refit" strategy yields identical fitted values and
is especially useful when packages like clubSandwich prohibit prior weights in robust variance estimation.
Includes realistic longitudinal synthetic data with fixed and random effects, dropout, and continuous covariates.
"""
from typing import List
@albertbuchard
albertbuchard / active_learning_voI_surprisal.md
Created May 15, 2025 13:58
O3 Answers: Value of Information and Active Learning

Active learning in one line Keep a model $p_\theta(y\mid x,\mathcal D)$ that already knows something ($\mathcal D$); then ask for the single measurement $(x^*,y^*)$ that, once answered, will help most.


1. Value of Information (VoI)

Concept Formal core Intuition for the next query
Decision-centric $\text{VoI}(x)=\mathbb E_{y\sim p_\theta(y\mid x,\mathcal D)}\big[\Delta U(\theta\mid x,y)\big]$ “If I were given the label of $x$, how much would my future utility rise on average?”
@albertbuchard
albertbuchard / partial_correlations.py
Created October 14, 2024 17:18
Partial Correlation Algorithms Evaluation
import matplotlib.pyplot as plt
import networkx as nx
from causallearn.utils.KCI.KCI import KCI_CInd, KCI_UInd
from npeet_plus import mi, mi_pvalue
from tqdm import tqdm
from src.causal_discovery.static_causal_discovery import (
run_causal_discovery,
visualize_causal_graph,
)
@albertbuchard
albertbuchard / textrepo.py
Last active January 4, 2026 19:20
textrepo: A Python Script to Concatenate All Files in a Repository into a Single Text File, Ignoring Specified Patterns
#!/usr/bin/env python3
"""
textrepo — dump a repository into a single text file, optimized for LLM input.
Features
- Always prints a folder tree of the discovered files (after ignores).
- Emits a per-file section for every *text-eligible* file (even if skipped/truncated).
- Respects .gitignore by default (disable with --no-gitignore).
- Supports extra ignore patterns and an additional ignore file (gitignore-ish).
- Detects and omits binary / non-text files from the per-file sections
@albertbuchard
albertbuchard / hh_ht_win_rate.py
Last active March 19, 2024 18:25
Parallelizing Simulations to Calculate HH vs HT Win Rates
from concurrent.futures import ProcessPoolExecutor
import numpy as np
from scipy.signal import convolve2d
from tqdm import tqdm
nsims = int(1e6)
nevents = int(1e5)
@albertbuchard
albertbuchard / .bashrc
Created November 2, 2023 16:49
Useful SLURM bash functions (add to your ~/.bashrc)
tailast() {
# Get the directory from the argument or use the current directory as default
local dir="${1:-.}"
# Find the most recently modified file in the directory without descending into subdirectories
local latest_file=$(find "$dir" -maxdepth 1 -type f -exec stat --format='%Y %n' {} \; | sort -n | tail -1 | awk '{print $2}')
# Check if a file was found
if [[ -z "$latest_file" ]]; then
echo "No files found in $dir"
@albertbuchard
albertbuchard / mine_mwe.py
Last active October 22, 2023 11:49
MINE: Mutual Information Neural Estimation | Minimal Working Example
import math
import torch.optim as optim
import torch
from torch import nn
class MineWrapper(nn.Module):
def __init__(self, stat_model, moving_average_rate=0.1, unbiased=False):
super(MineWrapper, self).__init__()
@albertbuchard
albertbuchard / ddp_debug.py
Created September 11, 2023 13:35
Distributed Data Parallel Debugging - Pytorch / PDB - ddp_pdb()
class DistributedIdentity:
"""
Singleton class to hold distributed identity information.
Handles SLURM, torchrun, and local runs.
Looks for the following environment variables:
- RANK
- WORLD_SIZE
- LOCAL_RANK