Skip to content

Instantly share code, notes, and snippets.

View mschauer's full-sized avatar

Moritz Schauer mschauer

View GitHub Profile
@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}()
@mschauer
mschauer / restart.jl
Last active January 25, 2024 19:57
Restart Julia (and keep directory and activated environment.)
function restart()
startup = """
Base.ACTIVE_PROJECT[]=$(repr(Base.ACTIVE_PROJECT[]))
Base.HOME_PROJECT[]=$(repr(Base.HOME_PROJECT[]))
cd($(repr(pwd())))
"""
cmd = `$(Base.julia_cmd()) -ie $startup`
atexit(()->run(cmd))
exit(0)
end
@mschauer
mschauer / kalmanmean.jl
Created August 23, 2023 17:58
Fancy mean with Kalman filter
using Kalman
using Kalman.GaussianDistributions
using Statistics, LinearAlgebra
# prior for time 0
x0 = 0.0
P0 = floatmax(x0)
# observation operator
H = 1.0
@mschauer
mschauer / mixture.jl
Created May 18, 2023 15:36
Hidden correlation
using GLMakie
using Random
# mixture distribution
X(c) = rand() < c ? sqrt(rand()) : 1 - sqrt(rand())
# sample
Random.seed!(1);
A = [0:0.01:1;; X.(0:0.01:1)];
@mschauer
mschauer / quasigaussian.txt
Last active March 2, 2023 08:42
Quasi Gaussian numbers
0.0 0.0
-0.032984387220723596 0.030216400626819087
0.005532055967160396 -0.06303492443179701
0.04716487617405366 0.06151822897388728
-0.08816371845816047 -0.01559492041364278
0.0844812855043274 -0.05374007858781831
-0.02848102330289795 0.10594798706320867
-0.054631054889518714 -0.10518877181328493
0.1190544569384441 0.04347849672678073
-0.12429436989984163 0.05130690904254036
@mschauer
mschauer / detailedbalance.md
Last active December 7, 2022 10:39
Being a bit didactic with ChatGPT.
  • You must have heard the term detailed balance in the context of Markov chains before.

Yes, I have heard of the term detailed balance in the context of Markov chains. In the context of Markov chains, detailed balance refers to a condition that must be satisfied in order for a given system to be in equilibrium. This condition states that the product of the transition probabilities between any two states in the system must be equal to the product of the reverse transition probabilities between those same two states. This ensures that the system will not tend to move towards one particular state over another, and will instead remain in a state of equilibrium.

  • That is not quite correct, let's see. Lets denote the transition probability from state x to state y by p(x → y). Detailed balance holds if there is a probability distribution π(x) over the states such that u(x,y) = π(x)p(x → y) is a symmetric function, hence u(x,y) = u(y,x).

You are correct. My previous explanation was not entirely accurate. Deta

@mschauer
mschauer / dual.jl
Created November 4, 2022 15:14
Dual numbers
struct Dual <: Real
value
δ
# w and Y and tag
end
Base.show(io::IO, x::Dual) = print(io, x.value, " + ", x.δ, " ϵ")
Base.:+(x::Dual, y::Dual) = Dual(x.value + y.value, x.δ + y.δ)
Base.:*(x::Dual, y::Dual) = Dual(x.value*y.value, x.δ*y.value + x.value*y.δ)
Dual(x) = Dual(x, zero(x))
Base.promote_rule(::Type{Dual},::Type{<:Real}) = Dual
@mschauer
mschauer / gist:517b1b3d8eb8301823864ba67bd67f4d
Last active October 22, 2022 22:16
Precision Brownian motion
d = 100
ts_ = range(0, 1, length=d+1)[2:end]
Γ = SymTridiagonal([2.0ones(d-1); 1.0], -ones(d-1))/ts_[1]
# Note the covariance matrix of B.M is
ts = range(0, 1, length=d+1)[2:end]
Γ0 = inv([min(i,j) for i in ts_, j in ts_])
@test norm(Γ0 - Γ) < 1e-7
@mschauer
mschauer / introx.jl
Last active October 17, 2022 07:02
Adam to maximise expectation or variational inference with https://github.com/gaurav-arya/StochasticAD.jl
using StochasticAD, Distributions, Optimisers, GLMakie
import Random # hide
Random.seed!(1234) # hide
# Function we want to maximize the expectation of
function X(p)
a = p*(1-p)
b = rand(Binomial(10, p))
c = 2 * b + 3 * rand(Bernoulli(p))
return a * c * rand(Normal(b, a))
@mschauer
mschauer / sampler.jl
Created August 12, 2020 14:24
Linear forward filtering backward smoothing/sampling with Zygote
# Linear state space system forward filtering backward sampling with Zygote
using LinearAlgebra
using GaussianDistributions
using GaussianDistributions: ⊕, logpdf
using StaticArrays
using Statistics
using Zygote