Skip to content

Instantly share code, notes, and snippets.

View mschauer's full-sized avatar

Moritz Schauer mschauer

View GitHub Profile
@mschauer
mschauer / check_claims.py
Last active October 13, 2026 18:27
Verification script "Bounding the median for the number of sinks in a random orientation"
from collections import Counter
from fractions import Fraction as F
from itertools import combinations, combinations_with_replacement
from math import comb, ceil
def seen_hist(edges):
hist = Counter()
for bits in range(1 << len(edges)):
seen = {edge[(bits >> j) & 1] for j, edge in enumerate(edges)}
hist[len(seen)] += 1
@mschauer
mschauer / crtgibbs.jl
Last active September 29, 2026 22:14
Update residue class of random integer with Gibbs
mods = [2, 3, 5, 7]
M = prod(mods)
# e.g. mod(basis[i], mods[j]) == 1 if i == j else 0
basis = [ M ÷ m * invmod(M ÷ m, m) for m in mods ]
function gibbs!(r, x, i, mods, basis, M)
old = r[i]
new = rand(0:mods[i]-1)
@mschauer
mschauer / prime.lean
Last active May 29, 2026 13:40
Using sigma algebras generated by primality test to show the infinitude of primes
import Mathlib
/-
This file formalizes a finite-prime-test version of Euclid's theorem using
Mathlib's sigma algebras, represented by `MeasurableSpace`.
Fix a finite set `P` of proposed prime tests. The basic observable questions
are the divisibility events
`DivEvent p = {n : ℕ | p ∣ n}`
@mschauer
mschauer / asian.jl
Last active February 19, 2026 09:13
Trying to use Black Scholes on market data estimating the volatility parameter
# Monte Carlo pricing for Asian options
function montecarlo_price_asian(payoff, S0, T, r, sigma, N)
# Generate N random samples from a standard normal distribution
mc_average = 0.0
for i in 1:N
Z = randn()
@mschauer
mschauer / martingales.jl
Created January 28, 2026 08:29
Martingales by simulation (Julia)
###############################################################################
# Martingales by simulation (Julia)
#
# This script illustrates several facts discussed in lecture:
#
# (1) Brownian motion W_t has independent increments and is a martingale.
# (2) Additive Brownian stock model S_t = S_0 + μ t + σ W_t.
# (3) The conditional expectation (filtered value)
# V_t = E[(S_T - K)^+ | 𝔽_t]
# is a martingale in t (tower property).
@mschauer
mschauer / demo.jl
Last active December 30, 2025 14:00
Simple Gaussian DAG hill-climber (score-based) in Julia
using Random
rng = MersenneTwister(0)
n = 2000
# True SEM: X -> Y -> Z
true_adj = Bool[
0 1 0
0 0 1
0 0 0]
@mschauer
mschauer / fit.jl
Last active February 21, 2025 12:09
Risk neutral derivative pricing
using DelimitedFiles, GLMakie, Distributions
# Black-Scholes formula
normcdf(x) = cdf(Normal(), x)
function blcall(S0, K, r, T, σ)
B = exp(r*T)
F = S0 * B
d1 = log(F/K) / (σ*sqrt(T)) + σ*sqrt(T)/2
(F*normcdf(d1) - K*normcdf((d1 - σ*sqrt(T))))/B
end
@mschauer
mschauer / intervention.jl
Created December 4, 2024 17:38
Z-Identification in Julia with CausalInference.jl
# Solving the problem in https://bsky.app/profile/p-hunermund.com/post/3lci6xojlmt25
using CausalInference, Graphs
# defining the graphical do-operator we need here
function do!(g, v)
for u in collect(inneighbors(g, v))
rem_edge!(g, u, v)
end
end
@mschauer
mschauer / Comp.jl
Last active December 4, 2024 11:55
Comparing CausalInference.pcalg with Associations.infer_graph
using CausalInference
using Associations: CorrTest, PC, Associations
using Test
using Graphs: SimpleDiGraph, Graphs, complete_graph
using StableRNGs
using LinearAlgebra, Random, Distributions
using CausalInference: pcalg, gausscitest, CausalInference
using Combinatorics
using Tables: table, istable
rng = StableRNG(123)
@mschauer
mschauer / bayesball.jl
Last active February 19, 2024 14:21
Bayes ball
using CausalInference, Graphs
V = [:U, :T, :P, :O]
ι = Dict(v=>i for (i,v) in enumerate(V))
g = digraph([1=>3, 2=>3, 3=>4, 2=>4, 1=>4])
# Can estimate total effect T=>O without observing U?
u = ι[:T]
v = ι[:O]
∅ = Set{Int}()