Skip to content

Instantly share code, notes, and snippets.

View mschauer's full-sized avatar

Moritz Schauer mschauer

View GitHub Profile
@mschauer
mschauer / markov.jl
Created September 15, 2021 12:13
Markov chain examples
using Random, LinearAlgebra, Distributions, StatsBase
# Linear Algebra of Markov chain
# Example: Weather Markov chain of Oz
S = [:R, :S, :C]
P = [0.5 0.25 0.25
0.5 0 0.5
0.25 0.25 0.5]
sum(P[2, :])
@mschauer
mschauer / shallow.jl
Last active September 9, 2021 19:18
Static observables
using Observables, BenchmarkTools, Test
struct StaticObservable{T, Listeners} <: Observables.AbstractObservable{T}
listeners::Listeners
val::Base.RefValue{T}
end
StaticObservable(obs::Observable) = StaticObservable(tuple(obs.listeners...), Ref(obs[]))
function StaticObservable(val::Ref, listeners...)
_notify(val, listeners)
@mschauer
mschauer / nonparabayes.jl
Last active October 13, 2021 13:07
Non-parametric Bayesian regression in Fourier domain
n = 1000
x = range(0, 1, length=n)
ς = 1.5 # noise level
μ = 3*x.*sin.(2pi*x) # periodic signal in time domain
#μ = 6*sqrt.(abs.(x .- 0.5)) # this one is difficult to estimate
# Model: Signal distorted by white noise
y = μ + ς*randn(n)
using Mitosis
using MitosisStochasticDiffEq
import MitosisStochasticDiffEq as MSDE
using StaticArrays, LinearAlgebra
using OrdinaryDiffEq
# Match with B and sigma
B(θ) = [-0.1 0.2θ; -0.2θ -0.1]
beta(θ) = [0.,0.]
Σ(θ) = 0.15*I(2)
@mschauer
mschauer / sde.jl
Last active May 12, 2021 15:32
Elementary sample SDE using forward simulation and random walk on Wiener process ("Crank-Nicolson" scheme).
using AdvancedMH
using Distributions
using Random
using MCMCChains
using StructArrays
struct MvWiener{Tu0,Ttrange} <: ContinuousMatrixDistribution
u0::Tu0
trange::Ttrange
end
@mschauer
mschauer / lingauss.jl
Created May 9, 2021 09:24
Bayesian linear Gaussian models
using LinearAlgebra, Test, Random, Statistics
outer(x) = x*x'
lchol(x) = cholesky(Symmetric(x)).L
Random.seed!(1)
# dimensions
n = 10 # observed
p = 5 # latent
# parameters
@mschauer
mschauer / solver.jl
Created May 4, 2021 15:36
Zero cost abstraction Maruyama solver
using StaticArrays
struct EulerMaruyama!
end
"""
tangent!(du, u, dz, P)
"""
@mschauer
mschauer / create_gist.jl
Created May 1, 2021 12:23
create_gist.jl
using GitHub, JSON
#myauth = GitHub.authenticate(ENV["GITHUB_AUTH"])
gist_json(fn; description="$fn", public=true) = JSON.parse(
"""
{
"description": "$description",
"public": $public,
"files": {
@mschauer
mschauer / mappairreduce.jl
Created April 30, 2021 16:51
Map (running) pairs with f and reduce with r
function mappairreduce(r, f, itr, dec)
ϕ = iterate(itr)
if ϕ === nothing
return dec
end
xlast, state = ϕ
while true
ϕ = iterate(itr, state)
if ϕ === nothing
return dec
@mschauer
mschauer / shadercalibration.jl
Created April 14, 2021 14:57
Calibration test image for Makie
using Makie
using Colors
n = 100
f(x) = 0.5 .*reim(((x[1] + x[2]*im)/33)^2)
function f2(x)
0.5.*abs(2*(x[1] + x[2]*im)/1089)
end
#f(x) = (x[1],x[2])
#f2(x) = 1.0