Skip to content

Instantly share code, notes, and snippets.

"""
Benchmark: Long Context vs Raw RAG vs Causal Structure RAG
Paper: "Inventory Competition and the Cost of a Stockout" (SSRN 5410063)
All three conditions have access to the same underlying research.
The only difference is HOW that research is delivered to the LLM.
Long context — full paper text injected every query (~18k tokens)
Raw RAG — top-3 retrieved prose chunks (~500 tokens)
Causal RAG — top-3 retrieved causal claims (~350 tokens)
"""
Supply Chain Network Inference from Inventory Snapshots
========================================================
Companion code for "Chain Reactions Are Delayed" — Affective Analytics
https://frankcorrigan.substack.com
Reproduces the three-method comparison from the article:
1. Correlation
2. Lag Correlation
3. Convergent Cross-Mapping (NIPS paper method)
@FrankRuns
FrankRuns / embeddings_transportation_experiment.py
Created October 19, 2025 13:56
Synthetic data experiment to show how embeddings might improve transportation rate predictions
# %% [markdown]
# # Why AI Thinks Phoenix and Miami Belong Together — Tutorial Notebook
#
# This notebook-style script walks through:
# 1) Clustering US cities by **geography** (lat/lon) vs **meaning** (embeddings)
# 2) Building a **synthetic lane-rate** dataset where semantic city characteristics
# (derived from embeddings) actually drive part of the rate variance
# 3) Training three models to predict rate-per-mile (RPM):
# - Baseline (Distance-only)
# - Name IDs (one-hot origin/destination) — "memorizer"
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Decision-preserving dimensionality reduction for supply-chain network design.
What this script does:
• Strict 1,000-mile lane cap (no k-nearest fallback).
• Supply-aware clustering: only merge demand points sharing the same TOP-2 nearest DC signature.
• Demand-weighted clustering guardrails:
- Mean distance to centroid ≤ CLUSTER_MEAN_MILES
# Accumulating Risk Analysis: "It's there, you just can't see it"
# Demonstrates how risk accumulates over time and the statistical challenges in detecting it
# Load required libraries
library(ggplot2)
library(dplyr)
library(broom)
# Set seed for reproducibility
set.seed(42)
@FrankRuns
FrankRuns / gist:13c76f300f15da7d74b3d60d7b7c0d5d
Created August 17, 2025 11:56
risk_complacency_model.R
###############################################################
# TUTORIAL: The “complacency model” for safety incidents
#
# Audience: Curious operators and analysts. No math background required.
#
# Big idea in plain English:
# - Instead of assuming risk is constant, let’s assume it RISES
# the longer we go without an incident or intervention.
# - Think of it like tension in a spring. The longer it goes untouched,
# the more tightly wound it gets. Eventually something snaps.
@FrankRuns
FrankRuns / gist:b00437d7717b8ab097a636af86156bb0
Created August 17, 2025 11:51
constant_risk_updating.R
###############################################################
# TUTORIAL: Estimating “chance of an incident next week”
# when you’ve seen zero incidents so far
#
# Audience: Curious operators and analysts. No Bayesian background needed.
#
# Big idea in plain English:
# - You start with a reasonable guess about the weekly incident rate
# (call this your PRIOR belief, based on history/industry norms).
# - You observe some weeks with no incidents.
@FrankRuns
FrankRuns / ml_jitter_experiment_prompt.txt
Created January 9, 2025 12:28
Prompt to get python script experimenting with jittering input training data for an ML model.
I want a Python script demonstrating a simple approach for “shaking up” that historical data. Specifically, show me how to:
Load the Boston Housing dataset (or a similar publicly available dataset).
Split the data into training and test sets.
Add a small amount of random noise (jitter) to the training set features.
Train one linear regression model on the unmodified data and another on the jittered data.
Compare the MSE (Mean Squared Error) of each model on the same test set.
For the jitter, just use a normal distribution with a small standard deviation, something like 0.01. Then show me how the MSE differs between the original and jittered data. If the jittered version yields a lower MSE, let me know in the script output. If it’s worse, let me know that, too.
Nothing too fancy, just enough that I can make a point about how “bad data” might become surprisingly helpful when we own the uncertainty and inject it. And please include some print statements that display the MSEs. That’s it.
You are a highly capable Python programmer who has access to locations.csv, which contains columns name, longitude, latitude, and type.
Please write a Python script that does the following:
Reads locations.csv into a pandas DataFrame.
Enumerates every possible Origin–Destination (OD) pair, but skips certain flows based on the following rules (via a helper function is_valid_flow(origin_type, dest_type)):
No shipments from Plant -> Customer
No shipments from DC -> Plant
No shipments from Customer -> DC
No shipments from Customer -> Plant
name longitude latitude type
Washington DC -77.0369 38.9072 DC
Dallas TX -96.797 32.7767 DC
Los Angeles CA -118.2437 34.0522 DC
Phoenix AZ -112.074 33.4484 Plant
Charlotte NC -80.8431 35.2271 Plant
0 Washington DC -76.16186430611484 38.96475995358956 Customer
1 Washington DC -77.85084407238416 40.23905626401316 Customer
2 Washington DC -78.33383248877686 37.28207518409593 Customer
3 Washington DC -77.18345675251808 38.38733808629542 Customer